#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, N = 3e5 + 5;
int a[N];
struct trie {
int tot;
trie *a[2];
trie() : tot(0) {
a[0] = a[1] = nullptr;
}
};
trie root;
void insert(int x) {
trie *tmp = &root;
tmp->tot++;
for (int i = MX; ~i; --i) {
bool f = (x >> i) & 1;
if (tmp->a[f] == nullptr) tmp->a[f] = new trie();
tmp = tmp->a[f];
tmp->tot++;
}
}
void erase(int x) {
trie *tmp = &root;
tmp->tot--;
for (int i = MX; ~i; --i) {
bool f = (x >> i) & 1;
tmp = tmp->a[f];
tmp->tot--;
}
}
int calc(int i) {
trie *tmp = &root;
int ret = 0, left = i ^ a[i];
for (int mask = MX; mask; --mask) {
if (tmp == nullptr) break;
int l = (left >> mask) & 1;
if (l == 1) {
int j = ((i >> mask) & 1) ^ 1;
tmp = tmp->a[j];
}
else {
int j = ((i >> mask) & 1) ^ 1;
if (tmp->a[j] != nullptr) ret += tmp->a[j]->tot;
j ^= 1;
tmp = tmp->a[j];
}
}
return ret;
}
void shelby() {
int n;
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
root = trie();
for (int i = 1; i <= n; ++i) insert(a[i] ^ i);
int ans = 0;
for (int i = 1; i <= n; ++i) {
erase(a[i] ^ i);
ans += calc(i);
}
cout << ans << '\n';
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
int t = 1;
cin >> t;
for (int _ = 1; _ <= t; ++_) {
// cout << "Case " << _ << ": ";
shelby();
}
}