T=int(input()) for _ in range(T): x,y,m=map(int,input().split()) red=[set() for i in range(y)] blue=[-1]*x cnt=[0]*x for i in range(m): u,v=map(int,input().split()) u-=1 v-=1 red[v].add(u) blue[u]=max(blue[u],v) for i in range(y): for j in red[i]: cnt[j]^=1 last=[[] for i in range(y)] for i in range(x): last[blue[i]].append(i) use=[0]*y odd=[0]*x for i in range(y): diff=[0,0] for j in last[i]: if cnt[j]==0: diff[odd[j]]+=1 if diff[0]>diff[1]: use[i]=1 for j in red[i]: odd[j]^=1 if sum(use)+sum(odd)<(x+y+1)//2: for i in range(y): use[i]^=1 for i in range(x): odd[i]^=cnt[i] print(sum(odd),sum(use)) assert(sum(odd)+sum(use)>=(x+y+1)//2) print(*[i+1 for i in range(x) if odd[i]]) print(*[i+1 for i in range(y) if use[i]])