We are given problem: For each test case, given n (0 ≤ n ≤ 100000 for large), output smallest composite number greater than n. Composite number: a positive integer that has at least one divisor other than 1 and itself. So composite numbers are numbers > 1 that are not prime. 1 is neither prime nor composite? Usually 1 is not composite. So composite numbers start at 4? Actually 4 is first composite. But note: n can be 0,1,2,3. For n=0,1,2,3 the smallest composite > n is 4? Check: n=0 -> smallest composite >0 is 4? But 1 is not composite, 2 prime,3 prime,4 composite => yes 4. n=1 -> 4? 2 prime,3 prime,4 composite -> 4. n=2 -> 4? 3 prime,4 composite -> 4. n=3 -> 4. n=4 -> next composite >4 is 6? Actually 5 prime,6 composite -> 6. So we need to compute for each n up to 100k, and t up to 50k. Simple: precompute array comp_next[n] for n from 0 to maybe 100001+? Actually we need to find smallest composite > n. So we can precompute list of composite numbers up to 100000+some offset. Since n max 100000, answer may be up to maybe 100003? Actually composite numbers up to 100000+? Need to find next composite after 100000. 100001? Is 100001 composite? We need to check. Could be prime or composite. But we can precompute up to some bound that is safe: Since n ≤ 100000, the next composite after 100000 is at most 100000+something. Could be 100001? 100001= 119091? Actually 100001=119091? Let's check: 119091=100001? 119090=99990, plus 11=100001, yes it is divisible by 11? 9091 maybe prime? Anyway it's composite. But what if n=1000000? Not needed. But we need to handle n up to 100000 only. However, the next composite after a prime near that might be at most that prime+? For n=100000, it might be 100000 itself? Actually 100000 is composite? 100000 is composite obviously, so answer > n, so we need > n. For n composite, we need the next composite after n. So answer can be as high as maybe 100005 or 100006? Let's test: If n=100000 (composite), then next composite >100000: 100001? we said composite? check 100001= 11*9091 composite => 100001. So answer within 100001. If n=99991 (prime?) Actually 99991 is prime? I'm not sure. But maximum prime gap for numbers up to 100k? Largest gap up to 100k is maybe 114 between primes 492113 and 492227? Actually known prime gaps: up to 100k, the maximum gap is less than 100? Let's check: gap between 31397 and 31469 is 72? Actually there are known gaps. But safe bound: we can precompute up to 100000+500 maybe. But we can also compute on the fly for each n: start from n+1 and check if composite. With t up to 50k and n up to 100k, worst-case each check could scan many numbers if we do primality test per number. However, we can precompute a boolean array is_composite up to some max limit like 100000+200 (since maximum gap between consecutive primes up to 100k is less than 100? Actually known maximum prime gap below 100k is 72? Wait check: The prime gap between 1327 and 1361 is 34? Not sure. Actually let's recall: The maximal prime gaps for numbers up to 10^5: According to known data, the largest gap for primes below 100,000 is 72, occurring between 31397 and 31469? Let's verify: List of maximal prime gaps: (from wikipedia: maximal prime gaps) Under 100k: gap 34 at 1327? Actually the maximal prime gaps increase. Let's recall: The first few maximal gaps: 1 at 2-3, 2 at 3-5, 4 at 7-11, 6 at 23-29, 8 at 89-97, 14 at 113-127, 18 at 523-541, 20 at 887-907, 22 at 1129-1151, 34 at 1327-1361, 36 at 9551-9587, 44 at 15683-15727, 52 at 19609-19661, 72 at 31397-31469, 86 at 155921-156007? That's above 100k? Actually 155921 >100k. So below 100k, the largest gap is 72 between 31397 and 31469, which is 72. So after 100k, maybe gap is smaller but anyway, the maximum gap up to 200k might be 114? Actually we need next composite after n, which is n+1 if n+1 composite, else n+2 if n+2 composite, ... until we hit a composite. Since composite numbers are abundant, the distance to next composite is small except when n+1 is prime and maybe n+2 also prime? Actually if n+1 is prime, then n+2 could be even (if n+1 odd, n+2 is even >2 -> composite). So the maximum distance to next composite from n is: if n+1 is prime and odd, then n+2 is even and >2, so composite. So distance is 2. If n+1=2? That's prime, but n+2=3 prime? Actually if n=1, n+1=2 prime, n+2=3 prime, n+3=4 composite -> distance 3. For n=0, n+1=1 not composite, n+2=2 prime, n+3=3 prime, n+4=4 composite -> distance 4? But 1 is not composite, so we start at 1? Actually we need > n, so smallest >0: check 1? not composite, 2 prime, 3 prime, 4 composite => distance 4. But for n>=1, the worst-case might be n=1? For n=1, answer=4 distance 3. For n=2, answer=4 distance 2. For n=3, answer=4 distance 1. So worst-case distance is 4 (for n=0) but we can treat n=0 separately. For n>=1, distance max 3 (when n=1?). Actually check: n=1 => n+1=2 prime, n+2=3 prime, n+3=4 composite => distance 3. n=0 => distance 4. But anyway, after n>3, maximum distance to next composite is 2? Let's analyze: For n >= 4, we need to find smallest composite > n. If n is composite, then we need next composite > n. Could be n+1 if composite; if n+1 is prime (maybe 5,7,11,...), then n+2 is even (since if n+1 is prime >2, it's odd, so n+2 is even and >2 => composite). So distance either 1 or 2. So maximum distance is 2, except when n+1 = 2? That can't happen for n>=4 because n+1 >=5 >2. So indeed for n >=4, answer is either n+1 or n+2. But we also need to consider n=4 (composite). Next composite >4: check 5 prime, 6 composite -> distance 2. So for n >=4, answer is n+1 if n+1 composite; else n+2. However, we must ensure n+1 composite? We need to check if n+1 is prime? Actually if n+1 is not prime and not 1, then it's composite? Wait: There is also the number 1, but n+1 >=5 >1, so it's either prime or composite. So if n+1 is not prime, it is composite. So for n >=4, answer is n+1 if n+1 is not prime; else n+2.
But we must also consider n=0,1,2,3 as special cases because answer=4. For n=4? Actually n=4: n>=4, but n+1=5 prime, so answer = n+2 =6, which is correct. So the above rule holds for n>=4? Let's test n=5 (prime). n+1=6 composite => answer=6, correct. n=6 (composite). n+1=7 prime, n+2=8 composite => answer=8? But smallest composite >6 is 8? Check: 7 prime, 8 composite => yes. So answer=8. So rule works.
But for n=9 (composite). n+1=10 composite => answer=10, correct. n=10 composite => n+1=11 prime, n+2=12 composite => answer=12 correct.
Now for n=1, n>=4 false so special case.
Thus we can compute answer quickly using primality check for numbers up to maybe 100002. Since t up to 50000, each test we can compute answer using O(1) with a precomputed array of is_prime (or is_composite) up to max_n+2 (like 100002). Precomputing sieve up to 100000+2 is trivial.
But also note constraints: small, medium, large. For large, t up to 50000, n up to 100000. So precomputation up to 100000+2 is fine.
However, we must be careful: The rule that answer is n+1 if n+1 composite else n+2 holds for n >=4. But what about n=4? Works. For n=3, answer=4, n+1=4 composite, so if we apply rule, answer=4? But n=3 is not >=4? Actually 3<4, so we treat separately. For n=0,1,2 also.
So we can implement:
if n < 4: print(4)
else:
if not is_prime[n+1]: print(n+1)
else: print(n+2)
We need to define is_prime array up to 100002 maybe, and ensure 1 is not prime.
But we must also consider n=4, n+1=5 prime, so else branch prints n+2=6 correct.
Edge case: n=100000: n+1=100001, check if prime. We need to know if 100001 is prime. It's composite as said, so answer=100001.
But also need to verify n=100000-1=99999? n=99999 (composite? 99999=911111? Actually 99999=333333, definitely composite). n+1=100000 composite => answer=100000? But careful: For n composite, answer should be > n. So if n is composite, we cannot output n itself even if it is composite; we need the smallest composite > n. So n+1=100000 composite, which is >99999, so answer=100000 correct. So it's fine.
Now, what about n=0? Special case prints 4.
Thus algorithm: Precompute primes up to MAX = 100000+2 = 100002 using sieve. Then for each n, output accordingly.
But confirm: n can be up to 100000, so MAX = 100002 is enough because we check n+1 and n+2 only. For n=99999, n+2=100001, which is within 100001 <= 100002? Yes. For n=100000, n+2=100002, we might need is_prime[100002]? But we only check n+2 if n+1 is prime. For n=100000, n+1=100001 (composite) so we don't need n+2. But to be safe, we can set MAX = 100000+3 = 100003.
Simplify: MAX = 100000 + 5.
Now, the problem says "composite number" is defined as a positive integer that has a positive divisor other than 1 or itself. So 1 is not composite, 0 and negative not considered. So our rule covers.
Thus solution is straightforward.
But wait: In problem statement they gave constraints: LARGE: 1 ≤ t ≤ 50000, 0 ≤ n ≤ 100000. So we can handle.
But also we must ensure reading input and output quickly for 50000 lines. Use sys.stdin read.
Now, we might implement sieve:
is_prime = [True](MAX+1)
is_prime[0]=is_prime[1]=False
for i in range(2,int(sqrt(MAX))+1):
if is_prime[i]:
for j in range(ii, MAX+1, i):
is_prime[j]=False
Then for each n:
if n<4:
print(4)
else:
if not is_prime[n+1]:
print(n+1)
else:
print(n+2)
But careful: n+1 could be 1? For n=0, n<4 case covers. For n=1, n<4 covers. For n=2, n<4 covers. For n=3, n<4 covers. So no problem.
Check n=4: n>=4, n+1=5 prime => else prints n+2=6 correct.
n=5: n+1=6 composite => print 6 correct.
n=6: n+1=7 prime => print 8 correct.
n=7: prime => n+1=8 composite => print 8? But smallest composite >7 is 8, yes.
n=8: composite? 8 composite, but answer should be 9? Actually 8 is composite, but we need >8, so check n+1=9 composite? 9 composite, so print 9 correct. So rule also works when n is composite but n+1 composite: we output n+1. So it's fine.
Now check n=9: n+1=10 composite -> 10 correct.
n=10: n+1=11 prime, n+2=12 composite -> 12 correct.
n=11: n+1=12 composite -> 12 correct.
Thus fine.
Now, note: For n=0, output 4.
But what about n=1? output 4.
n=2: output 4.
n=3: output 4.
Now we also need to ensure that n+1 or n+2 not equal to 1? For n>=4, n+1>=5, so fine.
Thus solution done.
But the problem might have trick: n can be as large as 100000, but what about n=100000? n+1=100001 composite? Actually we need to verify if 100001 is prime. Let's quickly check divisibility: 100001 = 11 * 9091? Actually 119091 = 999... 119091 = 119000=99000, plus 1191=1001 => total 100001, yes. So composite. So answer is 100001.
What about n=99991? That's prime? 99991 mod small primes? We can trust sieve.
But for n=99992? Composite? Anyway.
Thus we can implement.
But we might want to precompute an array next_comp[n] for all n up to 100000+something for quick lookup, but the logic is simple.
We must be careful about time: t up to 50000, each test O(1) is fine.
Alternatively, we could generate all composite numbers up to some bound and use binary search, but no need.
Thus solution.
But I'll double-check the rule for n=4: n+1=5 prime, so n+2=6 composite => answer 6. So indeed, for any n>=4, we can compute answer as n+1 if (n+1) is not prime, else n+2. Since if n+1 is prime, then n+2 is even and >2, so composite. But is it possible that n+2 is prime? For n+1 prime, n+2 is even >2, cannot be prime. So guarantee composite. So rule holds.
But wait: what about n=1? n+1=2 prime, n+2=3 prime, n+3=4 composite; we could have used n+? but we treat separately.
Thus we're good.
Now implementation details: we need to compute prime sieve up to MAX = 100000+2 (maybe plus margin). We'll compute up to 100000+3.
Now input: first line t, then t lines each containing n.
Output: For each test-case, print answer.
Edge case: For large t, reading and printing should be efficient: Use sys.stdin.read() and split, or sys.stdin.buffer.
We'll produce solution in Python.
Now verify sample:
Input: 2,3,8.
n=3 <4 => 4. n=8 >=4, n+1=9 composite => 9. So output 4 and 9 matches.
Thus correct.
Now confirm more cases:
n=0 => 4
n=4 => 6
n=5 => 6
n=6 => 8
n=10 => 12
n=100 => find? 100 composite, 101 prime? 101 prime, so answer 102? But 102 composite? 102 even, yes composite. So 102.
n=101 => 102.
Now, what about n=1? 4.
All good.
Now complexity: sieve O(MAX log log MAX) with MAX ~100005 trivial.
Thus final code.
But caution: Some languages may treat 1 as prime? We'll set is_prime[1] = False.
Now we can write.
But there is also possibility that n can be negative? But constraints say 0 ≤ n, so non-negative.
Now produce final answer.
I'll include an explanation in comments.
Now, but one nuance: In the problem statement, they say composite number greater than given no. So for n=4, composite numbers greater than 4: 6,8,9,... smallest is 6. Our rule gives 6. Good.
Now, check n=2: composite numbers >2: 4,6,... smallest is 4. Yes.
Now, check n=0: composite numbers >0: The first composite is 4? But is 1 considered composite? No. So answer 4.
Thus done.
Now produce code.
But I want to be thorough: Might there be any n where n+1 is 1? n=0 => n+1=1, but we treat specially. So fine.
Now, for n=0, answer=4 is correct. But we should verify if maybe 0 is considered composite? No, composite numbers are positive integers >1 that are not prime. So 0 is not even positive. So not composite.
Thus.
Now code in Python:
import sys
import math
def sieve(limit):
is_prime = [True] * (limit+1)
is_prime[0] = is_prime[1] = False
for i in range(2, int(limit**0.5)+1):
if is_prime[i]:
step = i
start = i*i
is_prime[start:limit+1:step] = [False] * ((limit - start)//step + 1)
return is_prime
def main():
data = sys.stdin.read().strip().split()
if not data:
return
t = int(data[0])
nums = list(map(int, data[1:]))
# ensure we have t numbers
max_n = max(nums) if nums else 0
MAX = max(100005, max_n+3) # ensure at least up to max_n+2
is_prime = sieve(MAX)
out_lines = []
for n in nums:
if n < 4:
out_lines.append('4')
else:
if not is_prime[n+1]:
out_lines.append(str(n+1))
else:
out_lines.append(str(n+2))
sys.stdout.write("\n".join(out_lines))
if name == "main":
main()
But we need to be careful: For n large, we might get index out of range if we access is_prime[n+2] when n+2 > MAX. But we set MAX = max(100005, max_n+3). So n+2 <= max_n+2 <= max_n+3-1, so within limit.
Alternatively, we can set MAX = 100000 + 5, but then if max_n is less, it's fine; if max_n is exactly 100000, MAX=100005, n+2=100002 <= 100005, so safe. So we can just set MAX = 100005 or 100003. Let's compute: n max 100000, n+2=100002, so we need up to 100002. 100005 covers that. So we can set constant MAX = 100003.
Simplify: MAX = 100003
But to be safe: MAX = 100000+3 = 100003. So set MAX = 100003.
However, when max_n is less than that, it's fine; sieve up to 100003 is cheap.
Thus we can set constant.
But note: For n=100000, is_prime[100001] we need. Is 100001 within sieve limit? Yes if limit >= 100001. 100003 > 100001.
Thus fine.
Now time: Sieve up to 100003 is fast.
Now final solution.
But the problem also says "SMALL: 1 ≤ t ≤ 10, 0 ≤ n ≤ 10, MEDIUM: 1 ≤ t ≤ 500, 0 ≤ n ≤ 1000, LARGE: 1 ≤ t ≤ 50000, 0 ≤ n ≤ 100000". So same solution works for all.
Thus answer.
But wait: There is a nuance: composite number definition: Some might consider 1 as neither prime nor composite, which we treat correctly. Also, note that 2 is prime, 3 is prime. So all fine.
Now confirm for n=0: 4.
But what about n=1? 4. n=2? 4. n=3? 4.
Now, what about n=4? 6. That's correct.
Now we can provide explanation.
Now I'll produce final answer.
But I should check if any other tricky cases: n=100000? As said.
Check n=100001? But n max is 100000, so not needed.
Now, confirm sample:
Input:
2
3
8
Our code: t=2, nums[0]=3 (<4) => 4, nums[1]=8 (>=4) n+1=9, is_prime[9]? False => output 9.
Matches.
Thus final solution.
But I realize: The problem might also have test where n is large but n+1 is 1? No.
Now one more check: n=4 => n+1=5 prime => else => n+2=6 composite, output 6.
Now, consider n=5 => n+1=6 composite => output 6.
Now, consider n=6 => n+1=7 prime => output 8.
Now, consider n=7 => n+1=8 composite => output 8.
Now, consider n=8 => n+1=9 composite => output 9.
Now, consider n=9 => n+1=10 composite => output 10.
Now, consider n=10 => n+1=11 prime => output 12.
All good.
Thus finalize.
Now I'll produce solution explanation and code.
But the user requested: "You are an AI assistant that responds to the user's queries. The user asks you to write code in python to solve the problem described." So output code with explanation? Usually we output just the code? The instruction: "Solve the problem" likely expects code. I'll include code with comments.
Given it's a typical competitive programming problem, I'll provide full code.
But also note that reading input may have extra spaces, so using sys.stdin.buffer.
Now, we might further optimize: Actually we don't even need full sieve if we just check n+1 primality for n>=4. But simpler to precompute.
Alternatively, we could for each n compute prime check on the fly with trial division up to sqrt(n+1) because n+1 ≤ 100001, and t ≤ 50000, worst-case 50000 * sqrt(100001) ≈ 50000*316 ≈ 15.8M, which is fine in Python? 15 million operations might be borderline but likely okay. But safer to precompute.
Thus precompute.
Now I'll output code.
But ensure we handle t lines of input, but we read all numbers. If there are extra whitespaces, it's fine.
Now code:
import sys
def main():
data = sys.stdin.buffer.read().split()
if not data:
return
t = int(data[0])
nums = list(map(int, data[1:1+t]))
max_n = max(nums) if nums else 0
LIMIT = max_n + 3 # to cover n+2
if LIMIT < 100: # but for small case maybe adjust
LIMIT = 100 # at least enough to include 4?
# Actually we need to have up to at least 5? But set to something.
# Let's set a minimum of 100005 to be safe for large constraints, but we can compute based on max_n.
# However max_n may be as low as 0, then LIMIT=3, but we need primes up to at least 5? Actually for n=4, need up to 6, so at least 6. So we need minimum maybe 6.
# So better to set LIMIT = max(100005, max_n+3)
LIMIT = max(100005, max_n+3)
is_prime = [True] * (LIMIT+1)
is_prime[0] = is_prime[1] = False
for i in range(2, int(LIMIT**0.5)+1):
if is_prime[i]:
for j in range(i*i, LIMIT+1, i):
is_prime[j] = False
out = []
for n in nums:
if n < 4:
out.append('4')
else:
if not is_prime[n+1]:
out.append(str(n+1))
else:
out.append(str(n+2))
sys.stdout.write('\n'.join(out))
if name == "main":
main()
But we can simplify: Instead of dynamic limit based on max_n, we can set a constant MAX = 100005. Since max_n ≤ 100000, MAX=100005 covers all. That's easier.
So final code:
import sys
MAX = 100005 # 100000 + 5
is_prime = [True](MAX+1)
is_prime[0] = is_prime[1] = False
for i in range(2, int(MAX**0.5)+1):
if is_prime[i]:
for j in range(ii, MAX+1, i):
is_prime[j] = False
def main():
data = sys.stdin.buffer.read().split()
if not data:
return
t = int(data[0])
nums = list(map(int, data[1:1+t]))
out = []
for n in nums:
if n < 4:
out.append('4')
else:
if not is_prime[n+1]:
out.append(str(n+1))
else:
out.append(str(n+2))
sys.stdout.write('\n'.join(out))
if name == 'main':
main()
But careful: We used global is_prime. Should be fine.
Now test with sample:
Input: 2\n3\n8\n -> prints 4 and 9.
Now test with n=0 -> 4.
Now test with n=4 -> 6.
Now test with n=100000 -> we need to ensure is_prime[100001] is defined: index 100001 within [0..MAX]? MAX=100005, so yes.
Now test: 100001 is composite, so answer 100001.
Now test with n=99991 (if input) -> n+1 = 99992? Actually 99991? Wait: if n=99991, n+1=99992 even composite, so answer 99992. If n=99989? n+1=99990 composite? Actually 99990 composite. So all good.
Now, but we must also consider if n+1 is 1? Not for n>=4.
Now, one more nuance: For n=1, answer 4. But could there be a composite number 2? No.
Thus final.
Now I'll produce final answer with explanation of logic and code.
But the user asked: "You are an AI assistant that responds to the user's queries. The user asks you to write code in python to solve the problem described." So likely they expect just the code.
But I can include brief explanation and then code.
Given it's a typical problem, I'll provide code in python.
Thus final answer. ```python
import sys
Precompute primality up to 100000 + 5 (enough for n+2 when n ≤ 100000)
MAX = 100005
is_prime = [True] * (MAX + 1)
is_prime[0] = is_prime[1] = False
for i in range(2, int(MAX ** 0.5) + 1):
if is_prime[i]:
for j in range(i * i, MAX + 1, i):
is_prime[j] = False
def main() -> None:
data = sys.stdin.buffer.read().split()
if not data:
return
t = int(data[0])
nums = list(map(int, data[1:1 + t]))
out_lines = []
for n in nums:
if n < 4:
out_lines.append('4')
else:
# For n >= 4, the answer is either n+1 or n+2.
if not is_prime[n + 1]:
out_lines.append(str(n + 1))
else:
out_lines.append(str(n + 2))
sys.stdout.write('\n'.join(out_lines))
if name == 'main':
main()