We need to compute number of unordered pairs (i, j) with 1 ≤ i < j ≤ N such that i XOR j ≤ N. Output that count.
Constraints: T up to 100, N up to 2000. So direct O(N^2) per test is fine because 2000^2 = 4e6, times 100 = 4e8 maybe borderline but could pass in C++ but maybe not in Python? 4e6 per test, 100 tests is 400 million pairs, which is heavy for Python. We need to be careful. But perhaps there's a formula? N is only 2000, but T up to 100, worst-case 100 * (2000^2)/2 = 200 million pairs, could be borderline in Python but with optimizations maybe passes? But we should aim for more efficient solution.
Alternatively, we can derive some combinatorial count: For each i from 1 to N-1, count j > i such that i xor j <= N. Could we precompute for all N up to 2000 once and answer queries? Since T up to 100 and N max 2000, we could precompute answer for all N from 2 to 2000 using DP or incremental approach O(N^2) overall. That would be about 4 million operations, which is fine. Then each query O(1). So we can do that.
Plan: Precompute array ans[N] for N from 2 to 2000. Initialize ans[1]=0? N>=2.
We'll compute for N from 2 to 2000: number of unordered pairs (i,j) with 1<=i<j<=N and i xor j <= N.
We can compute for each N: ans[N] = ans[N-1] + count of j from 1 to N-1 such that j xor N <= N. Because when we add N as new number, pairs involving N with smaller numbers must satisfy xor <= N. That gives incremental count.
Thus, ans[2] base: i=1,j=2: 1 xor 2 = 3 <= 2? 3>2, so ans[2]=0. Actually check: For N=2, possible pair (1,2): XOR=3 >2, so 0. So ans[2]=0.
Then for N=3: add pairs (1,3): 1 xor3=2 <=3 yes; (2,3):2 xor3=1 <=3 yes. So ans[3]=2. But careful: also previous pairs from N=2: none. So ans[3]=2.
For N=4: pairs (1,4): 1 xor4=5 >4; (2,4): 2 xor4=6>4; (3,4):3 xor4=7>4; So no new pairs? But total ans should be 3 as per sample: pairs (1,2), (1,3), (2,3). So ans[4]=3. That matches: ans[4]=ans[3] +0 =2+0? That would be 2, not 3. Wait, we missed something: For N=4, the pairs are (1,2) (XOR=3 <=4?), 3 <=4 yes. (1,3) XOR=2 <=4 yes. (2,3) XOR=1 <=4 yes. So all three pairs are among numbers 1,2,3. So ans[3] we computed as 2, but we missed (1,2) in ans[3]? Let's recompute for N=3: numbers 1,2,3. Pairs: (1,2): xor=3 <=3? 3<=3 yes, so qualifies. (1,3): xor=2 <=3 yes. (2,3): xor=1 <=3 yes. So there are three pairs! So ans[3] should be 3, not 2. My earlier was wrong: For N=3, indeed all three pairs satisfy condition. So ans[3]=3. Then for N=4, new pairs (1,4):5>4; (2,4):6>4; (3,4):7>4; no new, so ans[4]=3. That matches sample.
Thus base ans[2]=0, ans[3]=3, ans[4]=3.
Now increment approach: ans[N] = ans[N-1] + count_{i=1}^{N-1} ( (i xor N) <= N ).
We need to compute this count quickly for each N. Since N up to 2000, we can loop i from 1 to N-1 (max 1999) and compute xor and compare. That's O(N) per N, total O(N^2) sum_{N=2}^{2000} N ≈ 2 million, which is fine. Even if we do it for each test individually, O(N^2) per test might be okay? But we can precompute once.
Thus algorithm:
Precompute array ans[0..2000] (ans[0]=0, ans[1]=0? but N starts at 2). For N from 2 to 2000:
cnt = 0
for i in range(1, N):
if (i ^ N) <= N:
cnt += 1
ans[N] = ans[N-1] + cnt
But careful: ans[N-1] already includes pairs among 1..N-1. So we add new pairs with N.
Complexities: For N=2000, loop i=1..1999, that's about 20002000/2 = 2 million operations total (sum of N from 2..2000). Actually sum_{N=2}^{2000} (N-1) ≈ (20001999)/2 ≈ 2 million. Very acceptable.
Thus we can precompute ans[2..2000] quickly.
Edge Cases: N=2 => ans[2] = ans[1] + count_i from 1 to 1: (1^2)=3 >2 =>0. So ans[1] should be 0. So initialize ans[1]=0.
Now answer queries: read T, then for each N, print ans[N].
Let's test with sample:
Precompute:
N=2: i=1: 1^2=3>2 => cnt=0; ans[2]=0.
N=3: i=1: 1^3=2<=3 cnt=1; i=2:2^3=1<=3 cnt=2; ans[3]=ans[2]+2=0+2=2? Wait we earlier said ans[3] should be 3. There's discrepancy. Because ans[3] should be count of pairs among 1..3. According to incremental: ans[3] = ans[2] + number of i in [1,2] such that i xor 3 <= 3. That gave 2. But we missed the pair (1,2) which was already counted in ans[2]? But ans[2] was 0 because for N=2, pair (1,2) didn't satisfy? Wait check: For N=2, the condition is i xor j <= N. For N=2, the only pair (1,2) had xor=3, which is >2, so it doesn't satisfy. So ans[2]=0 correctly. But for N=3, ans[3] includes pairs among {1,2,3} that satisfy condition. The pair (1,2) now qualifies? Condition: i xor j <= N (which is 3). For pair (1,2), xor=3 <=3, so it qualifies. But it was not counted in ans[2] because at N=2 it didn't qualify. However, when we go to N=3, we only add pairs involving 3. The pair (1,2) does not involve 3, so it should be included in ans[3] but not in ans[2] nor added via incremental. That suggests our incremental formula is wrong because the condition for a pair (i,j) depends on N. The problem: "unordered pairs of distinct integers from 1 to N whose bit-wise XOR does not exceed N." The N is the given number. For each N, we consider the set {1,...,N} and count pairs with XOR <= N. So it's not a cumulative property: the condition depends on the specific N. For a smaller N, the threshold is smaller. So the answer for N=3 is not necessarily ans[2] plus something, because the threshold changes for existing pairs. For N=3, the threshold is 3, so the pair (1,2) that previously (for N=2) didn't meet threshold now does meet it. So we cannot build cumulative table by just adding new pairs with the new number; we need to recompute all pairs for each N, because the condition threshold changes.
Thus we need to compute each answer independently, or find a way to compute cumulative by adjusting for threshold changes.
We could precompute for all N by brute-force double loop for each N: for i from 1 to N-1, for j from i+1 to N, check xor <= N. That would be O(N^3) if done separately, not good.
But we can compute all answers for all N up to 2000 efficiently using some kind of DP or using the property that xor <= N. Since N max 2000, we can also compute a table of XORs and then for each N, count pairs with xor <= N. There are about 2000*2000/2 ≈ 2 million pairs total (since max N=2000, total pairs across all numbers is C(2000,2) ≈ 2e6). Actually C(2000,2) = 1,999,000. That's the total number of distinct pairs (i,j) with 1 <= i < j <= 2000. If we precompute all those pairs and their XOR values, we can then for each N, count how many of those pairs with both numbers <= N have XOR <= N. But note: the condition for each N is XOR <= N, and also both numbers must be <= N. So for each N, we need to consider subset of pairs where max(i,j) <= N. Since N increases, we can process N in increasing order and maintain a data structure that adds new pairs involving N, and also checks condition for all pairs as threshold changes? Actually the threshold N itself increases, so a pair that previously did not satisfy XOR <= (N-1) might now satisfy XOR <= N if XOR = N. So we need to adjust the count accordingly. However, we can avoid recomputing from scratch each N by iterating over XOR values.
Idea: For each unordered pair (i,j) with i<j, compute XOR = x = i xor j. The pair contributes to answer for all N such that N >= max(i,j) and N >= x. Actually condition: N must be at least j (since j is the larger number? Not necessarily: i and j are both <= N, but the condition is that both numbers are from 1 to N, so N must be >= max(i,j). And also the XOR must not exceed N, i.e., x <= N. So the pair contributes to ans[N] for all N >= max(i,j, x) = max(j, x) (since j >= i). Actually careful: The pair (i,j) is defined only if both are in the set {1..N}, so N must be at least j (since j is the larger). Also condition: x <= N. So for a given pair (i,j) with j and x, it contributes to ans[N] for N >= max(j, x). But also N is integer between 2 and 2000. So ans[N] = number of pairs (i,j) with i<j <= N and x <= N. Equivalently: For each N, count pairs with j <= N and x <= N.
Thus we can precompute all pairs and then for each N from 2 to 2000, we need to count pairs with j <= N and x <= N.
We can loop N from 1 to 2000 and add contributions as we increase N.
Approach:
Initialize an array count[0..2000] maybe for XOR values? Not exactly.
We can think: For each pair (i,j) with j fixed (the larger), the pair will start contributing to ans[N] once N is at least max(j, x). So we could precompute for each pair its "activation N0" = max(j, x). Then for N from N0 to 2000, the pair contributes 1 to ans[N]. Then ans[N] is cumulative sum of contributions from all pairs with activation <= N. So we can do: For each pair, compute N0 = max(j, i xor j). Then increment an array add[N0] by 1. Then after processing all pairs, compute ans[N] = ans[N-1] + add[N]? Actually careful: The contributions are not additive simply because as N increases, we include more pairs. For a given N, ans[N] = total number of pairs with activation N0 <= N. So if we have an array contrib[N0] = number of pairs with activation exactly N0, then ans[N] = sum_{k=2}^{N} contrib[k] (since activation can be from 2 upward). But note: Some pairs might have activation less than the minimal N? e.g., pair (1,2) has j=2, x=3, so activation = max(2,3)=3. So contrib[3]++. For pair (1,3): j=3, x=2 -> activation = max(3,2)=3, contrib[3]++. For pair (2,3): j=3, x=1 -> activation = max(3,1)=3, contrib[3]++. So for N=3, ans[3] = sum_{k<=3} contrib[k] = contrib[3] = 3. For N=4, ans[4] = sum_{k<=4} contrib[k] = contrib[3] + contrib[4]? But from earlier, ans[4]=3, and there are no pairs with activation 4? Let's check pairs with N0=4: need pairs where max(j,x)=4. For N=4, possible pairs: (1,4): j=4,x=5 => activation = max(4,5)=5; (2,4): activation = max(4,6)=6; (3,4): activation=7; (1,2) activation=3; (1,3) activation=3; (2,3) activation=3; (1,5) not exists yet; (2,5) etc. Also maybe (4,?) but j=4. Also (1,? ) no. So no activation=4. So ans[4]=3, correct. For N=5: we need to consider pairs up to j<=5. Additional pairs: (1,5): j=5, x=4? 1 xor5 = 4, activation = max(5,4)=5; (2,5): j=5, x=7? 2 xor5 =7 -> activation=7; (3,5): 3 xor5=6 -> activation=6; (4,5): 4 xor5=1 -> activation=5 (since max(5,1)=5). So activation=5 contributions from (1,5) and (4,5) maybe? Also (2,5) activation=7, (3,5)=6. So contrib[5] would get 2. Also possibly (2,? ) etc. Also need to include earlier pairs with activation 3, so ans[5] = 3 + 2 =5? Let's compute manually for N=5: numbers 1..5. Pairs: (1,2): xor=3 <=5 yes; (1,3):2<=5; (1,4):5<=5? 1 xor4=5 <=5 yes? Actually 1 xor4=5, yes <=5; (1,5):4<=5; (2,3):1<=5; (2,4):6<=5? no; (2,5):7<=5? no; (3,4):7<=5? no; (3,5):6<=5? no; (4,5):1<=5 yes. So qualifying pairs: (1,2),(1,3),(1,4),(1,5),(2,3),(4,5) => 6 pairs. That's 6, not 5. So my count is off: Did we miss (2,4) etc? No, they don't qualify. So total 6. So ans[5] should be 6. According to activation sum: earlier pairs contributed 3 (activation<=3). Now contributions with activation<=5 include activation=4 and 5. We had none at activation=4. For activation=5 we had (1,5) and (4,5) => 2, so sum=5, but we need 6. So missing something: Pair (1,4) should be counted? Check (1,4): j=4, x=5, activation = max(4,5)=5. Indeed (1,4) activation = 5 as well! But in my earlier enumeration for N=4, I said (1,4) didn't qualify because XOR=5 >4, so for N=4 it's not counted. For N=5, it is counted. So (1,4) has activation 5. Did I include (1,4) in my list for N=5? Yes, we have (1,4) qualifies. So we need to count (1,4) as having activation 5. But in my earlier activation calculation for pairs with j up to 5, I included (1,4) as j=4 (since j is the larger number), which we need to consider for N>=4. But when we are building pairs for all possible i<j up to maxN=2000, (1,4) is a pair with j=4 and x=5, so activation = max(4,5)=5. That means it contributes to ans[N] for N>=5. So for N=4, it does not contribute. For N=5, it does. So contrib[5] should include (1,4) as well. In my earlier count for activation=5 from new pairs with j=5 I omitted (1,4). So overall, we need to consider all pairs (i,j) with j up to Nmax, and for each pair compute activation N0 = max(j, i xor j). Then for each N from N0 to Nmax, the pair contributes. So ans[N] = number of pairs with activation <= N.
Thus we can precompute all pairs (i,j) for 1 <= i < j <= 2000 ( about 2 million pairs ), compute activation = max(j, i xor j). But careful: activation may be > Nmax? Since i xor j can be up to maybe around 2047? For i,j up to 2000, xor can be up to about 2047 (since 2000 < 2048). So activation could be up to 2047. But our Nmax is 2000, so pairs with activation > 2000 will never contribute to any ans[N] because N only goes to 2000. So we can ignore them, or cap. But we need to count only those with activation <= 2000. So we can compute for each pair, if max(j, xor) <= 2000, then we increment contrib[activation] by 1. Then compute prefix sums to get ans[N] for N=2..2000.
But is this correct? Check pair (2,5): j=5, x=7, activation=7 > 2000? Actually 7 <=2000, so it would be included? But activation=7, so for N>=7, this pair would contribute? But does it actually satisfy condition for N=7? Let's test: For N=7, numbers 1..7, pair (2,5) XOR=7 <=7, so qualifies. So yes, it contributes to ans[7] onward. So we must include it if activation <=2000. But for Nmax=2000, it will be counted for N>=7. So that's fine.
But note: The pair (2,5) has j=5 and x=7, so activation = 7. That means for N=5, it does not contribute (since 7>5). For N=6, also not. For N=7, it does. So our method works: Only include if activation <=2000, then add to contrib[activation]. But what about pairs where activation <=2000 but j may be > activation? Actually activation = max(j, x) >= j, so activation is always >= j. So if activation <=2000, then j <=2000 automatically. So all pairs with j<=2000 are considered. However, some pairs may have x > 2000 but j <=2000, activation = x >2000, then they are never counted, which is correct because for N up to 2000, x>N always, so they never satisfy condition. So we can filter.
Thus algorithm:
- Set MAX_N = 2000.
- Initialize array contrib of size MAX_N+1 (or maybe MAX_N+2) to zero.
- For j from 2 to MAX_N:
For i from 1 to j-1:
x = i ^ j
act = max(j, x)
if act <= MAX_N:
contrib[act] += 1
- After processing all pairs, compute prefix sum: ans[0]=0; for n in 1..MAX_N: ans[n] = ans[n-1] + contrib[n]
- But note: For N=1, there are no pairs, ans[1]=0, but N starts from 2. So for each query, output ans[N].
Check with sample:
Compute for MAX_N=8 maybe.
Let's manually compute contributions for pairs up to 8.
We'll try to compute activation for each pair.
List pairs (i,j) with j up to 8.
j=2: i=1: x=3, act = max(2,3)=3 <=2000, so contrib[3]++.
j=3: i=1: x=2, act = max(3,2)=3, contrib[3]++; i=2: x=1, act = max(3,1)=3, contrib[3]++.
j=4: i=1: x=5, act = max(4,5)=5, contrib[5]++; i=2: x=6, act=6; i=3: x=7, act=7.
j=5: i=1: x=4, act = max(5,4)=5, contrib[5]++; i=2: x=7, act=7; i=3: x=6, act=6; i=4: x=1, act = max(5,1)=5, contrib[5]++.
j=6: i=1: x=7, act=7; i=2: x=4, act = max(6,4)=6, contrib[6]++; i=3: x=5, act = max(6,5)=6? Actually 5<6 so act=6, contrib[6]++; i=4: x=2, act=6? 2<6, so act=6, contrib[6]++; i=5: x=3, act=6? 3<6, so act=6, contrib[6]++.
j=7: i=1: x=6, act = max(7,6)=7; i=2: x=5, act=7? 5<7 => act=7; i=3: x=4, act=7; i=4: x=3, act=7; i=5: x=2, act=7; i=6: x=1, act=7.
j=8: i=1: x=9, act = max(8,9)=9 >8? 9<=2000? 9>MAX_N? Actually MAX_N=8 for sample? We'll compute up to 8. 9>8, so act>8, we ignore if act>MAX_N? But for overall answer up to 8, if act=9, then it does not contribute for any N≤8. So we ignore. i=2: x=10 act=10 ignore; i=3: x=11 act=11 ignore; i=4: x=12 act=12 ignore; i=5: x=13 act=13 ignore; i=6: x=14 act=14 ignore; i=7: x=15 act=15 ignore.
Now, let's sum contributions per activation value up to 8:
activation 3: from j=2 (1), j=3 (2) = total 3. So contrib[3]=3.
activation 5: from j=4 i=1, j=5 i=1 and i=4 => 3? Actually j=4 i=1 gives act=5; j=5 i=1 act=5; j=5 i=4 act=5. Also any other? j=... maybe j=... also j=6? i? act=5? no, j=6 i? act=6 etc. j=... So contrib[5]=3.
activation 6: from j=4 i=2? act=6; j=4 i=3 act=7 not 6; j=5 i=2 act=7; j=5 i=3 act=6? Actually j=5 i=3: x=6, act=max(5,6)=6; So that's one. Also j=6: i=2 act=6; i=3 act=6; i=4 act=6; i=5 act=6. That's 4. Plus j=5 i=3 =1, j=4 i=2=1? Wait j=4 i=2: x=6, act=max(4,6)=6, yes that's another. So total: from j=4 i=2 =>1; j=5 i=3 =>1; j=6 i=2,i=3,i=4,i=5 =>4; total 6? But also maybe j=... Did we count j=5 i? Also j=5 i? None else. Also j=... So contrib[6]= 1+1+4=6. But also check j=4 i=2 is indeed act=6; j=5 i=3 act=6; j=6 i=2,3,4,5 act=6. That's 1+1+4=6. Also maybe j=3? no. j=7? act=7. So contrib[6]=6.
activation 7: from j=4 i=3: act=7; j=5 i=2: act=7; j=6 i=1: act=7; j=7 all i=1..6? That's 6 pairs, each act=7 (since j=7, x ranges 6,5,4,3,2,1 all less than 7, so act=7). So total: j=4 i=3:1; j=5 i=2:1; j=6 i=1:1; j=7 i=1..6:6; total 9? Also j=... Are there any others? j=8? act maybe 7? i? 8 xor something might be 7? 8 xor 15? no. So contrib[7]= 1+1+1+6=9. But also j=... possibly j=... Let's double-check j=5 i? i=2 gave act=7, i=3 gave act=6, i=1 act=5, i=4 act=5. j=4 i=3 gave 7. j=6 i=1 gave 7. j=7 all gave 7. So 9 is correct.
activation 8: Are there any pairs with act=8? For act=8, need max(j, x)=8. Since j <=8. So possibilities: j=8 and x<=8 => act=8 if x<=8? Actually act = max(j, x). If j=8, then act = max(8, x) = 8 if x<=8, else >8. But x=i^8. For i<8, x can range up to 15. For i=8? Not allowed i<j. So we need i from 1..7: compute i^8. Values: 1^8=9>8; 2^8=10; 3^8=11;4^8=12;5^8=13;6^8=14;7^8=15. All >8. So no act=8 from j=8. Also j<8 with x=8? For j<8, act could be 8 if x=8 and j<8, then max(j,8)=8. So we need pairs where i xor j = 8, and j<8. For example, i=1, j=9? no j<8. Let's find i<j≤7, i xor j = 8. Since max xor of numbers ≤7 is less than 8? Actually numbers up to 7, max xor is 7 (since 7 xor 0=7). So impossible to get 8. So act=8 none. So contrib[8]=0.
Now prefix sums:
ans[1]=0 (since no pairs with activation <=1? Actually contrib[1]=0, so ans[1]=0.)
ans[2]: sum up to 2: contrib[2] is 0, so ans[2]=0. (Matches)
ans[3]: contrib[3]=3, so ans[3]=3. (Matches sample? For N=3 we should get 3? Not given but it's 3.)
ans[4]: add contrib[4]=0 => ans[4]=3. (Matches sample)
ans[5]: add contrib[5]=3 => ans[5]=6. (Matches our manual count of 6 for N=5? We got 6 earlier, yes.)
ans[6]: add contrib[6]=6 => ans[6]=12. (Sample for N=6 is 12, matches.)
ans[7]: add contrib[7]=9 => ans[7]=21. (Sample for N=8 is 21? Wait sample: N=8 output 21. But here we got ans[7]=21. Let's compute ans[8] by adding contrib[8]=0 => ans[8]=21. That matches sample N=8 output 21. So good.)
Thus the activation method works.
Now we need to implement for N up to 2000. We'll generate all pairs (i, j) with 1 <= i < j <= 2000. For each compute x = i ^ j, act = max(j, x). If act <= 2000, then contrib[act] += 1.
Time complexity: total pairs = C(2000,2) = 1,999,000 ≈ 2e6. For each pair, we compute XOR and a max, and if condition holds, we increment an array. That's about 2 million operations, trivial.
Memory: contrib array size 2001 ints.
Thus we can precompute answers for all N from 1 to 2000 in O(MAX_N^2) and then answer queries O(1).
One nuance: The condition says "unordered pairs of distinct integers from 1 to N". So for each N, we count pairs with both numbers in [1,N] and XOR <= N. Our method counts pairs with activation <= N. Is that exactly the same as the definition? Let's verify: For a given pair (i,j) with i<j, it is counted in ans[N] if j <= N and x <= N. That's equivalent to max(j, x) <= N. So activation = max(j, x). If activation <= N, then the pair is counted. So yes.
But careful: What about pairs where both numbers are <= N, but XOR <= N. That's exactly max(j, x) <= N, because j is the larger number, so j <= N ensures both <= N. So indeed condition is max(j, x) <= N. So counting pairs with activation <= N is correct.
Thus precomputation as described yields correct ans.
Potential pitfalls: We must ensure we treat activation exactly as max(j, x). For j up to 2000, x can be up to 2047 (since 2000 < 2048). So activation may be >2000, we ignore. That's fine.
Now we also need to ensure that for each pair we only count once. Since we iterate i from 1 to j-1, we cover each unordered pair exactly once.
Now let's code.
Implementation Steps:
- Set MAX = 2000.
- Initialize list contrib of length MAX+2 (index up to MAX maybe) with zeros.
- For j in range(2, MAX+1):
for i in range(1, j):
x = i ^ j
act = max(j, x)
if act <= MAX:
contrib[act] += 1
- Then compute ans list of length MAX+1:
ans = [0]*(MAX+1)
total = 0
for n in range(1, MAX+1):
total += contrib[n]
ans[n] = total
- Note: ans[1] will be 0, but N starts at 2, but we can still compute.
- Input T, then for each N, print ans[N].
Check sample.
Edge Cases: N=2, answer 0. N=3, answer 3? But sample didn't include N=3; but our method yields 3. That's fine.
Double-check for some values manually perhaps.
Let's test with N=2: pairs (1,2): xor=3 >2 so not counted, answer 0.
For N=3: pairs all three counted, answer 3.
N=4: as sample, 3.
Our computed contributions for MAX=4 (simulate quickly):
pairs up to 4: (1,2): act=3; (1,3): act=3; (2,3): act=3; (1,4): act=5>4 ignore; (2,4): act=6 ignore; (3,4): act=7 ignore. So contrib[3]=3, others zero. ans[2]=0, ans[3]=3, ans[4]=3.
Works.
Now complexity: For MAX=2000, loops: j from 2 to 2000, inner i from 1 to j-1, total ~ 2 million. Python can handle easily.
But careful: In Python, nested loops with 2 million iterations is fast (<0.1 sec). Actually 2 million is trivial. Even if we do 2 million loops with simple operations, it's fine.
Now we need to output answer per test case.
Thus solution is straightforward.
We should also consider if there's any overflow? Count of pairs for N=2000 could be large. Let's estimate maximum possible number of pairs: total pairs C(2000,2) = 1,999,000. The answer is at most that. So fits in 32-bit int, but Python int fine.
Now we need to verify with some more test manually, maybe N=10, to ensure.
We'll compute quickly using code mental? Could do small.
But we trust the method.
Double-check the activation condition: For a pair (i,j) with i<j, it is counted for N if j <= N and i xor j <= N, i.e., max(j, xor) <= N. So yes.
Now what about the possibility that i xor j could be less than i or j? No problem.
Thus solution is good.
Now we need to write the answer.
We'll precompute as described.
But careful: In the loops, we use range(2, MAX+1) for j, and for i in range(1, j). That's fine.
Now possible optimization: The activation condition act <= MAX can be checked early; but it's fine.
Implementation:
def precompute(MAX=2000):
contrib = [0]*(MAX+1)
for j in range(2, MAX+1):
for i in range(1, j):
x = i ^ j
act = j if j >= x else x
if act <= MAX:
contrib[act] += 1
ans = [0]*(MAX+1)
total = 0
for n in range(1, MAX+1):
total += contrib[n]
ans[n] = total
return ans
Then read input, for each N print ans[N].
Edge Cases: N can be as low as 2, as per constraints.
But note: The problem statement says 2 ≤ N ≤ 2000. So we don't need to handle N=1. But our ans[1] is defined anyway.
Now test with given sample:
MAX=2000 precompute, then output for 4,6,8: should be 3,12,21.
Now ensure we consider the case where activation exactly equal to N? We include it because activation <= N includes equality. In our prefix sum, we add contrib[act] at index act. Then ans[N] includes contributions for all act <= N. Good.
Now is there any case where a pair with activation = N but j > N? That can't happen because activation >= j. If activation = N and j > N, then j > N, but activation = max(j,x) would be at least j > N, contradicting activation = N. So j <= N automatically. So all good.
Thus solution done.
Now consider if there's a more efficient way to compute contributions without double loop over all pairs? For N=2000, it's fine.
But note: T up to 100, N up to 2000, so precomputing once is best.
Now implement code accordingly.
One more check: The problem statement says "unordered pairs of distinct integers from 1 to N whose bit-wise XOR does not exceed N." So for each pair, both numbers are in 1..N, and XOR <= N. That's exactly as we did.
Now let's test a few more manually to ensure correct.
N=2: ans=0. N=3: ans=3. N=5: ans=6 as we computed. N=6: ans=12. N=7: ans=21? Wait earlier we had ans[7]=21? Actually we computed ans[7]=21 (since ans[6]=12, contrib[7]=9, so 21). But sample for N=8 is 21, so ans[8]=21 means ans[7] might be less? Actually we computed ans[7] = ans[6] + contrib[7] = 12+9=21. So ans[7]=21, ans[8]=21+0=21. So both 7 and 8 give 21? That seems plausible: For N=7, we counted all pairs up to 7 with XOR <=7. For N=8, maybe no new pairs that satisfy? Let's test manually for N=8: numbers 1..8. Pairs that qualify? Let's list quickly: We'll compute using our method. Pairs up to 7 gave 21. Now add pairs with 8: (1,8): xor=9 >8 no; (2,8):10>8; (3,8):11>8; (4,8):12>8; (5,8):13>8; (6,8):14>8; (7,8):15>8. So no new qualifying pairs. So ans[8]=21. So sample is consistent.
Thus ans[7] also 21? That would be interesting because N=7 and N=8 same answer? Check if any pair with j=7 and xor<=7? All pairs with j=7 were counted in ans[7] already. So ans[7] includes all pairs with j<=7. For N=8, it includes same plus any pairs with j=8 that satisfy xor<=8. As none, answer same. So indeed ans[7] =21, ans[8]=21. So fine.
Now verify ans[9] maybe: we could compute activation for j=9 and see if any new pairs. But not needed.
Now we should confirm that our precomputation for MAX=2000 includes all pairs with activation up to 2000. Since activation can be as high as max(j, x). For j up to 2000, x can be up to 2047, so activation up to 2047. But we only care up to 2000. So we ignore those with act >2000. That's correct.
Now what about pairs where activation >2000 but j <=2000 and x <=2000? Actually if activation >2000, it means either j>2000 (not possible as j loop stops at 2000) or x >2000. So if x >2000, then for N up to 2000, since x>N always, it never qualifies. So correct to ignore.
Thus solution stands.
Now we produce final answer.
But before finalizing, let's think about correctness for all N.
We can also test small N by brute force to verify.
N=1: none, ans=0 (though not in constraints).
N=2: (1,2): xor=3>2 ->0.
N=3: all three ->3.
N=4: (1,2),(1,3),(2,3) ->3.
N=5: we found 6.
N=6: we found 12.
N=7: 21.
N=8: 21.
N=9: Let's compute manually to test further.
We'll write a quick mental/approximate computation using activation method.
We can compute contributions up to activation 9 for pairs up to j=9.
But let's brute by writing code mentally? Might be overkill. We'll trust.
Now we should consider if the problem expects the answer to be "the number of times she has to tap", which is the count we computed. So output is integer.
Now implement.
Implementation details:
- Read T.
- Precompute ans list up to 2000.
- For each test, read N, print ans[N].
Potential pitfalls: Input may have extra spaces, but using int conversion fine.
Now let's test with sample:
Input:
3
4
6
8
Our precomputed ans[4]=3, ans[6]=12, ans[8]=21. Output matches.
Now test with N=3 (if we wanted): output 3.
Now check N=2: 0.
Now check N=2000: we'll compute some value, but not needed.
Thus final solution.
Now I'll produce a clear explanation in the final answer.
But before final, double-check the loop bounds: In Python, for j in range(2, MAX+1): includes 2 through MAX inclusive. For i in range(1, j): includes 1 through j-1. That's correct.
Now small nuance: For j=2, i=1: x=3, act=3, if act<=MAX (2000), contrib[3]++. Good.
Now we must ensure that we don't access contrib[act] out of range if act exactly equals MAX. contrib list size is MAX+1? We defined contrib = [0] * (MAX+1). Indices 0..MAX inclusive. So act can be from 1 to MAX. So act=MAX allowed. So fine.
Now compute ans: for n from 1 to MAX: total += contrib[n]; ans[n] = total. So ans[MAX] includes all contrib up to MAX.
Now check that we correctly count pairs where activation <= N. For N=MAX, ans[MAX] is sum of contrib[act] for act <= MAX. That includes all pairs with activation <= MAX. But are there any pairs with j <= MAX but activation > MAX? Those are not counted, which is correct because they don't qualify for N=MAX (since need activation <= MAX). So ans[MAX] is correct.
Now verify that we haven't missed any pairs where activation <= MAX but we didn't include because we only considered act <= MAX? We included those exactly. So good.
Now consider if for some pair activation <= N but j > N? Not possible because activation >= j.
Thus algorithm stands.
Now produce final solution.
But wait: The pair (i,j) where i xor j = j? That could be if i=0, but i starts at 1. So not an issue.
One more nuance: The problem says "distinct integers from 1 to N". So we consider unordered pairs (i,j) with i != j, both between 1 and N inclusive. So we treat (i,j) same as (j,i). Our loops over i<j cover each unordered pair exactly once. So fine.
Now final answer.
But before concluding, let's think about if there is any subtlety: The XOR condition is "does not exceed N" i.e., <= N. So we used <=. Our activation uses max(j, x) <= N. Since j is integer, x is integer, and we check <= N. So correct.
Now we can write solution.
Now I'm going to produce explanation and code.
In the final answer, I'll include:
- Explanation of condition: count pairs (i,j) with 1≤i<j≤N and i xor j ≤ N.
- Precomputation approach: For each possible unordered pair (i,j) with 1≤i<j≤2000, compute m = max(j, i xor j). If m ≤ 2000, then this pair will be counted for all N ≥ m. So we increment contrib[m] by 1. Then ans[N] = sum_{k=1}^{N} contrib[k].
- Complexity: O(MAX^2) precomputation, O(1) per query.
Now produce Python code.
Note: In Python, loops may be slightly slow if we use pure Python but 2 million iterations is trivial (about 0.05-0.1 seconds). So fine.
One optimization: Use local variables, maybe use list comprehension? But fine.
Now I'll write:
import sys
MAX = 2000
contrib = [0]*(MAX+1)
for j in range(2, MAX+1):
for i in range(1, j):
x = i ^ j
if x <= j:
act = j
else:
act = x
if act <= MAX:
contrib[act] += 1
ans = [0]*(MAX+1)
total = 0
for n in range(1, MAX+1):
total += contrib[n]
ans[n] = total
def solve():
import sys
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
out.append(str(ans[n]))
sys.stdout.write("\n".join(out))
if __name__ == "__main__":
solve()
We should verify that for j loop includes all j up to MAX, and i from 1 to j-1. This covers all pairs.
Now test with sample.
I'll quickly mentally test: The code should produce correct output.
One more subtlety: For N=2, ans[2] is computed as sum contrib[1]+contrib[2]. contrib[1] is 0, contrib[2] is 0 (since no pair has activation 2). So ans[2]=0. Good.
Now for N=3, contrib[1]=0, contrib[2]=0, contrib[3]=? We'll see from precomputation: For j=2, i=1 gives act=3, contrib[3]++. For j=3, i=1 gives act=3, i=2 gives act=3, contrib[3] gets +2, total contrib[3]=3. So ans[3]=3. Good.
Now for N=4, ans[4] = ans[3] + contrib[4]. contrib[4] is 0, so ans[4]=3. Good.
Now for N=6, we should get 12. Our earlier contributions: contrib[3]=3, contrib[5]=3, contrib[6]=6, contrib[4]=0? So sum up to 6: 3+0+3+6=12. Good.
Now all set.
But wait: Did we include j=5 i=3? That contributed to act=6. In our loops, for j=5, i=3, x=6, act=6, added to contrib[6]. Good.
Now done.
Now we should ensure we handle T up to 100.
Now edge case: Input might have N=2000, which is within MAX, ans[2000] will be computed.
Now code final.
But is there any possibility that a pair with activation <= MAX but x > MAX? Actually if x > MAX, then activation = x (since x > j? maybe, but if x > MAX, then activation > MAX, so it wouldn't be added because we check act <= MAX. So correct.
Thus solution correct.
Now final answer includes explanation and code.
Now produce final answer. ```python
import sys
MAX_N = 2000
------------------------------------------------------------
Precompute contributions: contrib[m] = number of unordered pairs
(i, j) with 1 <= i < j <= MAX_N and max(j, i^j) == m
------------------------------------------------------------
contrib = [0] * (MAX_N + 1)
for j in range(2, MAX_N + 1):
for i in range(1, j):
x = i ^ j
# activation value = max(j, i^j)
act = j if j >= x else x
if act <= MAX_N:
contrib[act] += 1
------------------------------------------------------------
Build answers: ans[N] = total pairs with activation <= N
------------------------------------------------------------
ans = [0] * (MAX_N + 1)
total = 0
for n in range(1, MAX_N + 1):
total += contrib[n]
ans[n] = total
------------------------------------------------------------
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
out.append(str(ans[n]))
sys.stdout.write("\n".join(out))
if name == "main":
solve()