#include using namespace std; //* ATCODER #include using namespace atcoder; typedef modint998244353 mint; //*/ /* BOOST MULTIPRECISION #include using namespace boost::multiprecision; //*/ typedef long long ll; #define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++) #define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--) template bool chmin(T &a, const T &b) { if (a <= b) return false; a = b; return true; } template bool chmax(T &a, const T &b) { if (a >= b) return false; a = b; return true; } template T max(vector &a){ assert(!a.empty()); T ret = a[0]; for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]); return ret; } template T min(vector &a){ assert(!a.empty()); T ret = a[0]; for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]); return ret; } template T sum(vector &a){ T ret = 0; for (int i=0; i<(int)a.size(); i++) ret += a[i]; return ret; } // defcomp template vector compress(vector &X) { vector vals = X; sort(vals.begin(), vals.end()); vals.erase(unique(vals.begin(), vals.end()), vals.end()); return vals; } // ----- // importbisect template int bisect_left(vector &X, T v){ return lower_bound(X.begin(), X.end(), v) - X.begin(); } template int bisect_right(vector &X, T v){ return upper_bound(X.begin(), X.end(), v) - X.begin(); } // ----- int main(){ ll h,w,n; cin >> h >> w >> n; vector a(n), b(n), ind(n); vector> tar; rep(i,0,n){ cin >> a[i] >> b[i]; tar.push_back(make_tuple(b[i],a[i],i)); } sort(tar.begin(), tar.end()); rep(i,0,n){ a[i] = get<1>(tar[i]); b[i] = get<0>(tar[i]); ind[i] = get<2>(tar[i]); } vector bc = compress(b); auto get = [&](int i) -> int { return bisect_left(bc, b[i]); }; auto hav = [&](ll z) -> bool { int tar = bisect_left(bc, z); if (tar >= (int)bc.size() || bc[tar] != z) return false; return true; }; vector cost(n, 4e18); vector> st((int)bc.size()); rep(i,0,n){ st[get(i)].insert(a[i]); } int piv = 0; ll nwmin = 4e18; rep(i,0,n){ int x = get(i); { auto itr = st[x].lower_bound(a[i]); if (itr != st[x].begin()){ itr--; ll val = *itr; chmin(cost[ind[i]], abs(a[i] - val)); } } { auto itr = st[x].upper_bound(a[i]); if (itr != st[x].end()){ ll val = *itr; chmin(cost[ind[i]], abs(a[i] - val)); } } for (ll y: {b[i]-1, b[i]+1}){ if (!hav(y)) continue; int z = get(y); { auto itr = st[z].upper_bound(a[i]); if (itr != st[z].begin()){ itr--; chmin(cost[ind[i]], a[i]); } } { auto itr = st[z].upper_bound(a[i]); if (itr != st[z].end()){ ll val = *itr; chmin(cost[ind[i]], val); } } } while (piv < n && b[piv] <= b[i] - 2){ chmin(nwmin, a[piv] - b[piv]); piv++; } // (1, b[i]-2) にいくのに ll geta = abs(1LL - a[i]) + 2LL; ll migiue_parameta = 1LL - (b[i] - 2LL); chmin(cost[ind[i]], (nwmin - migiue_parameta) + geta); } piv = n-1; nwmin = 4e18; rrep(i,0,n){ while(piv >= 0 && b[piv] >= b[i] + 2){ chmin(nwmin, a[piv] + b[piv]); piv--; } ll geta = abs(1LL - a[i]) + 2LL; ll migiue_parameta = 1LL + (b[i] + 2LL); chmin(cost[ind[i]], (nwmin - migiue_parameta) + geta); } rep(i,0,n){ cout << cost[i] << '\n'; } }