import sys
from collections import deque
sys.setrecursionlimit(10**9)
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
def dfs(x, y):
global count
if x < 0 or y < 0 or x >= N or y >= M:
return
if board[x][y] == 1:
count += 1
board[x][y] = 0
for i in range(4):
nx = x + dx[i]; ny = y + dy[i]
dfs(nx, ny)
T = int(input())
for _ in range(T):
M, N, K = map(int, sys.stdin.readline().split())
board = []
for n in range(N):
board.append([0 for m in range(M)])
for __ in range(K):
t0, t1 = map(int, sys.stdin.readline().split())
board[t1][t0] = 1
count = 0; count_list = []
for i in range(N):
for j in range(M):
if board[i][j] == 1:
dfs(i, j)
count_list.append(count)
count = 0
print(len(count_list))