#include using namespace std; using ll = long long; #define FOR(i, l, r) for(ll i = l; i < r; i++) #define rep(i, n) FOR(i, 0, n) #define FORR(i, l, r) for(ll i = r - 1; i >= l; i--) #define ALL(ar) begin(ar), end(ar) template inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); } template inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); } template istream &operator>>(istream &is, pair &p) { is >> p.first >> p.second; return is; } template ostream &operator<<(ostream &os, const pair &p) { os << p.first << ' ' << p.second; return os; } template istream &operator>>(istream &is, vector &v) { for(T &x : v) is >> x; return is; } template ostream &operator<<(ostream &os, const vector &v) { for(size_t i = 0; i < v.size(); i++) os << (i ? " " : "") << v[i]; return os; } //------------------------------------------------- const ll inf = 1LL << 60; void solve() { ll h, w, n; cin >> h >> w >> n; vector> p(n); cin >> p; vector ans(n, inf); // mp[j].push_back(pair(i, idx)) map>> mp; rep(i, n) { mp[p[i].second].emplace_back(p[i].first, i); } ll mn = inf; auto itr = mp.begin(); while(itr != mp.end()) { sort(ALL(itr->second)); for(auto [i, idx] : itr->second) { chmin(ans[idx], i - 1 + mn); } chmin(mn, itr->second[0].first - 1); mn -= itr->first; itr++; if(itr == mp.end()) break; mn += itr->first; } itr = mp.end(); mn = inf; while(itr != mp.begin()) { itr--; mn -= itr->first; for(auto [i, idx] : itr->second) { chmin(ans[idx], i - 1 + mn); } chmin(mn, itr->second[0].first - 1); mn += itr->first; } // cout << ans << endl; itr = mp.begin(); while(itr != mp.end()) { auto &vec = itr->second; rep(i, vec.size() - 1) { chmin(ans[vec[i].second], vec[i + 1].first - vec[i].first); chmin(ans[vec[i + 1].second], vec[i + 1].first - vec[i].first); } auto nxt = next(itr); if(nxt != mp.end() && nxt->first - itr->first == 1) { auto &nvec = nxt->second; for(auto [i, idx] : vec) { auto lower = lower_bound(ALL(nvec), pair{i, 0}); if(lower != nvec.end()) { chmin(ans[idx], lower->first); } if(lower != nvec.begin()) { chmin(ans[idx], i); } } for(auto [i, idx] : nvec) { auto lower = lower_bound(ALL(vec), pair{i, 0}); if(lower != vec.end()) { chmin(ans[idx], lower->first); } if(lower != vec.begin()) { chmin(ans[idx], i); } } } itr = nxt; } rep(i, n) cout << ans[i] << endl; } int main() { int t = 1; // cin >> t; while(t--) solve(); }