#include using namespace std; using Pii = pair; #define int long long mt19937_64 rnd(time(0)); const int N = 1e5 + 5; int t, x, y, m, vis[N]; set s; vector g[N]; void Solve() { cin >> x >> y >> m; for (int i = 1, u, v; i <= m; i++) { cin >> u >> v; s.insert({u, v}); } while (!s.empty()) { auto cur = *s.begin(); s.erase(s.begin()); g[cur.first].push_back(cur.second); } for (int _ = 1; _ <= 1145141919810; _++) { int cnt = 0; for (int i = 1; i <= y; i++) { vis[i] = rnd() % 2; cnt += vis[i]; } for (int i = 1; i <= x; i++) { int tmp = 0; for (auto v : g[i]) { tmp += vis[v]; } if (tmp % 2 == 1) { cnt++; } } if (cnt >= (x + y) / 2) { break; } } vector ans1, ans2; for (int i = 1; i <= x; i++) { if (vis[i] == true) { ans1.push_back(i); } } for (int i = 1; i <= x; i++) { int tmp = 0; for (auto v : g[i]) { tmp += vis[v]; } if (tmp % 2 == 1) { ans2.push_back(i); } } cout << ans1.size() << " " << ans2.size() << "\n"; for (auto cur : ans1) { cout << cur << " "; } cout << "\n"; for (auto cur : ans2) { cout << cur << " "; } cout << "\n"; cout << "\n"; } signed main() { ios::sync_with_stdio(0); cin.tie(0); cin >> t; while (t--) { Solve(); } return 0; }