#include "bits/stdc++.h" using namespace std; #define REP(i, n) for(ll i = 0;i < n;i++) #define ll long long #define INF (ll)1ll << 60 #define ALL(n) n.begin(),n.end() #define P pair vector

v; map to, fr; map pos; vector g[210000], ans(210000, INF); priority_queue, greater

> qf, qb; int main(){ ll h, w, n; cin >> h >> w >> n; REP(i, n){ ll a, b; cin >> a >> b; v.push_back(P(b, a)); fr[b]++; pos[P(b, a)] = i; } ll itr = 0; for (auto& i : fr) { to[itr] = i.first; i.second = itr; itr++; } sort(ALL(v)); REP(i, n) g[fr[v[i].first]].push_back(v[i].second); REP(i, 210000) sort(ALL(g[i])); REP(i, n) qb.push(P(v[i].second - 1 + v[i].first, fr[v[i].first])); REP(i, n){ ll row = fr[v[i].first]; //おなじれつ auto it = lower_bound(ALL(g[row]), v[i].second); if (it != g[row].begin()) ans[pos[v[i]]] = min(ans[pos[v[i]]], abs(*(it - 1) - v[i].second)); if (it + 1 != g[row].end()) ans[pos[v[i]]] = min(ans[pos[v[i]]], abs(*(it + 1) - v[i].second)); //となりれつ if (row >= 0 and to[row - 1] == v[i].first - 1 and g[row - 1].size()) ans[pos[v[i]]] = min(ans[pos[v[i]]], max(g[row - 1][0], v[i].second)); if (to[row + 1] == v[i].first + 1 and g[row + 1].size()) ans[pos[v[i]]] = min(ans[pos[v[i]]], max(g[row + 1][0], v[i].second)); //とおいれつ while(!qb.empty() and qb.top().second <= row) qb.pop(); if (!qb.empty()) ans[pos[v[i]]] = min(ans[pos[v[i]]], qb.top().first - v[i].first + v[i].second - 1); if (!qf.empty()) ans[pos[v[i]]] = min(ans[pos[v[i]]], qf.top().first + v[i].first + v[i].second - 1); qf.push(P(v[i].second - 1 - v[i].first, fr[v[i].first])); } REP(i, n)cout << ans[i] << endl; }