//gpt
#ifndef LOCAL
#include <bits/stdc++.h>
#define debug(...)
#endif
using namespace std;
#define int long long
#define cinv(v) for (auto &it : v) cin >> it;
#define coutv(v) for (auto &it : v) cout << it << ' '; cout << '\n';
const int MX = 30;
const int N = 300000 + 5;
int a[N];
struct trie {
int tot;
trie *a[2];
trie() : tot(0) {
a[0] = a[1] = nullptr;
}
};
trie root;
void insert_x(int x) {
trie *cur = &root;
cur->tot++;
for (int b = MX; ~b; --b) {
int bit = (x >> b) & 1;
if (!cur->a[bit])
cur->a[bit] = new trie();
cur = cur->a[bit];
cur->tot++;
}
}
void erase_x(int x) {
trie *cur = &root;
cur->tot--;
for (int b = MX; ~b; --b) {
int bit = (x >> b) & 1;
cur = cur->a[bit];
cur->tot--;
}
}
int count_greater(int i) {
// count how many stored x satisfy: (i ^ a[i]) < (i ^ x)
trie *cur = &root;
int left = i ^ a[i];
int cnt = 0;
for (int b = MX; ~b; --b) {
if (!cur) break;
int lb = (left >> b) & 1; // the bit of (i ^ a[i])
int ib = (i >> b) & 1; // the bit of i
if (lb == 0) {
// any x whose (i^x)_b == 1 makes the whole number larger
// (i^x)_b == 1 <=> x_b == ib^1
if (cur->a[ib ^ 1])
cnt += cur->a[ib ^ 1]->tot;
// then continue down the branch where (i^x)_b == 0
cur = cur->a[ib];
} else {
// left_b == 1, we can only tie & continue if (i^x)_b == 1
cur = cur->a[ib ^ 1];
}
}
return cnt;
}
void solve() {
int n;
cin >> n;
for (int i = 1; i <= n; ++i)
cin >> a[i];
// reset trie
root = trie();
// insert all x_j = j ^ a[j]
for (int j = 1; j <= n; ++j)
insert_x(j ^ a[j]);
long long ans = 0;
// for each i, remove its own x_i and count how many remaining satisfy
// (i^a[i]) < (i^x_j)
for (int i = 1; i <= n; ++i) {
erase_x(i ^ a[i]);
ans += count_greater(i);
}
cout << ans << "\n";
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
while (T--) solve();
return 0;
}