import sys
t=int(sys.stdin.readline())
def dfs(x,y):
if x<=-1 or x>=n or y<=-1 or y>=n:
return False
if graph[x][y]==1:
graph[x][y]=0
dfs(x-1,y)
dfs(x+1,y)
dfs(x,y-1)
dfs(x,y+1)
return True
for _ in range(t):
result=0
m,n,k=map(int,input().split())
graph=[[0]*(m) for _ in range(n)]# 그래프
for _ in range(k):
x,y=map(int,input().split())
graph[y][x]=1
for i in range(m):
for j in range(n):
if dfs(i,j)==True:
result+=1
print(result)