//* Problem  : B Cell Phones
//* Contest  : acm.tju.edu.cn/toj
//* Type     : Solution
//* Date     : 2008.08.05
//* Author   : alt
//* Language : C++
//* Compiler : Microsoft Visual C++ 6.0

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <vector>
#include <queue>
#include <string>
#include <stack>
#include <set>
#include <map>
#include <algorithm>

using namespace std;

typedef pair <int,int> PIII;
typedef vector <int> VI;
typedef vector <vector<int> > VVI;
typedef vector <pair<int,int> > VPII;
typedef vector <vector<pair<int,int> > > VVPII;

#define int64 long long
#define PI (2.0 * acos(0.0))
#define MP make_pair
#define PB push_back
#define SZ(a) (int)a.size()
#define FOR(i, n) for (int i = 0; i < (int)n; i++)
#define FORSZ(i, a) FOR(i, SZ(a))
#define FORE(i, a, b) for (int i = (int)(a); i <= (int)(b); i++)
#define FORR(i, a, b) for (int i = (int)(a); i >= int(b); i--)
#define INF 1000000000
#define INFL 1000000000000000000LL
#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()
#define CLEARA(a) memset(a, 0, sizeof(a))

inline int si(){int tt; scanf("%d", &tt); return tt;}

struct point{
	int64 x, y;
}a[100000];

int64 y[100000];

int64 t[100000];

int n; 
int64 res;

int cmp_f(const void *a, const void *b)
{
	point *aa = (point*)a;
	point *bb = (point*)b;
	int k = aa->x - bb->x;
	if (k) return k;
	return aa->y - bb->y;
}

void inc(int i)
{
	for (; i <= n + 10; i |= i + 1)
		t[i]++;
}

int64 sum(int i)
{
	int64 res = 0;
	for (; i >= 0; i = (i & (i+1)) - 1)
		res += t[i];
	return res;
}

void solve()
{
	sort(y, y + n);
	reverse(y, y + n);
	map <int, int> mp; int imp = 0;
	FOR(i,n)
		if (!mp[y[i]]) mp[y[i]] = ++imp;
	FOR(i,n)
		a[i].y = mp[a[i].y];
	qsort(a, n, sizeof(a[0]), cmp_f);
	memset(t, 0, sizeof(t)); res = 0;
	FOR(i, n)
	{
		int64 y = a[i].y;
		res = res + (int64)sum(y);
		inc(y);
	}
}

void result()
{
	printf("%.0lf\n", 1.0 * res);
}

int main()
{
#ifdef _DEBUG
	freopen("1064", "r", stdin);
	freopen("1064w", "w", stdout);
#endif
	int nt = si();
	while (nt--)
	{
		n = si();
		FOR(i,n)
		{
			a[i].x = si(); 
			a[i].y = si();
			y[i] = a[i].y;
		}
		solve();
		result();
	}
	return 0;
}

