#include <iostream>
#include <cstdlib>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>



using namespace std;
int dx[4] = { 1,0,-1,0 };//아래 오른쪽 위 왼쪽
int dy[4] = { 0,1,0,-1 };
int arr[51][51];
queue<pair<int, int>>q;
int bug;
void BFS(int a, int b,int m, int n) {
	int xx, yy;
	q.push({a,b});
	while (q.size()) {
		int s = q.size();
		for (int i = 0; i < s; i++) {
			xx = q.front().first;
			yy = q.front().second;
			for (int i = 0; i < 4; i++) {
				int nx = xx + dx[i];
				int ny = yy + dy[i];
				if (0 <= nx && nx < n && 0 <= ny && ny < m && arr[nx][ny] == 1)
				{
					q.push({ nx, ny });
					arr[nx][ny] = 0;

				}
			}
			q.pop();
		}
	}
	bug++;
}

int main(void) {
	int t;
	cin >> t;
	for (int i = 0; i < t; i++) {
		bug = 0;
		int m, n, k;
		cin >> m >> n>>k;
		for (int i = 0; i < k; i++) {
			int x, y;
			cin >> x >> y;
			arr[y][x] = 1;
		}

		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (arr[i][j] == 1) BFS(i, j, m, n);
				else continue;
			}
		}
		cout << bug << "\n";

	}
	return 0;
}
