package BOJ._0319;
import java.util.Scanner;
public class BOJ_1012 {
static int[][] field;
static int x;
static int y;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
x = sc.nextInt();
y = sc.nextInt();
int n = sc.nextInt();
field = new int[x][y];
for (int i = 0; i < n; i++) {
int x1 = sc.nextInt();
int y1 = sc.nextInt();
field[x1][y1] = 1;
}
solve();
}
}
private static void solve() {
int adjField = 1;
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
if (field[i][j] == 1)
dfs(i, j, ++adjField);
}
}
System.out.println(adjField - 1);
}
static int[] goX = {1, 0, -1, 0};
static int[] goY = {0, 1, 0, -1};
private static void dfs(int i, int j, int adjField) {
field[i][j] = adjField;
for (int k = 0; k < 4; k++) {
int x1 = i + goX[k];
int y1 = j + goY[k];
if ((-1 < x1 && x1 < x) && (-1 < y1 && y1 < y) && (field[x1][y1] == 1))
dfs(x1, y1, adjField);
}
}
}