import sys
# 재귀함수에 깊이를 제한하는 방법
sys.setrecursionlimit(10000)
T = int(input())
B, ck = [], []
dx, dy = [1,0,-1,0],[0,1,0,-1]
def dfs(x,y):
global B, ck
if ck[x][y] == 1:
return
ck[x][y] = 1
for i in range(4):
xx, yy = x+dx[i], y+dy[i]
if B[xx][yy] ==0 or ck[xx][yy] == 1:
continue
dfs(xx,yy)
def process():
global B, ck
M, N, K = map(int, input().split())
B = [[0 for i in range(M+2)] for _in range(N+2)]
ck = [[0 for i in range(M+2)] for _in range(N+2)]
for _ in range(K):
x, y = map(int, input().split())
B[Y+1][X+1] = 1
ans = 0
for i in range(1, M+1):
for j in range(1, M+1):
if B[i][j] == 0 or ck[i][j] == 1:
continue:
dfs(i,j)
ans += 1
print(answer)
for _ in range(T):
process()