//g++ 1.cpp -std=c++14 -O2 -I . #include using namespace std; #include using namespace atcoder; using ll = long long; using ld = long double; using vi = vector; using vvi = vector; using vll = vector; using vvll = vector; using vld = vector; using vvld = vector; #define fi first #define se second #define pb push_back #define pq_big(T) priority_queue,less> #define pq_small(T) priority_queue,greater> #define all(a) a.begin(),a.end() #define rep(i,start,end) for(ll i=start;i<(ll)(end);i++) #define per(i,start,end) for(ll i=start;i>=(ll)(end);i--) vi seen(410000,0); vi fin(410000,0); vi hist; int pos=-1; void dfs(vvi &g,int now,int p){ seen[now]=1; hist.pb(now); for(int next:g[now]){ if(next==p){ continue; } if(fin[next]==1){ continue; } if(seen[next]==1&&fin[next]==0){ pos=next; return; } dfs(g,next,now); if(pos!=-1){ return; } } hist.pop_back(); fin[now]=1; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int h,w,n; cin>>h>>w>>n; vvi g(h+w); vector> place(n); map,int> inv; rep(i,0,n){ int x,y; cin>>x>>y; x--;y--; place[i].fi=x,place[i].se=y; g[x].pb(h+y); g[h+y].pb(x); inv[{x,h+y}]=i; inv[{h+y,x}]=i; } rep(i,0,h+w){ if(seen[i]==1)continue; if(g[i].size()==0)continue; dfs(g,i,-1); if(pos!=-1){ break; } } if(pos==-1){ cout<<"-1\n"; return 0; } vi cycle; while(!hist.empty()){ int t=hist[hist.size()-1]; cycle.pb(t); hist.pop_back(); if(t==pos){ break; } } vi ans; rep(i,0,cycle.size()){ int now=cycle[i],next=cycle[(i+1)%cycle.size()]; ans.pb(inv[{now,next}]); } cout<