#include #define _1 (__int128)1 using namespace std; using ll = long long; void FileIO (const string s) { freopen(string(s + ".in").c_str(), "r", stdin); freopen(string(s + ".out").c_str(), "w", stdout); } const int N = 1e5 + 10; int T, n, m, ed, stk[N], top, stk_[N], top_, ans, lst[N]; set g[N]; bool f[N]; void Solve () { mt19937 rnd(time(0)); cin >> n >> m >> ed, top = top_ = 0; for (int i = 1; i <= n; i++) g[i] = set (); for (int i = 1; i <= m; i++) f[i] = 0, lst[i] = 0; for (int i = 1, x, y; i <= ed; i++) cin >> x >> y, g[x].insert(y), lst[y] = max(lst[y], x); for (int i = 1; i <= n; i++) { int cnt = 1, cnt_ = 0; for (int j : g[i]) if (i == lst[j]) cnt += !f[j], cnt_ += f[j]; if (cnt > cnt_) { stk[++top] = i; for (int j : g[i]) f[j] ^= 1; } } for (int i = 1; i <= m; i++) if (f[i]) stk_[++top_] = i; cout << top << ' ' << top_ << '\n'; for (int i = 1; i <= top; i++) cout << stk[i] << ' '; cout << '\n'; for (int i = 1; i <= top_; i++) cout << stk_[i] << ' '; } signed main () { ios::sync_with_stdio(0), cin.tie(0); // FileIO(""); for (cin >> T; T--; Solve(), cout << '\n') {} return 0; }