#include using namespace std; const int MAXN = 1e5 + 5; bool f[MAXN], vis[MAXN]; vector g[MAXN]; int T, n, k, m, a[MAXN], deg[MAXN]; void Solve() { cin >> n >> k >> m; for (int i = 1, u, v; i <= m; i++) { cin >> u >> v, deg[u]++; g[v].push_back(u); } vector p, q; for (int i = 1; i <= k; i++) { sort(g[i].begin(), g[i].end()); g[i].erase(unique(g[i].begin(), g[i].end()), g[i].end()); int c1 = 0, c2 = 1; for (auto u : g[i]) { deg[u]--; if (!deg[u]) { c1 += f[u]; c2 += (!f[u]); } } if (c1 <= c2) p.push_back(i); for (auto u : g[i]) { if (!deg[u]) { if (c1 <= c2) { f[u] ^= 1; } if (f[u]) { q.push_back(u); } } } } for (int i = 1; i <= n; i++) { f[i] = 0; } cout << q.size() << ' ' << p.size() << '\n'; for (auto u : q) cout << u << ' '; cout << '\n'; for (auto u : p) cout << u << ' '; cout << '\n'; } int main() { ios::sync_with_stdio(0), cin.tie(0); for (cin >> T; T--; ) { Solve(); } return 0; }