We need to solve: For each test case n (up to 10^18, t up to 10^4), find smallest x >= n such that gcd(x, sum_of_digits(x)) > 1.
We need to compute answers efficiently. Brute force from n upwards could be too slow because n up to 1e18 and t up to 1e4. However, we can reason about the condition.
Let s(x) denote sum of digits of x. We need gcd(x, s(x)) > 1, i.e., there exists some prime p (maybe composite) dividing both x and s(x). In other words, x ≡ 0 mod p and s(x) ≡ 0 mod p for some p >= 2.
Observation: For any integer x, we know that x ≡ s(x) (mod 9) but not just mod 9; it's true mod 9 but not mod other numbers. However, there is property: x mod 9 = s(x) mod 9 (digital root). But we need common divisor >1. So p could be 2,3,5,7,... Possibly many.
We need to find the next number >= n satisfying this property.
Given the range up to 1e18, we cannot iterate many numbers. However, we can note that the condition is not extremely rare. In fact, roughly at least 1/3 of numbers? Possibly many numbers are divisible by 3 and also sum of digits divisible by 3 (since sum of digits divisible by 3 is equivalent to number divisible by 3). So any number divisible by 3 will automatically have s(x) divisible by 3 because of divisibility rule for 3. Indeed, for p=3, the condition that 3 divides x is equivalent to 3 divides s(x) (since 3 divides x iff 3 divides s(x)). Therefore, any number divisible by 3 satisfies gcd(x,s(x)) >= 3 (unless maybe x=0? but positive) so >1. Similarly, for p=9? But divisibility by 9 does not guarantee that gcd>1 is >1? Actually if x divisible by 9, then s(x) divisible by 9? The divisibility rule for 9: 9 divides x iff 9 divides s(x). So again, if x divisible by 9, then both divisible by 9, so gcd at least 9. But more generally, for 3, it's a perfect rule. For other primes, there's no direct equivalence. So one straightforward approach: Starting from n, check if n satisfies condition; if not, increment until we find a multiple of 3? Wait, but there could be numbers not divisible by 3 that satisfy condition, like 762 from example: 762 divisible by 2? 762 is even but s=15 odd, so gcd=3. 762 is divisible by 3? Actually 762 / 3 = 254, so it is divisible by 3. Indeed, 762 is divisible by 3: 7+6+2=15 divisible by 3. So it's also divisible by 3. So maybe all examples with gcd>1 involve 3? Let's test other primes: Consider x=2, s=2, gcd=2 >1, and 2 is not divisible by 3. So there exist numbers where gcd is 2. e.g., x=2,4? 4: s=4, gcd=4>1. 5: s=5,gcd=5>1. So many small numbers. But for larger numbers, condition may be satisfied by other primes. However, it seems that the condition is not extremely restrictive. The worst-case gap until a number with gcd>1 could be large? Possibly numbers that are prime and whose digit sum is coprime to it. But need to find next such number.
We need an efficient method.
Observation: Since t up to 1e4 and n up to 1e18, we can afford to check maybe up to a few thousand numbers per test? 1e4*1e3 = 1e7, which is okay in Python if each check is cheap. But is gap guaranteed to be small? Could be large if we hit a long run of numbers where condition fails. Consider numbers that are 1 mod something? Let's analyze condition for failure: gcd(x, s(x)) = 1. That means x and s(x) are coprime. When does that happen? x is such that no prime dividing x also divides its digit sum.
One might suspect that such numbers become rarer as numbers grow. But is there an upper bound on the gap? I recall a known problem: "GCD Sum" from Codeforces Round #... Actually there is a Codeforces problem "GCD Sum" (maybe problem 1498A?) which asks exactly this: For given n, find smallest x>=n such that gcd(x, sum of digits) > 1. And the solution is to brute force from n until condition is satisfied, and it's guaranteed that within at most something like 3 steps you'll find a number divisible by 3? But not exactly: For any integer, if it's not divisible by 3, then maybe after a few increments you hit a multiple of 3? But if n is not divisible by 3, n+1 may be, or n+2? Actually among any three consecutive numbers, one is divisible by 3. So at most 2 steps to reach a multiple of 3? But wait: If n is not divisible by 3, then n mod 3 =1 or 2. Then n+ (3 - (n mod 3)) steps to reach next multiple of 3 is at most 2. For n mod 3 =1, next multiple of 3 is n+2; for mod 3=2, it's n+1. So within at most 2 numbers, we reach a multiple of 3. However, does a multiple of 3 always satisfy gcd>1? Yes, because if x divisible by 3, then s(x) divisible by 3 (by divisibility rule), so gcd(x, s(x)) is at least 3. But wait: Is it always true that if x divisible by 3 then s(x) divisible by 3? Yes, because x ≡ s(x) (mod 3). Actually x ≡ sum of digits mod 3 (since 10 ≡ 1 mod 3). So x mod 3 = s(x) mod 3. Therefore if x ≡ 0 mod 3, then s(x) ≡ 0 mod 3. So 3 divides both. So gcd>=3 >1. Therefore ANY multiple of 3 satisfies condition. So the answer is at most the next multiple of 3 after n (or n itself if n is multiple of 3). So the gap is at most 2. Wait, is that always true? Let's test with n=1: next multiple of 3 is 3. 1: gcd(1,1)=1; 2: gcd(2,2)=2 >1. So answer is 2, which is not a multiple of 3. But the next multiple of 3 is 3, which is greater than 2. So answer could be less than next multiple of 3 if there is an earlier number satisfying condition. But the maximum gap to satisfy condition might be at most something like 2? But we found n=11: next multiple of 3 after 11 is 12, answer is 12. n=31: next multiple of 3 after 31 is 33 (since 32 not multiple of 3, 33 is), answer is 33. n=75: 75 is multiple of 3, answer itself. So indeed answer often is the next multiple of 3, but sometimes there is a number before that which satisfies condition (e.g., n=1 gave answer 2, which is not multiple of 3 but less than 3). So the answer is always <= next multiple of 3 (or n itself if multiple). But could it be less than next multiple of 3? Yes, like n=1 answer 2. So the maximum distance to answer is at most 2? Let's test: n=2 answer 2; n=3 answer 3; n=4? 4: gcd(4,4)=4 >1, so answer 4 (multiple of 3 is 6, but answer 4 <6). So answer can be immediate. But could there be a case where the answer is more than 2 away from n? If n is such that n, n+1, n+2 all fail condition, but n+2 is multiple of 3? Actually n+2 is multiple of 3 only if n mod 3 =1. Then n+2 is multiple of 3, and we argued that any multiple of 3 satisfies condition. So n+2 always satisfies if it's a multiple of 3. So worst-case: if n mod 3 =0, answer n. If n mod 3 =1, then n+1 (mod 3=2) might fail, but n+2 is multiple of 3 and must satisfy. If n mod 3 =2, then n+1 is multiple of 3 and must satisfy. So indeed, in all cases, at least one of n, n+1, n+2 must satisfy condition? Let's verify: If n mod3=1: n fails? Possibly. n+1 mod3=2 might fail, but n+2 mod3=0 always satisfies (since multiple of 3). So at most 2 steps. If n mod3=2: n+1 mod3=0 always satisfies. If n mod3=0: n satisfies. So answer always within at most 2 increments from n.
But wait: Are there any numbers that are multiples of 3 but for which gcd(x, s(x)) = 1? That would contradict the divisibility rule. Let's check: x=3, s=3, gcd=3. x=6, s=6, gcd=6. x=9, s=9, gcd=9. x=12, s=3, gcd=3. x=15, s=6, gcd=3. x=18, s=9, gcd=9. So yes, always at least 3. But could there be x=0 mod 3 but s(x) mod 3 =0 always because x≡s(x) mod 3. So 3 divides both. So gcd >= 3. So condition holds.
Therefore, answer is simply: Check n, n+1, n+2; at least one will satisfy condition. But careful: n could be up to 10^18, but checking three numbers is trivial. However, we must also ensure that n+1 or n+2 could be the answer and not n+? But is it always guaranteed that among n, n+1, n+2 there is a number with gcd>1? As argued, if n divisible by 3, then n works. If n ≡ 2 mod 3, then n+1 divisible by 3, works. If n ≡ 1 mod 3, then n+2 divisible by 3, works. So yes, always. But wait, consider n=1: 1 mod3=1, n=1 fails, n+1=2 (mod3=2) is not multiple of 3 but we need to check if 2 works. 2 works (gcd=2). So indeed 2 works, which is n+1, even though it's not multiple of 3. But according to our argument, n+2=3 would also work. So the existence is guaranteed. So we can simply test n, n+1, n+2 and output the first that satisfies condition.
But is there any corner case where n+1 is multiple of 3? That works, no issue. So indeed, answer always within at most 2 steps. But wait, we need to confirm that if n mod3=2, then n+1 is multiple of 3, works. So that covers. So indeed answer is at most n+2.
But could there be a scenario where n, n+1, n+2 all fail? That would require: n mod3 ≠0, n+1 mod3 ≠0, n+2 mod3 ≠0? Impossible because three consecutive numbers cover all residues mod 3. So one is multiple of 3. That multiple will satisfy condition. So at least one of them satisfies.
Thus the solution is straightforward: For each test case n, compute s(n), check if gcd(n,s)>1, if yes output n; else compute for n+1; else n+2. But we must be careful: n up to 10^18, sum of digits can be computed easily. Compute gcd using math.gcd.
Complexity: O(t * (log10(n)) ), trivial.
Let's double-check with some examples: n=11 → 11 fails, 12 passes → output 12. n=31 → 31 fails, 32 fails, 33 passes → output 33. n=75 → passes → output 75. n=1 → 1 fails (gcd(1,1)=1), 2 passes → output 2. n=2 → passes → output 2. n=4 → 4 passes (gcd=4) -> output 4. n=5 → gcd(5,5)=5 >1, output 5. n=7 → 7 (gcd=7) >1, output 7. n=8 → 8 (gcd(8,8)=8) >1? But sum digits 8, gcd=8, yes >1. So many numbers already satisfy.
But wait: 10? n=10: s=1, gcd(10,1)=1, so fails. 11 fails, 12 works. So answer 12. That matches.
Now, is there any scenario where a multiple of 3 might not have gcd >1? For x=0? But positive integer. Or if x is 0? Not allowed. So fine.
Thus solution is trivial. But is it really that simple? Let's think: The problem constraints: n up to 1e18, t up to 1e4. This seems like a typical Codeforces problem "GCD Sum" (Problem 1498A?). Actually I recall a Codeforces problem where answer is at most 3 steps because of 3 property. Yes, it's exactly problem 1498A "GCD Sum". The solution indeed is to check up to 3 consecutive numbers. So it's correct.
But just to be thorough, we should consider any potential pitfalls: What about n=0? Not in range (positive integers). Good.
Edge: n=10^18 (1000000000000000000). Let's compute sum digits of that: 1. So gcd = 1, fails. n+1 = 1000000000000000001, sum digits = 2, gcd(1000000000000000001,2) = ? 1000000000000000001 mod 2 = 1, so gcd=1, fails. n+2 = 1000000000000000002, sum digits = 1+0... +0+2 = 3, gcd(1000000000000000002,3). Is 1000000000000000002 divisible by 3? Sum digits=3, so yes divisible by 3. So gcd at least 3. So answer = n+2 = 1000000000000000002. Works.
Now confirm that n+1 could be the answer even if not multiple of 3. For example n=7? 7 mod3=1, n works itself. n=8 mod3=2, n works. So n+1 might be answer only if n fails and n+1 is not multiple of 3 but still has gcd>1. Is that possible? Yes, n=1 gave n+1=2 works. n=10 gave n+1=11 fails, n+2=12 works. n=2 works itself. So the algorithm of checking three numbers in order is correct.
Thus answer is trivial.
Implementation: For each test case:
- read n as int
- for x in [n, n+1, n+2]:
- compute s = sum of digits of x (convert to string or use division)
- if math.gcd(x, s) > 1: print(x); break
But careful: x can be up to n+2 which may exceed 10^18? n max 10^18, so n+2 <= 1e18+2, fits in Python int.
Time: O(t * digits) ~ 10^4 * 18 = 180k operations, trivial.
Now we should ensure that gcd(x, s) > 1 check works for large numbers; math.gcd is efficient.
Thus solution is straightforward.
But we might think: Could there be any case where x and s are both divisible by a common factor >1 but not necessarily 3? The check covers it.
One more nuance: The problem states "All test cases in one test are different." Not needed.
So answer is simple.
We'll produce final solution code.
Let's also test some random numbers:
n=19: s=10, gcd=1; 20: s=2, gcd(20,2)=2 >1, answer 20. But next multiple of 3 after 19 is 21. However 20 works earlier. So our loop would check 19->fail, 20->pass, output 20. Good.
n=49: s=13, gcd=1; 50: s=5, gcd=5>1, answer 50, which is before 51 (multiple of 3). So okay.
n=199: s=19, gcd=1; 200: s=2, gcd(200,2)=2>1, answer 200. So works.
Thus algorithm fine.
But wait, check n=89: s=17, gcd=1; 90: s=9, gcd(90,9)=9>1, answer 90 (multiple of 3). 90 is n+1? 89 mod3=2, so n+1=90 is multiple of 3, works.
So indeed in many cases the answer is n+1 or n+2 or n.
We must be careful with Python implementation: sum(map(int, str(x))) for large numbers is fine.
Now we can present final answer.
But is there any scenario where n+2 might exceed the bound? Not a problem.
Thus solution is trivial.
But the problem statement might be from a Codeforces problem that expects such solution.
We'll include explanation.
Now produce final answer. ```python
import sys
import math
def digit_sum(x: int) -> int:
"""Return sum of decimal digits of x."""
s = 0
while x:
s += x % 10
x //= 10
return s
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
t = int(data[0])
out = []
idx = 1
for _ in range(t):
n = int(data[idx]); idx += 1
for x in (n, n+1, n+2):
s = digit_sum(x)
if math.gcd(x, s) > 1:
out.append(str(x))
break
sys.stdout.write("\n".join(out))
if name == "main":
solve()