#include using namespace std; typedef long long int ll; int main() { cin.tie(0); ios::sync_with_stdio(0); int q; cin >> q; while (q--) { int x, y, m; cin >> x >> y >> m; vector> g(x), v(y); for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; a -= 1, b -= 1; g[a].push_back(b); } for (int i = 0; i < x; ++i) { sort(g[i].begin(), g[i].end()); g[i].erase(unique(g[i].begin(), g[i].end()), g[i].end()); for (int j : g[i]) { v[j].push_back(i); } } vector r1, r2; vector deg(x); for (int i = 0; i < y; ++i) { vector h1, h2; for (int j : v[i]) { if (g[j].back() == i) { if (deg[j] % 2 == 0) h1.push_back(j); else h2.push_back(j); } } if (h1.size() + 1 >= h2.size()) { r2.push_back(i); for (int j : v[i]) { deg[j] += 1; } for (int j : h1) { r1.push_back(j); } } else { for (int j : h2) { r1.push_back(j); } } } cout << r1.size() << " " << r2.size() << "\n"; for (int i : r1) { cout << i + 1 << " "; } cout << "\n"; for (int i : r2) { cout << i + 1 << " "; } cout << "\n"; } }