#include using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int H,W,N; cin >> H >> W >> N; map> X; vector> XY(N); int idx = 0; for(auto &[x,y,pos] : XY) cin >> x >> y,swap(x,y),X[x].push_back(y),pos = idx++; long long inf = 4e18; vector answer(N,inf); for(int i=0; i> man; for(auto [x,y,pos] : XY){ if(man.size() == 0){man.insert({x+y,pos}); continue;} auto[mind,pos2] = *man.begin(); long long now = mind-1-x + y-1; answer.at(pos) = min(answer.at(pos),now); answer.at(pos2) = min(answer.at(pos2),now); man.insert({x+y,pos}); } reverse(XY.begin(),XY.end()); man.clear(); for(auto [x,y,pos] : XY){ if(man.size() == 0){man.insert({y-x,pos}); continue;} auto[mind,pos2] = *man.begin(); long long now = x+mind-1 + y-1; answer.at(pos) = min(answer.at(pos),now); answer.at(pos2) = min(answer.at(pos2),now); man.insert({y-x,pos}); } for(auto ans : answer) cout << ans << "\n"; }