n,m<=3e5

# 分析

1. 线段树上二分找到第x个数
2. 加、改常数个点

#include <cstdio>
#include <iostream>
using namespace std;
const int N = 3e5+10,MXP = 5500000;
typedef long long ll;
ll n,m,q;
int root[N],lc[MXP],rc[MXP],size[MXP],tot;
int tail[N];
ll v[MXP];

void set(int &x,int l,int r,int tg,ll va=0) {
if (!x) x=++tot;
if (l==r) {
v[x]=va;
return;
}
if (tg<=(l+r>>1)) set(lc[x],l,l+r>>1,tg,va);
else set(rc[x],(l+r>>1)+1,r,tg,va);
size[x]=size[lc[x]]+size[rc[x]];
}
ll kth(int &x,int l,int r,int tg,int &ret) {
if (!x) x=++tot;
if (l==r) {
ret=l;
size[x]=1;
return v[x];
}
ll mid=l+r>>1,rrr;
if (mid-l+1-size[lc[x]]>=tg) rrr=kth(lc[x],l,mid,tg,ret);
else rrr=kth(rc[x],mid+1,r,tg-(mid-l+1-size[lc[x]]),ret);
size[x]=size[lc[x]]+size[rc[x]];
return rrr;
}

int main() {
freopen("phalanx.in","r",stdin);
freopen("phalanx.out","w",stdout);
cin>>n>>m>>q;
for (int i=1; i<=n; i++) tail[i]=m-1;
tail[n+1]=n;

int loc,x,y;
ll tmp;
for (int i=1; i<=q; i++) {
scanf("%d %d",&x,&y);
if (y==m) {
tmp=kth(root[n+1],1,n+q,x,loc);
if (loc<=n) tmp=loc*m;
printf("%lld\n",tmp);
set(root[n+1],1,n+q,++tail[n+1],tmp);
} else {
tmp=kth(root[x],1,m+q,y,loc);
if (loc<m) tmp=(x-1)*m+loc;
printf("%lld\n",tmp);
set(root[n+1],1,n+q,++tail[n+1],tmp);
tmp=kth(root[n+1],1,n+q,x,loc);
if (loc<=n) tmp=loc*m;
set(root[x],1,m+q,++tail[x],tmp);
}
}
//cout<<tot<<endl;
}