#include <bits/stdc++.h>
using namespace std;
const int N = 1e9+100;
const int M = 1e5+100;
const int MA = 1e4+100;
const int MOD = 1e9+7;

struct O {
	int c, d;
}one[M];

int t, n, m, v, x[M];
bool mp[MA][MA];

int main () {
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	scanf("%d", &t);
	while (t--) {
		scanf("%d%d%d", &n, &m, &v);
		for (int i = 1;i <= m;i++) scanf("%d%d", &one[i].c, &one[i].d), x[one[i].c] = one[i].d;
		int ans = 0;
		for (int i = 2;i <= m;i++) {
			int pre = i-1;
			int u = one[pre].d, v = one[i].d;
			if (!mp[u][v]) ans++, mp[u][v] = true;
			else continue;
		}
		printf("%d\n", ans);
	}
	return 0;
}
