// F.R.I.E.N.D.S
// Time Complexity:- O(NlogN)
// Space Complexity:- O(N)
// Idea?
// 1. For Each index 'i', find the set of those intervals which contains 'i'
// 2. If you have set of intervals which contains i, store the indices of those intervals in a Fenwick Tree
// 3. Now, answer for current index 'i' will be Sum(Y[i]) - Sum(X[i] - 1)
// 4. Remember Friendship (i, i) won't be counted.
// 5. Remember Frienship (i, j) and (j, i) are same.
// 6. Hence, our answer is friendship / 2
#include <bits/stdc++.h>
using namespace std;
void Saraff(){
int N;
cin >> N;
vector <array <int, 3>> store;
vector <int> X(N + 1), Y(N + 1), BIT(N + 1);
for(int i=1; i<=N; i++){
cin >> X[i] >> Y[i];
store.push_back({X[i], Y[i], i});
}
auto Add = [&](int pos, int value){
while(pos <= N){
BIT[pos] += value;
pos += pos & -pos;
}
};
auto Sum = [&](int pos){
int res = 0;
while(pos > 0){
res += BIT[pos];
pos -= pos & -pos;
}
return res;
};
sort(store.begin(), store.end());
int j = 0;
priority_queue <array <int, 2>> pq;
int friendship = 0;
for(int i=1; i<=N; i++){
// insert all those end time whose whose start time <= i
while(j < N && store[j][0] <= i){
Add(store[j][2], 1);
pq.push({-store[j][1], store[j][2]});
j++;
}
// remove all those end time which is strictly less than i
while(!pq.empty() && -pq.top()[0] < i){
Add(pq.top()[1], -1);
pq.pop();
}
friendship += Sum(Y[i]) - Sum(X[i] - 1);
if(Sum(i) - Sum(i - 1) > 0){
friendship--;
}
}
cout << friendship / 2 << "\n";
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int test = 1;
cin>>test;
while(test--){
Saraff();
}
}
Ly8gRi5SLkkuRS5OLkQuUwovLyBUaW1lIENvbXBsZXhpdHk6LSBPKE5sb2dOKQovLyBTcGFjZSBDb21wbGV4aXR5Oi0gTyhOKQoKLy8gSWRlYT8KLy8gMS4gRm9yIEVhY2ggaW5kZXggJ2knLCBmaW5kIHRoZSBzZXQgb2YgdGhvc2UgaW50ZXJ2YWxzIHdoaWNo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sZShwb3MgPiAwKXsKICAgICAgICAgICAgcmVzICs9IEJJVFtwb3NdOwogICAgICAgICAgICBwb3MgLT0gcG9zICYgLXBvczsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIHJlczsKICAgIH07CgogICAgc29ydChzdG9yZS5iZWdpbigpLCBzdG9yZS5lbmQoKSk7CgogICAgaW50IGogPSAwOwogICAgcHJpb3JpdHlfcXVldWUgPGFycmF5IDxpbnQsIDI+PiBwcTsKCiAgICBpbnQgZnJpZW5kc2hpcCA9IDA7CiAgICBmb3IoaW50IGk9MTsgaTw9TjsgaSsrKXsKICAgICAgICAvLyBpbnNlcnQgYWxsIHRob3NlIGVuZCB0aW1lIHdob3NlIHdob3NlIHN0YXJ0IHRpbWUgPD0gaQogICAgICAgIHdoaWxlKGogPCBOICYmIHN0b3JlW2pdWzBdIDw9IGkpewogICAgICAgICAgICBBZGQoc3RvcmVbal1bMl0sIDEpOwogICAgICAgICAgICBwcS5wdXNoKHstc3RvcmVbal1bMV0sIHN0b3JlW2pdWzJdfSk7CiAgICAgICAgICAgIGorKzsKICAgICAgICB9CgogICAgICAgIC8vIHJlbW92ZSBhbGwgdGhvc2UgZW5kIHRpbWUgd2hpY2ggaXMgc3RyaWN0bHkgbGVzcyB0aGFuIGkKICAgICAgICB3aGlsZSghcHEuZW1wdHkoKSAmJiAtcHEudG9wKClbMF0gPCBpKXsKICAgICAgICAgICAgQWRkKHBxLnRvcCgpWzFdLCAtMSk7CiAgICAgICAgICAgIHBxLnBvcCgpOwogICAgICAgIH0KCiAgICAgICAgZnJpZW5kc2hpcCArPSBTdW0oWVtpXSkgLSBTdW0oWFtpXSAtIDEpOwogICAgICAgIGlmKFN1bShpKSAtIFN1bShpIC0gMSkgPiAwKXsKICAgICAgICAgICAgZnJpZW5kc2hpcC0tOwogICAgICAgIH0KICAgIH0KCiAgICBjb3V0IDw8IGZyaWVuZHNoaXAgLyAyIDw8ICJcbiI7Cn0KCnNpZ25lZCBtYWluKCl7CiAgICBp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