#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#define ll long long
using namespace std;
inline ll fread() {
	ll x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9') {
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9') {
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x*f;
}
ll n,q;
ll A[100005];
ll num1[100005];
ll num2[100005];
stack<ll> _0;
int main(){
	n=fread(),q=fread();
	for(int i=1;i<=n;i++){
		A[i]=fread();
	}
	for(int i=1;i<=n;i++){
		A[i]=abs(A[i+1]-A[i]);
	}
	A[n--]=0;
	while(q--){
		stack<ll> sta;
		ll a=fread(),b=fread();
		b--;
		for(int i=a;i<=b;i++){
			while(!sta.empty()&&A[sta.top()]<A[i]){
				sta.pop();
				num1[i]++;
			}
			if(!sta.empty()) num1[i]=i-sta.top();
			else num1[i]=i-a+1;
			sta.push(i);
//			printf("%d ",num1[i]);
		}
//		puts("");
		ll ans=0;
		sta=_0;
		for(int i=b;i>=a;i--){
//			cout<<"[";
			while(!sta.empty()&&A[sta.top()]<=A[i]){
//				cout<<' '<<sta.top();
				sta.pop();
				num2[i]++;
			}
			if(!sta.empty()) num2[i]=sta.top()-i;
			else num2[i]=b-i+1;
//			cout<<" ] ";
			sta.push(i);
//			printf("%d ",num2[i]);
			ans+=num1[i]*num2[i]*A[i];
//			printf("[%d]",ans);
		}
//		puts("");
		printf("%I64d\n",ans);
	}
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int N = 1e5 + 1, L = 18;
int a[N], d[N];
int p[N][L];

void build(int len) {
	for (int i = 0; i < len; i++)
		p[i][0] = i;
	for (int j = 1; 1 << j < len; j++)
		for (int i = 0; i + (1 << j) - 1 < len; i++) {
			p[i][j] = p[i][j - 1];
			if (d[p[i][j - 1]] < d[p[i + (1 << (j - 1))][j - 1]])
				p[i][j] = p[i + (1 << (j - 1))][j - 1];
		}
}

int query(int l, int r) {
	int lg = 0;
	while (1 << (lg + 1) <= r - l + 1)
		lg++;
	int ind1 = p[l][lg], ind2 = p[r - (1 << lg) + 1][lg];
	if (d[ind1] > d[ind2])
		return ind1;
	return ind2;
}

ll get(int l, int r) {
	if (l >= r) return 0;
	int ind = query(l, r - 1);
	ll ans = 1LL * (ind - l + 1) * (r - ind) * d[ind] + get(l, ind) + get(ind + 1, r);
	return ans;
}

int main() {
	int n, q;
	scanf("%d %d", &n, &q);
	for (int i = 0; i < n; i++) {
		scanf("%d", a + i);
		if (i) d[i - 1] = abs(a[i] - a[i - 1]);
	}
	build(n - 1);
	while (q--) {
		int l, r;
		scanf("%d %d", &l, &r);
		printf("%I64d\n", get(--l, --r));
	}
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int N = 1e5 + 1, L = 18;
ll a[N], d[N];
ll p[N][L];

void build(int len) {
	for (int i = 0; i < len; i++)
		p[i][0] = i;
	for (int j = 1; (1 << j) - 1 < len; j++)
		for (int i = 0; i + (1 << j) - 1 < len; i++) {
			p[i][j] = p[i][j - 1];
			if (d[p[i][j - 1]] < d[p[i + (1 << (j - 1))][j - 1]])
				p[i][j] = p[i + (1 << (j - 1))][j - 1];
		}
}

int query(int l, int r) {
	int lg = 0;
	while (1 << (lg + 1) <= r - l + 1)
		lg++;
	int ind1 = p[l][lg], ind2 = p[r - (1 << lg) + 1][lg];
	if (d[ind1] > d[ind2])
		return ind1;
	return ind2;
}

ll get(int l, int r) {
	if (l >= r) return 0;
	int ind = query(l, r - 1);
	ll ans = 1LL * (ind - l + 1) * (r - ind) * d[ind] + get(l, ind) + get(ind + 1, r);
	return ans;
}

int main() {
	int n, q;
	scanf("%d %d", &n, &q);
	for (int i = 0; i < n; i++) {
		scanf("%I64d", a + i);
		if (i) d[i - 1] = a[i] - a[i - 1];
		if (d[i - 1] < 0) d[i - 1] *= -1;
	}
	build(n - 1);
	while (q--) {
		int l, r;
		scanf("%d %d", &l, &r);
		printf("%I64d\n", get(--l, --r));
	}
}


#include<stdio.h>
#include<string.h>
#include<string>
#include<algorithm>
#include<math.h>
using namespace std;
typedef long long ll;
const int maxn=111111;
int A[maxn],B[maxn],R[maxn],L[maxn];
int main()
{
    int n,q;
    while(scanf("%d%d",&n,&q)!=EOF)
    {
        scanf("%d",&A[0]);
        B[0]=-1;
        B[n]=-1;
        for(int i=1;i<n;i++)
        {
            scanf("%d",&A[i]);
            B[i]=fabs(A[i]-A[i-1]);
            //printf("%d ",B[i]);
        }
        //printf("\n");
        for(int i=1;i<n;i++)
        {
            int a=i;
            while(a>1&&B[i]>B[a-1])
            {
                a=L[a-1];
            }
            L[i]=a;
            //printf("%d ",L[i]);
        }
        //printf("\n");
        for(int i=n-1;i>=1;i--)
        {
            int a=i;
            while(a<n-1&&B[i]>=B[a+1])
            {
                a=R[a+1];
            }
            R[i]=a;
           // printf("%d ",R[i]);
        }
       // printf("\n");
        int l,r;
        ll ans=0,maxv=-1;
        for(int i=0;i<q;i++)
        {
            ans=0;
            scanf("%d%d",&l,&r);
            for(int i=l;i<r;i++)
            {
                int k=L[i],k2=R[i];
                if(R[i]>=r)
                    k2=r-1;
                if(L[i]<l)
                    k=l;
                ans+=(ll)(i-k+1)*(k2-i+1)*B[i];
            }
            printf("%lld\n",ans);
        }

    }
    return 0;
}


#include <iostream>
#include <fstream>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
ll num[100000];
pii diff[100000];
int above[100001];
int below[100001];
int main()
{
    long long ans=0;
    int N, Q;
    cin >> N >> Q;
    for (int i=0; i<N; i++)
        cin >> num[i];
    for (int i=0; i<N-1; i++)
        diff[i]=pii (abs(num[i+1]-num[i]), i);
    sort(diff, diff+N-1);
    for (int i=0; i<Q; i++)
    {
        ans=0;
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        b--;
        for (int i=0; i<N; i++)
        {
            above[i]=i+1;
            below[i]=i-1;
        }
        for (int i=0; i<N-1; i++)
        {
            if (a<=diff[i].second && diff[i].second<=b)
            {
                ans+=diff[i].first*(min(above[diff[i].second], b+1)-diff[i].second)*(diff[i].second-max(a-1, below[diff[i].second]));
                below[above[diff[i].second]]=below[diff[i].second];
                if (below[diff[i].second]!=-1)
                above[below[diff[i].second]]=above[diff[i].second];
            }
        }
        cout << ans << endl;
    }

}


#include <iostream>
#include <fstream>
#include <cmath>
#include <algorithm>
using namespace std;
typedef pair<int, int> pii;
int num[100000];
pii diff[100000];
int above[100001];
int below[100001];
int main()
{
    long long ans=0;
    int N, Q;
    cin >> N >> Q;
    for (int i=0; i<N; i++)
        cin >> num[i];
    for (int i=0; i<N-1; i++)
        diff[i]=pii (abs(num[i+1]-num[i]), i);
    sort(diff, diff+N-1);
    for (int i=0; i<Q; i++)
    {
        ans=0;
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        b--;
        for (int i=0; i<N; i++)
        {
            above[i]=i+1;
            below[i]=i-1;
        }
        for (int i=0; i<N-1; i++)
        {
            if (a<=diff[i].second && diff[i].second<=b)
            {
                ans+=diff[i].first*(min(above[diff[i].second], b+1)-diff[i].second)*(diff[i].second-max(a-1, below[diff[i].second]));
                below[above[diff[i].second]]=below[diff[i].second];
                if (below[diff[i].second]!=-1)
                above[below[diff[i].second]]=above[diff[i].second];
            }
        }
        cout << ans << endl;
    }

}


#include <bits/stdc++.h>

using namespace std;

#define sd(x) scanf("%d",&x)
#define sl(x) scanf("%lld",&x)
#define pd(x) printf("%d\n",x)
#define pl(x) printf("%lld\n",x)
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define Fill(a, b) memset(a, b, sizeof(a))

#define GET_MACRO(_1,_2,_3,NAME,...) NAME
#define FOR(...) GET_MACRO(__VA_ARGS__, FORC, FORA)(__VA_ARGS__)
#define FORA(i,x) for(int i=0;i<x;i++)
#define FORC(i,k,x) for(int i=k;i<x;i++)

#define N 100010
#define LOGN 20
#define MAX (1+(1<<21))
#define INF 1000000009
#define MOD 1000000007

typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;

LL ar[N];
LL t1, t2;
LL n, q, l, r;
LL rmq[N][LOGN], power[LOGN];

void max_rmq(int);
int max_query(int, int);

LL compute_ans(LL l, LL r) {
    if (r - l < 1)  return 0;
    if (r - l == 1) return ar[l];
    LL pos = max_query(l, r-1);
    //printf("%lld %lld %lld\n", l, r, pos);
    // getchar();
    LL ans = (pos-l+1)*(r-pos)*(ar[pos]);
    ans += compute_ans(l, pos);
    ans += compute_ans(pos+1, r);
    return ans;
}

void solve() {
    sl(n); sl(q);
    sl(t1);
    FOR(i, 1, n) {
        sl(t2);
        ar[i-1] = abs(t2 - t1);
        t1 = t2;
    }
    ar[n-1] = -1;
    max_rmq(n-1);

    while(q--) {
        // printf("%d\n", query_tree_max(1, 0, n-1, 1, 3));
        sl(l); sl(r);
        // printf("%d\n", query_tree_max(1, 0, n-2, l, r));
        printf("%lld\n", compute_ans(l-1, r-1));
    }

}

int main() {
    int t=1;
    // scanf("%d",&t);
    for(int i=1;i<=t;i++) {
        // printf("Case #%d: ", i);
        solve();
    }
}

int max_query(int i, int j) {
    if (j == i) return i;
    int k = log2(j-i);
    if (ar[rmq[i][k]] >= ar[rmq[j-power[k]+1][k]])
        return rmq[i][k];
    return rmq[j-power[k]+1][k];
}

void max_rmq(int n) {
    power[0] = 1;
    FORC(i, 1, LOGN) {
        power[i] = power[i-1]*2;
    }

    int l = log2(n), x;
    FOR(i ,l+1) rmq[n-1][i] = n-1;
    FOR(i, n)   rmq[i][0] = i;

    for(int i=n-2; i >= 0; i--) {
        for(int j=1; j <= l; j++) {
            x = min((LL)n-1, i+power[j-1]);
            if (ar[rmq[i][j-1]] >= ar[rmq[x][j-1]]) {
                rmq[i][j] = rmq[i][j-1];
            } else {
                rmq[i][j] = rmq[x][j-1];
            }
        }
    }
}


#include <bits/stdc++.h>
#define x first
#define y second
#define gcd(x, y) __gcd((x), (y))
#define lcm(x, y) ((x) / gcd((x), (y)) * (y))
using namespace std;

using i64 = long long;
using pll = pair<i64, i64>;

const int N = 1e6;

i64 rev[N];

int n, m;

int main() {
#ifdef HOME
	freopen("cf.in", "r", stdin);
	freopen("cf.out", "w", stdout);
#endif
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	pll res;
	i64 ant, g;
	int n, m, k, x, y;

	cin >> n >> m >> k;
	n*= 2, m*= 2;

	for (int i = 0; i < m; ++i)
		rev[i] = 2e9;
	for (int i = m - 1; i >= 0; --i)
		rev[i * n % m] = i;

	while (k--) {
		cin >> x >> y;
		if (x % 2 != y % 2 || (y - x) % gcd(n, m) != 0) {
			cout << "-1\n";
			continue; }

		ant = 1e18;
		for (auto nx: {x, n - x})
		for (auto ny: {y, m - y}) {
			i64 dif = (ny - nx + 4 * n * m) % m;
			ant = min(ant, m * rev[dif] + nx); }

		cout << (ant > n / gcd(n, m) * m ? -1 : ant) << '\n'; }

	return 0; }


#include <bits/stdc++.h>
#define x first
#define y second
#define gcd(x, y) __gcd((x), (y))
#define lcm(x, y) ((x) / gcd((x), (y)) * (y))
using namespace std;

using i64 = long long;
using pll = pair<i64, i64>;

const int N = 1e6;

i64 rev[N];

int n, m;

int main() {
#ifdef HOME
	freopen("cf.in", "r", stdin);
	freopen("cf.out", "w", stdout);
#endif
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	pll res;
	i64 ant, g;
	int n, m, k, x, y;

	cin >> n >> m >> k;
	n*= 2, m*= 2;

	for (int i = 0; i < m; ++i)
		rev[i] = 2e9;
	for (int i = m - 1; i >= 0; --i)
		rev[i * n % m] = i;

	while (k--) {
		cin >> x >> y;
		if (x % 2 != y % 2 || (y - x) % gcd(n, m) != 0) {
			cout << "-1\n";
			continue; }

		ant = 1e18;
		for (auto nx: {x, n - x})
		for (auto ny: {y, m - y}) {
			i64 dif = (ny - nx + 4 * n * m) % m;
			ant = min(ant, n * rev[dif] + nx); }

		cout << (ant > n / gcd(n, m) * m ? -1 : ant) << '\n'; }

	return 0; }


#include <bits/stdc++.h>
#define x first
#define y second
#define gcd(x, y) __gcd((x), (y))
#define lcm(x, y) ((x) / gcd((x), (y)) * (y))
using namespace std;

using i64 = long long;
using pll = pair<i64, i64>;

const int N = 1e6;

i64 rev[N];

int n, m;

int main() {
#ifdef HOME
	freopen("cf.in", "r", stdin);
	freopen("cf.out", "w", stdout);
#endif
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	pll res;
	i64 ant, g;
	int n, m, k, x, y;

	cin >> n >> m >> k;
	n*= 2, m*= 2;

	for (int i = 0; i < m; ++i)
		rev[i] = 2e9;
	for (int i = m - 1; i >= 0; --i)
		rev[i * n % m] = i;

	while (k--) {
		cin >> x >> y;
		if (x % 2 != y % 2) {
			cout << "-1\n";
			continue; }

		ant = 1e18;
		for (auto nx: {x, n - x})
		for (auto ny: {y, m - y}) {
			i64 dif = (ny - nx + 4 * n * m) % m;
			ant = min(ant, n * rev[dif] + nx); }

		cout << (ant > n / gcd(n, m) * m ? -1 : ant) << '\n'; }

	return 0; }


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int MAX_N = 100030;     // Change as necessary
const ll  MODD  = 1000000009; //

#define ll int

int x[MAX_N],y[MAX_N];

int gcd(int a, int b, int &s, int &t) { // a*s+b*t = g
  if (b==0) { t = 0; s = (a < 0) ? -1 : 1; return (a < 0) ? -a : a;
  } else { int g = gcd(b, a%b, t, s);  t -= a/b*s;  return g; }
}

void do_case(){
  int m,n,k; cin >> m >> n >> k;

  for(int i=0;i<k;i++)
    cin >> x[i] >> y[i];

  int s,t;
  int g = gcd(2*m,2*n,s,t);

  for(int i=0;i<k;i++){
    if(x == y){
      cout << x << endl;
      continue;
    }
    int x1 = x[i], x2 = 2*m - x1;
    int y1 = y[i], y2 = 2*n - y1;

    vector<pair<int,int> > v = { {x1,y1}, {x1,y2}, {x2,y1}, {x2,y2} };
    vector<int> poss;
    for(auto p : v){
      int xx = p.first, yy = p.second;

      if(xx != yy){
        if(xx % 2 != yy % 2) continue;
        if((yy-xx) % g != 0) continue;

        int mult = -(yy-xx)/g;

        int S = s*mult;
        int T = t*mult;

        xx += 2*m*S;
        yy -= 2*n*T;

        xx %= (4*m*n/g);
        if(xx < 0) xx += (4*m*n/g);
        poss.push_back(xx);
      } else poss.push_back(xx);

    }
    if(poss.size() == 0) cout << -1 << "\n";
    else cout << *min_element(poss.begin(),poss.end()) << "\n";
  }
}

#undef int

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  do_case();

  return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int MAX_N = 100030;     // Change as necessary
const ll  MODD  = 1000000009; //

#define ll int

int x[MAX_N],y[MAX_N];

int gcd(int a, int b, int &s, int &t) { // a*s+b*t = g
  if (b==0) { t = 0; s = (a < 0) ? -1 : 1; return (a < 0) ? -a : a;
  } else { int g = gcd(b, a%b, t, s);  t -= a/b*s;  return g; }
}

void do_case(){
  int m,n,k; cin >> m >> n >> k;

  for(int i=0;i<k;i++)
    cin >> x[i] >> y[i];

  int s,t;
  int g = gcd(2*m,2*n,s,t);

  for(int i=0;i<k;i++){
    if(x == y){
      cout << x << endl;
      continue;
    }
    int x1 = x[i], x2 = 2*m - x1;
    int y1 = y[i], y2 = 2*n - y1;

    vector<pair<int,int> > v = { {x1,y1}, {x1,y2}, {x2,y1}, {x2,y2} };
    vector<int> poss;
    for(auto p : v){
      int xx = p.first, yy = p.second;

      if(xx != yy){
        if(xx % 2 != yy % 2) continue;
        if((yy-xx) % g != 0) continue;

        int mult = -(yy-xx)/g;

        int S = s*mult;
        int T = t*mult;

        xx -= 2*m*S;
        yy += 2*n*T;

        xx %= (4*m*n/g);
        if(xx < 0) xx += (4*m*n/g);
        if(xx < m*n/g)
          poss.push_back(xx);
      } else poss.push_back(xx);

    }
    if(poss.size() == 0) cout << -1 << "\n";
    else cout << *min_element(poss.begin(),poss.end()) << "\n";
  }
}

#undef int

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  do_case();

  return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int MAX_N = 100030;     // Change as necessary
const ll  MODD  = 1000000009; //

#define int ll

int x[MAX_N],y[MAX_N];

int gcd(int a, int b, int &s, int &t) { // a*s+b*t = g
  if (b==0) { t = 0; s = (a < 0) ? -1 : 1; return (a < 0) ? -a : a;
  } else { int g = gcd(b, a%b, t, s);  t -= a/b*s;  return g; }
}

void do_case(){
  int m,n,k; cin >> m >> n >> k;

  for(int i=0;i<k;i++)
    cin >> x[i] >> y[i];

  int s,t;
  int g = gcd(2*m,2*n,s,t);

  for(int i=0;i<k;i++){
    if(x == y){
      cout << x << endl;
      continue;
    }
    int x1 = x[i], x2 = 2*m - x1;
    int y1 = y[i], y2 = 2*n - y1;

    vector<pair<int,int> > v = { {x1,y1}, {x1,y2}, {x2,y1}, {x2,y2} };
    vector<int> poss;
    for(auto p : v){
      int xx = p.first, yy = p.second;

      if(xx != yy){
        if(xx % 2 != yy % 2) continue;
        if((yy-xx) % g != 0) continue;

        int mult = (yy-xx)/g;

        int S = s*mult;
        int T = t*mult;

        xx += 2*m*S;
        yy -= 2*n*T;

        xx %= (4*m*n/g);
        if(xx < 0) xx += (4*m*n/g);
        //if(xx < m*n/g)
          poss.push_back(xx);
      } else poss.push_back(xx);

    }
    if(poss.size() == 0) cout << -1 << "\n";
    else cout << *min_element(poss.begin(),poss.end()) << "\n";
  }
}

#undef int

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  do_case();

  return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int MAX_N = 100030;     // Change as necessary
const ll  MODD  = 1000000009; //

#define ll int

int x[MAX_N],y[MAX_N];

int gcd(int a, int b, int &s, int &t) { // a*s+b*t = g
  if (b==0) { t = 0; s = (a < 0) ? -1 : 1; return (a < 0) ? -a : a;
  } else { int g = gcd(b, a%b, t, s);  t -= a/b*s;  return g; }
}

void do_case(){
  int m,n,k; cin >> m >> n >> k;

  for(int i=0;i<k;i++)
    cin >> x[i] >> y[i];

  int s,t;
  int g = gcd(2*m,2*n,s,t);

  for(int i=0;i<k;i++){
    if(x == y){
      cout << x << endl;
      continue;
    }
    int x1 = x[i], x2 = 2*m - x1;
    int y1 = y[i], y2 = 2*n - y1;

    vector<pair<int,int> > v = { {x1,y1}, {x1,y2}, {x2,y1}, {x2,y2} };
    vector<int> poss;
    for(auto p : v){
      int xx = p.first, yy = p.second;

      if(xx != yy){
        if(xx % 2 != yy % 2) continue;
        if((yy-xx) % g != 0) continue;

        int mult = (yy-xx)/g;

        int S = s*mult;
        int T = t*mult;

        xx += 2*m*S;
        yy -= 2*n*T;

        xx %= (4*m*n/g);
        if(xx < 0) xx += (4*m*n/g);
        //if(xx < m*n/g)
          poss.push_back(xx);
      } else poss.push_back(xx);

    }
    if(poss.size() == 0) cout << -1 << "\n";
    else cout << *min_element(poss.begin(),poss.end()) << "\n";
  }
}

#undef int

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  do_case();

  return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int MAX_N = 100030;     // Change as necessary
const ll  MODD  = 1000000009; //

#define ll int

int x[MAX_N],y[MAX_N];

int gcd(int a, int b, int &s, int &t) { // a*s+b*t = g
  if (b==0) { t = 0; s = (a < 0) ? -1 : 1; return (a < 0) ? -a : a;
  } else { int g = gcd(b, a%b, t, s);  t -= a/b*s;  return g; }
}

void do_case(){
  int m,n,k; cin >> m >> n >> k;

  for(int i=0;i<k;i++)
    cin >> x[i] >> y[i];

  int s,t;
  int g = gcd(2*m,2*n,s,t);

  for(int i=0;i<k;i++){
    if(x == y){
      cout << x << endl;
      continue;
    }
    int x1 = x[i], x2 = 2*m - x1;
    int y1 = y[i], y2 = 2*n - y1;

    vector<pair<int,int> > v = { {x1,y1}, {x1,y2}, {x2,y1}, {x2,y2} };
    vector<int> poss;
    for(auto p : v){
      int xx = p.first, yy = p.second;

      if(xx != yy){
        if(xx % 2 != yy % 2) continue;
        if((yy-xx) % g != 0) continue;

        int mult = -(yy-xx)/g;

        int S = s*mult;
        int T = t*mult;

        xx += 2*m*S;
        yy -= 2*n*T;

        xx %= (4*m*n/g);
        if(xx < 0) xx += (4*m*n/g);
        if(xx < m*n/g)
          poss.push_back(xx);
      } else poss.push_back(xx);

    }
    if(poss.size() == 0) cout << -1 << "\n";
    else cout << *min_element(poss.begin(),poss.end()) << "\n";
  }
}

#undef int

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  do_case();

  return 0;
}


#include <iostream>
#include <list>
#include <cmath>
#include <algorithm>
#include <map>
#include <vector>
#include <cstdio>
#include <queue>

using namespace std;

const long long inf = 1e17;
int n, m;
int x, y;
long long t;
int curr;
int dx, dy;

int get_t(int x, int y){
    if(dx > 0){
        x = n - x;
    }
    if(dy > 0){
        y = m - y;
    }
    return min(x, y);
}

int get_number(int x,int y){
    int curr;
    if(x == 0){curr = y;} else
    if(x == n){curr = m + 1 + y;} else
    if(y == 0) curr = 2 * m + x + 1; else
        curr = 2 * m + n + x;
    return curr;
}

void go(){
    int tt = get_t(x, y);
    x += dx * tt;
    y += dy * tt;
    t += tt;
    curr = get_number(x, y);
    if(x == 0 || x == n) dx *= -1;
    if(y == 0 || y == m) dy *= -1;

}

int _dx[] = {1,1,-1,-1};
int _dy[] = {1,-1,1,-1};
long long d[400500];

int main()
{
    int k;
    scanf("%d%d%d", &n, &m, &k);
    x = 0;
    y = 0;
    dx = dy = 1;
    for(int i = 0; i < 2 * m + 2 * n; i++)
        d[i] = inf;
    curr = 0;
    t = 0;
    while(d[curr] == inf){
        d[curr] = t;
        go();
    }
    while(k--){
        scanf("%d%d", &x, &y);
        long long ans = inf;
        int xx = x;
        int yy = y;
        for(int i = 0; i < 4; i++){
            t = 0;
            x = xx;
            y = yy;
            dx = _dx[i];
            dy = _dy[i];
            go();
            if(d[curr] + t < ans) ans = d[curr] + t;
        }
        if(ans == inf){ans = -1;}
        cout << ans << endl;
    }
    return 0;
}


#include <iostream>
#include <list>
#include <cmath>
#include <algorithm>
#include <map>
#include <vector>
#include <cstdio>
#include <queue>

using namespace std;

const long long inf = 1e17;
int n, m;
int x, y;
long long t;
int curr;
int dx, dy;

int get_t(int x, int y){
    if(dx > 0){
        x = n - x;
    }
    if(dy > 0){
        y = m - y;
    }
    return min(x, y);
}

void get_number(int x,int y){
    int z = max(m, n) + 5;
    if(x == 0){curr = y;} else
    if(x == n){curr = z + y;} else
    if(y == 0) curr = 2 * z + x; else
        curr = 3 * z + x;

}

void go(){
    int tt = get_t(x, y);
    x += dx * tt;
    y += dy * tt;
    t += tt;
    get_number(x, y);
    if(x == 0 || x == n) dx *= -1;
    if(y == 0 || y == m) dy *= -1;

}

int _dx[] = {1,1,-1,-1};
int _dy[] = {1,-1,1,-1};
long long d[400500];

int main()
{
    ios_base::sync_with_stdio(false);
    int k;
    scanf("%d%d%d", &n, &m, &k);
    x = 0;
    y = 0;
    dx = dy = 1;
    for(int i = 0; i < 2 * m + 2 * n + 148; i++)
        d[i] = inf;
    curr = 0;
    t = 0;
    while(d[curr] == inf){
        d[curr] = t;
        go();
    }
    while(k--){
        scanf("%d%d", &x, &y);
        long long ans = inf;
        int xx = x;
        int yy = y;
        for(int i = 0; i < 4; i++){
            t = 0;
            x = xx;
            y = yy;
            dx = _dx[i];
            dy = _dy[i];
            go();
            if(d[curr] + t < ans) ans = d[curr] + t;
        }
        if(ans == inf){ans = -1;}
        cout << ans <<endl;
    }
    return 0;
}


#include <iostream>
#include <list>
#include <cmath>
#include <algorithm>
#include <map>
#include <vector>
#include <cstdio>
#include <queue>

using namespace std;

const long long inf = 1e17;
int n, m;
int x, y;
long long t;
int curr;
int dx, dy;

int get_t(int x, int y){
    if(dx > 0){
        x = n - x;
    }
    if(dy > 0){
        y = m - y;
    }
    return min(x, y);
}

void get_number(int x,int y){
    int z = max(m, n) + 5;
    if(x == 0){curr = y;} else
    if(x == n){curr = z + y;} else
    if(y == 0) curr = 2 * z + x; else
        curr = 3 * z + x;

}

void go(){
    int tt = get_t(x, y);
    x += dx * tt;
    y += dy * tt;
    t += tt;
    get_number(x, y);
    if(x == 0 || x == n) dx *= -1;
    if(y == 0 || y == m) dy *= -1;

}

int _dx[] = {1,1,-1,-1};
int _dy[] = {1,-1,1,-1};
long long d[400500];

int main()
{
    int k;
    scanf("%d%d%d", &n, &m, &k);
    x = 0;
    y = 0;
    dx = dy = 1;
    for(int i = 0; i < 2 * m + 2 * n + 148; i++)
        d[i] = inf;
    curr = 0;
    t = 0;
    while(d[curr] == inf){
        d[curr] = t;
        go();
    }
    while(k--){
        scanf("%d%d", &x, &y);
        int ans = inf;
        int xx = x;
        int yy = y;
        for(int i = 0; i < 4; i++){
            t = 0;
            x = xx;
            y = yy;
            dx = _dx[i];
            dy = _dy[i];
            go();
            if(d[curr] + t < ans) ans = d[curr] + t;
        }
        if(ans == inf){ans = -1;}
        printf("%d\n",ans);
    }
    return 0;
}


#include <iostream>
#include <list>
#include <cmath>
#include <algorithm>
#include <map>
#include <vector>
#include <cstdio>
#include <queue>

using namespace std;

const int inf = 1e9;
int n, m;
int x, y;
int t, curr;
int dx, dy;

int get_t(int x, int y){
    if(dx > 0){
        x = n - x;
    }
    if(dy > 0){
        y = m - y;
    }
    return min(x, y);
}

int get_number(int x,int y){
    int curr;
    if(x == 0){curr = y;} else
    if(x == n){curr = m + 1 + y;} else
    if(y == 0) curr = 2 * m + x + 1; else
        curr = 2 * m + n + x;
    return curr;
}

void go(){
    int tt = get_t(x, y);
    x += dx * tt;
    y += dy * tt;
    t += tt;
    curr = get_number(x, y);
    if(x == 0 || x == n) dx *= -1;
    if(y == 0 || y == m) dy *= -1;

}

int _dx[] = {1,1,-1,-1};
int _dy[] = {1,-1,1,-1};
int d[400500];

int main()
{
    int k;
    scanf("%d%d%d", &n, &m, &k);
    x = 0;
    y = 0;
    dx = dy = 1;
    for(int i = 0; i < 2 * m + 2 * n; i++)
        d[i] = inf;
    curr = 0;
    t = 0;
    while(d[curr] == inf){
        d[curr] = t;
        go();
    }
    while(k--){
        scanf("%d%d", &x, &y);
        int ans = inf;
        int xx = x;
        int yy = y;
        for(int i = 0; i < 4; i++){
            t = 0;
            x = xx;
            y = yy;
            dx = _dx[i];
            dy = _dy[i];
            go();
            if(d[curr] + t < ans) ans = d[curr] + t;
        }
        if(ans == inf){ans = -1;}
        printf("%d\n",ans);
    }
    return 0;
}


#include <iostream>
#include <list>
#include <cmath>
#include <algorithm>
#include <map>
#include <vector>
#include <cstdio>
#include <queue>

using namespace std;

const long long inf = 1e17;
int n, m;
int x, y;
long long t;
int curr;
int dx, dy;

int get_t(int x, int y){
    if(dx > 0){
        x = n - x;
    }
    if(dy > 0){
        y = m - y;
    }
    return min(x, y);
}

void get_number(int x,int y){
    int z = max(m, n) + 5;
    if(x == 0){curr = y;} else
    if(x == n){curr = z + y;} else
    if(y == 0) curr = 2 * z + x; else
        curr = 3 * z + x;

}

void go(){
    int tt = get_t(x, y);
    x += dx * tt;
    y += dy * tt;
    t += tt;
    get_number(x, y);
    if(x == 0 || x == n) dx *= -1;
    if(y == 0 || y == m) dy *= -1;

}

int _dx[] = {1,1,-1,-1};
int _dy[] = {1,-1,1,-1};
long long d[400500];

int main()
{
    int k;
    scanf("%d%d%d", &n, &m, &k);
    x = 0;
    y = 0;
    dx = dy = 1;
    for(int i = 0; i < 2 * m + 2 * n + 148; i++)
        d[i] = inf;
    curr = 0;
    t = 0;
    while(d[curr] == inf){
        d[curr] = t;
        go();
    }
    while(k--){
        scanf("%d%d", &x, &y);
        long long ans = inf;
        int xx = x;
        int yy = y;
        for(int i = 0; i < 4; i++){
            t = 0;
            x = xx;
            y = yy;
            dx = _dx[i];
            dy = _dy[i];
            go();
            if(d[curr] + t < ans) ans = d[curr] + t;
        }
        if(ans == inf){ans = -1;}
        printf("%I64d\n",ans);
    }
    return 0;
}


#include <iostream>
#include <list>
#include <cmath>
#include <algorithm>
#include <map>
#include <vector>
#include <cstdio>
#include <queue>

using namespace std;

const int inf = 1e8;
int n, m;
int x, y;
int t, curr;
int dx, dy;

int get_t(int x, int y){
    if(dx > 0){
        x = n - x;
    }
    if(dy > 0){
        y = m - y;
    }
    return min(x, y);
}

int get_number(int x,int y){
    int z = max(m, n) + 5;
    int curr;
    if(x == 0){curr = y;} else
    if(x == n){curr = z + y;} else
    if(y == 0) curr = 2 * z + x; else
        curr = 3 * z + x;
    return curr;
}

void go(){
    int tt = get_t(x, y);
    x += dx * tt;
    y += dy * tt;
    t += tt;
    curr = get_number(x, y);
    if(x == 0 || x == n) dx *= -1;
    if(y == 0 || y == m) dy *= -1;

}

int _dx[] = {1,1,-1,-1};
int _dy[] = {1,-1,1,-1};
int d[400500];

int main()
{
    int k;
    scanf("%d%d%d", &n, &m, &k);
    x = 0;
    y = 0;
    dx = dy = 1;
    for(int i = 0; i < 2 * m + 2 * n; i++)
        d[i] = inf;
    curr = 0;
    t = 0;
    while(d[curr] == inf){
        d[curr] = t;
        go();
    }
    while(k--){
        scanf("%d%d", &x, &y);
        int ans = inf;
        int xx = x;
        int yy = y;
        for(int i = 0; i < 4; i++){
            t = 0;
            x = xx;
            y = yy;
            dx = _dx[i];
            dy = _dy[i];
            go();
            if(d[curr] + t < ans) ans = d[curr] + t;
        }
        if(ans == inf){ans = -1;}
        printf("%d\n",ans);
    }
    return 0;
}


#include <iostream>
#include <list>
#include <cmath>
#include <algorithm>
#include <map>
#include <vector>
#include <cstdio>
#include <queue>

using namespace std;

const int inf = 1e8;
int n, m;
int x, y;
int t, curr;
int dx, dy;

int get_t(int x, int y){
    if(dx > 0){
        x = n - x;
    }
    if(dy > 0){
        y = m - y;
    }
    return min(x, y);
}

void get_number(int x,int y){
    int z = max(m, n) + 5;
    if(x == 0){curr = y;} else
    if(x == n){curr = z + y;} else
    if(y == 0) curr = 2 * z + x; else
        curr = 3 * z + x;

}

void go(){
    int tt = get_t(x, y);
    x += dx * tt;
    y += dy * tt;
    t += tt;
    get_number(x, y);
    if(x == 0 || x == n) dx *= -1;
    if(y == 0 || y == m) dy *= -1;

}

int _dx[] = {1,1,-1,-1};
int _dy[] = {1,-1,1,-1};
int d[400500];

int main()
{
    int k;
    scanf("%d%d%d", &n, &m, &k);
    x = 0;
    y = 0;
    dx = dy = 1;
    for(int i = 0; i < 2 * m + 2 * n + 148; i++)
        d[i] = inf;
    curr = 0;
    t = 0;
    while(d[curr] == inf){
        d[curr] = t;
        go();
    }
    while(k--){
        scanf("%d%d", &x, &y);
        int ans = inf;
        int xx = x;
        int yy = y;
        for(int i = 0; i < 4; i++){
            t = 0;
            x = xx;
            y = yy;
            dx = _dx[i];
            dy = _dy[i];
            go();
            if(d[curr] + t < ans) ans = d[curr] + t;
        }
        if(ans == inf){ans = -1;}
        printf("%d\n",ans);
    }
    return 0;
}


#include <cstdio>
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <iterator>
#include <ctime>
#include <random>
using namespace std;
void solve(), solve1();
int main(int argc, const char * argv[]) {
    srand((unsigned int)time(0));
    solve();
    return 0;
}
#define int long long
#define mp make_pair
#define sqr(x) ((x)*(x))
#define double long double
int mod = 1e9;
int inf = 2 * 1e18;

#define N 400000
int n, m, k, ans[N];

struct point
{
    int x, y, id;
} p[N];

int gcd(int a, int b)
{
    while (b)
        swap(b, a %= b);
    return a;
}

int gcd(int a, int b, int & x, int & y)
{
    if (a == 0)
    {
        x = 0; y = 1;
        return b;
    }
    int xx, yy;
    int d = gcd (b%a, a, xx, yy);
    x = yy - (b / a) * xx;
    y = xx;
    return d;
}

bool f2(int a, int b, int c, int & x0, int & y0, int & g)
{
    g = gcd (abs(a), abs(b), x0, y0);
    if (c % g != 0)
        return false;
    x0 *= c / g;
    y0 *= c / g;
    if (a < 0)
    {

    x0 *= -1;

    }
    if (b < 0)
    {

        y0 *= -1;

    }
    return true;
}

void f(int a, int b, int c, int& x, int& y, int n, int m, int px, int py)
{
    if (c == 0)
    {
        int w = a, h = -b;
        int g = gcd(w, h);
        w /= g, h /= g;
        x = h - 1;
        y = w - 1;
    }
    int x0, y0, g0;
    if (!f2(a, b, c, x0, y0, g0))
    {
        return;
    }
    int g = gcd(abs(a), abs(b));
    int l = -1e9, r = 1e9;
    while (l < r)
    {
        int mid = (l + r)/2;
        if ((x0 - b * mid / g) >= 0 && (y0 + a * mid / g) >= 0)
            r = mid;
        else
            l = mid + 1;
    }
    if ((x0 - b * r / g) >= 0 && (y0 + a * r / g) >= 0)
    {
        x = (x0 - b * r / g), y = (y0 + a * r / g);
        return;
    }
}

void solve()
{
    //     ifstream cin("input.txt");
    //  ofstream cout("output.txt");
    ios::sync_with_stdio(0);
    cin >> n >> m >> k;
    for (int i = 0; i < k; ++i)
    {
        cin >> p[i].x >> p[i].y;
        p[i].id = i;
        ans[i] = -1;
    }
    int g = gcd(n, m);
    int h = m / g;
    int tx = h;
    int t = n * tx;

    int kk = k;
    for (int i = 0; i < k; ++i)
    {
        p[k + i].id = p[i].id;
        p[k + i].x = p[i].x;
        p[k + i].y = m + m - p[i].y;
    }
    k+=k;

    for (int i = 0; i < kk; ++i)
    {
        p[k + i].id = p[i].id;
        p[k + i].x = n + n - p[i].x;
        p[k + i].y = p[i].y;
    }
    k+=kk;
    for (int i = 0; i < kk; ++i)
    {
        p[k + i].id = p[i].id;
        p[k + i].x = n + n - p[i].x;
        p[k + i].y = m + m - p[i].y;
    }
    k +=kk;
    for (int i = 0; i < k; ++i)
    {
        int x = -1, y = -1;
        f(2*n, -2*m, p[i].y - p[i].x, x, y, n, m, p[i].x, p[i].y);
        if (x != -1 && y != -1)
        {
            int r = 2 * n * x + p[i].x;
            if (r <= t)
                if (ans[p[i].id] == -1 || ans[p[i].id] > r)
                    ans[p[i].id] = r;

        }
    }
    for (int i = 0; i < k / 4; ++i, cout << '\n')
        cout << ans[i] ;

}


#include <cstdio>
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <iterator>
#include <ctime>
#include <random>
using namespace std;
void solve(), solve1();
int main(int argc, const char * argv[]) {
    srand((unsigned int)time(0));
    solve();
    return 0;
}
#define int long long
#define mp make_pair
#define sqr(x) ((x)*(x))
#define double long double
int mod = 1e9;
int inf = 2 * 1e18;

#define N 400000
int n, m, k, ans[N];

struct point
{
    int x, y, id;
} p[N];

int gcd(int a, int b)
{
    while (b)
        swap(b, a %= b);
    return a;
}

int gcd(int a, int b, int & x, int & y)
{
    if (a == 0)
    {
        x = 0; y = 1;
        return b;
    }
    int xx, yy;
    int d = gcd (b%a, a, xx, yy);
    x = yy - (b / a) * xx;
    y = xx;
    return d;
}

bool f2(int a, int b, int c, int & x0, int & y0, int & g)
{
    g = gcd (abs(a), abs(b), x0, y0);
    if (c % g != 0)
        return false;
    x0 *= c / g;
    y0 *= c / g;
    if (a < 0)
    {

    x0 *= -1;

    }
    if (b < 0)
    {

        y0 *= -1;

    }
    return true;
}

void f(int a, int b, int c, int& x, int& y, int n, int m, int px, int py)
{
    if (c == 0)
    {
        int w = a, h = -b;
        int g = gcd(w, h);
        w /= g, h /= g;
        x = h - 1;
        y = w - 1;
    }
    int x0, y0, g0;
    if (!f2(a, b, c, x0, y0, g0))
    {
        return;
    }
    int g = gcd(abs(a), abs(b));
    int l = -1e11, r = 1e11;
    while (l < r)
    {
        int mid = (l + r)/2;
        if ((x0 - b * mid / g) >= 0 && (y0 + a * mid / g) >= 0)
            r = mid;
        else
            l = mid + 1;
    }
    if ((x0 - b * r / g) >= 0 && (y0 + a * r / g) >= 0)
    {
        x = (x0 - b * r / g), y = (y0 + a * r / g);
        return;
    }
}

void solve()
{
    //     ifstream cin("input.txt");
    //  ofstream cout("output.txt");
    ios::sync_with_stdio(0);
    cin >> n >> m >> k;
    for (int i = 0; i < k; ++i)
    {
        cin >> p[i].x >> p[i].y;
        p[i].id = i;
        ans[i] = -1;
    }
    int g = gcd(n, m);
    int h = m / g;
    int tx = h;
    int t = n * tx;

    int kk = k;
    for (int i = 0; i < k; ++i)
    {
        p[k + i].id = p[i].id;
        p[k + i].x = p[i].x;
        p[k + i].y = m + m - p[i].y;
    }
    k+=k;

    for (int i = 0; i < kk; ++i)
    {
        p[k + i].id = p[i].id;
        p[k + i].x = n + n - p[i].x;
        p[k + i].y = p[i].y;
    }
    k+=kk;
    for (int i = 0; i < kk; ++i)
    {
        p[k + i].id = p[i].id;
        p[k + i].x = n + n - p[i].x;
        p[k + i].y = m + m - p[i].y;
    }
    k +=kk;
    for (int i = 0; i < k; ++i)
    {
        int x = -1, y = -1;
        f(2*n, -2*m, p[i].y - p[i].x, x, y, n, m, p[i].x, p[i].y);
        if (x != -1 && y != -1)
        {
            int r = 2 * n * x + p[i].x;
            if (r <= t)
                if (ans[p[i].id] == -1 || ans[p[i].id] > r)
                    ans[p[i].id] = r;

        }
    }
    for (int i = 0; i < k / 4; ++i, cout << '\n')
        cout << ans[i] ;

}


#include <cstdio>
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <iterator>
#include <ctime>
#include <random>
using namespace std;
void solve(), solve1();
int main(int argc, const char * argv[]) {
    srand((unsigned int)time(0));
    solve();
    return 0;
}
#define int long long
#define mp make_pair
#define sqr(x) ((x)*(x))
#define double long double
int mod = 1e9;
int inf = 2 * 1e18;

#define N 400000
int n, m, k, ans[N];

struct point
{
    int x, y, id;
} p[N];

int gcd(int a, int b)
{
    while (b)
        swap(b, a %= b);
    return a;
}

int gcd(int a, int b, int & x, int & y)
{
    if (a == 0)
    {
        x = 0; y = 1;
        return b;
    }
    int xx, yy;
    int d = gcd (b%a, a, xx, yy);
    x = yy - (b / a) * xx;
    y = xx;
    return d;
}

bool f2(int a, int b, int c, int & x0, int & y0, int & g)
{
    g = gcd (abs(a), abs(b), x0, y0);
    if (c % g != 0)
        return false;
    x0 *= c / g;
    y0 *= c / g;
    if (a < 0)
    {

    x0 *= -1;

    }
    if (b < 0)
    {

        y0 *= -1;

    }
    return true;
}

void f(int a, int b, int c, int& x, int& y, int n, int m, int px, int py)
{
    if (c == 0)
    {
        int w = a, h = -b;
        int g = gcd(w, h);
        w /= g, h /= g;
        x = h - 1;
        y = w - 1;
    }
    int x0, y0, g0;
    if (!f2(a, b, c, x0, y0, g0))
    {
        return;
    }
    int g = gcd(abs(a), abs(b));
    int l = -1e12, r = 1e12;
    while (l < r)
    {
        int mid = (l + r)/2;
        if ((x0 - b * mid / g) >= 0 && (y0 + a * mid / g) >= 0)
            r = mid;
        else
            l = mid + 1;
    }
    if ((x0 - b * r / g) >= 0 && (y0 + a * r / g) >= 0)
    {
        x = (x0 - b * r / g), y = (y0 + a * r / g);
        return;
    }
}

void solve()
{
    //     ifstream cin("input.txt");
    //  ofstream cout("output.txt");
    ios::sync_with_stdio(0);
    cin >> n >> m >> k;
    for (int i = 0; i < k; ++i)
    {
        cin >> p[i].x >> p[i].y;
        p[i].id = i;
        ans[i] = -1;
    }
    int g = gcd(n, m);
    int h = m / g;
    int tx = h;
    int t = n * tx;

    int kk = k;
    for (int i = 0; i < k; ++i)
    {
        p[k + i].id = p[i].id;
        p[k + i].x = p[i].x;
        p[k + i].y = m + m - p[i].y;
    }
    k+=k;

    for (int i = 0; i < kk; ++i)
    {
        p[k + i].id = p[i].id;
        p[k + i].x = n + n - p[i].x;
        p[k + i].y = p[i].y;
    }
    k+=kk;
    for (int i = 0; i < kk; ++i)
    {
        p[k + i].id = p[i].id;
        p[k + i].x = n + n - p[i].x;
        p[k + i].y = m + m - p[i].y;
    }
    k +=kk;
    for (int i = 0; i < k; ++i)
    {
        int x = -1, y = -1;
        f(2*n, -2*m, p[i].y - p[i].x, x, y, n, m, p[i].x, p[i].y);
        if (x != -1 && y != -1)
        {
            int r = 2 * n * x + p[i].x;
            if (r <= t)
                if (ans[p[i].id] == -1 || ans[p[i].id] > r)
                    ans[p[i].id] = r;

        }
    }
    for (int i = 0; i < k / 4; ++i, cout << '\n')
        cout << ans[i] ;

}


#include <cstdio>
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <iterator>
#include <ctime>
#include <random>
using namespace std;
void solve(), solve1();
int main(int argc, const char * argv[]) {
    srand((unsigned int)time(0));
    solve();
    return 0;
}
#define int long long
#define mp make_pair
#define sqr(x) ((x)*(x))
#define double long double
int mod = 1e9;
int inf = 2 * 1e18;

#define N 400000
int n, m, k, ans[N];

struct point
{
    int x, y, id;
} p[N];

int gcd(int a, int b)
{
    while (b)
        swap(b, a %= b);
    return a;
}

int gcd(int a, int b, int & x, int & y)
{
    if (a == 0)
    {
        x = 0; y = 1;
        return b;
    }
    int xx, yy;
    int d = gcd (b%a, a, xx, yy);
    x = yy - (b / a) * xx;
    y = xx;
    return d;
}

bool f2(int a, int b, int c, int & x0, int & y0, int & g)
{
    g = gcd (abs(a), abs(b), x0, y0);
    if (c % g != 0)
        return false;
    x0 *= c / g;
    y0 *= c / g;
    if (a < 0)
    {

    x0 *= -1;

    }
    if (b < 0)
    {

        y0 *= -1;

    }
    return true;
}

void f(int a, int b, int c, int& x, int& y, int n, int m, int px, int py)
{
    if (c == 0)
    {
        int w = a, h = -b;
        int g = gcd(w, h);
        w /= g, h /= g;
        x = h - 1;
        y = w - 1;
    }
    int x0, y0, g0;
    if (!f2(a, b, c, x0, y0, g0))
    {
        return;
    }
    int g = gcd(abs(a), abs(b));
    int l = -1e11, r = 1e11;
    while (l < r)
    {
        int mid = (l + r)>>1;
        if ((x0 - b * mid / g) >= 0 && (y0 + a * mid / g) >= 0)
            r = mid;
        else
            l = mid + 1;
    }
    if ((x0 - b * r / g) >= 0 && (y0 + a * r / g) >= 0)
    {
        x = (x0 - b * r / g), y = (y0 + a * r / g);
        return;
    }
}

void solve()
{
    //     ifstream cin("input.txt");
    //  ofstream cout("output.txt");
    ios::sync_with_stdio(0);
    cin >> n >> m >> k;
    for (int i = 0; i < k; ++i)
    {
        cin >> p[i].x >> p[i].y;
        p[i].id = i;
        ans[i] = -1;
    }
    int g = gcd(n, m);
    int h = m / g;
    int tx = h;
    int t = n * tx;

    int kk = k;
    for (int i = 0; i < k; ++i)
    {
        p[k + i].id = p[i].id;
        p[k + i].x = p[i].x;
        p[k + i].y = m + m - p[i].y;
    }
    k+=k;

    for (int i = 0; i < kk; ++i)
    {
        p[k + i].id = p[i].id;
        p[k + i].x = n + n - p[i].x;
        p[k + i].y = p[i].y;
    }
    k+=kk;
    for (int i = 0; i < kk; ++i)
    {
        p[k + i].id = p[i].id;
        p[k + i].x = n + n - p[i].x;
        p[k + i].y = m + m - p[i].y;
    }
    k +=kk;
    for (int i = 0; i < k; ++i)
    {
        int x = -1, y = -1;
        f(2*n, -2*m, p[i].y - p[i].x, x, y, n, m, p[i].x, p[i].y);
        if (x != -1 && y != -1)
        {
            int r = 2 * n * x + p[i].x;
            if (r <= t)
                if (ans[p[i].id] == -1 || ans[p[i].id] > r)
                    ans[p[i].id] = r;

        }
    }
    for (int i = 0; i < k / 4; ++i, cout << '\n')
        cout << ans[i] ;

}


#include <iostream>
#include <cstdio>
#include <cmath>
#define INF 99999999999999999
using namespace std;
typedef long long ll;

ll extgcd(ll a,ll b,ll &x,ll &y)
{
	if(b==0){x=1;y=0;return a;}
	int g=extgcd(b,a%b,y,x);
	y-=(a/b)*x;
	return g;
}

ll m,n,num,g,p,q,p1,q1;

ll work(ll x,ll y)
{
	if((y-x)%g!=0) return INF;
	if(y==x) return x;

	int nowp=(y-x)/g*p;
	int nowq=(y-x)/g*q;

	while(nowp>0) nowp-=p1;
	while(nowp<0) nowp+=p1;

	return 2*n*nowp+x;
}

int main()
{
	int i,x,y;
	ll ans;
	//freopen("in.txt","r",stdin);
	scanf("%d %d %d\n",&n,&m,&num);
	g=extgcd(2*n,-2*m,p,q);
	p1=abs(2*m/g);q1=abs(2*n/g);

	for(i=0;i<num;i++)
	{
		ans=INF;
		scanf("%d %d\n",&x,&y);
		ans=min(ans,work(x,y));
		ans=min(ans,work(2*n-x,2*m-y));
		ans=min(ans,work(x,2*m-y));
		ans=min(ans,work(2*n-x,y));

		if(ans!=INF) cout<<ans<<endl;else cout<<"-1\n";
	}
}


#include <iostream>
#include <cstdio>
#include <cmath>
#define INF 999999
using namespace std;

int extgcd(int a,int b,int &x,int &y)
{
	if(b==0){x=1;y=0;return a;}
	int g=extgcd(b,a%b,y,x);
	y-=(a/b)*x;
	return g;
}

int m,n,num,g,p,q,p1,q1;

int work(int x,int y)
{
	if((y-x)%g!=0) return INF;
	if(y==x) return x;

	int nowp=(y-x)/g*p;
	int nowq=(y-x)/g*q;

	while(nowp>=0) nowp-=p1;
	nowp+=p1;

	return 2*m*nowp+x;
}

int main()
{
	int i,x,y,ans,tmp;
	//freopen("in.txt","r",stdin);
	scanf("%d %d %d\n",&n,&m,&num);
	g=extgcd(2*n,-2*m,p,q);
	p1=abs(2*m/g);q1=abs(2*n/g);

	for(i=0;i<num;i++)
	{
		ans=INF;
		scanf("%d %d\n",&x,&y);
		ans=min(ans,work(x,y));
		ans=min(ans,work(2*n-x,2*m-y));
		ans=min(ans,work(x,2*m-y));
		ans=min(ans,work(2*n-x,y));

		if(ans!=INF) printf("%d\n",ans);else printf("-1\n");
	}
}


#include <iostream>
#include <cstdio>
#include <cmath>
#define INF 999999999
using namespace std;

int extgcd(int a,int b,int &x,int &y)
{
	if(b==0){x=1;y=0;return a;}
	int g=extgcd(b,a%b,y,x);
	y-=(a/b)*x;
	return g;
}

int m,n,num,g,p,q,p1,q1;

int work(int x,int y)
{
	if((y-x)%g!=0) return INF;
	if(y==x) return x;

	int nowp=(y-x)/g*p;
	int nowq=(y-x)/g*q;

	while(nowp>0) nowp-=p1;
	while(nowp<0) nowp+=p1;

	return 2*n*nowp+x;
}

int main()
{
	int i,x,y,ans,tmp;
	//freopen("in.txt","r",stdin);
	scanf("%d %d %d\n",&n,&m,&num);
	g=extgcd(2*n,-2*m,p,q);
	p1=abs(2*m/g);q1=abs(2*n/g);

	for(i=0;i<num;i++)
	{
		ans=INF;
		scanf("%d %d\n",&x,&y);
		ans=min(ans,work(x,y));
		ans=min(ans,work(2*n-x,2*m-y));
		ans=min(ans,work(x,2*m-y));
		ans=min(ans,work(2*n-x,y));

		if(ans!=INF) printf("%d\n",ans);else printf("-1\n");
	}
}


#include <iostream>
#include <cstdio>
#include <cmath>
#define INF 99999999999999999
using namespace std;
typedef long long ll;

ll extgcd(ll a,ll b,ll &x,ll &y)
{
	if(b==0){x=1;y=0;return a;}
	int g=extgcd(b,a%b,y,x);
	y-=(a/b)*x;
	return g;
}

ll m,n,num,g,p,q,p1,q1;

ll work(ll x,ll y)
{
	if((y-x)%g!=0) return INF;
	if(y==x) return x;

	int nowp=(y-x)/g*p;
	int nowq=(y-x)/g*q;

	while(nowp>0) nowp-=p1;
	while(nowp<0) nowp+=p1;

	return 2*n*nowp+x;
}

int main()
{
	ll i,x,y,ans,tmp;
	//freopen("in.txt","r",stdin);
	scanf("%d %d %d\n",&n,&m,&num);
	g=extgcd(2*n,-2*m,p,q);
	p1=abs(2*m/g);q1=abs(2*n/g);

	for(i=0;i<num;i++)
	{
		ans=INF;
		scanf("%d %d\n",&x,&y);
		ans=min(ans,work(x,y));
		ans=min(ans,work(2*n-x,2*m-y));
		ans=min(ans,work(x,2*m-y));
		ans=min(ans,work(2*n-x,y));

		if(ans!=INF) printf("%d\n",ans);else printf("-1\n");
	}
}


#include <iostream>
#include <cstdio>
#include <cmath>
#define INF 999999
using namespace std;

int extgcd(int a,int b,int &x,int &y)
{
	if(b==0){x=1;y=0;return a;}
	int g=extgcd(b,a%b,y,x);
	y-=(a/b)*x;
	return g;
}

int m,n,num,g,p,q,p1,q1;

int work(int x,int y)
{
	if((y-x)%g!=0) return INF;
	if(y==x) return x;

	int nowp=(y-x)/g*p;
	int nowq=(y-x)/g*q;

	while(2*m*nowp+x>=0) nowp-=p1;
	nowp+=p1;

	return 2*m*nowp+x;
}

int main()
{
	int i,x,y,ans,tmp;
	scanf("%d %d %d\n",&n,&m,&num);
	g=extgcd(2*n,-2*m,p,q);
	p1=abs(2*m/g);q1=abs(2*n/g);

	for(i=0;i<num;i++)
	{
		ans=INF;
		scanf("%d %d\n",&x,&y);
		ans=min(ans,work(x,y));
		ans=min(ans,work(2*n-x,2*m-y));
		ans=min(ans,work(x,2*m-y));
		ans=min(ans,work(2*n-x,y));

		if(ans!=INF) printf("%d\n",ans);else printf("-1\n");
	}
}


#include <iostream>
#include <cstdio>
using namespace std;

int extgcd(int a,int b,int &x,int &y)
{
	if(b==0)
	{
		x=1;y=0;return a;
	}

	int g=extgcd(b,a%b,y,x);
	y-=(a/b)*x;
	return g;
}

int m,n,num;

int work(int x,int y)
{
	int p,q,g;
	g=extgcd(2*m,2*n,p,q);
	if(y-x%g!=0) return -1;

	p=p*((y-x)/g);
	return 2*m*p+x;
}

int main()
{
	//freopen("in.txt","r",stdin);
	scanf("%d %d %d\n",&n,&m,&num);
	int i,x,y,ans,tmp;
	for(i=0;i<num;i++)
	{
		ans=99999999;
		scanf("%d %d\n",&x,&y);
		tmp=work(x,y);
		if(tmp!=-1) ans=min(ans,tmp);
		tmp=work(2*m-x,2*n-y);
		if(tmp!=-1) ans=min(ans,tmp);
		tmp=work(x,2*n-y);
		if(tmp!=-1) ans=min(ans,tmp);
		tmp=work(2*m-x,y);
		if(tmp!=-1) ans=min(ans,tmp);

		if(ans!=99999999) printf("%d\n",ans);else printf("-1\n");
	}
}


#include <iostream>
#include <cstdio>
#include <cmath>
#define INF 999999
using namespace std;

int extgcd(int a,int b,int &x,int &y)
{
	if(b==0){x=1;y=0;return a;}
	int g=extgcd(b,a%b,y,x);
	y-=(a/b)*x;
	return g;
}

int m,n,num,g,p,q,p1,q1;

int work(int x,int y)
{
	if((y-x)%g!=0) return INF;
	if(y==x) return x;

	int nowp=(y-x)/g*p;
	int nowq=(y-x)/g*q;

	while(nowp>0) nowp-=p1;
	while(nowp<0) nowp+=p1;

	return 2*n*nowp+x;
}

int main()
{
	int i,x,y,ans,tmp;
	//freopen("in.txt","r",stdin);
	scanf("%d %d %d\n",&n,&m,&num);
	g=extgcd(2*n,-2*m,p,q);
	p1=abs(2*m/g);q1=abs(2*n/g);

	for(i=0;i<num;i++)
	{
		ans=INF;
		scanf("%d %d\n",&x,&y);
		ans=min(ans,work(x,y));
		ans=min(ans,work(2*n-x,2*m-y));
		ans=min(ans,work(x,2*m-y));
		ans=min(ans,work(2*n-x,y));

		if(ans!=INF) printf("%d\n",ans);else printf("-1\n");
	}
}


#include <bits/stdc++.h>
using namespace std;
//#define DEBUG
#define ll long long
#define INF 0x3f3f3f3f
#define INF16 16000000000000000LL
#define INF18 1000000000000000000LL
#define pii pair<int,int>
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define mod9 (int)(1e9 + 9)
#define mod7 (int)(1e9 + 7)
#define eps 1e-9
#define mse(x , y) memset(x , y , sizeof(x))
#define ALL(x) x.begin(),x.end()
#define RALL(x) x.rbegin(),x.rend()
#define IOS do { ios::sync_with_stdio(0);cin.tie(0); } while (0)

inline ll rit(){
	int key = 1;
	char c = getchar();
	while(!isdigit(c)){
		if(c == '-') key = -1;
		c = getchar();
	}

	ll x = 0;
	while(isdigit(c)){
		x = x * 10 + c - '0';
		c = getchar();
	}
	return x * key;
}

/*********default*********/
#define MAXN 100010

int n , m , k;
bool test(int x , int y){
	if(x == 0 && y == 0) return false;
	if(x == 0 && y == m) return false;
	if(x == n && y == 0) return false;
	if(x == n && y == m) return false;
	return true;
}

int r1[2 * MAXN + 100] , r2[2 * MAXN + 100];

struct track{
	int x , y , newx , newy , s;
}tr[MAXN * 2];

int main(){
	mse(r1 , INF);
	mse(r2 , INF);
	int x = 0 , y = 0 , op = 1 , cur = 1 , first = 1;
	n = rit() , m = rit() , k = rit();
	tr[0].s = 0;
	while(test(x , y) || first){
		first = 0;
		tr[cur].x = x , tr[cur].y = y;
		if(op == 1){
			int move = min(n - x , m - y);
			x = x + move;
			y = y + move;
			tr[cur].s = tr[cur-1].s + move;
			tr[cur].newx = x , tr[cur].newy = y;
			int tmp = (x - y + MAXN) % MAXN;
			r1[tmp] = min(r1[tmp] , cur++);
			if(tr[cur-1].x + move == n) op = 2;
			else op = 4;
		}
		else if(op == 2){
			int move = min(m - y , x - 0);
			x = x - move;
			y = y + move;
			tr[cur].s = tr[cur-1].s + move;
			tr[cur].newx = x , tr[cur].newy = y;
			int tmp = x + y;
			r2[tmp] = min(r2[tmp] , cur++);
			if(tr[cur-1].x - move == 0) op = 1;
			else op = 3;
		}
		else if(op == 3){
			int move = min(y - 0, x - 0);
			x = x - move;
			y = y - move;
			tr[cur].s = tr[cur-1].s + move;
			tr[cur].newx = x , tr[cur].newy = y;
			int tmp = (x - y + MAXN) % MAXN;
			r1[tmp] = min(r1[tmp] , cur++);
			if(tr[cur-1].x - move == 0) op = 4;
			else op = 2;
		}
		else if(op == 4){
			int move = min(y - 0 , n - x);
			x = x + move;
			y = y - move;
			tr[cur].s = tr[cur-1].s + move;
			tr[cur].newx = x , tr[cur].newy = y;
			int tmp = (x + y);
			r2[tmp] = min(r2[tmp] , cur++);
			if(tr[cur-1].x + move == n) op = 3;
			else op = 1;
		}
	}

	for(int i = 0; i < k; i++){
		x = rit() , y = rit();
		int tmp1 = (x - y + MAXN) % MAXN;
		int tmp2 = x + y;
		int idx = min(r1[tmp1] , r2[tmp2]);
		if(idx == INF) printf("-1\n");
		else{
			int ans = tr[idx - 1].s;
			ans += abs(x - tr[idx].x);
			printf("%d\n" , ans);
		}
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
//#define DEBUG
#define ll long long
#define INF 0x3f3f3f3f
#define INF16 16000000000000000LL
#define INF18 1000000000000000000LL
#define pii pair<int,int>
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define mod9 (int)(1e9 + 9)
#define mod7 (int)(1e9 + 7)
#define eps 1e-9
#define mse(x , y) memset(x , y , sizeof(x))
#define ALL(x) x.begin(),x.end()
#define RALL(x) x.rbegin(),x.rend()
#define IOS do { ios::sync_with_stdio(0);cin.tie(0); } while (0)

inline ll rit(){
	int key = 1;
	char c = getchar();
	while(!isdigit(c)){
		if(c == '-') key = -1;
		c = getchar();
	}

	ll x = 0;
	while(isdigit(c)){
		x = x * 10 + c - '0';
		c = getchar();
	}
	return x * key;
}

/*********default*********/
#define MAXN 100010

int n , m , k;
bool test(int x , int y){
	if(x == 0 && y == 0) return false;
	if(x == 0 && y == m) return false;
	if(x == n && y == 0) return false;
	if(x == n && y == m) return false;
	return true;
}

int r1[2 * MAXN + 100] , r2[2 * MAXN + 100];

struct track{
	int x , y , newx , newy , s;
}tr[MAXN * 3];

int main(){
	mse(r1 , INF);
	mse(r2 , INF);
	int x = 0 , y = 0 , op = 1 , cur = 1 , first = 1;
	n = rit() , m = rit() , k = rit();
	tr[0].s = 0;
	while(test(x , y) || first){
		first = 0;
		tr[cur].x = x , tr[cur].y = y;
		if(op == 1){
			int move = min(n - x , m - y);
			x = x + move;
			y = y + move;
			tr[cur].s = tr[cur-1].s + move;
			tr[cur].newx = x , tr[cur].newy = y;
			int tmp = (x - y + MAXN) % MAXN;
			r1[tmp] = min(r1[tmp] , cur++);
			if(tr[cur-1].x + move == n) op = 2;
			else op = 4;
		}
		else if(op == 2){
			int move = min(m - y , x - 0);
			x = x - move;
			y = y + move;
			tr[cur].s = tr[cur-1].s + move;
			tr[cur].newx = x , tr[cur].newy = y;
			int tmp = x + y;
			r2[tmp] = min(r2[tmp] , cur++);
			if(tr[cur-1].x - move == 0) op = 1;
			else op = 3;
		}
		else if(op == 3){
			int move = min(y - 0, x - 0);
			x = x - move;
			y = y - move;
			tr[cur].s = tr[cur-1].s + move;
			tr[cur].newx = x , tr[cur].newy = y;
			int tmp = (x - y + MAXN) % MAXN;
			r1[tmp] = min(r1[tmp] , cur++);
			if(tr[cur-1].x - move == 0) op = 4;
			else op = 2;
		}
		else if(op == 4){
			int move = min(y - 0 , n - x);
			x = x + move;
			y = y - move;
			tr[cur].s = tr[cur-1].s + move;
			tr[cur].newx = x , tr[cur].newy = y;
			int tmp = (x + y);
			r2[tmp] = min(r2[tmp] , cur++);
			if(tr[cur-1].x + move == n) op = 3;
			else op = 1;
		}
	}

	for(int i = 0; i < k; i++){
		x = rit() , y = rit();
		int tmp1 = (x - y + MAXN) % MAXN;
		int tmp2 = x + y;
		int idx = min(r1[tmp1] , r2[tmp2]);
		if(idx == INF) printf("-1\n");
		else{
			int ans = tr[idx - 1].s;
			ans += abs(x - tr[idx].x);
			printf("%d\n" , ans);
		}
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
//#define DEBUG
#define ll long long
#define INF 0x3f3f3f3f
#define INF16 16000000000000000LL
#define INF18 1000000000000000000LL
#define pii pair<int,int>
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define mod9 (int)(1e9 + 9)
#define mod7 (int)(1e9 + 7)
#define eps 1e-9
#define mse(x , y) memset(x , y , sizeof(x))
#define ALL(x) x.begin(),x.end()
#define RALL(x) x.rbegin(),x.rend()
#define IOS do { ios::sync_with_stdio(0);cin.tie(0); } while (0)

inline ll rit(){
	int key = 1;
	char c = getchar();
	while(!isdigit(c)){
		if(c == '-') key = -1;
		c = getchar();
	}

	ll x = 0;
	while(isdigit(c)){
		x = x * 10 + c - '0';
		c = getchar();
	}
	return x * key;
}

/*********default*********/
#define MAXN 100010

pii pr[MAXN];
int n , m , k;
bool test(int x , int y){
	if(x == 0 && y == 0) return false;
	if(x == 0 && y == m) return false;
	if(x == n && y == 0) return false;
	if(x == n && y == m) return false;
}

int r1[2 * MAXN + 100] , r2[2 * MAXN + 100];

struct track{
	int x , y , newx , newy , s;
}tr[MAXN];

int main(){
	mse(r1 , INF);
	mse(r2 , INF);
	int x = 0 , y = 0 , op = 1 , cur = 1 , first = 1;
	n = rit() , m = rit() , k = rit();
	tr[0].s = 0;
	while(test(x , y) || first){
		first = 0;
		tr[cur].x = x , tr[cur].y = y;
		if(op == 1){
			int move = min(n - x , m - y);
			x = x + move;
			y = y + move;
			tr[cur].s = tr[cur-1].s + move;
			tr[cur].newx = x , tr[cur].newy = y;
			int tmp = (x - y + MAXN) % MAXN;
			r1[tmp] = min(r1[tmp] , cur++);
			if(tr[cur-1].x + move == n) op = 2;
			else op = 4;
		}
		else if(op == 2){
			int move = min(m - y , x - 0);
			x = x - move;
			y = y + move;
			tr[cur].s = tr[cur-1].s + move;
			tr[cur].newx = x , tr[cur].newy = y;
			int tmp = x + y;
			r2[tmp] = min(r2[tmp] , cur++);
			if(tr[cur-1].x - move == 0) op = 1;
			else op = 3;
		}
		else if(op == 3){
			int move = min(y - 0, x - 0);
			x = x - move;
			y = y - move;
			tr[cur].s = tr[cur-1].s + move;
			tr[cur].newx = x , tr[cur].newy = y;
			int tmp = (x - y + MAXN) % MAXN;
			r1[tmp] = min(r1[tmp] , cur++);
			if(tr[cur-1].x - move == 0) op = 4;
			else op = 2;
		}
		else if(op == 4){
			int move = min(y - 0 , n - x);
			x = x + move;
			y = y - move;
			tr[cur].s = tr[cur-1].s + move;
			tr[cur].newx = x , tr[cur].newy = y;
			int tmp = (x + y);
			r2[tmp] = min(r2[tmp] , cur++);
			if(tr[cur-1].x + move == n) op = 3;
			else op = 1;
		}
	}

	for(int i = 0; i < k; i++){
		x = rit() , y = rit();
		int tmp1 = (x - y + MAXN) % MAXN;
		int tmp2 = x + y;
		int idx = min(r1[tmp1] , r2[tmp2]);
		if(idx == INF) printf("-1\n");
		else{
			int ans = tr[idx - 1].s;
			ans += abs(x - tr[idx].x);
			printf("%d\n" , ans);
		}
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
//#define DEBUG
#define ll long long
#define INF 0x3f3f3f3f
#define INF16 16000000000000000LL
#define INF18 1000000000000000000LL
#define pii pair<int,int>
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define mod9 (int)(1e9 + 9)
#define mod7 (int)(1e9 + 7)
#define eps 1e-9
#define mse(x , y) memset(x , y , sizeof(x))
#define ALL(x) x.begin(),x.end()
#define RALL(x) x.rbegin(),x.rend()
#define IOS do { ios::sync_with_stdio(0);cin.tie(0); } while (0)

inline ll rit(){
	int key = 1;
	char c = getchar();
	while(!isdigit(c)){
		if(c == '-') key = -1;
		c = getchar();
	}

	ll x = 0;
	while(isdigit(c)){
		x = x * 10 + c - '0';
		c = getchar();
	}
	return x * key;
}

/*********default*********/
#define MAXN 100010

int n , m , k;
bool test(int x , int y){
	if(x == 0 && y == 0) return false;
	if(x == 0 && y == m) return false;
	if(x == n && y == 0) return false;
	if(x == n && y == m) return false;
	return true;
}

int r1[5 * MAXN + 100] , r2[5 * MAXN + 100];

struct track{
	int x , y , newx , newy;
	ll s;
}tr[MAXN * 5];

int main(){
	mse(r1 , INF);
	mse(r2 , INF);
	int x = 0 , y = 0 , op = 1 , cur = 1 , first = 1;
	n = rit() , m = rit() , k = rit();
	tr[0].s = 0;
	while(test(x , y) || first){
		first = 0;
		tr[cur].x = x , tr[cur].y = y;
		if(op == 1){
			int move = min(n - x , m - y);
			x = x + move;
			y = y + move;
			tr[cur].s = tr[cur-1].s + move;
			tr[cur].newx = x , tr[cur].newy = y;
			int tmp = (x - y + (m+n)) % (m+n);
			r1[tmp] = min(r1[tmp] , cur++);
			if(tr[cur-1].x + move == n) op = 2;
			else op = 4;
		}
		else if(op == 2){
			int move = min(m - y , x - 0);
			x = x - move;
			y = y + move;
			tr[cur].s = tr[cur-1].s + move;
			tr[cur].newx = x , tr[cur].newy = y;
			int tmp = x + y;
			r2[tmp] = min(r2[tmp] , cur++);
			if(tr[cur-1].x - move == 0) op = 1;
			else op = 3;
		}
		else if(op == 3){
			int move = min(y - 0, x - 0);
			x = x - move;
			y = y - move;
			tr[cur].s = tr[cur-1].s + move;
			tr[cur].newx = x , tr[cur].newy = y;
			int tmp = (x - y + (m+n)) % (m+n);
			r1[tmp] = min(r1[tmp] , cur++);
			if(tr[cur-1].x - move == 0) op = 4;
			else op = 2;
		}
		else if(op == 4){
			int move = min(y - 0 , n - x);
			x = x + move;
			y = y - move;
			tr[cur].s = tr[cur-1].s + move;
			tr[cur].newx = x , tr[cur].newy = y;
			int tmp = (x + y);
			r2[tmp] = min(r2[tmp] , cur++);
			if(tr[cur-1].x + move == n) op = 3;
			else op = 1;
		}
	}

	for(int i = 0; i < k; i++){
		x = rit() , y = rit();
		int tmp1 = (x - y + (m+n)) % (m+n);
		int tmp2 = x + y;
		int idx = min(r1[tmp1] , r2[tmp2]);
		if(idx == INF) printf("-1\n");
		else{
			ll ans = tr[idx - 1].s;
			ans += abs(x - tr[idx].x);
			printf("%I64d\n" , ans);
		}
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
//#define DEBUG
#define ll long long
#define INF 0x3f3f3f3f
#define INF16 16000000000000000LL
#define INF18 1000000000000000000LL
#define pii pair<int,int>
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define mod9 (int)(1e9 + 9)
#define mod7 (int)(1e9 + 7)
#define eps 1e-9
#define mse(x , y) memset(x , y , sizeof(x))
#define ALL(x) x.begin(),x.end()
#define RALL(x) x.rbegin(),x.rend()
#define IOS do { ios::sync_with_stdio(0);cin.tie(0); } while (0)

inline ll rit(){
	int key = 1;
	char c = getchar();
	while(!isdigit(c)){
		if(c == '-') key = -1;
		c = getchar();
	}

	ll x = 0;
	while(isdigit(c)){
		x = x * 10 + c - '0';
		c = getchar();
	}
	return x * key;
}

/*********default*********/
#define MAXN 100010

int n , m , k;
bool test(int x , int y){
	if(x == 0 && y == 0) return false;
	if(x == 0 && y == m) return false;
	if(x == n && y == 0) return false;
	if(x == n && y == m) return false;
	return true;
}

int r1[3 * MAXN + 100] , r2[3 * MAXN + 100];

struct track{
	int x , y , newx , newy;
	ll s;
}tr[MAXN * 5];

int main(){
	mse(r1 , INF);
	mse(r2 , INF);
	int x = 0 , y = 0 , op = 1 , cur = 1 , first = 1;
	n = rit() , m = rit() , k = rit();
	tr[0].s = 0;
	while(test(x , y) || first){
		first = 0;
		tr[cur].x = x , tr[cur].y = y;
		if(op == 1){
			int move = min(n - x , m - y);
			x = x + move;
			y = y + move;
			tr[cur].s = tr[cur-1].s + move;
			tr[cur].newx = x , tr[cur].newy = y;
			int tmp = (x - y + MAXN) % MAXN;
			r1[tmp] = min(r1[tmp] , cur++);
			if(tr[cur-1].x + move == n) op = 2;
			else op = 4;
		}
		else if(op == 2){
			int move = min(m - y , x - 0);
			x = x - move;
			y = y + move;
			tr[cur].s = tr[cur-1].s + move;
			tr[cur].newx = x , tr[cur].newy = y;
			int tmp = x + y;
			r2[tmp] = min(r2[tmp] , cur++);
			if(tr[cur-1].x - move == 0) op = 1;
			else op = 3;
		}
		else if(op == 3){
			int move = min(y - 0, x - 0);
			x = x - move;
			y = y - move;
			tr[cur].s = tr[cur-1].s + move;
			tr[cur].newx = x , tr[cur].newy = y;
			int tmp = (x - y + MAXN) % MAXN;
			r1[tmp] = min(r1[tmp] , cur++);
			if(tr[cur-1].x - move == 0) op = 4;
			else op = 2;
		}
		else if(op == 4){
			int move = min(y - 0 , n - x);
			x = x + move;
			y = y - move;
			tr[cur].s = tr[cur-1].s + move;
			tr[cur].newx = x , tr[cur].newy = y;
			int tmp = (x + y);
			r2[tmp] = min(r2[tmp] , cur++);
			if(tr[cur-1].x + move == n) op = 3;
			else op = 1;
		}
	}

	for(int i = 0; i < k; i++){
		x = rit() , y = rit();
		int tmp1 = (x - y + MAXN) % MAXN;
		int tmp2 = x + y;
		int idx = min(r1[tmp1] , r2[tmp2]);
		if(idx == INF) printf("-1\n");
		else{
			ll ans = tr[idx - 1].s;
			ans += abs(x - tr[idx].x);
			printf("%I64d\n" , ans);
		}
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <map>
#include <set>

using namespace std;

long long n, m, K;

vector<pair<long long,long long>> sensor;

long long gcd(long long a, long long b)
{
	if(a>b) swap(a,b);

	long long t;
	while(a%b!=0)
	{
		t = b;
		b = a%b;
		a = t;
	}

	return b;
}

pair<long long,long long> extended(long long a, long long b)
{
    long long q,x,lastx,y,lasty,temp,temp1,temp2,temp3;

    bool isSwap = false;

    if (b>a)
    {
    	isSwap = true;
    	swap(a,b);
    }

    x=0;
    y=1;
    lastx=1;
    lasty=0;
    while (b!=0) {
        q= a/b;
        temp1= a%b;
        a=b;
        b=temp1;

        temp2 = x;
    	x=lastx-q*x;
   	 lastx = temp2;

   	 temp3 = y;
    y = lasty-q*y;
    lasty=temp3;
    }

    if(isSwap) swap(lastx, lasty);

    return make_pair(lastx, lasty);
}

int main()
{
	long long x, y;
	long long p1, p2;

	scanf("%lld %lld %lld", &n, &m, &K);
	for(long long i=1;i<=K;i++)
	{
		scanf("%lld %lld", &x, &y);
		sensor.push_back({x,y});
	}

	long long g = gcd(2*n,2*m);
	pair<long long,long long> temp = extended(2*n,2*m);

	long long s1 = temp.first;
	long long s2 = temp.second;

	for(auto pos : sensor)
	{
		bool flag = false;
		long long A, B, LCM, ans;

		ans = 9223372036854775807LL;
		LCM = (2*n)/g * (2*m);

		x = pos.first;
		y = pos.second;

		A = x; B = y;
		if((B-A)%g==0)
		{
			p1 = s1 * (B-A)/g;
			p2 = s2 * (B-A)/g;

			long long posA = A + p1*2*n;
			long long posB = B - p2*2*m;

			if(posA < 0)
			{
				long long t = (-posA)/LCM + 1;
				posA += t*LCM;
				posB += t*LCM;
			}
			posA %= LCM; posB%=LCM;

			if(posA>=0 && posB>=0)
			{
				flag = true;
				ans = min(ans, posA);
			}
		}

		A = 2*n-x; B = y;
		if((B-A)%g==0)
		{
			p1 = s1 * (B-A)/g;
			p2 = s2 * (B-A)/g;

			long long posA = A + p1*2*n;
			long long posB = B - p2*2*m;

			if(posA < 0)
			{
				long long t = (-posA)/LCM + 1;
				posA += t*LCM;
				posB += t*LCM;
			}
			posA %= LCM; posB%=LCM;

			if(posA>=0 && posB>=0)
			{
				flag = true;
				ans = min(ans, posA);
			}
		}

		A = x; B = 2*m-y;
		if((B-A)%g==0)
		{
			p1 = s1 * (B-A)/g;
			p2 = s2 * (B-A)/g;

			long long posA = A + p1*2*n;
			long long posB = B - p2*2*m;

			if(posA < 0)
			{
				long long t = (-posA)/LCM + 1;
				posA += t*LCM;
				posB += t*LCM;
			}
			posA %= LCM; posB%=LCM;

			if(posA>=0 && posB>=0)
			{
				flag = true;
				ans = min(ans, posA);
			}
		}

		A = 2*n-x; B = 2*m-y;
		if((B-A)%g==0)
		{
			p1 = s1 * (B-A)/g;
			p2 = s2 * (B-A)/g;

			long long posA = A + p1*2*n;
			long long posB = B - p2*2*m;

			if(posA < 0)
			{
				long long t = (-posA)/LCM + 1;
				posA += t*LCM;
				posB += t*LCM;
			}
			posA %= LCM; posB%=LCM;

			if(posA>=0 && posB>=0)
			{
				flag = true;
				ans = min(ans, posA);
			}
		}

		if(flag)
			printf("%lld\n", ans);
		else
			printf("-1\n");
	}

	return 0;
}


#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <map>
#include <set>

using namespace std;

long long n, m, K;

vector<pair<long long,long long>> sensor;

long long gcd(long long a, long long b)
{
	if(a>b) swap(a,b);

	long long t;
	while(a%b!=0)
	{
		t = b;
		b = a%b;
		a = t;
	}

	return b;
}

pair<long long,long long> extEuclid(long long a, long long b)
{
    long long q, t;
    long long x, y, preX, preY;

    x = 0; preX = 1;
    y = 1; preY = 0;

    while (b!=0)
    {
        q = a/b;
        t = a%b;
        a = b;
        b = t;

        t = x;
    	x = preX - q*x;
        preX = t;

        t = y;
        y = preY - q*y;
        preY = t;
    }

    return make_pair(preX, preY);
}

int main()
{
	long long x, y;
	long long p1, p2;

	scanf("%lld %lld %lld", &n, &m, &K);
	for(long long i=1;i<=K;i++)
	{
		scanf("%lld %lld", &x, &y);
		sensor.push_back({x,y});
	}

	long long g = gcd(2*n,2*m);
	pair<long long,long long> temp = extEuclid(2*n,2*m);

	long long s1 = temp.first;
	long long s2 = temp.second;

	for(auto pos : sensor)
	{
		bool flag = false;
		long long A, B, LCM, ans;

		ans = 9223372036854775807LL;
		LCM = (2*n)/g * (2*m);

		x = pos.first;
		y = pos.second;

		A = x; B = y;
		if((B-A)%g==0)
		{
			p1 = s1 * (B-A)/g;
			p2 = s2 * (B-A)/g;

			long long posA = A + p1*2*n;
			long long posB = B - p2*2*m;

			if(posA < 0)
			{
				long long t = (-posA)/LCM + 1;
				posA += t*LCM;
				posB += t*LCM;
			}
			posA %= LCM; posB%=LCM;

			if(posA>=0 && posB>=0)
			{
				flag = true;
				ans = min(ans, posA);
			}
		}

		A = 2*n-x; B = y;
		if((B-A)%g==0)
		{
			p1 = s1 * (B-A)/g;
			p2 = s2 * (B-A)/g;

			long long posA = A + p1*2*n;
			long long posB = B - p2*2*m;

			if(posA < 0)
			{
				long long t = (-posA)/LCM + 1;
				posA += t*LCM;
				posB += t*LCM;
			}
			posA %= LCM; posB%=LCM;

			if(posA>=0 && posB>=0)
			{
				flag = true;
				ans = min(ans, posA);
			}
		}

		A = x; B = 2*m-y;
		if((B-A)%g==0)
		{
			p1 = s1 * (B-A)/g;
			p2 = s2 * (B-A)/g;

			long long posA = A + p1*2*n;
			long long posB = B - p2*2*m;

			if(posA < 0)
			{
				long long t = (-posA)/LCM + 1;
				posA += t*LCM;
				posB += t*LCM;
			}
			posA %= LCM; posB%=LCM;

			if(posA>=0 && posB>=0)
			{
				flag = true;
				ans = min(ans, posA);
			}
		}

		A = 2*n-x; B = 2*m-y;
		if((B-A)%g==0)
		{
			p1 = s1 * (B-A)/g;
			p2 = s2 * (B-A)/g;

			long long posA = A + p1*2*n;
			long long posB = B - p2*2*m;

			if(posA < 0)
			{
				long long t = (-posA)/LCM + 1;
				posA += t*LCM;
				posB += t*LCM;
			}
			posA %= LCM; posB%=LCM;

			if(posA>=0 && posB>=0)
			{
				flag = true;
				ans = min(ans, posA);
			}
		}

		if(flag)
			printf("%lld\n", ans);
		else
			printf("-1\n");
	}

	return 0;
}


#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <map>
#include <set>

using namespace std;

long long n, m, K;

vector<pair<long long,long long>> sensor;

long long gcd(long long a, long long b)
{
	if(a>b) swap(a,b);

	long long t;
	while(a%b!=0)
	{
		t = b;
		b = a%b;
		a = t;
	}

	return b;
}

pair<long long,long long> extEuclid(long long a, long long b)
{
    long long q, t;
    long long x, y, preX, preY;

    x = 0; preX = 1;
    y = 1; preY = 0;

    while (b!=0)
    {
        q = a/b;
        t = a%b;
        a = b;
        b = t;

        t = x;
    	x = preX - q*x;
        preX = t;

        t = y;
        y = preY - q*y;
        preY = t;
    }

    return make_pair(preX, preY);
}

int main()
{
	long long x, y;
	long long p1, p2;

	scanf("%lld %lld %lld", &n, &m, &K);
	for(long long i=1;i<=K;i++)
	{
		scanf("%lld %lld", &x, &y);
		sensor.push_back({x,y});
	}

	long long g = gcd(2*n,2*m);
	pair<long long,long long> temp = extended(2*n,2*m);

	long long s1 = temp.first;
	long long s2 = temp.second;

	for(auto pos : sensor)
	{
		bool flag = false;
		long long A, B, LCM, ans;

		ans = 9223372036854775807LL;
		LCM = (2*n)/g * (2*m);

		x = pos.first;
		y = pos.second;

		A = x; B = y;
		if((B-A)%g==0)
		{
			p1 = s1 * (B-A)/g;
			p2 = s2 * (B-A)/g;

			long long posA = A + p1*2*n;
			long long posB = B - p2*2*m;

			if(posA < 0)
			{
				long long t = (-posA)/LCM + 1;
				posA += t*LCM;
				posB += t*LCM;
			}
			posA %= LCM; posB%=LCM;

			if(posA>=0 && posB>=0)
			{
				flag = true;
				ans = min(ans, posA);
			}
		}

		A = 2*n-x; B = y;
		if((B-A)%g==0)
		{
			p1 = s1 * (B-A)/g;
			p2 = s2 * (B-A)/g;

			long long posA = A + p1*2*n;
			long long posB = B - p2*2*m;

			if(posA < 0)
			{
				long long t = (-posA)/LCM + 1;
				posA += t*LCM;
				posB += t*LCM;
			}
			posA %= LCM; posB%=LCM;

			if(posA>=0 && posB>=0)
			{
				flag = true;
				ans = min(ans, posA);
			}
		}

		A = x; B = 2*m-y;
		if((B-A)%g==0)
		{
			p1 = s1 * (B-A)/g;
			p2 = s2 * (B-A)/g;

			long long posA = A + p1*2*n;
			long long posB = B - p2*2*m;

			if(posA < 0)
			{
				long long t = (-posA)/LCM + 1;
				posA += t*LCM;
				posB += t*LCM;
			}
			posA %= LCM; posB%=LCM;

			if(posA>=0 && posB>=0)
			{
				flag = true;
				ans = min(ans, posA);
			}
		}

		A = 2*n-x; B = 2*m-y;
		if((B-A)%g==0)
		{
			p1 = s1 * (B-A)/g;
			p2 = s2 * (B-A)/g;

			long long posA = A + p1*2*n;
			long long posB = B - p2*2*m;

			if(posA < 0)
			{
				long long t = (-posA)/LCM + 1;
				posA += t*LCM;
				posB += t*LCM;
			}
			posA %= LCM; posB%=LCM;

			if(posA>=0 && posB>=0)
			{
				flag = true;
				ans = min(ans, posA);
			}
		}

		if(flag)
			printf("%lld\n", ans);
		else
			printf("-1\n");
	}

	return 0;
}


#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <map>
#include <set>

using namespace std;

long long n, m, K;

vector<pair<long long,long long>> sensor;

long long gcd(long long a, long long b)
{
	if(a>b) swap(a,b);

	long long t;
	while(a%b!=0)
	{
		t = b;
		b = a%b;
		a = t;
	}

	return b;
}

pair<long long,long long> extended(long long a, long long b)
{
    long long q,x,lastx,y,lasty,temp,temp1,temp2,temp3;

    if (b>a) {//we switch them
        temp=a; a=b; b=temp;
    }

    x=0;
    y=1;
    lastx=1;
    lasty=0;
    while (b!=0) {
        q= a/b;
        temp1= a%b;
        a=b;
        b=temp1;

        temp2 = x;
    	x=lastx-q*x;
   	 lastx = temp2;

   	 temp3 = y;
    y = lasty-q*y;
    lasty=temp3;
    }

    return make_pair(lastx, lasty);
}

int main()
{
	long long x, y;
	long long p1, p2;

	scanf("%lld %lld %lld", &n, &m, &K);
	for(long long i=1;i<=K;i++)
	{
		scanf("%lld %lld", &x, &y);
		sensor.push_back({x,y});
	}

	long long g = gcd(2*n,2*m);
	pair<long long,long long> temp = extended(2*n,2*m);

	long long s1 = temp.first;
	long long s2 = temp.second;

	for(auto pos : sensor)
	{
		bool flag = false;
		long long A, B, LCM, ans;

		ans = 9223372036854775807LL;
		LCM = (2*n)/g * (2*m);

		x = pos.first;
		y = pos.second;

		A = x; B = y;
		if((B-A)%g==0)
		{
			p1 = s1 * (B-A)/g;
			p2 = s2 * (B-A)/g;

			long long posA = A + p1*2*n;
			long long posB = B - p2*2*m;

			if(posA < 0)
			{
				long long t = (-posA)/LCM + 1;
				posA += t*LCM;
				posB += t*LCM;
			}
			posA %= LCM; posB%=LCM;

			if(posA>=0 && posB>=0)
			{
				flag = true;
				ans = min(ans, posA);
			}
		}

		A = 2*n-x; B = y;
		if((B-A)%g==0)
		{
			p1 = s1 * (B-A)/g;
			p2 = s2 * (B-A)/g;

			long long posA = A + p1*2*n;
			long long posB = B - p2*2*m;

			if(posA < 0)
			{
				long long t = (-posA)/LCM + 1;
				posA += t*LCM;
				posB += t*LCM;
			}
			posA %= LCM; posB%=LCM;

			if(posA>=0 && posB>=0)
			{
				flag = true;
				ans = min(ans, posA);
			}
		}

		A = x; B = 2*m-y;
		if((B-A)%g==0)
		{
			p1 = s1 * (B-A)/g;
			p2 = s2 * (B-A)/g;

			long long posA = A + p1*2*n;
			long long posB = B - p2*2*m;

			if(posA < 0)
			{
				long long t = (-posA)/LCM + 1;
				posA += t*LCM;
				posB += t*LCM;
			}
			posA %= LCM; posB%=LCM;

			if(posA>=0 && posB>=0)
			{
				flag = true;
				ans = min(ans, posA);
			}
		}

		A = 2*n-x; B = 2*m-y;
		if((B-A)%g==0)
		{
			p1 = s1 * (B-A)/g;
			p2 = s2 * (B-A)/g;

			long long posA = A + p1*2*n;
			long long posB = B - p2*2*m;

			if(posA < 0)
			{
				long long t = (-posA)/LCM + 1;
				posA += t*LCM;
				posB += t*LCM;
			}
			posA %= LCM; posB%=LCM;

			if(posA>=0 && posB>=0)
			{
				flag = true;
				ans = min(ans, posA);
			}
		}

		if(flag)
			printf("%lld\n", ans);
		else
			printf("-1\n");
	}

	return 0;
}


#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <map>
#include <set>

using namespace std;

long long n, m, K;

vector<pair<long long,long long>> sensor;

long long gcd(long long a, long long b)
{
	if(a>b) swap(a,b);

	long long t;
	while(a%b!=0)
	{
		t = b;
		b = a%b;
		a = t;
	}

	return b;
}

pair<long long,long long> extended(long long a, long long b)
{
    long long q,x,lastx,y,lasty,temp,temp1,temp2,temp3;

    if (b>a) {//we switch them
        temp=a; a=b; b=temp;
    }

    x=0;
    y=1;
    lastx=1;
    lasty=0;
    while (b!=0) {
        q= a/b;
        temp1= a%b;
        a=b;
        b=temp1;

        temp2 = x;
    	x=lastx-q*x;
   	 lastx = temp2;

   	 temp3 = y;
    y = lasty-q*y;
    lasty=temp3;
    }

    return make_pair(lastx, lasty);
}

int main()
{
	long long x, y;
	long long p1, p2;

	scanf("%lld %lld %lld", &n, &m, &K);
	for(long long i=1;i<=K;i++)
	{
		scanf("%lld %lld", &x, &y);
		sensor.push_back({x,y});
	}

	long long g = gcd(2*n,2*m);
	pair<long long,long long> temp = extended(2*n,2*m);

	long long s1 = temp.first;
	long long s2 = temp.second;

	for(auto pos : sensor)
	{
		bool flag = false;
		long long A, B, LCM, ans;

		ans = 0x7fffffff;
		LCM = (2*n)/g * (2*m);

		x = pos.first;
		y = pos.second;

		A = x; B = y;
		if((B-A)%g==0)
		{
			p1 = s1 * (B-A)/g;
			p2 = s2 * (B-A)/g;

			long long posA = A + p1*2*n;
			long long posB = B - p2*2*m;

			if(posA < 0)
			{
				long long t = (-posA)/LCM + 1;
				posA += t*LCM;
				posB += t*LCM;
			}
			posA %= LCM; posB%=LCM;

			if(posA>=0 && posB>=0)
			{
				flag = true;
				ans = min(ans, posA);
			}
		}

		A = 2*n-x; B = y;
		if((B-A)%g==0)
		{
			p1 = s1 * (B-A)/g;
			p2 = s2 * (B-A)/g;

			long long posA = A + p1*2*n;
			long long posB = B - p2*2*m;

			if(posA < 0)
			{
				long long t = (-posA)/LCM + 1;
				posA += t*LCM;
				posB += t*LCM;
			}
			posA %= LCM; posB%=LCM;

			if(posA>=0 && posB>=0)
			{
				flag = true;
				ans = min(ans, posA);
			}
		}

		A = x; B = 2*m-y;
		if((B-A)%g==0)
		{
			p1 = s1 * (B-A)/g;
			p2 = s2 * (B-A)/g;

			long long posA = A + p1*2*n;
			long long posB = B - p2*2*m;

			if(posA < 0)
			{
				long long t = (-posA)/LCM + 1;
				posA += t*LCM;
				posB += t*LCM;
			}
			posA %= LCM; posB%=LCM;

			if(posA>=0 && posB>=0)
			{
				flag = true;
				ans = min(ans, posA);
			}
		}

		A = 2*n-x; B = 2*m-y;
		if((B-A)%g==0)
		{
			p1 = s1 * (B-A)/g;
			p2 = s2 * (B-A)/g;

			long long posA = A + p1*2*n;
			long long posB = B - p2*2*m;

			if(posA < 0)
			{
				long long t = (-posA)/LCM + 1;
				posA += t*LCM;
				posB += t*LCM;
			}
			posA %= LCM; posB%=LCM;

			if(posA>=0 && posB>=0)
			{
				flag = true;
				ans = min(ans, posA);
			}
		}

		if(flag)
			printf("%lld\n", ans);
		else
			printf("-1\n");
	}

	return 0;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;

int n,m,t;

struct Node
{
    int num;
    int x,y;

};

Node getPoint(Node st,int k)
{
    Node p;
    if(k*(-st.x)+st.y!=st.y&&k*(-st.x)+st.y>=0&&k*(-st.x)+st.y<=m)
    {
        p.x=0;
        p.y=k*(-st.x)+st.y;
        return p;
    }
    if(k*(n-st.x)+st.y!=st.y&&k*(n-st.x)+st.y>=0&&k*(n-st.x)+st.y<=m)
    {
        p.x=n;
        p.y=k*(n-st.x)+st.y;
        return p;
    }
    if((-st.y+k*st.x)/k!=st.x&&(-st.y+k*st.x)/k>=0&&(-st.y+k*st.x)/k<=n)
    {
        p.x=(-st.y+k*st.x)/k;
        p.y=0;
        return p;
    }
    if((m-st.y+k*st.x)/k!=st.x&&(m-st.y+k*st.x)/k>=0&&(m-st.y+k*st.x)/k<=n)
    {
        p.x=(m-st.y+k*st.x)/k;
        p.y=m;
        return p;
    }
}

bool isEnd(int x,int y)
{
    if(x==n&&y==0)
        return 1;
    if(x==n&&y==m)
        return 1;
    if(x==0&&y==m)
        return 1;
    return 0;
}

int getCost(int sx,int sy,int k,int tmpx,int tmpy)
{
    int b=sy-k*sx;
    if(tmpy!=k*tmpx+b)
        return -1;
    return abs(tmpx-sx);
}

long long ans[100005];
vector<Node> v1[200005];
vector<Node> v2[200005];
int main()
{
    scanf("%d%d%d",&n,&m,&t);
    /*Node a;
    a.x=3;
    a.y=3;
    Node b=getPoint(a,-1);
    cout<<b.x<<" "<<b.y<<endl;*/
    int all=0;
    for(int i=0; i<t; i++)
    {
        Node node;
        node.num=i;
        scanf("%d%d",&node.x,&node.y);
        int b1=node.y-node.x+100005;
        int b2=node.y+node.x;
        v1[b1].push_back(node);
        v2[b2].push_back(node);
        all++;
    }
    //for(int i=0;i<v2[6].size();i++)
      //  cout<<v2[6][i].num<<" "<<v2[6][i].x<<" "<<v2[6][i].y<<endl;
    Node st;
    st.x=0;
    st.y=0;
    int cnt=0;
    long long time=0;
    while(!isEnd(st.x,st.y))
    {
        int k;
        if(cnt%2)
            k=-1;
        else
            k=1;
        cnt++;
        Node en=getPoint(st,k);
        int b=st.y-k*st.x;
        if(k==1)
        {
            b+=100005;
            for(int i=0; i<v1[b].size(); i++)
            {
                if(ans[v1[b][i].num]==0)
                {
                    int cost=getCost(st.x,st.y,k,v1[b][i].x,v1[b][i].y);
                    ans[v1[b][i].num]=time+cost;
                }
            }
        }
        if(k==-1)
        {
            for(int i=0; i<v2[b].size(); i++)
            {
                if(ans[v2[b][i].num]==0)
                {
                    int cost=getCost(st.x,st.y,k,v2[b][i].x,v2[b][i].y);
                    ans[v2[b][i].num]=time+cost;
                }
            }
        }
        time+=abs(en.x-st.x);
        st=en;
    }
    //cout<<"cnt:"<<cnt<<endl;
    for(int i=0; i<all; i++)
        if(ans[i]==0)
            printf("-1\n");
        else
            printf("%I64d\n",ans[i]);
    return 0;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;

int n,m,t;

struct Node
{
    int x,y;
};

vector<Node> p;

Node getPoint(Node st,int k)
{
    Node p;
    if(k*(-st.x)+st.y!=st.y&&k*(-st.x)+st.y>=0&&k*(-st.x)+st.y<=m)
    {
        p.x=0;
        p.y=k*(-st.x)+st.y;
        return p;
    }
    if(k*(n-st.x)+st.y!=st.y&&k*(n-st.x)+st.y>=0&&k*(n-st.x)+st.y<=m)
    {
        p.x=n;
        p.y=k*(n-st.x)+st.y;
        return p;
    }
    if((-st.y+k*st.x)/k!=st.x&&(-st.y+k*st.x)/k>=0&&(-st.y+k*st.x)/k<=n)
    {
        p.x=(-st.y+k*st.x)/k;
        p.y=0;
        return p;
    }
    if((m-st.y+k*st.x)/k!=st.x&&(m-st.y+k*st.x)/k>=0&&(m-st.y+k*st.x)/k<=n)
    {
        p.x=(m-st.y+k*st.x)/k;
        p.y=m;
        return p;
    }
}

bool isEnd(int x,int y)
{
    if(x==n&&y==0)
        return 1;
    if(x==n&&y==m)
        return 1;
    if(x==0&&y==m)
        return 1;
    return 0;
}

int getCost(int sx,int sy,int k,int tmpx,int tmpy)
{
    int b=sy-k*sx;
    if(tmpy!=k*tmpx+b)
        return -1;
    return abs(tmpx-sx);
}

int ans[100005];
int main()
{
    scanf("%d%d%d",&n,&m,&t);
    /*Node a;
    a.x=3;
    a.y=3;
    Node b=getPoint(a,-1);
    cout<<b.x<<" "<<b.y<<endl;*/
    for(int i=0; i<t; i++)
    {
        Node node;
        scanf("%d%d",&node.x,&node.y);
        p.push_back(node);
    }
    Node st;
    st.x=0;
    st.y=0;
    int cnt=0;
    int time=0;
    while(!isEnd(st.x,st.y))
    {
        int k;
        if(cnt%2)
            k=-1;
        else
            k=1;
        cnt++;
        Node en=getPoint(st,k);
        for(int i=0; i<p.size(); i++)
        {
            if(ans[i]==0)
            {
                int cost=getCost(st.x,st.y,k,p[i].x,p[i].y);
                if(cost>=0)
                    ans[i]=time+cost;
            }
        }
        time+=abs(en.x-st.x);
        st=en;
    }
    //cout<<"cnt:"<<cnt<<endl;
    for(int i=0;i<p.size();i++)
        if(ans[i]==0)
            printf("-1\n");
        else
            printf("%d\n",ans[i]);
    return 0;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;

int n,m,t;

struct Node
{
    int num;
    int x,y;

};

Node getPoint(Node st,int k)
{
    Node p;
    if(k*(-st.x)+st.y!=st.y&&k*(-st.x)+st.y>=0&&k*(-st.x)+st.y<=m)
    {
        p.x=0;
        p.y=k*(-st.x)+st.y;
        return p;
    }
    if(k*(n-st.x)+st.y!=st.y&&k*(n-st.x)+st.y>=0&&k*(n-st.x)+st.y<=m)
    {
        p.x=n;
        p.y=k*(n-st.x)+st.y;
        return p;
    }
    if((-st.y+k*st.x)/k!=st.x&&(-st.y+k*st.x)/k>=0&&(-st.y+k*st.x)/k<=n)
    {
        p.x=(-st.y+k*st.x)/k;
        p.y=0;
        return p;
    }
    if((m-st.y+k*st.x)/k!=st.x&&(m-st.y+k*st.x)/k>=0&&(m-st.y+k*st.x)/k<=n)
    {
        p.x=(m-st.y+k*st.x)/k;
        p.y=m;
        return p;
    }
}

bool isEnd(int x,int y)
{
    if(x==n&&y==0)
        return 1;
    if(x==n&&y==m)
        return 1;
    if(x==0&&y==m)
        return 1;
    return 0;
}

int getCost(int sx,int sy,int k,int tmpx,int tmpy)
{
    int b=sy-k*sx;
    if(tmpy!=k*tmpx+b)
        return -1;
    return abs(tmpx-sx);
}

int ans[100005];
vector<Node> v1[200005];
vector<Node> v2[200005];
int main()
{
    scanf("%d%d%d",&n,&m,&t);
    /*Node a;
    a.x=3;
    a.y=3;
    Node b=getPoint(a,-1);
    cout<<b.x<<" "<<b.y<<endl;*/
    int all=0;
    for(int i=0; i<t; i++)
    {
        Node node;
        node.num=i;
        scanf("%d%d",&node.x,&node.y);
        int b1=node.y-node.x+100005;
        int b2=node.y+node.x;
        v1[b1].push_back(node);
        v2[b2].push_back(node);
        all++;
    }
    //for(int i=0;i<v2[6].size();i++)
      //  cout<<v2[6][i].num<<" "<<v2[6][i].x<<" "<<v2[6][i].y<<endl;
    Node st;
    st.x=0;
    st.y=0;
    int cnt=0;
    int time=0;
    while(!isEnd(st.x,st.y))
    {
        int k;
        if(cnt%2)
            k=-1;
        else
            k=1;
        cnt++;
        Node en=getPoint(st,k);
        int b=st.y-k*st.x;
        if(k==1)
        {
            b+=100005;
            for(int i=0; i<v1[b].size(); i++)
            {
                if(ans[v1[b][i].num]==0)
                {
                    int cost=getCost(st.x,st.y,k,v1[b][i].x,v1[b][i].y);
                    ans[v1[b][i].num]=time+cost;
                }
            }
        }
        if(k==-1)
        {
            for(int i=0; i<v2[b].size(); i++)
            {
                if(ans[v2[b][i].num]==0)
                {
                    int cost=getCost(st.x,st.y,k,v2[b][i].x,v2[b][i].y);
                    ans[v2[b][i].num]=time+cost;
                }
            }
        }
        time+=abs(en.x-st.x);
        st=en;
    }
    //cout<<"cnt:"<<cnt<<endl;
    for(int i=0; i<all; i++)
        if(ans[i]==0)
            printf("-1\n");
        else
            printf("%d\n",ans[i]);
    return 0;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;

int n,m,t;

struct Node
{
    int x,y;
};

vector<pair<int,Node> >p;

Node getPoint(Node st,int k)
{
    Node p;
    if(k*(-st.x)+st.y!=st.y&&k*(-st.x)+st.y>=0&&k*(-st.x)+st.y<=m)
    {
        p.x=0;
        p.y=k*(-st.x)+st.y;
        return p;
    }
    if(k*(n-st.x)+st.y!=st.y&&k*(n-st.x)+st.y>=0&&k*(n-st.x)+st.y<=m)
    {
        p.x=n;
        p.y=k*(n-st.x)+st.y;
        return p;
    }
    if((-st.y+k*st.x)/k!=st.x&&(-st.y+k*st.x)/k>=0&&(-st.y+k*st.x)/k<=n)
    {
        p.x=(-st.y+k*st.x)/k;
        p.y=0;
        return p;
    }
    if((m-st.y+k*st.x)/k!=st.x&&(m-st.y+k*st.x)/k>=0&&(m-st.y+k*st.x)/k<=n)
    {
        p.x=(m-st.y+k*st.x)/k;
        p.y=m;
        return p;
    }
}

bool isEnd(int x,int y)
{
    if(x==n&&y==0)
        return 1;
    if(x==n&&y==m)
        return 1;
    if(x==0&&y==m)
        return 1;
    return 0;
}

int getCost(int sx,int sy,int k,int tmpx,int tmpy)
{
    int b=sy-k*sx;
    if(tmpy!=k*tmpx+b)
        return -1;
    return abs(tmpx-sx);
}

int ans[100005];
int main()
{
    scanf("%d%d%d",&n,&m,&t);
    /*Node a;
    a.x=3;
    a.y=3;
    Node b=getPoint(a,-1);
    cout<<b.x<<" "<<b.y<<endl;*/
    int all=0;
    for(int i=0; i<t; i++)
    {
        Node node;
        scanf("%d%d",&node.x,&node.y);
        p.push_back(make_pair(i,node));
        all++;
    }
    Node st;
    st.x=0;
    st.y=0;
    int cnt=0;
    int time=0;
    while(!isEnd(st.x,st.y))
    {
        int k;
        if(cnt%2)
            k=-1;
        else
            k=1;
        cnt++;
        Node en=getPoint(st,k);
        vector<pair<int ,Node> >::iterator it;
        for(it=p.begin(); it<p.end(); it++)
        {
            int cost=getCost(st.x,st.y,k,it->second.x,it->second.y);
            if(cost>=0)
            {
                ans[it->first]=time+cost;
                p.erase(it);
            }
        }
        time+=abs(en.x-st.x);
        st=en;
    }
    //cout<<"cnt:"<<cnt<<endl;
    for(int i=0; i<all; i++)
        if(ans[i]==0)
            printf("-1\n");
        else
            printf("%d\n",ans[i]);
    return 0;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;

int n,m,t;

struct Node
{
    int x,y;
};

vector<pair<int,Node> >po;

Node getPoint(Node st,int k)
{
    Node p;
    if(k*(-st.x)+st.y!=st.y&&k*(-st.x)+st.y>=0&&k*(-st.x)+st.y<=m)
    {
        p.x=0;
        p.y=k*(-st.x)+st.y;
        return p;
    }
    if(k*(n-st.x)+st.y!=st.y&&k*(n-st.x)+st.y>=0&&k*(n-st.x)+st.y<=m)
    {
        p.x=n;
        p.y=k*(n-st.x)+st.y;
        return p;
    }
    if((-st.y+k*st.x)/k!=st.x&&(-st.y+k*st.x)/k>=0&&(-st.y+k*st.x)/k<=n)
    {
        p.x=(-st.y+k*st.x)/k;
        p.y=0;
        return p;
    }
    if((m-st.y+k*st.x)/k!=st.x&&(m-st.y+k*st.x)/k>=0&&(m-st.y+k*st.x)/k<=n)
    {
        p.x=(m-st.y+k*st.x)/k;
        p.y=m;
        return p;
    }
}

bool isEnd(int x,int y)
{
    if(x==n&&y==0)
        return 1;
    if(x==n&&y==m)
        return 1;
    if(x==0&&y==m)
        return 1;
    return 0;
}

int getCost(int sx,int sy,int k,int tmpx,int tmpy)
{
    int b=sy-k*sx;
    if(tmpy!=k*tmpx+b)
        return -1;
    return abs(tmpx-sx);
}

int ans[100005];
int main()
{
    scanf("%d%d%d",&n,&m,&t);
    /*Node a;
    a.x=3;
    a.y=3;
    Node b=getPoint(a,-1);
    cout<<b.x<<" "<<b.y<<endl;*/
    int all=0;
    for(int i=0; i<t; i++)
    {
        Node node;
        scanf("%d%d",&node.x,&node.y);
        po.push_back(make_pair(i,node));
        all++;
    }
    Node st;
    st.x=0;
    st.y=0;
    int cnt=0;
    int time=0;
    while(!isEnd(st.x,st.y))
    {
        int k;
        if(cnt%2)
            k=-1;
        else
            k=1;
        cnt++;
        Node en=getPoint(st,k);
        vector<pair<int ,Node> >::iterator it;
        //cout<<p.end()->first<<endl;;
        for(it=po.begin(); it<po.end(); it++)
        {
            if(ans[it->first]==0)
            {
                int cost=getCost(st.x,st.y,k,it->second.x,it->second.y);
                if(cost>=0)
                {
                    ans[it->first]=time+cost;
                    po.erase(it);
                    it--;
                }
            }
        }
        time+=abs(en.x-st.x);
        st=en;
    }
    //cout<<"cnt:"<<cnt<<endl;
    for(int i=0; i<all; i++)
        if(ans[i]==0)
            printf("-1\n");
        else
            printf("%d\n",ans[i]);
    return 0;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;

int n,m,t;

struct Node
{
    int x,y;
};

vector<pair<int,Node> >p;

Node getPoint(Node st,int k)
{
    Node p;
    if(k*(-st.x)+st.y!=st.y&&k*(-st.x)+st.y>=0&&k*(-st.x)+st.y<=m)
    {
        p.x=0;
        p.y=k*(-st.x)+st.y;
        return p;
    }
    if(k*(n-st.x)+st.y!=st.y&&k*(n-st.x)+st.y>=0&&k*(n-st.x)+st.y<=m)
    {
        p.x=n;
        p.y=k*(n-st.x)+st.y;
        return p;
    }
    if((-st.y+k*st.x)/k!=st.x&&(-st.y+k*st.x)/k>=0&&(-st.y+k*st.x)/k<=n)
    {
        p.x=(-st.y+k*st.x)/k;
        p.y=0;
        return p;
    }
    if((m-st.y+k*st.x)/k!=st.x&&(m-st.y+k*st.x)/k>=0&&(m-st.y+k*st.x)/k<=n)
    {
        p.x=(m-st.y+k*st.x)/k;
        p.y=m;
        return p;
    }
}

bool isEnd(int x,int y)
{
    if(x==n&&y==0)
        return 1;
    if(x==n&&y==m)
        return 1;
    if(x==0&&y==m)
        return 1;
    return 0;
}

int getCost(int sx,int sy,int k,int tmpx,int tmpy)
{
    int b=sy-k*sx;
    if(tmpy!=k*tmpx+b)
        return -1;
    return abs(tmpx-sx);
}

int ans[100005];
int main()
{
    scanf("%d%d%d",&n,&m,&t);
    /*Node a;
    a.x=3;
    a.y=3;
    Node b=getPoint(a,-1);
    cout<<b.x<<" "<<b.y<<endl;*/
    int all=0;
    for(int i=0; i<t; i++)
    {
        Node node;
        scanf("%d%d",&node.x,&node.y);
        p.push_back(make_pair(i,node));
        all++;
    }
    Node st;
    st.x=0;
    st.y=0;
    int cnt=0;
    int time=0;
    while(!isEnd(st.x,st.y))
    {
        int k;
        if(cnt%2)
            k=-1;
        else
            k=1;
        cnt++;
        Node en=getPoint(st,k);
        vector<pair<int ,Node> >::iterator it;
        for(it=p.begin(); it!=p.end(); it++)
        {
            int cost=getCost(st.x,st.y,k,it->second.x,it->second.y);
            if(cost>=0)
            {
                ans[it->first]=time+cost;
                p.erase(it);
            }
        }
        time+=abs(en.x-st.x);
        st=en;
    }
    //cout<<"cnt:"<<cnt<<endl;
    for(int i=0; i<all; i++)
        if(ans[i]==0)
            printf("-1\n");
        else
            printf("%d\n",ans[i]);
    return 0;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;

int n,m,t;

struct Node
{
    int x,y;
};

vector<pair<int,Node> >p;

Node getPoint(Node st,int k)
{
    Node p;
    if(k*(-st.x)+st.y!=st.y&&k*(-st.x)+st.y>=0&&k*(-st.x)+st.y<=m)
    {
        p.x=0;
        p.y=k*(-st.x)+st.y;
        return p;
    }
    if(k*(n-st.x)+st.y!=st.y&&k*(n-st.x)+st.y>=0&&k*(n-st.x)+st.y<=m)
    {
        p.x=n;
        p.y=k*(n-st.x)+st.y;
        return p;
    }
    if((-st.y+k*st.x)/k!=st.x&&(-st.y+k*st.x)/k>=0&&(-st.y+k*st.x)/k<=n)
    {
        p.x=(-st.y+k*st.x)/k;
        p.y=0;
        return p;
    }
    if((m-st.y+k*st.x)/k!=st.x&&(m-st.y+k*st.x)/k>=0&&(m-st.y+k*st.x)/k<=n)
    {
        p.x=(m-st.y+k*st.x)/k;
        p.y=m;
        return p;
    }
}

bool isEnd(int x,int y)
{
    if(x==n&&y==0)
        return 1;
    if(x==n&&y==m)
        return 1;
    if(x==0&&y==m)
        return 1;
    return 0;
}

int getCost(int sx,int sy,int k,int tmpx,int tmpy)
{
    int b=sy-k*sx;
    if(tmpy!=k*tmpx+b)
        return -1;
    return abs(tmpx-sx);
}

int ans[100005];
int main()
{
    scanf("%d%d%d",&n,&m,&t);
    /*Node a;
    a.x=3;
    a.y=3;
    Node b=getPoint(a,-1);
    cout<<b.x<<" "<<b.y<<endl;*/
    int all=0;
    for(int i=0; i<t; i++)
    {
        Node node;
        scanf("%d%d",&node.x,&node.y);
        p.push_back(make_pair(i,node));
        all++;
    }
    Node st;
    st.x=0;
    st.y=0;
    int cnt=0;
    int time=0;
    while(!isEnd(st.x,st.y))
    {
        int k;
        if(cnt%2)
            k=-1;
        else
            k=1;
        cnt++;
        Node en=getPoint(st,k);
        vector<pair<int ,Node> >::iterator it;
        //cout<<p.end()->first<<endl;;
        for(it=p.begin(); it<p.end(); it++)
        {
            if(ans[it->first]==0)
            {
                int cost=getCost(st.x,st.y,k,it->second.x,it->second.y);
                if(cost>=0)
                {
                    ans[it->first]=time+cost;
                    //p.erase(it);
                }
            }
        }
        time+=abs(en.x-st.x);
        st=en;
    }
    //cout<<"cnt:"<<cnt<<endl;
    for(int i=0; i<all; i++)
        if(ans[i]==0)
            printf("-1\n");
        else
            printf("%d\n",ans[i]);
    return 0;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;

int n,m,t;

struct Node
{
    int x,y;
};

vector<pair<int,Node> >p;

Node getPoint(Node st,int k)
{
    Node p;
    if(k*(-st.x)+st.y!=st.y&&k*(-st.x)+st.y>=0&&k*(-st.x)+st.y<=m)
    {
        p.x=0;
        p.y=k*(-st.x)+st.y;
        return p;
    }
    if(k*(n-st.x)+st.y!=st.y&&k*(n-st.x)+st.y>=0&&k*(n-st.x)+st.y<=m)
    {
        p.x=n;
        p.y=k*(n-st.x)+st.y;
        return p;
    }
    if((-st.y+k*st.x)/k!=st.x&&(-st.y+k*st.x)/k>=0&&(-st.y+k*st.x)/k<=n)
    {
        p.x=(-st.y+k*st.x)/k;
        p.y=0;
        return p;
    }
    if((m-st.y+k*st.x)/k!=st.x&&(m-st.y+k*st.x)/k>=0&&(m-st.y+k*st.x)/k<=n)
    {
        p.x=(m-st.y+k*st.x)/k;
        p.y=m;
        return p;
    }
}

bool isEnd(int x,int y)
{
    if(x==n&&y==0)
        return 1;
    if(x==n&&y==m)
        return 1;
    if(x==0&&y==m)
        return 1;
    return 0;
}

int getCost(int sx,int sy,int k,int tmpx,int tmpy)
{
    int b=sy-k*sx;
    if(tmpy!=k*tmpx+b)
        return -1;
    return abs(tmpx-sx);
}

int ans[100005];
int main()
{
    scanf("%d%d%d",&n,&m,&t);
    /*Node a;
    a.x=3;
    a.y=3;
    Node b=getPoint(a,-1);
    cout<<b.x<<" "<<b.y<<endl;*/
    int all=0;
    for(int i=0; i<t; i++)
    {
        Node node;
        scanf("%d%d",&node.x,&node.y);
        p.push_back(make_pair(i,node));
        all++;
    }
    Node st;
    st.x=0;
    st.y=0;
    int cnt=0;
    int time=0;
    while(!isEnd(st.x,st.y))
    {
        int k;
        if(cnt%2)
            k=-1;
        else
            k=1;
        cnt++;
        Node en=getPoint(st,k);
        vector<pair<int ,Node> >::iterator it;
        //cout<<p.end()->first<<endl;;
        for(it=p.begin(); it<p.end(); it++)
        {
            if(ans[it->first]==0)
            {
                int cost=getCost(st.x,st.y,k,it->second.x,it->second.y);
                if(cost>=0)
                {
                    ans[it->first]=time+cost;
                    p.erase(it);
                }
            }
        }
        time+=abs(en.x-st.x);
        st=en;
    }
    //cout<<"cnt:"<<cnt<<endl;
    for(int i=0; i<all; i++)
        if(ans[i]==0)
            printf("-1\n");
        else
            printf("%d\n",ans[i]);
    return 0;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<algorithm>
#include<set>
using namespace std;

int n,m,t;

struct Node
{
    int num;
    int x,y;

    bool operator <(const Node& tmp)const
    {
        return num>tmp.num;
    }
};

set<Node>po;

Node getPoint(Node st,int k)
{
    Node p;
    if(k*(-st.x)+st.y!=st.y&&k*(-st.x)+st.y>=0&&k*(-st.x)+st.y<=m)
    {
        p.x=0;
        p.y=k*(-st.x)+st.y;
        return p;
    }
    if(k*(n-st.x)+st.y!=st.y&&k*(n-st.x)+st.y>=0&&k*(n-st.x)+st.y<=m)
    {
        p.x=n;
        p.y=k*(n-st.x)+st.y;
        return p;
    }
    if((-st.y+k*st.x)/k!=st.x&&(-st.y+k*st.x)/k>=0&&(-st.y+k*st.x)/k<=n)
    {
        p.x=(-st.y+k*st.x)/k;
        p.y=0;
        return p;
    }
    if((m-st.y+k*st.x)/k!=st.x&&(m-st.y+k*st.x)/k>=0&&(m-st.y+k*st.x)/k<=n)
    {
        p.x=(m-st.y+k*st.x)/k;
        p.y=m;
        return p;
    }
}

bool isEnd(int x,int y)
{
    if(x==n&&y==0)
        return 1;
    if(x==n&&y==m)
        return 1;
    if(x==0&&y==m)
        return 1;
    return 0;
}

int getCost(int sx,int sy,int k,int tmpx,int tmpy)
{
    int b=sy-k*sx;
    if(tmpy!=k*tmpx+b)
        return -1;
    return abs(tmpx-sx);
}

int ans[100005];
int main()
{
    scanf("%d%d%d",&n,&m,&t);
    /*Node a;
    a.x=3;
    a.y=3;
    Node b=getPoint(a,-1);
    cout<<b.x<<" "<<b.y<<endl;*/
    int all=0;
    for(int i=0; i<t; i++)
    {
        Node node;
        node.num=i;
        scanf("%d%d",&node.x,&node.y);
        po.insert(node);
        all++;
    }
    Node st;
    st.x=0;
    st.y=0;
    int cnt=0;
    int time=0;
    while(!isEnd(st.x,st.y))
    {
        int k;
        if(cnt%2)
            k=-1;
        else
            k=1;
        cnt++;
        Node en=getPoint(st,k);
        set<Node>::iterator it;
        //cout<<p.end()->first<<endl;;
        for(it=po.begin(); it!=po.end(); it++)
        {
            if(ans[it->num]==0)
            {
                int cost=getCost(st.x,st.y,k,it->x,it->y);
                if(cost>=0)
                {
                    ans[it->num]=time+cost;
                    po.erase(it);
                    it--;
                }
            }
        }
        time+=abs(en.x-st.x);
        st=en;
    }
    //cout<<"cnt:"<<cnt<<endl;
    for(int i=0; i<all; i++)
        if(ans[i]==0)
            printf("-1\n");
        else
            printf("%d\n",ans[i]);
    return 0;
}