#include #include #define rep(i,n) for(int i=0;i vi; typedef vector vl; typedef vector> vvi; typedef vector> vvl; typedef long double ld; typedef pair P; ostream& operator<<(ostream& os, const modint& a) {os << a.val(); return os;} template ostream& operator<<(ostream& os, const static_modint& a) {os << a.val(); return os;} template ostream& operator<<(ostream& os, const dynamic_modint& a) {os << a.val(); return os;} template istream& operator>>(istream& is, vector& v){int n = v.size(); assert(n > 0); rep(i, n) is >> v[i]; return is;} template ostream& operator<<(ostream& os, const pair& p){os << p.first << ' ' << p.second; return os;} template ostream& operator<<(ostream& os, const vector& v){int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "\n" : " "); return os;} template ostream& operator<<(ostream& os, const vector>& v){int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "\n" : ""); return os;} template ostream& operator<<(ostream& os, const set& se){for(T x : se) os << x << " "; os << "\n"; return os;} template ostream& operator<<(ostream& os, const unordered_set& se){for(T x : se) os << x << " "; os << "\n"; return os;} template ostream& operator<<(ostream& os, const atcoder::segtree& seg){int n = seg.max_right(0, [](S){return true;}); rep(i, n) os << seg.get(i) << (i == n - 1 ? "\n" : " "); return os;} template ostream& operator<<(ostream& os, const atcoder::lazy_segtree& seg){int n = seg.max_right(0, [](S){return true;}); rep(i, n) os << seg.get(i) << (i == n - 1 ? "\n" : " "); return os;} template void chmin(T& a, T b){a = min(a, b);} template void chmax(T& a, T b){a = max(a, b);} int solve(){ int b, r, m; cin >> b >> r >> m; vector end(b); vector> G(r); rep(i, m){ int u, v; cin >> u >> v; u--; v--; chmax(end[u], v); G[v].insert(u); } vector> event(r); rep(ib, b){ event[end[ib]].push_back(ib); } vector selected_r; vector selected_b; vector cnt(b); rep(ir, r){ auto& vec = event[ir]; vector odd; vector even; for(auto ib : vec){ if(cnt[ib] % 2) odd.push_back(ib); else even.push_back(ib); } if(int(even.size()) + 1 < int(odd.size())){ for(int ib : odd) selected_b.push_back(ib); }else{ for(int ib : G[ir]) cnt[ib]++; for(int ib : even) selected_b.push_back(ib); selected_r.push_back(ir); } } // cout << cnt; cout << int(selected_b.size()) << ' ' << int(selected_r.size()) << "\n"; for(int ib : selected_b) cout << ib + 1 << ' '; cout << "\n"; for(int ir : selected_r) cout << ir + 1 << ' '; cout << "\n"; return 0; }; int main(){ int t; cin >> t; rep(_, t) solve(); return 0; }