#include <bits/stdc++.h>   using namespace std;   void Rd(int &res) {     res = 0;     char p;     while (p = getchar(), p <  0 )       ;     do {       res = (res << 1) + (res << 3) + (p ^ 48);     } while (p = getchar(), p >=  0 );   }   void Pt(long long x) {     if (x == 0) return;     Pt(x / 10);     putchar(x % 10 ^ 48);   }   void Ps(long long x) {     if (x < 0) {       putchar( - );       x = -x;     }     if (x == 0)       putchar( 0 );     else       Pt(x);   }   long long ans[100005];   struct W {     int x, y, id;     bool operator<(const W &a) const { return x < a.x; }   };   vector<W> son[2][100005 * 3];   bool mark[4][100005 * 3];   int n, m, k;   int id(int x, int y, int f) {     if (f == 0 || f == 3) return y - x + 100005;     return x + y + 100005;   }   void dfs(int x, int y, int f, long long t) {     int FF = id(x, y, f);     if (mark[f][FF]) return;     mark[f][FF] = 1;     if (f == 0) {       int heng = n - x, shu = m - y;       int kkk = y - x + 100005;       vector<W>::iterator it = son[0][kkk].begin();       for (; it != son[0][kkk].end(); it++) {         W op = (*it);         int mm = op.x - x;         if (ans[op.id] == -1) ans[op.id] = t + mm;       }       son[0][kkk].clear();       if (heng > shu) {         dfs(x + shu, m, 1, t + shu);       } else if (shu > heng) {         dfs(n, y + heng, 2, t + heng);       } else if (shu == heng) {         return;       }     } else if (f == 1) {       int heng = n - x, shu = y;       int kkk = x + y + 100005;       vector<W>::iterator it = son[1][kkk].begin();       for (; it != son[1][kkk].end(); it++) {         W op = (*it);         int mm = op.x - x;         if (ans[op.id] == -1) ans[op.id] = t + mm;       }       son[1][kkk].clear();       if (heng > shu) {         dfs(x + shu, 0, 0, t + shu);       } else if (shu > heng) {         dfs(n, y - heng, 3, t + heng);       } else if (shu == heng) {         return;       }     } else if (f == 2) {       int heng = x, shu = m - y;       int kkk = x + y + 100005;       vector<W>::iterator it = son[1][kkk].begin();       for (; it != son[1][kkk].end(); it++) {         W op = (*it);         int mm = x - op.x;         if (ans[op.id] == -1) ans[op.id] = t + mm;       }       son[1][kkk].clear();       if (heng > shu) {         dfs(x - shu, m, 3, t + shu);       } else if (shu > heng) {         dfs(0, y + heng, 0, t + heng);       } else if (shu == heng) {         return;       }     } else if (f == 3) {       int heng = x, shu = y;       int kkk = y - x + 100005;       vector<W>::iterator it = son[0][kkk].begin();       for (; it != son[0][kkk].end(); it++) {         W op = (*it);         int mm = x - op.x;         if (ans[op.id] == -1) ans[op.id] = t + mm;       }       son[0][kkk].clear();       if (heng > shu) {         dfs(x - shu, 0, 2, t + shu);       } else if (shu > heng) {         dfs(0, y - heng, 1, t + heng);       } else if (shu == heng) {         return;       }     }   }   int main() {     memset(ans, -1, sizeof(ans));     Rd(n), Rd(m), Rd(k);     for (int i = 1; i <= k; i++) {       int x, y;       Rd(x), Rd(y);       int tot = x + y + 100005;       son[1][tot].push_back((W){x, y, i});       tot = y - x + 100005;       son[0][tot].push_back((W){x, y, i});     }     dfs(0, 0, 0, 0);     for (int i = 1; i <= k; i++) Ps(ans[i]), puts(  );     return 0;   }