#include using namespace std; const int N = 1e5 + 10; int T, x, y, m; bool f[N], vis[N]; vector g[N]; mt19937 rnd(time(0)); void Solve() { cin >> x >> y >> m; while (m--) { int u, v; cin >> u >> v; g[u].push_back(v); } while (1) { int ct = 0; for (int i = 1; i <= y; i++) { f[i] = rnd() % 2, ct += f[i]; } for (int i = 1; i <= x; i++) { int cnt = 0; for (int j : g[i]) cnt += f[j]; if (cnt % 2) ct++, vis[i] = 1; } if (ct >= (x + y + 1) / 2) { vector p, q; for (int i = 1; i <= x; i++) { if (vis[i]) p.push_back(i); } for (int i = 1; i <= y; i++) { if (f[i]) q.push_back(i); } cout << p.size() << ' ' << q.size() << '\n'; for (int x : p) cout << x << ' '; cout << '\n'; for (int x : q) cout << x << ' '; cout << '\n'; break; } fill(vis + 1, vis + x + 1, 0); } for (int i = 1; i <= x; i++) g[i].clear(); } int main() { ios::sync_with_stdio(0), cin.tie(0); cin >> T; while (T--) Solve(); return 0; } ////