#include using namespace std; using ll = long long; int main() { ll h, w, n; cin >> h >> w >> n; vector> a(n); map> as; for (int i = 0; i < n; i++) { cin >> a[i].second >> a[i].first; as[a[i].first].insert(a[i].second); } vector ans(n, 1e10); // case a[i].first == a[j].first for (int i = 0; i < n; i++) { auto itr = as[a[i].first].upper_bound(a[i].second); if (itr != as[a[i].first].end()) { ans[i] = min(ans[i], *itr - a[i].second); } itr = as[a[i].first].find(a[i].second); if (itr != as[a[i].first].begin()) { itr = prev(itr); ans[i] = min(ans[i], a[i].second - *itr); } } // case abs(a[i].first - a[j].first) == 1 for (int i = 0; i < n; i++) { if (as.find(a[i].first - 1) != as.end()) { ans[i] = min(ans[i], max(a[i].second, *as[a[i].first - 1].begin())); } if (as.find(a[i].first + 1) != as.end()) { ans[i] = min(ans[i], max(a[i].second, *as[a[i].first + 1].begin())); } } // case abs(a[i].first - a[j].first) > 1 vector iot(n); iota(iot.begin(), iot.end(), 0); sort(iot.begin(), iot.end(), [&](int i, int j){ return a[i] < a[j]; }); {// l to r ll b = 1e10; for (auto i : iot) { ans[i] = min(ans[i], a[i].first + a[i].second - 1 + b); b = min(b, a[i].second - 1 - a[i].first); } } {// r to l ll b = 1e10; reverse(iot.begin(), iot.end()); for (auto i : iot) { ans[i] = min(ans[i], -a[i].first + a[i].second - 1 + b); b = min(b, a[i].second - 1 + a[i].first); } } for(int i = 0; i < n; i++){ cout << ans[i] << endl; } }