#include <bits/stdc++.h>   using namespace std;   struct node {     int x, y, id;     bool operator<(const node &A) const { return x < A.x; }   };   vector<node> line[2][100005 << 2];   bool mark[2][100005 << 2];   int n, m, k;   long long t[100005];   void add(int x, int y, int i) {     node a;     a.x = x, a.y = y, a.id = i;     line[1][y - x + 200000].push_back(a);     line[0][x + y + 200000].push_back(a);   }   void solve() {     bool flag = 0, first = 0;     long long res = 0;     int k = 1;     int x = 0, y = 0;     while (!flag) {       if (x == 0 || y == 0 || x == n || y == m) {         if (first) {           if ((x == 0 || x == n) && (y == 0 || y == m)) {             flag = 1;             break;           }           k = -k;         } else           first = 1;       }       int cur = (k == 1);       if (line[cur][y - k * x + 200000].size()) {         if (mark[cur][y - k * x + 200000])           flag = 1;         else {           mark[cur][y - k * x + 200000] = 1;           if (k == 1 && (!x || !y) || k == -1 && ((!x && y) || (y == m && x))) {             for (int i = 0; i < line[cur][y - k * x + 200000].size(); i++) {               int j = line[cur][y - k * x + 200000][i].id;               node tmp = line[cur][y - k * x + 200000][i];               if (t[j]) {               } else                 t[j] = res + abs(line[cur][y - k * x + 200000][i].x - x);             }           } else {             for (int i = line[cur][y - k * x + 200000].size() - 1; i >= 0; i--) {               int j = line[cur][y - k * x + 200000][i].id;               node tmp = line[cur][y - k * x + 200000][i];               if (t[j]) {               } else                 t[j] = res + abs(line[cur][y - k * x + 200000][i].x - x);             }           }         }       }       int nx1, ny1, nx2, ny2, t1 = x, t2 = y;       if (k == 1) {         if (0 <= n + y - x && n + y - x <= m)           nx1 = n, ny1 = n + y - x;         else           ny1 = m, nx1 = m + x - y;         if (0 <= y - x && y - x <= m)           nx2 = 0, ny2 = y - x;         else           ny2 = 0, nx2 = x - y;         if (nx1 == x) {           x = nx2, y = ny2;         } else           x = nx1, y = ny1;       } else {         if (0 <= x + y && x + y <= m)           nx1 = 0, ny1 = x + y;         else           ny1 = m, nx1 = x + y - m;         if (0 <= x + y - n && x + y - n <= m)           nx2 = n, ny2 = x + y - n;         else           ny2 = 0, nx2 = x + y;         if (nx1 == x) {           x = nx2, y = ny2;         } else           x = nx1, y = ny1;       }       res += abs(x - t1);     }   }   inline void print(long long x) {     if (!x) return;     print(x / 10);     putchar(x % 10 ^ 48);   }   inline void pf(long long x) {     print(x);     if (!x)       puts( 0 );     else       puts(  );   }   inline void rd(int &res) {     res = 0;     char c;     while (c = getchar(), c <  0  || c >  9 )       ;     do {       res = (res << 3) + (res << 1) + (c ^ 48);     } while (c = getchar(),  0  <= c && c <=  9 );   }   int main() {     rd(n);     rd(m);     rd(k);     for (int i = 1; i <= k; i++) {       int a, b;       rd(a);       rd(b);       add(a, b, i);     }     for (int i = 0; i < 2; i++)       for (int j = 0; j < 2 * 100005; j++) {         if (line[i][j].size()) {           sort(line[i][j].begin(), line[i][j].end());         }       }     solve();     for (int i = 1; i <= k; i++) {       if (t[i])         pf(t[i]);       else         puts( -1 );     }     return 0;   }   