#include using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); long long H,W,N; cin >> H >> W >> N; map> X; vector> XY(N); long long idx = 0; for(auto &[x,y,pos] : XY) cin >> x >> y,swap(x,y),X[x].push_back(y),pos = idx++; for(auto &[k,v] : X) sort(v.begin(),v.end()); long long inf = 4e18; vector answer(N,inf); for(long long i=0; i= 0) answer.at(i) = min(answer.at(i),y-look.at(pos)); auto itr = X.find(x-1); if(itr != X.end() && X[x-1].size()) answer.at(i) = min(answer.at(i),max(y,X[x-1].at(0))); itr = X.find(x+1); if(itr != X.end() && X[x+1].size()) answer.at(i) = min(answer.at(i),max(y,X[x+1].at(0))); } sort(XY.rbegin(),XY.rend()); set> 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"; }