// 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();
    }
}