#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 () { cin >> n >> m >> ed, top = top_ = 0; for (int i = 1; i <= m; i++) g[i] = set (); for (int i = 1; i <= n; i++) f[i] = 0, lst[i] = 0; for (int i = 1, x, y; i <= ed; i++) cin >> x >> y, g[y].insert(x), lst[x] = max(lst[x], y); for (int i = 1; i <= m; 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 <= n; 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; }