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

const int pow2 = (1 << 17);
const int INF = (int)1e9 + 100;

struct SegmentTree
{
	int tree[pow2 * 2];
	void build()
	{
		for (int i = 0; i < pow2 * 2; i++)
			tree[i] = INF;
	}
	void relaxVal(int pos, int val, int v = 1, int l = 0, int r = pow2 - 1)
	{
		if (l == r)
		{
			tree[v] = min(tree[v], val);
			return;
		}
		int m = (l + r) / 2;
		if (pos <= m)
			relaxVal(pos, val, 2 * v, l, m);
		else
			relaxVal(pos, val, 2 * v + 1, m + 1, r);
		tree[v] = min(tree[2 * v], tree[2 * v + 1]);
	}
	int getMin(int a, int b, int v = 1, int l = 0, int r = pow2 - 1)
	{
		if (l >= r && r <= b)
			return tree[v];
		if (l > b || r < a)
			return INF;
		int m = (l + r) / 2;
		return min(getMin(a, b, 2 * v, l, m), getMin(a, b, 2 * v + 1, m + 1, r));
	}
} tree;

int perm[pow2];
int x[pow2], y[pow2], z[pow2];

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


int main()
{
	int T;
	scanf("%d", &T);
	while (T--)
	{
		tree.build();
		int n;
		scanf("%d", &n);
		int ans = 0;
		for (int i = 0; i < n; i++)
		{
			scanf("%d%d%d", &x[i], &y[i], &z[i]);
			perm[i] = i;
		}
		sort(perm, perm + n, cmp);
		for (int i = 0; i < n;)
		{
			int index = perm[i];
			int curZ = z[index];
			int it = i;
			while (it < n && z[perm[it]] == curZ)
			{
				int curX = x[perm[it]];
				int curY = y[perm[it]];
				if (curX == 1 || tree.getMin(1, curX - 1) >= curY)
				{
					ans++;
				}
				it++;
			}
			for (int s = i; s < it; s++)
				tree.relaxVal(x[perm[s]], y[perm[s]]);
			i = it;
		}
		printf("%d\n", ans);
	}
	return 0;
}