#include using namespace std; const int MAXN = 1e5 + 5; vector g[MAXN]; int T, n, k, m, deg[MAXN], co[MAXN]; void Solve() { cin >> n >> k >> m; for (int i = 1, u, v; i <= m; i++) { cin >> u >> v; g[v].push_back(u); } 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()); for (auto u : g[i]) deg[u]++; } vector a, b; for (int i = 1; i <= k; i++) { int c0 = 0, c1 = 1; for (auto u : g[i]) { deg[u]--; if (!deg[u]) { c0 += co[u], c1 += (!co[u]); } } if (c0 >= c1) { for (auto u : g[i]) { if (!deg[u] && co[u]) { b.push_back(u), co[u] = 0; } } } else { a.push_back(i); for (auto u : g[i]) { co[u] ^= 1; if (!deg[u] && co[u]) { b.push_back(u), co[u] = 0; } } } g[i].clear(); } cout << a.size() << ' ' << b.size() << '\n'; for (auto u : a) cout << u << ' '; cout << '\n'; for (auto u : b) cout << u << ' '; cout << '\n'; //cout << '\n'; } int main() { ios::sync_with_stdio(0), cin.tie(0); //cout << '\n'; for (cin >> T; T--; ) { Solve(); } return 0; }