#pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include using namespace std; #define rep(i,n) for(ll i=0;i=l;i--) using ll = long long; using ld = long double; constexpr ll mod = 998244353ll; #define vout(v) for(auto i :v) cout< #define pb push_back #define vc vector #define vb vector #define uniq(x) sort((x).begin(),(x).end());(x).erase(unique((x).begin(),(x).end()),(x).end()) #define eb emplace_back void no(void) { cout<<"No"< dx={-1,0,1,0,1,1,-1,-1}; vector dy={0,-1,0,1,-1,1,-1,1}; bool isin(ll i,ll j,ll h,ll w) { if(i<0 || i>=h || j<0 || j>=w) return false; return true; } template bool chmin(T& a, T b){if(a > b){a = b; return true;} return false;} template bool chmax(T& a, T b){if(a < b){a = b; return true;} return false;} templateT vecmax(const vector&v){return *max_element(all(v));} templateT vecmin(const vector&v){return *min_element(all(v));} ll safemod(ll num,ll rule) { return (num%rule+rule)%rule; } //alias ojb='oj-bundle -I /home/leal/beginner/atc/library' ll sum(vector &a) { return accumulate(all(a),0ll); } int main() { ll x,y,n; cin>>x>>y>>n; rep(i,n) { ll u,v; cin>>u>>v; ll ans; function branch=[&](ll p)->ll { return (p-1)/y; }; function rootdist=[&](ll p)->ll { return ((p%y)==0 ? y : p%y); }; if(u==v) { ans=0; } else if(u>v) swap(u,v); else if(u==0) { ans=rootdist(v); } else if(branch(u)==branch(v)) ans=abs(u-v); else ans=rootdist(u)+rootdist(v); cout<