#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;

const int N = (int)1e5 + 100;
const int INF = (int)1e9 + 100;

int f[N];
void build()
{
	fill(f, f + N, INF);
}

void relaxVal(int pos, int val)
{
	for (int v = pos; v < N; v = (v | (v + 1)))
		f[v] = min(f[v], val);
}

int getMin(int x)
{
	int res = INF;
	for (int v = x; v >= 0; v = (v & (v + 1)) - 1)
		res = min(res, f[v]);
	return res;
}

int perm[N];
int x[N], y[N], z[N];
vector <int> listZ[N];

bool cmp(int a, int b)
{
	return z[a] < z[b];
}


int main()
{
	int T;
	scanf("%d", &T);
	while (T--)
	{
		build();
		int n;
		scanf("%d", &n);
		int ans = 0;
		for (int i = 1; i <= n; i++)
			listZ[i].clear();

		for (int i = 0; i < n; i++)
		{
			scanf("%d%d%d", &x[i], &y[i], &z[i]);
			perm[i] = i;
		}
		for (int i = 0; i < n; i++)
			listZ[z[i]].push_back(i);
		for (int i = 1; i <= n; i++)
		{
			for (int s = 0; s < (int)listZ[i].size(); s++)
			{
				int index = listZ[i][s];
				int curX = x[index];
				int curY = y[index];
				if (curX == 1 || getMin(curX - 1) >= curY)
				{
					ans++;
				}
			}
			for (int s = 0; s < (int)listZ[i].size(); s++)
			{
				int index = listZ[i][s];
				int curX = x[index];
				int curY = y[index];
				relaxVal(curX, curY);
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}