We need to generate for each test case an n x n matrix (square) satisfying:
- All entries are non-negative integers ≤ 100,000.
- There are no prime numbers in the square. So every entry must be composite or 0 or 1? Actually condition: "there are no prime numbers in the square". So every cell must not be a prime. But note: 0 and 1 are not prime. So allowed: 0, 1, composite numbers. Also numbers > 1 that are not prime.
- Sums of each row and each column are prime numbers.
We need to output any such matrix.
Constraints: n up to 100, t up to 10. We need to be able to generate quickly.
We need to ensure sums are prime. Since n can be up to 100, row sum must be prime. We can choose a prime p (maybe same for all rows and columns? Not necessarily, but could be same for simplicity). We need each row sum = prime, each column sum = prime. Could be same prime for all rows and columns? Then total sum = n * p (row sums) also equals n * p (col sums). So total sum consistent. So we can aim for all row sums equal to some prime P and all column sums also equal to P. That would give total sum = n*P.
Now we need to fill matrix with non-prime numbers (composite or 0 or 1) such that row sums = P, col sums = P. Also all entries ≤ 100k.
One idea: use a constant value a (composite) for all cells except perhaps some adjustments to get sums prime. For example, if we fill entire matrix with a composite number x, then each row sum = nx, each column sum = nx. For these to be prime, nx must be prime. Since n≥2, nx is composite unless maybe n=1 but not. So can't be constant.
We need to adjust.
Another approach: Use a matrix that is symmetric and has a pattern. Perhaps we can set all diagonal entries to some composite number a, and all off-diagonals to another composite number b. Then each row sum = (n-1)*b + a. Each column sum = (n-1)*b + a. So they are equal. We need to pick a,b (non-prime, ≤100k) such that (n-1)*b + a is prime. Also a,b not prime. Also values within limit.
We can choose b=1 (1 is not prime) and a chosen such that a + (n-1) is prime, with a composite (or 1? but a must be non-prime). a could be 1? 1 not prime, okay. But then row sum = a + (n-1). If a=1, sum = n. n may be prime or not? For n=4, sum=4 not prime. For n=2, sum=2 prime? But 2 is prime, but row sum must be prime, yes 2 is prime. But a=1 is allowed (non-prime). However, we need to ensure a is not prime. 1 is not prime. b=1 also not prime. So for n=2, matrix with all ones gives row sum=2, col sum=2 prime? Actually each row sum = 1+1=2 prime, each column sum=2 prime. All entries are 1, not prime. So that works for n=2? Let's test: n=2, matrix [[1,1],[1,1]] all entries 1 (non-prime), row sums=2 (prime), column sums=2 (prime). That works! But sample output for n=2 shows "1 1; 1 1"? Actually sample output: for 2: output is "1 1" and "1 1"? The sample input:
2
4
2
Output:
4 6 8 1
4 9 9 9
4 10 10 65
1 4 4 4
1 1
1 1
Yes, they gave [[1,1],[1,1]] for n=2. So that pattern works for n=2. For n=4, they gave a specific matrix. Their matrix:
Row1: 4 6 8 1 -> sum=4+6+8+1=19 prime? 19 is prime. Row2: 4 9 9 9 sum=31? 4+9+9+9=31 prime. Row3: 4+10+10+65=89 prime. Row4:1+4+4+4=13 prime. Column sums: col1:4+4+4+1=13 prime; col2:6+9+10+4=29 prime; col3:8+9+10+4=31 prime; col4:1+9+65+4=79 prime. All entries: 4,6,8,1,4,9,9,9,4,10,10,65,1,4,4,4. Check primes: 4,6,8,1,9,10,65 are all composite or 1. So works.
But we need to generate for arbitrary n up to 100. Idea: Use two composite numbers a and b on off-diagonal and diagonal? The pattern a on diagonal and b off-diagonal gave row sum = a + (n-1)*b. If we set b=1 and a = p - (n-1) for some prime p, we need a non-prime and ≥0 and ≤100k. So choose a composite (or 1) that satisfies p - (n-1) = a. We can choose p to be a prime such that p > n-1 and p - (n-1) is not prime and also not too large (≤100k). Since n≤100, n-1≤99, so p could be something like a prime just above n-1 such that p - (n-1) is composite or 1. We also need a≥0, so p>=n-1. Since we want a non-prime, we can avoid a=0? 0 is allowed? 0 is non-prime, yes. But then row sum = 0 + (n-1)*1 = n-1, which is prime only for some n. Not reliable. So maybe choose a composite a.
But also we must ensure that a is ≤100k, which will be trivial if p not huge. However, we need to also make sure a is not prime. Could we always find a prime p such that p - (n-1) is composite or 1? For n≥2. Let's analyze: For any n, we need p prime, p > n-1 maybe. Set b=1. Then a = p - (n-1). We need a non-prime, and also a must be between 0 and 100k. Since p can be small, but we need p >= n-1+1? Actually a could be 1? 1 is not prime, allowed. So if p - (n-1) = 1, then p = n. Then we need n to be prime. For n prime, we can set p = n, a=1, b=1 gives row sum = 1 + (n-1)*1 = n, which is prime. So for n prime, the all-ones matrix works? Wait, all-ones gives each row sum = n, each column sum = n. But if n is prime, then row sum = n prime, column sum = n prime, and all entries are 1 (non-prime). So that works for any prime n. But for composite n, n is not prime, so all-ones fails because row sum is composite (not prime). So for composite n, we need a different p.
So for composite n, we could try to find prime p such that p - (n-1) is composite (or 1, but p would then be n if 1, but n is composite, so p would be composite, not prime). So a=1 only works if p = n which is not prime. So we need a composite a. So we need to find prime p > n-1 such that p - (n-1) is composite. Is that always possible? Let's consider n=4 (composite). n-1=3. We need prime p such that p-3 is composite. Example p=7 gives a=4 composite, works: row sum = 4+3*1=7 prime. That matches sample? Actually sample used different numbers per row/col, not uniform. But uniform pattern would be: diagonal entries =4, off-diagonal =1, gives each row sum = 4+1+1+1=7 prime. Column sums also 7 prime. All entries: 4 and 1 are non-prime. So that's a valid matrix! So for n=4 we could output:
1 1 1 4? Actually careful: In uniform pattern with diagonal a and off-diagonal b, we need to set b=1, a=4, but we must ensure that the matrix entries are not prime: 4 (composite) and 1 (non-prime) ok. So matrix:
Row1: a (diag) at (1,1) = 4, others b=1 => [4,1,1,1]
Row2: [1,4,1,1]
Row3: [1,1,4,1]
Row4: [1,1,1,4]
Check row sums = 4+1+1+1=7 prime, col sums also 7. So that's simpler. Why sample didn't use that? Probably just demonstration. So this pattern seems great.
Now for n=6 (composite). n-1=5. Need prime p such that a = p - 5 is composite (or 1 but can't because p would be 6 composite). So pick p=11 gives a=6 composite, row sum = 6+5=11 prime. a=6 composite ok. So matrix: diag=6, off-diag=1. Row sum=6+5*1=11 prime. So works.
But need to ensure a <= 100000, which is fine. Also b=1 is okay.
What about n=2? That's prime, so all-ones works (diag=1, off-diag=1 yields row sum=2). That's also covered by p=n? But n=2 prime, p=2 works, a= p - (n-1)=2-1=1, b=1. So pattern works as well.
What about n=3? n is prime. All-ones: row sum =3, which is prime, okay. So we can use b=1, a=1? Actually diagonal a=1 as well? Then row sum=3 prime. So all ones works. But we could also use the pattern with a=1, b=1. So basically we can always use the uniform pattern: set all off-diagonal entries = 1, and diagonal entries = some a (could be 1 as well) such that row sum = a + (n-1)*1 = a + n - 1 is prime, and a is non-prime (composite or 1). Since 1 is not prime, a=1 always gives sum = n. So if n is prime, a=1 works. If n is composite, we need to choose a composite a such that a + (n-1) is prime. So we need to find a composite number a (>=2? composite means at least 4? Actually composite numbers are >1 and not prime, so 4,6,8,9,10,... also 0? 0 is not prime, but 0+ (n-1) = n-1, which may be prime for some n? But we need a to be non-prime, so 0 allowed? 0 is not prime. But row sum = n-1, which is prime only for some n. For n=4, n-1=3 prime, but a=0 would be allowed? 0 is non-prime, but 0 is allowed? Condition: all numbers on the square are non-negative integers not exceeding 1e5, no restriction that they must be positive? So 0 allowed. So we could set a=0, b=1, row sum = n-1. But n-1 might be prime for some composite n. For n=4, n-1=3 prime, so that would work too: diagonal 0, off-diagonal 1, row sum=3 prime. But check: 0 is not prime, allowed. So that works as well. But for n=6, n-1=5 prime, so a=0, b=1 gives row sum=5 prime. That would also work. But careful: we need all entries ≤1e5, 0 and 1 are fine. So for any n where n-1 is prime, we could simply use a=0, b=1, giving row sum = n-1 (prime). For n where n-1 is composite, we need to find another a.
But we can always find an appropriate a? Since we can choose a composite a such that a + (n-1) is prime. That is essentially: find a prime p > n-1 such that p - (n-1) is composite (or 1? but if p - (n-1)=1 then p=n, which is prime only when n is prime; for composite n, n is not prime so p cannot be n). So for composite n, we need p a prime > n-1 with p - (n-1) composite. Is it always possible? Let's check for small n: n=4 (n-1=3). Prime p such that p-3 composite: p=5 gives a=2 (prime, not allowed); p=7 gives a=4 (composite ok); p=11 gives a=8 (composite ok). So possible. n=6 (n-1=5): p=7 gives a=2 (prime); p=11 gives a=6 (composite); so ok. n=8 (n-1=7): p=11 gives a=4 (composite); ok. n=9 (n-1=8): p=11 gives a=3 (prime); p=13 gives a=5 (prime); p=17 gives a=9 (composite? 9 is composite); so ok. n=10 (n-1=9): p=11 gives a=2 (prime); p=13 gives a=4 (composite); ok. n=12 (n-1=11): p=13 gives a=2 (prime); p=17 gives a=6 (composite); ok. n=14 (n-1=13): p=17 gives a=4 (composite); ok. n=15 (n-1=14): p=17 gives a=3 (prime); p=19 gives a=5 (prime); p=23 gives a=9 (composite); ok. n=16 (n-1=15): p=17 gives a=2 (prime); p=19 gives a=4 (composite); ok. n=18 (n-1=17): p=19 gives a=2 (prime); p=23 gives a=6 (composite); ok. n=20 (n-1=19): p=23 gives a=4 (composite); ok. n=21 (n-1=20): p=23 gives a=3 (prime); p=29 gives a=9 (composite); ok. n=22 (n-1=21): p=23 gives a=2 (prime); p=29 gives a=8 (composite); ok. n=24 (n-1=23): p=29 gives a=6 (composite); ok. n=25 (n-1=24): p=29 gives a=5 (prime); p=31 gives a=7 (prime); p=37 gives a=13 (prime); p=41 gives a=17 (prime); p=43 gives a=19 (prime); p=47 gives a=23 (prime); p=53 gives a=29 (prime); p=59 gives a=35 (composite); so eventually works. It seems likely that for any n≥2, there exists a prime p > n-1 such that p - (n-1) is composite. Is there a counterexample? We need to consider the possibility that for some n, all primes p > n-1 might have p - (n-1) be prime. That would mean that for every prime p > n-1, p - (n-1) is prime. That seems impossible for large n because primes become sparse. But we need to be rigorous for n up to 100. Let's test all n from 2 to 100 manually (we can write code, but we can reason). For n where n-1 is prime, we can just use a=0 (since 0 is composite? Actually composite numbers are usually defined as positive integers having at least two distinct positive divisors; 0 is not considered composite. The condition says "no prime numbers" – it doesn't forbid composite; it just says there are no prime numbers. So 0 and 1 are allowed because they are not prime. So a=0 is fine. So for n-1 prime, we can use a=0, b=1. So we only need to handle n where n-1 is composite. For those n, we need to find prime p > n-1 with p - (n-1) composite. Let's list n-1 composite: n = 2? n-1=1 (neither prime nor composite but 1 is not prime). But n=2 case we can use all ones. n=3: n-1=2 prime -> use a=0? But n=3 is prime, also all ones works. So it's fine. For n where n-1 composite, we can attempt to find p close to n-1. Typically small primes exist. Let's check the worst-case: n-1 composite up to 99. We'll check each n-1 composite value C from 4 to 99. For each C, we need a prime p > C such that p - C is not prime. Since p = C + a, we need a composite a. So we need to find a composite a such that C + a is prime. Equivalent: find a composite number a (>=1? Actually a must be non-prime, could be 1? 1 is not prime, but if a=1 then p = C+1. C+1 could be prime or not. But a=1 is not composite but allowed because non-prime. However, if we allow a=1, then we need p = C+1 prime. But if C+1 is prime, then we could set a=1 which is not prime (since 1 is not prime). So for any C such that C+1 is prime, we can use a=1. But C+1 = n, so if n is prime, then a=1 works. But we are considering n where n-1 composite, so n = C+1 is composite? Wait: C = n-1 composite. Then n = C+1. If C is composite, C+1 could be prime (e.g.,