// This code is AI-generated. (AI 生成的代码)
// NOIP2017 列队: offline. For each row and for the last column, replay only the
// queries that touch it through a Fenwick "select k-th present / remove", which
// maps each request to either an original position or an appended slot. Then
// replay all queries, resolving appended slots with per-row / last-column vectors.
#include <sys/auxv.h>
#include <stdint.h>
#include <vector>
struct DuckInfo {
unsigned long abi; const char *in; unsigned long in_size;
char *out; unsigned long out_limit, out_size;
char *err; unsigned long err_limit, err_size;
const char *IB; unsigned long IB_limit; char *OB; unsigned long OB_limit; unsigned long tsc;
} __attribute__((packed));
static const char *ip; static char *op;
static inline int rd() {
const char *p = ip;
while ((unsigned)(*p - '0') > 9u) p++;
int x = 0;
do { x = x * 10 + (*p++ - '0'); } while ((unsigned)(*p - '0') <= 9u);
ip = p; return x;
}
static inline void wr(long long v) {
if (v < 0) { *op++ = '-'; v = -v; }
char t[24]; int k = 0;
if (!v) t[k++] = '0';
while (v) { t[k++] = (char)('0' + v % 10); v /= 10; }
while (k) *op++ = t[--k];
*op++ = '\n';
}
using namespace std;
typedef long long ll;
enum { MAXN = 300005 };
struct Req { int qid, x; };
static vector<Req> rowReq[MAXN];
static vector<Req> colReq;
static int ind[2 * MAXN];
static int qx[MAXN], qy[MAXN];
static int fw[1 << 21];
static int FW;
static inline int lowbit(int x) { return x & -x; }
static void rebuild(int size) {
FW = 1; while (FW < size) FW <<= 1;
for (int i = 1; i <= FW; i++) fw[i] = lowbit(i);
}
static int rmv(int k) {
int pos = 0, rem = k;
for (int pw = FW >> 1; pw; pw >>= 1)
if (pos + pw <= FW && fw[pos + pw] < rem) { pos += pw; rem -= fw[pos]; }
int p = pos + 1;
for (int i = p; i <= FW; i += lowbit(i)) fw[i]--;
return p;
}
static void addp(int p) { for (int i = p; i <= FW; i += lowbit(i)) fw[i]++; }
int main() {
int n, m, q;
struct DuckInfo *d = (struct DuckInfo *)getauxval(0x6b637564UL);
ip = d->in; op = d->out;
n = rd(); m = rd(); q = rd();
int qc = 0;
for (int i = 1; i <= q; i++) {
qx[i] = rd(); qy[i] = rd();
if (qy[i] == m) colReq.push_back(Req{++qc, qx[i]});
else {
rowReq[qx[i]].push_back(Req{++qc, qy[i]});
colReq.push_back(Req{++qc, qx[i]});
}
}
rebuild(m + q + 2);
for (int i = 1; i <= n; i++) {
for (size_t j = 0; j < rowReq[i].size(); j++) ind[rowReq[i][j].qid] = rmv(rowReq[i][j].x);
for (size_t j = 0; j < rowReq[i].size(); j++) addp(ind[rowReq[i][j].qid]);
}
rebuild(n + q + 2);
for (size_t j = 0; j < colReq.size(); j++) ind[colReq[j].qid] = rmv(colReq[j].x);
static vector<ll> rr[MAXN];
static vector<ll> rc;
qc = 0;
for (int i = 1; i <= q; i++) {
if (qy[i] == m) {
++qc; ll v;
if (ind[qc] <= n) v = (ll)ind[qc] * m; else v = rc[ind[qc] - n - 1];
rc.push_back(v); wr(v);
} else {
++qc; ll v;
if (ind[qc] < m) v = (ll)ind[qc] + (ll)(qx[i] - 1) * m;
else v = rr[qx[i]][ind[qc] - m];
wr(v); rc.push_back(v);
++qc;
if (ind[qc] <= n) v = (ll)ind[qc] * m; else v = rc[ind[qc] - n - 1];
rr[qx[i]].push_back(v);
}
}
d->out_size = (unsigned long)(op - d->out);
return 0;
}