We need to understand problem: We have n matches. We want to form a correct match equation (CME): a + b = c, where a,b,c are positive integers. The representation: each number is represented by that many matches (like tally sticks). The equation uses plus sign "+" and equals sign "=". We need to know how many matches are needed for the symbols.
We have to use all matches (n plus any bought) to assemble the entire equation. We can buy extra matches. We want to minimize the number bought.
So we need to find minimal m >= 0 such that there exist positive integers a,b,c satisfying a + b = c and total matches used = (matches for a) + (matches for b) + (matches for c) + matches for operators (+ and =). Then total = a + b + c + (matches for "+") + (matches for "="). Let's denote the cost of "+" as p, and cost of "=" as e. Usually, in matchstick puzzles, the plus sign is made of 2 matches (like cross) and equals sign is made of 2 matches (two parallel lines). But we need to confirm from problem statement. In examples: For n=5 they bought 1 match to assemble ||+|=|||. Let's count: a=2, b=1, c=3. So matches: a uses 2, b uses 1, c uses 3, plus sign uses? Usually plus sign is 2 matches (vertical and horizontal). Equals sign is 2 matches (two parallel). Count total: 2+1+3 = 6, plus operators: plus sign (2) and equals (2) gives total 10? That would be 6+4=10, but they used n=5 + bought 1 =6 matches? Actually they said n=5 you can buy one match and assemble ||+|=|||. That would be total matches used = a + b + c + plus + equals = 2+1+3+2+2 =10. That cannot be right. Let's recount: They might have a different representation: In the statement: "assemble ||+|=|||". That's the equation as string: "||+|=|||". That includes: "||" for a, "+", "|" for b, "=", "|||" for c. Count matches: The digits: each '|' is a match. So for a=2, two matches. '+' sign: likely made of 2 matches? But then there would be 2 matches for '+' symbol. Then '|' for b: 1 match. '=': 2 matches. Then '|||' for c: 3 matches. Total = 2+2+1+2+3 = 10. But they say n=5, buy one match => total 6. So clearly the plus and equals do not cost matches? That can't be. Alternatively, maybe the representation uses matches to form the equation in a specific way: the equation itself is built with matches; each character uses matches. But they gave example: for n=2, buy two matches and assemble |+|=||. That uses total: a=1, b=1, c=2. So 1+1+2=4 matches for numbers. Plus + and = might cost additional. But they have only 4 total after buying? Actually n=2 + buy 2 =4. If plus and equals cost matches, then total would be >4. So maybe plus and equals do NOT cost matches? Then total matches = a + b + c. For n=2, a=1,b=1,c=2 => total=4, which matches after buying 2. For n=5, a=2,b=1,c=3 => total=6, after buying 1 they have 6. That matches: 5+1=6. So it appears that only the numbers cost matches; the '+' and '=' are just symbols that do not require matches. Wait but in matchstick puzzles, they usually do require matches. However, the problem statement says: "assemble a CME using all your matches." And then examples: "2 + 2 = 4 (||+||=||||)" and "1 + 2 = 3 (|+||=|||)". They represent numbers with matches, but they don't explicitly say that the plus and equals signs use matches. Possibly they assume that the plus and equals signs are formed by placing matches in certain positions but they might not count as separate matches? But it's typical in matchstick puzzles that each match is used as a line segment, and plus and equals signs also require matches. However, the examples given as strings "||+||=||||" might just be a textual representation, not indicating that the + and = are made of matches. But they explicitly say "assemble a CME using all your matches." In the illustration, there is a picture (not provided) maybe showing that plus and equals are made with matches? Let's examine the example: n=5, they buy one match and assemble ||+|=|||. If plus and equals require matches, they'd need extra matches for those symbols, making total >6. Since they only have 5+1=6, plus/equals must not require matches. Also the first example: n=2, buy two matches and assemble |+|=||, total 4 matches. If + and = required matches, they'd need at least 2 more matches, total 6. So they don't count.
Thus, the only matches used are those representing the numbers a, b, c. The operators are just conceptual and don't consume matches. So total matches used = a + b + c.
We need a+b=c, and a,b,c > 0 integers. So total matches = c + (a+b) = c + c = 2c, because a+b=c. So total matches = 2c.
Thus, for a valid CME, the total number of matches must be even and at least 2*1=2? But a,b,c>0 implies c>=2? Actually if a,b>=1, then c = a+b >=2. So total matches = 2c >=4. But n can be as low as 2, but we can buy matches. So we need to find minimal m such that n+m = 2c for some integer c>=2. Additionally, we need to be able to represent a and b positive such that a+b=c. For any c>=2, we can always choose a=1,b=c-1, both positive. So the only requirement is that total matches = 2c for some integer c>=2.
Thus, the problem reduces to: Given n, find smallest non-negative integer m such that n+m is even and at least 4, and (n+m)/2 >= 2? Actually c = (n+m)/2, need c >= 2, i.e., n+m >= 4. Since n>=2, it might be less than 4 when n=2 or 3. So we need to ensure after buying, total >=4. But also note that we must use all matches, meaning that after buying, we have exactly total matches, and we can form the numbers with those matches. Since total = 2c, we need to check if we can split c into a+b with a,b>=1. That's always possible for c>=2. So condition: n+m is even and n+m >= 4. Also if n+m = 2? That would be c=1, impossible. So >=4.
But is there any additional constraints? Possibly the numbers must be represented exactly by the matches, but that's trivial: a matches for a, etc. So yes.
Now we need minimal m satisfying these. That's straightforward: For each n, we need to make n+m divisible by 2 and at least 4. But also we might be able to avoid buying any if n already even and n>=4? For n=8, output 0. 8 is even and >=4. For n=11, output 1: 11+1=12 even, >=4. For n=5, output 1: 5+1=6 even. For n=2, output 2: 2+2=4.
But what about n=3? Not in constraints? n>=2, so n=3: 3 is odd, need m=1 to make 4? 3+1=4 even and >=4. So answer would be 1? Let's test: Could we assemble with 3 matches? n=3, total matches =3. But need total = 2c, so c = total/2 must be integer. 3 is odd, impossible. Minimum m=1 -> total=4, c=2, a=1,b=1. So answer 1. That makes sense. But is there any issue that a and b must be positive? Yes.
Thus solution: if n < 4: need to reach at least 4. But also parity. Let's compute m = ( (4 - n) mod? Actually we can compute: we want smallest m >=0 s.t. (n+m) % 2 == 0 and n+m >= 4. This is simple: if n >=4: if n even, m=0; else m=1. If n <4: we need to reach at least 4: if n=2 (even) but 2<4, we need m=2 (to get 4). If n=3 (odd) we need to check: n+m >=4: if we add m=1, total=4 even? 4 even. So m=1. So algorithm: if n%2==0: if n>=4 then m=0 else m=4-n (since n=2 => m=2). If n%2==1: if n>=4 then m=1 else (n=1 not allowed, n=3 => m=1?). But n>=2, n odd possible: n=3 => need m=1? Let's compute: n=3, 3<4, we need at least 4; 3+1=4 even; m=1. So general rule: if n%2==0: m = (4 - n) mod? Actually compute: m = (4 - n) mod 2? Not necessary. We can compute: desired total = max(4, n + (n%2))? But careful: If n even and n<4: n=2 -> need total 4 -> m=2. If n even and n>=4 -> m=0. If n odd: if n>=4 -> need m=1 to make even; if n=3 -> need m=1 to make total 4; if n=1 not allowed. So essentially: if n%2 == 0: if n<4: m = 4-n; else m=0. If n%2 ==1: if n<4: m = 4-n? 4-3=1, yes; if n>=4: m=1.
But note: For n=5 (odd, >=4) -> m=1. That matches. For n=11 (odd, >=4) -> m=1. For n=8 (even, >=4) -> m=0. For n=2 (even, <4) -> m=2.
So that's it? But wait, check n=4: even, >=4 -> m=0. Could we form a CME with 4 matches? total=4 => c=2, a=1,b=1. So yes, "|+|=||"? That uses 4 matches? a=1,b=1,c=2 total=4. So answer 0. So it's consistent.
But what about n=6? Even, >=4 -> m=0, total=6 => c=3, a=1,b=2 etc. So yes.
So answer is simply: if n<4: answer = 4-n. Else: answer = n%2. Because if n>=4 and odd, need 1; if even, 0. That's it.
But wait, is there any hidden nuance? The problem statement: "you want to assemble a CME using all your matches." "Unfortunately, it is possible that you can't assemble the CME using all matches. But you can buy some extra matches and then assemble CME!" So as we reasoned, you need total matches to be twice the sum c, and c>=2. So the condition is total matches must be even and at least 4.
But is there any restriction that you must use exactly all matches, not more than? You can buy extra matches, then assemble CME using all matches (the ones you originally had plus the ones you bought). So total = n + m.
Thus answer = minimal m >= 0 s.t. n+m is even and n+m >= 4.
That's straightforward.
But why is the maximum n up to 1e9? Possibly there is a twist? Could there be a scenario where you cannot use all matches even if total is even and >=4? Because you have to allocate matches to a,b,c such that a+b=c. That's always possible as argued: given total = 2c, choose a=1, b=c-1 (both >0 as long as c>=2). So always possible.
Thus the solution is trivial.
But the problem might be a trick: maybe the operators also require matches? Then the total would be a+b+c + cost_plus + cost_equals. Let's re-analyze carefully. The problem statement: "Let's denote correct match equation (we will denote it as CME) an equation a + b = c there all integers a, b and c are greater than zero.
For example, equations 2 + 2 = 4 (||+||=||||) and 1 + 2 = 3 (|+||=|||) are CME but equations 1 + 2 = 4 (|+||=||||), 2 + 2 = 3 (||+||=|||), and 0 + 1 = 1 (+|=|) are not.
Now, you have n matches. You want to assemble a CME using all your matches. Unfortunately, it is possible that you can't assemble the CME using all matches. But you can buy some extra matches and then assemble CME!
For example, if n = 2, you can buy two matches and assemble |+|=||, and if n = 5 you can buy one match and assemble ||+|=|||."
Wait, they gave example with n=2: they buy two matches and assemble |+|=||. Let's count matches if we assume plus and equals also use matches. The common matchstick puzzle representation: plus sign uses 2 matches (cross), equals uses 2 matches (two lines). So total matches needed for equation: a+b+c + 2 (for plus) + 2 (for equals) = (a+b+c)+4. In example: a=1,b=1,c=2 => total matches needed = 1+1+2+4=8. But they only have n=2 + bought 2 =4 matches. So that can't be. Thus plus/equals must not cost matches. Or maybe the plus and equals signs are made by positioning the matches of numbers? That seems unlikely.
Alternatively, maybe the plus and equals signs are not made of matches; they are just printed on the surface, and you only use matches to represent the numbers. The equation is just a conceptual representation. That seems plausible: The problem says "assemble a CME using all your matches." It might mean you physically arrange the matches to display the equation, but the '+' and '=' signs are not made from matches? That would be weird.
Let's see the picture provided in the statement (though we can't see it). The example text includes "||+||=||||" as a representation, but perhaps that's just a string for us, not indication of match usage.
Considering the sample: n=5 buys 1, total 6, used for numbers. So it's consistent that only numbers use matches.
Thus answer is simple.
But before concluding, we must check if there is any other hidden condition: The equation must be "a + b = c" with a, b, c positive integers. That's it. So total matches = sum of digits (each digit represented by matches? But numbers are not digits but value. Representation: each unit uses one match. So a uses a matches, b uses b, c uses c. So total = a+b+c.
Given a+b=c, total = 2c.
Thus answer is as above.
Double-check with sample: n=2 -> answer 2. n=5 -> 1. n=8 -> 0. n=11 -> 1. All match.
So solution: For each query, if n<4: print 4-n; else print n%2.
But wait, what about n=1? Not in constraints.
Edge: n=4 -> 0. n=6 -> 0. n=3 -> 1.
Thus answer is straightforward.
But let's read constraints: n up to 1e9, q up to 100. So we can answer per query.
But could there be any scenario where total = 2c, but you cannot use all matches because you need to have at least 1 match for each number? Already c>=2 ensures a and b at least 1 each. So fine.
Thus solution is trivial. However, is there any nuance about using all matches exactly: we must use all matches for the numbers. That means we must assign exactly a matches to a, b matches to b, c matches to c. Since we can break matches? No, matches are indivisible. So a,b,c are integers. That's fine.
Thus answer computed.
But many Codeforces problems have such a simple answer? Let's check if this problem is known: Possibly it's Codeforces Round #1385? Actually I recall a problem "Correct Match Equation" maybe from Codeforces? The solution indeed is: answer = (4 - n % 2) % 2? No. I'm not sure.
Wait, I'm slightly suspicious: The problem might have been misunderstood. Maybe the plus and equals signs also require matches. Let's test with examples if plus and equals require matches. Suppose each operator requires some matches. Let's denote p for plus, e for equals. Then total = a+b+c + p + e. And we know a+b=c, so total = 2c + p + e. Now we need to find minimal m such that n+m = 2c + p + e for some c>=2, and we can choose a,b accordingly. This would be a different problem. But then can we explain sample? For n=2, they buy 2 matches and assemble |+|=||. If p and e require matches, let's find p,e. Possibly they are formed with matches but maybe they require 1 match each? Let's test: Suppose plus uses 1 match? But typical matchstick plus uses 2. However, maybe they use 1 match for plus? Not typical. Let's assume p and e are unknown. Solve: For example, they say with n=2 they buy 2 and use |+|=||. So total used = 2+? Actually they have total matches after purchase = 4. So equation must use 4 matches. In this equation, numbers: a=1,b=1,c=2 => sum numbers =4. So p+e must be 0. So that indicates operators require 0 matches. That is consistent with our earlier interpretation. So operators don't require matches.
Thus our solution is correct.
Now, check n=5: they buy 1, total 6. Numbers sum =2+1+3=6, operators 0.
Thus it's clear.
Therefore answer per query: if n<4: print 4-n; else print n%2.
Implementation: For each query, read n, compute answer = (n%2) if n>=4 else (4-n). But careful: n%2 for n>=4 gives 0 for even, 1 for odd. For n=2, n%2=0 but answer should be 2, not 0. So use condition.
Alternatively, we can derive formula: answer = ( (4 - n) % 2 + something? Not needed.
Simplify: if n%2==0: answer = 0 if n>=4 else 2; if n%2==1: answer = 1 (since n>=2 and odd implies n>=3, and both n=3 and n>=5 need 1? n=3: answer 1, correct; n=5: 1; n=1 not allowed). So we can compute: answer = (n%2) if n>3 else (4-n). But n>3 includes n=4,5,6,... So answer = (n%2) for n>3? For n=4 (even) answer 0; n=5 (odd) answer 1; n=3 -> n>3 false, so answer 4-3=1; n=2 -> 4-2=2. So yes.
Thus code.
But we have to answer q queries. That's trivial.
But wait, what about n=10^9? Even -> answer 0.
Thus solution.
Now, I'm going to write a proper explanation.
Let me double-check if there's any catch: The problem says "you have to answer q independent queries." That's it.
Thus answer.
Now, the only nuance is: Could there be a scenario where even if total is even and >=4, you cannot allocate matches because the numbers must be positive, but you also need to represent the equation visually such that the plus and equals signs are formed from matches placed appropriately? For example, if you have matches only for numbers, you might not be able to form the equation shape? The problem likely doesn't care about geometry; it just cares about the representation using tally marks.
Given the simplicity, I'll produce the solution.
But to be thorough, we can think: Could there be a case where total = 2c, but we need to represent a and b separately and c separately, and they need to be distinct groups of matches? That's always possible.
Thus answer.
Given that this is a problem from Codeforces (maybe #1385 A?), I recall there is a problem "Correct Placement" but not this.
Anyway.
I'll produce code: read q, loop, read n, if n <= 3: print(4-n) else: print(n%2).
But check n=3: 4-3=1, correct. n=2:2, n=1: not allowed.
Thus final answer.
Now, let me think if any corner case: n=0 not allowed. So fine.
Thus final solution. ```python
import sys
def solve():
data = sys.stdin.read().strip().split()
if not data:
return
q = int(data[0])
out = []
for i in range(1, q+1):
n = int(data[i])
if n <= 3:
out.append(str(4 - n))
else:
out.append(str(n % 2))
sys.stdout.write("\n".join(out))
if name == "main":
solve()