#include using namespace std; const int MAXN = 100001; int t, n[2], m, ans, ans2; mt19937_64 rnd(time(0)); bool vis[MAXN], vis2[MAXN]; vector e[MAXN]; bool check() { int cnt = 0; for(int i = 1; i <= n[0]; ++i) { vis2[i] = 0; } for(int i = 1; i <= n[1]; ++i) { vis[i] = rnd() % 2; cnt += vis[i]; if(vis[i]) { for(int v : e[1]) { vis2[v] ^= 1; } } } for(int i = 1; i <= n[0]; ++i) { cnt += vis2[i]; } return cnt >= (n[0] + n[1] + 1) / 2; } void Solve() { cin >> n[0] >> n[1] >> m; for(int i = 1; i <= n[1]; ++i) { e[i].clear(); } for(int i = 1, u, v; i <= m; ++i) { cin >> u >> v; e[v].emplace_back(u); } for(; !check(); ) { } ans = ans2 = 0; for(int i = 1; i <= n[0]; ++i) { ans += vis2[i]; } for(int i = 1; i <= n[1]; ++i) { ans2 += vis[i]; } cout << ans << " " << ans2 << "\n"; for(int i = 1; i <= n[0]; ++i) { if(vis2[i]) { cout << i << " "; } } cout << "\n"; for(int i = 1; i <= n[1]; ++i) { if(vis[i]) { cout << i << " "; } } cout << "\n\n"; } int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); for(cin >> t; t--; Solve()) { } return 0; }