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