#include #define fi first #define se second #define eb emplace_back #define mp make_pair using namespace std; typedef long double ld; typedef long long ll; typedef unsigned long long ull; typedef __int128 i128; template T ceil(T x, U y) {return (x>0?(x+y-1)/y:x/y);} template T floor(T x, U y) {return (x>0?x/y:(x-y+1)/y);} template bool chmax(T &a,const S b) {return (a bool chmin(T &a,const S b) {return (a>b?a=b,1:0);} int popcnt(int x) {return __builtin_popcount(x);} int popcnt(ll x) {return __builtin_popcountll(x);} int topbit(int x) {return (x==0?-1:31-__builtin_clz(x));} int topbit(ll x) {return (x==0?-1:63-__builtin_clzll(x));} int lowbit(int x) {return (x==0?-1:__builtin_ctz(x));} int lowbit(ll x) {return (x==0?-1:__builtin_ctzll(x));} #define rep(i,a,b) for(int i=(a);i<=(b);i++) #define per(i,a,b) for(int i=(a);i>=(b);i--) typedef pair pii; typedef vector vi; typedef vector vp; int read() { int x=0,w=1; char c=getchar(); while(!isdigit(c)) {if(c=='-') w=-1; c=getchar();} while(isdigit(c)) {x=x*10+(c-'0'); c=getchar();} return x*w; } const int N=1e5+5; int x,y,m,lst[N],f[N],vst[N]; vi e[N],A,B; void work() { x=read(), y=read(), m=read(); A.clear(), B.clear(); rep(i,1,x) lst[i]=f[i]=0; rep(i,1,y) e[i].clear(); rep(i,1,m) { int u=read(), v=read(); e[v].eb(u), chmax(lst[u],v); } rep(i,1,y) { vi p; for(int j:e[i]) if(!vst[j]) p.eb(j), vst[j]=1; e[i]=p; for(int j:e[i]) vst[j]=0; } rep(i,1,y) { vi s,t; for(int j:e[i]) if(lst[j]==i) { if(f[j]==0) s.eb(j); else t.eb(j); } if(s.size()