import sys
sys.setrecursionlimit(10000)
def dfs(x,y):
if x <= -1 or y <= -1 or x >= m or y >= n:
return False
if d[x][y] == 1:
d[x][y] = 0
dfs(x-1, y)
dfs(x+1, y)
dfs(x, y-1)
dfs(x, y+1)
return True
return False
if __name__ == '__main__':
t = int(input())
for _ in range(t):
m,n,k = map(int, input().split())
d= [[0 for _ in range(n)] for _ in range(m)]
for _ in range(k):
x,y = map(int, input().split())
d[x][y] = 1
dx = [-1,1,0,0]
dy = [0,0,-1,1]
result = 0
for i in range(m):
for j in range(n):
if dfs(i, j) == True:
result += 1
print(result)