#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]++; } /*for (int i = 1; i <= n; i++) { cout << deg[i] << " \n"[i == n]; }*/ 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]); } } //cout << c0 << ' ' << c1 << '\n'; if (c0 >= c1) { for (auto u : g[i]) { if (!deg[u] && co[u]) { a.push_back(u), co[u] = 0; } } } else { b.push_back(i); for (auto u : g[i]) { co[u] ^= 1; if (!deg[u] && co[u]) { a.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'; } int main() { ios::sync_with_stdio(0), cin.tie(0); //cout << '\n'; for (cin >> T; T--; ) { Solve(); } return 0; } /* 1 3 3 6 1 1 1 1 3 1 2 3 3 2 3 3 */