from collections import * import sys import heapq from heapq import heapify, heappop, heappush import bisect from bisect import bisect_left, bisect_right import itertools from functools import lru_cache from types import GeneratorType from fractions import Fraction import math import copy import random # import numpy as np # sys.setrecursionlimit(int(1e7)) # @lru_cache(maxsize=None) # CPython特化 # @bootstrap # PyPy特化(こっちのほうが速い) yield dfs(), yield Noneを忘れずに def bootstrap(f, stack=[]): # yield def wrappedfunc(*args, **kwargs): if stack: return f(*args, **kwargs) else: to = f(*args, **kwargs) while True: if type(to) is GeneratorType: stack.append(to) to = next(to) else: stack.pop() if not stack: break to = stack[-1].send(to) return to return wrappedfunc dxdy1 = ((0, 1), (0, -1), (1, 0), (-1, 0)) # 上下右左 dxdy2 = ( (0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (-1, -1), (1, -1), (-1, 1), ) # 8方向すべて dxdy3 = ((0, 1), (1, 0)) # 右 or 下 dxdy4 = ((1, 1), (1, -1), (-1, 1), (-1, -1)) # 斜め INF = float("inf") _INF = 1 << 60 MOD = 998244353 mod = 998244353 MOD2 = 10**9 + 7 mod2 = 10**9 + 7 # memo : len([a,b,...,z])==26 # memo : 2^20 >= 10^6 # 小数の計算を避ける : x/y -> (x*big)//y ex:big=10**9 # @:小さい文字, ~:大きい文字,None: 空の文字列 # ユークリッドの互除法:gcd(x,y)=gcd(x,y-x) # memo : d 桁以下の p 進表記を用いると p^d-1 以下のすべての # 非負整数を表現することができる # memo : (X,Y) -> (X+Y,X−Y) <=> 点を原点を中心に45度回転し、√2倍に拡大 # memo : (x,y)のx正から見た偏角をラジアンで(-πからπ]: math.atan2(y, x) # memo : a < bのとき ⌊a⌋ ≦ ⌊b⌋ input = lambda: sys.stdin.readline().rstrip() mi = lambda: map(int, input().split()) li = lambda: list(mi()) ii = lambda: int(input()) py = lambda: print("Yes") pn = lambda: print("No") pf = lambda: print("First") ps = lambda: print("Second") T = ii() for _ in range(T): x, y, m = mi() B = [[] for _ in range(x)] R = [[] for _ in range(y)] for _ in range(m): u, v = mi() u -= 1 v -= 1 B[u].append(v) R[v].append(u) degB = [0] * x turn = [[] for _ in range(y)] for i in range(x): turn[max(B[i])].append(i) X = [] Y = [] for yy in range(y): add = 1 nadd = 0 memo = [] for i in turn[yy]: if degB[i] == 0: add += 1 memo.append(i) else: nadd += 1 if add >= nadd: Y.append(yy + 1) for xx in R[yy]: degB[xx] ^= 1 for xx in memo: X.append(xx + 1) print(len(X), len(Y)) print(*X) print(*Y)