import sys,random,bisect from collections import deque,defaultdict from heapq import heapify,heappop,heappush from itertools import permutations from math import gcd,log input = lambda :sys.stdin.readline().rstrip() mi = lambda :map(int,input().split()) li = lambda :list(mi()) def solve(x,y,m,edge): while True: A = [random.randint(0,99) & 1 for i in range(y)] deg = [0] * x for yi in range(y): if not A[yi]: continue for xi in edge[yi]: deg[xi] ^= 1 if sum(deg) + sum(A) >= (x+y+1)//2: return ([xi for xi in range(x) if deg[xi]],[yi for yi in range(y) if A[yi]]) for _ in range(int(input())): x,y,m = mi() edge = [set() for yi in range(y)] for i in range(m): u,v = mi() edge[v-1].add(u-1) X,Y = solve(x,y,m,edge) print(len(X),len(Y)) print(*[xi+1 for xi in X]) print(*[yi+1 for yi in Y])