#include <bits/stdc++.h>
using namespace std;
// Speed
#define fast_io ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
// Typedefs
#define int long long
#define pb push_back
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((int)(x).size())
#define endl '\n'
#define yes cout << "yes\n"
#define no cout << "no\n"
// Loops
#define rep(i,a,b) for(int i=a;i<b;++i)
#define per(i,a,b) for(int i=b-1;i>=a;--i)
#define each(x, a) for (auto& x : a)
// Consts
const int INF = 1e18;
const int MOD = 1e9+7;
const int N = 2e5 + 5;

void solve() {
    int n;
    cin>>n;
    vector <int> a(n),b(n);
    rep(i,0,n){
    	cin>>a[i]>>b[i];
    }
    int count=0;
    rep(i,0,n){
    	rep(j,i+1,n){
    		if((a[i]>a[j] && b[i]<b[j]) || (a[i]<a[j] && b[i]>b[j]) ) count++;
    	}
    }
    cout<<count<<endl;
    
}

int32_t main() {
    fast_io;
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}