# 1012번. 유기농 배추
importsysinput=sys.stdin.readlinesys.setrecursionlimit(50000)dx=[0,1,-1,0]dy=[1,0,0,-1]defdfs(y,x):box[y][x]=0foriinrange(4):ny=y+dy[i]nx=x+dx[i]ifnx<0orny<0orny>=nornx>=m:continueifbox[ny][nx]==1:dfs(ny,nx)t=int(input())for_inrange(t):m,n,k=map(int,input().split())# 가로 m, 세로 n
box=[[0foriinrange(m)]foriinrange(n)]foriinrange(k):x,y=map(int,input().split())box[y][x]=1cnt=0foriinrange(n):forjinrange(m):ifbox[i][j]==1:dfs(i,j)cnt+=1print(cnt)