#include <bits/stdc++.h>
using namespace std;
vector<int> ord2;
int f(int l, int r){
int ans = 0;
if(l + 1 >= r) return ans;
int mid = (l + r) / 2;
ans += f(l, mid) + f(mid, r);
int i = l, j = mid;
while(i < mid && j < r){
if(ord2[i] < ord2[j]){
i++;
}else{
ans += mid - i;
j++;
}
}
inplace_merge(ord2.begin()+l, ord2.begin()+mid, ord2.begin()+r);
return ans;
}
void solve(){
int n;
cin >> n;
vector<pair<int, int>> vec;
for(int i = 0; i < n; i++){
int a, b;
cin >> a >> b;
vec.push_back({b, a});
}
sort(vec.begin(), vec.end(), greater<pair<int, int>>());
vector<pair<int, int>> ord;
for(int i = 0; i < n; i++){
ord.push_back({vec[i].second, i + 1});
}
sort(ord.begin(), ord.end(), greater<pair<int, int>>());
for(int i = 0; i < n; i++){
ord2.push_back(ord[i].second);
}
cout << f(0, n) << endl;
}
int main() {
int t;
cin >> t;
while(t--){
solve();
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnZlY3RvcjxpbnQ+IG9yZDI7CmludCBmKGludCBsLCBpbnQgcil7CglpbnQgYW5zID0gMDsKCWlmKGwgKyAxID49IHIpIHJldHVybiBhbnM7CglpbnQgbWlkID0gKGwgKyByKSAvIDI7CglhbnMgKz0gZihsLCBtaWQpICsgZihtaWQsIHIpOwoJaW50IGkgPSBsLCBqID0gbWlkOwoJd2hpbGUoaSA8IG1pZCAmJiBqIDwgcil7CgkJaWYob3JkMltpXSA8IG9yZDJbal0pewoJCQlpKys7CgkJfWVsc2V7CgkJCWFucyArPSBtaWQgLSBpOwoJCQlqKys7CgkJCQoJCX0KCX0KCWlucGxhY2VfbWVyZ2Uob3JkMi5iZWdpbigpK2wsIG9yZDIuYmVnaW4oKSttaWQsIG9yZDIuYmVnaW4oKStyKTsKCXJldHVybiBhbnM7IAoJCn0Kdm9pZCBzb2x2ZSgpewoJaW50IG47CgljaW4gPj4gbjsKCXZlY3RvcjxwY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