#include using namespace std; const int MAXN = 100001; int t, n[2], m, in[MAXN], res; bool vis[MAXN]; vector e[MAXN], ans; void Solve() { cin >> n[0] >> n[1] >> m; for(int i = 1; i <= n[0]; ++i) { vis[i] = in[i] = 0; } 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(int i = 1; i <= n[1]; ++i) { sort(e[i].begin(), e[i].end()); e[i].erase(unique(e[i].begin(), e[i].end()), e[i].end()); for(int v : e[i]) { in[v]++; } } ans.clear(); for(int i = 1; i <= n[1]; ++i) { int cnt = 1; for(int v : e[i]) { cnt += (in[v] == 1) * (vis[v] ? -1 : 1); in[v]--; } if(cnt >= 0) { ans.emplace_back(i); for(int v : e[i]) { vis[v] ^= 1; } } } res = 0; for(int i = 1; i <= n[0]; ++i) { res += vis[i]; } cout << res << " " << ans.size() << "\n"; for(int i = 1; i <= n[0]; ++i) { if(vis[i]) { cout << i << " "; } } cout << "\n"; for(int x : ans) { cout << x << " "; } cout << "\n\n"; } int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); for(cin >> t; t--; Solve()) { } return 0; }