#include<bits/stdc++.h>
#define MEM(a,x) memset(a,x,sizeof(a));
#define MEMINF(a) memset(a,0x3f,sizeof(a));
using namespace std;
typedef long long LL;
const int MAXN=1e5+10;
struct node {
  int x,y;
  int step;
  int dir;
  int id;
};
int dx[4]={1,1,-1,-1};
int dy[4]={-1,1,-1,1};

int vis[MAXN];
typedef pair<int,int> PAIR;
typedef map<PAIR,int> mpp;
mpp mp;
queue<node>q;
int qsize;
double k1,k2;

node judge(node u,node lim) {
  int k=qsize;
  for(int i=0; i<k; ++i){
    node v=q.front();
    q.pop();
    //printf("%d %d\n",v.x,v.y);
   // printf("%d %d\n",u.x,u.y);
   // printf("%d %d\n",lim.x,lim.y);

    //printf("%lf  ",(double)(v.x-u.x));
    //printf("%lf\n",(double)(v.y-u.y));
    k2=((double)(lim.x-u.x))/((double)(lim.y-u.y));
    double left=double(v.y-u.y);
    double right=double(v.x-u.x)*k2;
    double aa=left-right;

   //if(v.x==3&&v.y==1) printf("in k2:%lf left:%lf right:%lf aa:%lf\n",k2,left,right,aa);
    //printf("aa:%lf  v.id:%d step:%d\n",aa,v.id,abs(v.x-u.x)+u.step);
    if (aa==0&&!vis[v.id]) {
      vis[v.id]=abs(v.x-u.x)+u.step,qsize--;
    //  printf("id:%d step:%d\n",v.id,vis[v.id]);
    }
    else q.push(v);
  }
}

int main() {
  int n,m,k;
  scanf("%d %d %d",&m,&n,&k);
  int x,y;
  for (int i=1; i<=k; ++i) {
    scanf("%d %d",&x,&y);
    node v;
    v.x=y,v.y=x;
    v.id=i;
    q.push(v);
    qsize++;
  }
  node now;
  now.x=1,now.y=1,now.dir=1,now.step=1;
  int mx,my;
  bool flag=false;
   // printf("now:%d %d %d\n",now.x,now.y,now.dir);
  while (true) {
    if (!qsize) break;
    node lim;
    if (now.dir==1) {
      int r=m-now.y,u=n-now.x;
      if (u<r) lim.dir=3,mx=u,my=u;
      else if(r<u)lim.dir=0,mx=r,my=r;
      else if (r==u) mx=my=r,flag=true;

    }
    else if(now.dir==0) {
      int l=now.y-0,u=n-now.x;
      if (l<u) mx=l,my=-l,lim.dir=1;
      else if(u<l)mx=u,my=-u,lim.dir=2;
      else mx=l,my=-l,flag=true;
    }
    else if(now.dir==2) {
      int d=now.x-0,l=now.y-0;
      if (l<d) mx=my=-l,lim.dir=3;
      else if(d<l)mx=my=-d,lim.dir=0;
      else mx=my=-d,flag=true;
    }
    else if(now.dir==3) {
      int r=m-now.y,d=now.x-0;
      if (r<d) mx=-r,my=r,lim.dir=2;
      else if(d<r)mx=-d,my=d,lim.dir=1;
      else mx=-d,my=d,flag=true;
    }
        lim.x=now.x+mx;
        lim.y=now.y+my;
        lim.step=now.step+abs(mx);
        judge(now,lim);
        now=lim;
       if(now.x==0&&now.y==0) break;
       else if (now.x==0&&now.y==m) break;
       else if (now.x==n&&now.y==m) break;
       else if (now.x==n&&now.y==0) break;
    //printf("now:%d %d %d\n",now.x,now.y,now.dir);
  }
  for (int i=1; i<=k; ++i) {
    if (vis[i]) printf("%d\n",vis[i]);
    else puts("-1");
  }
}


#include<bits/stdc++.h>
#define MEM(a,x) memset(a,x,sizeof(a));
#define MEMINF(a) memset(a,0x3f,sizeof(a));
using namespace std;
typedef long long LL;
const int MAXN=1e5+10;
const LL INF=0x3f3f3f3f3f3f3f3fll;
struct node {
  int x,y;
  LL step;
  int dir;
  int id;
};
int vis[MAXN];
typedef pair<int,int> PAIR;
LL ans[MAXN];
map<int,vector<pair<int,int> > >sz,sf;
void judge(node now) {
  int b;
  int flag=0;
  if (now.dir==1||now.dir==2) b=now.y-now.x;
  else b=now.x+now.y,flag=1;
  if (flag==1) {
    for (auto it :sf[b]) {
      int rx=it.first,id=it.second;
      ans[id]=min(ans[id],now.step+abs(now.x-rx));
    }
    sf.erase(b);
  }
  else {
    for (auto it : sz[b]) {
      int rx=it.first,id=it.second;
      ans[id]=min(ans[id],now.step+abs(now.x-rx));
    }
    sz.erase(b);
  }
}

int main() {
  int n,m,k;
  scanf("%d %d %d",&m,&n,&k);
  int x,y;
  for (int i=1; i<=k; ++i) {
    scanf("%d %d",&x,&y);
    swap(x,y);
    sz[y-x].push_back(PAIR(x,i));
    sf[y+x].push_back(PAIR(x,i));
  }
  MEMINF(ans);
  node now;
  now.x=0,now.y=0,now.dir=1,now.step=0;
  int mx,my;
  bool flag=false;
  int cnt=0;
  while (true) {
    node lim;
    if (now.dir==1) {
      int r=m-now.y,u=n-now.x;
      if (u<r) lim.dir=3,mx=u,my=u;
      else if(r<u)lim.dir=0,mx=r,my=r;
      else if (r==u) mx=my=r,flag=true;

    }
    else if(now.dir==0) {
      int l=now.y-0,u=n-now.x;
      if (l<u) mx=l,my=-l,lim.dir=1;
      else if(u<l)mx=u,my=-u,lim.dir=2;
      else mx=l,my=-l,flag=true;
    }
    else if(now.dir==2) {
      int d=now.x-0,l=now.y-0;
      if (l<d) mx=my=-l,lim.dir=3;
      else if(d<l)mx=my=-d,lim.dir=0;
      else mx=my=-d,flag=true;
    }
    else if(now.dir==3) {
      int r=m-now.y,d=now.x-0;
      if (r<d) mx=-r,my=r,lim.dir=2;
      else if(d<r)mx=-d,my=d,lim.dir=1;
      else mx=-d,my=d,flag=true;
    }
        lim.x=now.x+mx;
        lim.y=now.y+my;
        lim.step=now.step+abs(mx);
        judge(now);
        now=lim;
       if(now.x==0&&now.y==0) break;
       else if (now.x==0&&now.y==m) break;
       else if (now.x==n&&now.y==m) break;
       else if (now.x==n&&now.y==0) break;
  }
  for (int i=1; i<=k; ++i) {
    ans[i]==INF?puts("-1"):printf("%lld\n",ans[i]);
  }
}


#include<bits/stdc++.h>
#define MEM(a,x) memset(a,x,sizeof(a));
#define MEMINF(a) memset(a,0x3f,sizeof(a));
using namespace std;
typedef long long LL;
const int MAXN=1e5+10;
struct node {
  int x,y;
  int step;
  int dir;
  int id;
};
int dx[4]={1,1,-1,-1};
template<class T>
inline bool read(T &n)
{
    T x = 0, tmp = 1;
    char c = getchar();
    while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();
    if(c == EOF) return false;
    if(c == '-') c = getchar(), tmp = -1;
    while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();
    n = x*tmp;
    return true;
}
template <class T>
inline void write(T n)
{
    if(n < 0)
    {
        putchar('-');
        n = -n;
    }
    int len = 0,data[20];
    while(n)
    {
        data[len++] = n%10;
        n /= 10;
    }
    if(!len) data[len++] = 0;
    while(len--) putchar(data[len]+48);
}
int dy[4]={-1,1,-1,1};

int vis[MAXN];
typedef pair<int,int> PAIR;
typedef map<PAIR,int> mpp;
mpp mp;
queue<node>q;
int qsize;
double k1,k2;

void judge(node u,node lim) {
  int k=qsize;
  for(int i=0; i<k; ++i){
    node v=q.front();
    q.pop();
    k2=((double)(lim.x-u.x))/((double)(lim.y-u.y));
    double left=double(v.y-u.y);
    double right=double(v.x-u.x)*k2;
    double aa=left-right;

    if (aa==0&&!vis[v.id]) {
      vis[v.id]=abs(v.x-u.x)+u.step,qsize--;
    }
    else q.push(v);
  }
}

int main() {
  int n,m,k;
  read(m),read(n),read(k);
  int x,y;
  for (int i=1; i<=k; ++i) {
    read(x),read(y);
    node v;
    v.x=y,v.y=x;
    v.id=i;
    q.push(v);
    qsize++;
  }
  node now;
  now.x=1,now.y=1,now.dir=1,now.step=1;
  int mx,my;
  bool flag=false;
  int cnt=0;
  while (true) {
    if (!qsize) break;
    cnt++;
    node lim;
    if (cnt>1e5) break;
    if (now.dir==1) {
      int r=m-now.y,u=n-now.x;
      if (u<r) lim.dir=3,mx=u,my=u;
      else if(r<u)lim.dir=0,mx=r,my=r;
      else if (r==u) mx=my=r,flag=true;

    }
    else if(now.dir==0) {
      int l=now.y-0,u=n-now.x;
      if (l<u) mx=l,my=-l,lim.dir=1;
      else if(u<l)mx=u,my=-u,lim.dir=2;
      else mx=l,my=-l,flag=true;
    }
    else if(now.dir==2) {
      int d=now.x-0,l=now.y-0;
      if (l<d) mx=my=-l,lim.dir=3;
      else if(d<l)mx=my=-d,lim.dir=0;
      else mx=my=-d,flag=true;
    }
    else if(now.dir==3) {
      int r=m-now.y,d=now.x-0;
      if (r<d) mx=-r,my=r,lim.dir=2;
      else if(d<r)mx=-d,my=d,lim.dir=1;
      else mx=-d,my=d,flag=true;
    }
        lim.x=now.x+mx;
        lim.y=now.y+my;
        lim.step=now.step+abs(mx);
        judge(now,lim);
        now=lim;
       if(now.x==0&&now.y==0) break;
       else if (now.x==0&&now.y==m) break;
       else if (now.x==n&&now.y==m) break;
       else if (now.x==n&&now.y==0) break;
  }
  for (int i=1; i<=k; ++i) {
    if (vis[i]) write(vis[i]),puts("");
    else puts("-1");
  }
}


#include<bits/stdc++.h>
#define MEM(a,x) memset(a,x,sizeof(a));
#define MEMINF(a) memset(a,0x3f,sizeof(a));
using namespace std;
typedef long long LL;
const int MAXN=1e5+10;
struct node {
  int x,y;
  int step;
  int dir;
};
int dx[4]={1,1,-1,-1};
int dy[4]={-1,1,-1,1};

int ans[MAXN];
bool vis[MAXN];
typedef pair<int,int> PAIR;
typedef map<PAIR,int> mpp;
mpp mp;

int main() {
  int n,m,k;
  scanf("%d %d %d",&n,&m,&k);
  int x,y;
  for (int i=1; i<=k; ++i) {
    scanf("%d %d",&x,&y);
    mp[PAIR(x,y)]=i;
  }
  node pre,now;
  node lu,ru,ld,rd;

  MEM(vis,0);
  MEM(ans,0);
  now.x=1,now.y=1,now.step=1,now.dir=1;
  int flag=0;

  while(true) {
    mpp::iterator it;
    it=mp.find(PAIR(now.x,now.y));
    int v=it->second;
    if (v&&!vis[v]) {
      ans[v]=now.step;
      vis[v]=1;
      flag++;
    }
    if (flag==k) break;
    now.x+=dx[now.dir];
    now.y+=dy[now.dir];
    now.step++;
    if (now.x==0&&now.y==0)break;
    if (now.x==n&&now.y==m) break;
    if (now.x==0&&now.y==m) break;
    if (now.x==n&&now.y==m)break;

    if (now.x==0) {
      if (now.dir==3) now.dir=1;
      else if (now.dir==2) now.dir=0;
    }
    else if (now.x==n) {
      if (now.dir==1) now.dir=3;
      else if (now.dir==0) now.dir=2;
    }
    else if (now.y==0) {
      if (now.dir==2) now.dir=3;
      else if (now.dir==0) now.dir=1;
    }
    else if (now.y==m) {
      if (now.dir==3) now.dir=2;
      else if (now.dir==1) now.dir=0;
    }
  }
  for (int i=1; i<=k; ++i) {
    if (ans[i]==0)puts("-1");
    else printf("%d\n",ans[i]);
  }
}


#include<bits/stdc++.h>
#define MEM(a,x) memset(a,x,sizeof(a));
#define MEMINF(a) memset(a,0x3f,sizeof(a));
using namespace std;
typedef long long LL;
const int MAXN=1e5+10;
const LL INF=0x3f3f3f3f3f3f3f3fll;
struct node {
  int x,y;
  LL step;
  int dir;
  int id;
};
struct noo{
  int x,y;
}sensor[MAXN];
int dx[4]={1,1,-1,-1};
int dy[4]={-1,1,-1,1};

int vis[MAXN];
typedef pair<int,int> PAIR;
typedef map<PAIR,int> mpp;
mpp mp;
queue<node>q;
int qsize;
double k1,k2;
LL ans[MAXN];

map<int,vector<pair<int,int> > >sz,sf;
void judge(node now,node lim) {
  int b;
  int flag=0;
  if (now.dir==1||now.dir==2) b=now.y-now.x;
  else b=now.x+now.y,flag=1;
    //printf("flag=%d\n",flag);
  if (flag==1) {
    for (auto it :sf[b]) {
      int rx=it.first,id=it.second;
      ans[id]=min(ans[id],now.step+abs(now.x-rx));
     // printf("ans[%d]=%lld\n",id,ans[id]);
    }
    sf.erase(b);
  }
  else {
    for (auto it : sz[b]) {
      int rx=it.first,id=it.second;
      ans[id]=min(ans[id],now.step+abs(now.x-rx));
      //printf("ans[%d]=%lld\n",id,ans[id]);
    }
    sz.erase(b);
  }
}

int main() {
  int n,m,k;
  scanf("%d %d %d",&m,&n,&k);
  int x,y;
  for (int i=1; i<=k; ++i) {
    scanf("%d %d",&x,&y);
    swap(x,y);
    sz[y-x].push_back(PAIR(x,i));
    sf[y+x].push_back(PAIR(x,i));
  }
  MEMINF(ans);
  node now;
  now.x=0,now.y=0,now.dir=1,now.step=0;
  int mx,my;
  bool flag=false;
  int cnt=0;
  while (true) {
    node lim;
    //printf("now:%d %d %d\n",now.x,now.y,now.dir);
    if (now.dir==1) {
      int r=m-now.y,u=n-now.x;
      if (u<r) lim.dir=3,mx=u,my=u;
      else if(r<u)lim.dir=0,mx=r,my=r;
      else if (r==u) mx=my=r,flag=true;

    }
    else if(now.dir==0) {
      int l=now.y-0,u=n-now.x;
      if (l<u) mx=l,my=-l,lim.dir=1;
      else if(u<l)mx=u,my=-u,lim.dir=2;
      else mx=l,my=-l,flag=true;
    }
    else if(now.dir==2) {
      int d=now.x-0,l=now.y-0;
      if (l<d) mx=my=-l,lim.dir=3;
      else if(d<l)mx=my=-d,lim.dir=0;
      else mx=my=-d,flag=true;
    }
    else if(now.dir==3) {
      int r=m-now.y,d=now.x-0;
      if (r<d) mx=-r,my=r,lim.dir=2;
      else if(d<r)mx=-d,my=d,lim.dir=1;
      else mx=-d,my=d,flag=true;
    }
        lim.x=now.x+mx;
        lim.y=now.y+my;
        lim.step=now.step+abs(mx);
        judge(now,lim);
        now=lim;
        //printf("lim:%d %d %d\n",lim.x,lim.y,lim.dir);
       if(now.x==0&&now.y==0) break;
       else if (now.x==0&&now.y==m) break;
       else if (now.x==n&&now.y==m) break;
       else if (now.x==n&&now.y==0) break;
  }
  for (int i=1; i<=k; ++i) {
    ans[i]==INF?puts("-1"):printf("%lld\n",ans[i]);
  }
}


#include<bits/stdc++.h>
#define MEM(a,x) memset(a,x,sizeof(a));
#define MEMINF(a) memset(a,0x3f,sizeof(a));
using namespace std;
typedef long long LL;
const int MAXN=1e5+10;
struct node {
  int x,y;
  int step;
  int dir;
};
int dx[4]={1,1,-1,-1};
int dy[4]={-1,1,-1,1};

int ans[MAXN];
bool vis[MAXN];
typedef pair<int,int> PAIR;
typedef map<PAIR,int> mpp;
mpp mp;

int main() {
  int n,m,k;
  scanf("%d %d %d",&n,&m,&k);
  int x,y;
  for (int i=1; i<=k; ++i) {
    scanf("%d %d",&x,&y);
    mp[PAIR(x,y)]=i;
  }
  node pre,now;
  node lu,ru,ld,rd;

  MEM(vis,0);
  MEM(ans,0);
  now.x=1,now.y=1,now.step=1,now.dir=1;
  int flag=0;

  int cnt=0;

  while(true) {
    mpp::iterator it;
    it=mp.find(PAIR(now.x,now.y));
    int v=it->second;
    if (v&&!vis[v]) {
      ans[v]=now.step;
      vis[v]=1;
      flag++;
    }
    if (cnt>1e5)break;
    if (flag==k) break;
    now.x+=dx[now.dir];
    now.y+=dy[now.dir];
    now.step++;
    if (now.x==0&&now.y==0)break;
    if (now.x==n&&now.y==m) break;
    if (now.x==0&&now.y==m) break;
    if (now.x==n&&now.y==m)break;

    if (now.x==0) {
      if (now.dir==3) now.dir=1;
      else if (now.dir==2) now.dir=0;
    }
    else if (now.x==n) {
      if (now.dir==1) now.dir=3;
      else if (now.dir==0) now.dir=2;
    }
    else if (now.y==0) {
      if (now.dir==2) now.dir=3;
      else if (now.dir==0) now.dir=1;
    }
    else if (now.y==m) {
      if (now.dir==3) now.dir=2;
      else if (now.dir==1) now.dir=0;
    }
  }
  for (int i=1; i<=k; ++i) {
    if (ans[i]==0)puts("-1");
    else printf("%d\n",ans[i]);
  }
}


#include<bits/stdc++.h>
#define MEM(a,x) memset(a,x,sizeof(a));
#define MEMINF(a) memset(a,0x3f,sizeof(a));
using namespace std;
typedef long long LL;
const int MAXN=1e5+10;
struct node {
  int x,y;
  int step;
  int dir;
  int id;
};
int dx[4]={1,1,-1,-1};
int dy[4]={-1,1,-1,1};

int vis[MAXN];
typedef pair<int,int> PAIR;
typedef map<PAIR,int> mpp;
mpp mp;
queue<node>q;
int qsize;
double k1,k2;

void judge(node u,node lim) {
  int k=qsize;
  for(int i=0; i<k; ++i){
    node v=q.front();
    q.pop();
    k2=((double)(lim.x-u.x))/((double)(lim.y-u.y));
    double left=double(v.y-u.y);
    double right=double(v.x-u.x)*k2;
    double aa=left-right;

    if (aa==0&&!vis[v.id]) {
      vis[v.id]=abs(v.x-u.x)+u.step,qsize--;
    }
    else q.push(v);
  }
}

int main() {
  int n,m,k;
  scanf("%d %d %d",&m,&n,&k);
  int x,y;
  for (int i=1; i<=k; ++i) {
    scanf("%d %d",&x,&y);
    node v;
    v.x=y,v.y=x;
    v.id=i;
    q.push(v);
    qsize++;
  }
  node now;
  now.x=1,now.y=1,now.dir=1,now.step=1;
  int mx,my;
  bool flag=false;
  int cnt=0;
  while (true) {
    if (!qsize) break;
    cnt++;
    node lim;
    if (cnt>1e4) break;
    if (now.dir==1) {
      int r=m-now.y,u=n-now.x;
      if (u<r) lim.dir=3,mx=u,my=u;
      else if(r<u)lim.dir=0,mx=r,my=r;
      else if (r==u) mx=my=r,flag=true;

    }
    else if(now.dir==0) {
      int l=now.y-0,u=n-now.x;
      if (l<u) mx=l,my=-l,lim.dir=1;
      else if(u<l)mx=u,my=-u,lim.dir=2;
      else mx=l,my=-l,flag=true;
    }
    else if(now.dir==2) {
      int d=now.x-0,l=now.y-0;
      if (l<d) mx=my=-l,lim.dir=3;
      else if(d<l)mx=my=-d,lim.dir=0;
      else mx=my=-d,flag=true;
    }
    else if(now.dir==3) {
      int r=m-now.y,d=now.x-0;
      if (r<d) mx=-r,my=r,lim.dir=2;
      else if(d<r)mx=-d,my=d,lim.dir=1;
      else mx=-d,my=d,flag=true;
    }
        lim.x=now.x+mx;
        lim.y=now.y+my;
        lim.step=now.step+abs(mx);
        judge(now,lim);
        now=lim;
       if(now.x==0&&now.y==0) break;
       else if (now.x==0&&now.y==m) break;
       else if (now.x==n&&now.y==m) break;
       else if (now.x==n&&now.y==0) break;
  }
  for (int i=1; i<=k; ++i) {
    if (vis[i]) printf("%d\n",vis[i]);
    else puts("-1");
  }
}


#include<bits/stdc++.h>
#define MEM(a,x) memset(a,x,sizeof(a));
#define MEMINF(a) memset(a,0x3f,sizeof(a));
using namespace std;
typedef long long LL;
const int MAXN=1e5+10;
struct node {
  int x,y;
  int step;
  int dir;
  int id;
};
int dx[4]={1,1,-1,-1};
int dy[4]={-1,1,-1,1};

int vis[MAXN];
typedef pair<int,int> PAIR;
typedef map<PAIR,int> mpp;
mpp mp;
queue<node>q;
int qsize;
double k1,k2;

void judge(node u,node lim) {
  int k=qsize;
  for(int i=0; i<k; ++i){
    node v=q.front();
    q.pop();
    k2=((double)(lim.x-u.x))/((double)(lim.y-u.y));
    double left=double(v.y-u.y);
    double right=double(v.x-u.x)*k2;
    double aa=left-right;

    if (aa==0&&!vis[v.id]) {
      vis[v.id]=abs(v.x-u.x)+u.step,qsize--;
    }
    else q.push(v);
  }
}

int main() {
  int n,m,k;
  scanf("%d %d %d",&m,&n,&k);
  int x,y;
  for (int i=1; i<=k; ++i) {
    scanf("%d %d",&x,&y);
    node v;
    v.x=y,v.y=x;
    v.id=i;
    q.push(v);
    qsize++;
  }
  node now;
  now.x=1,now.y=1,now.dir=1,now.step=1;
  int mx,my;
  bool flag=false;
  int cnt=0;
  while (true) {
    if (!qsize) break;
    cnt++;
    node lim;
    if (cnt>1e5) break;
    if (now.dir==1) {
      int r=m-now.y,u=n-now.x;
      if (u<r) lim.dir=3,mx=u,my=u;
      else if(r<u)lim.dir=0,mx=r,my=r;
      else if (r==u) mx=my=r,flag=true;

    }
    else if(now.dir==0) {
      int l=now.y-0,u=n-now.x;
      if (l<u) mx=l,my=-l,lim.dir=1;
      else if(u<l)mx=u,my=-u,lim.dir=2;
      else mx=l,my=-l,flag=true;
    }
    else if(now.dir==2) {
      int d=now.x-0,l=now.y-0;
      if (l<d) mx=my=-l,lim.dir=3;
      else if(d<l)mx=my=-d,lim.dir=0;
      else mx=my=-d,flag=true;
    }
    else if(now.dir==3) {
      int r=m-now.y,d=now.x-0;
      if (r<d) mx=-r,my=r,lim.dir=2;
      else if(d<r)mx=-d,my=d,lim.dir=1;
      else mx=-d,my=d,flag=true;
    }
        lim.x=now.x+mx;
        lim.y=now.y+my;
        lim.step=now.step+abs(mx);
        judge(now,lim);
        now=lim;
       if(now.x==0&&now.y==0) break;
       else if (now.x==0&&now.y==m) break;
       else if (now.x==n&&now.y==m) break;
       else if (now.x==n&&now.y==0) break;
  }
  for (int i=1; i<=k; ++i) {
    if (vis[i]) printf("%d\n",vis[i]);
    else puts("-1");
  }
}


/*NAME: SANYAM GARG
INSTITUTION : DELHI TECHNOLOGICAL UNIVERSITY*/
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<bits/stdc++.h>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<bitset>
#define mod 1000000007
#define li long long int
#define ll long long int
#define readi(x) scanf("%d",&x)
#define  reads(x)  scanf("%s",&x)
#define readl(x) scanf("%I64d",&x)
#define rep(i,n) for(i=0;i<n;i++)
#define revp(i,n) for(i=(n-1);i>=0;i--)
#define myrep1(i,a,b) for(i=a;i<=b;i++)
#define myrep2(i,a,b) for(i=b;i>=a;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define sec second
#define MAXN 1000000000
#define MINN -1000000000000
#define pii pair<long long int,long long int>
#define N 200010
#define ran rank
using namespace std;
ll n,m,deb=1,flag=0;
map<pair<ll,ll> ,ll> dist;
vector<list<pair<ll,ll> > > diff(N);
vector<list<pair<ll,ll> > > sum(N);
pii up(pii cur,ll s)
{
	if(s==0)
	return mp(m-cur.sec+cur.fi,m);
	else
	return mp(cur.sec+cur.fi-m,m);
}
pii right(pii cur,ll s)
{
	if(s==0)
	return mp(n,n+cur.sec-cur.fi);
	else
	return mp(n,cur.sec+cur.fi-n);
}
pii down(pii cur,ll s)
{
	if(s==0)
	return mp(cur.fi-cur.sec,0);
	else
	return mp(cur.fi+cur.sec,0);
}
pii left(pii cur,ll s)
{
	if(s==0)
	return mp(0,cur.sec-cur.fi);
	else
	return mp(0,cur.fi+cur.sec);
}
int main()
{
	ll k,i;
	cin>>n>>m>>k;
	ll a[k],b[k];
    rep(i,k)
    {
    	cin>>a[i]>>b[i];
    	diff[abs(a[i]-b[i])].pb(mp(a[i],b[i]));
        sum[abs(a[i]+b[i])].pb(mp(a[i],b[i]));

	}
	ll sl=0;//slope!!!
	pair<ll,ll> st,en;
	st.fi=0;
	st.sec=0;
	char c='d';
	ll t=0;
	while(!(((st.fi==0)&&(st.sec==(m)))||((st.fi==n)&&(st.sec==(0)))||((st.fi==n)&&(st.sec==(m)))))
    {
//   	cout<<"i";
    	if(sl%2==0)
    	{
    		//increase both x and y whichever reaches n and m respectively first will give
    		if(c=='d')
    		{
    			if((n-st.fi)>=(m-st.sec))
    			{
    				en=up(st,sl%2);
    				c='u';
				}
				else
				{
					en=right(st,sl%2);
					c='r';
				}
//				cout<<c<<endl;
			}
			else if(c=='r')
    		{
    			if((st.fi)>=(st.sec))
    			{
    				en=down(st,sl%2);
    				c='d';
				}
				else
				{
					en=left(st,sl%2);
					c='l';
				}
			}
				else if(c=='u')
    		{
    			if((st.fi)>=(st.sec))
    			{
    				en=down(st,sl%2);
    				c='d';
				}
				else
				{
					en=left(st,sl%2);
					c='l';
				}
			}
				else if(c=='l')
    		{
    			if((n-st.fi)>=(m-st.sec))
    			{
    				en=up(st,sl%2);
    				c='u';
				}
				else
				{
					en=right(st,sl%2);
					c='r';
				}
			}
    	}
		else
		{
//			cout<<"hi";
			if(c=='d')
    		{
    			if((st.fi)>=(m-st.sec))
    			{
    				en=up(st,sl%2);
    				c='u';
				}
				else
				{
					en=left(st,sl%2);
					c='l';
				}
			}
			else if(c=='r')
    		{
    			if((st.fi)>=(m-st.sec))
    			{
    				en=up(st,sl%2);
    				c='u';
				}
				else
				{
					en=left(st,sl%2);
					c='l';
				}
			}
				else if(c=='u')
    		{
    			if((n-st.fi)>=(st.sec))
    			{
    				en=down(st,sl%2);
    				c='d';
				}
				else
				{
					en=right(st,sl%2);
					c='r';
				}
			}
				else if(c=='l')
    		{
    			if((n-st.fi)>=(st.sec))
    			{
    				en=down(st,sl%2);
    				c='d';
				}
				else
				{
					en=right(st,sl%2);
					c='r';
				}
			}
//			cout<<en.first<<" "<<en.sec;
		}
    	if(sl%2==0)
    	{
    		//diff
//    		cout<<"hi";
    		ll p=st.sec-st.fi;
    		for(list<pair<ll,ll> > ::iterator it=diff[abs(p)].begin();it!=diff[abs(p)].end();it++)
    		{
    			if((it->sec-it->fi)==p)
    			{

    			if(dist[mp(it->fi,it->sec)]==0)
    			{
    				dist[mp(it->fi,it->sec)]=t+abs(it->fi-st.fi);
				}

			    }
			}

		}
		else
		{
//			cout<<"hell";
				ll p=st.sec+st.fi;
//				cout<<p<<endl;;
    		for(list<pair<ll,ll> > ::iterator it=sum[abs(p)].begin();it!=sum[abs(p)].end();it++)
    		{
//    			cout<<"hi";
    			if((it->sec+it->fi)==p)
    			{

    			if(dist[mp(it->fi,it->sec)]==0)
    			{
    				dist[mp(it->fi,it->sec)]=t+abs(it->fi-st.fi);
				}

			    }
			}
//			cout<<"done";
		}
		t+=abs(en.first-st.fi);
//		cout<<t<<endl;
		st.fi=en.fi;
		st.second=en.sec;
		if(st.first==0&&st.sec==0)
		{
			break;
		}
//		cout<<st.first<<" "<<st.second<<endl;
//		cout<<en.first<<" "<<en.sec<<endl;;
//		while(deb--)
//		{
//
//
//		cout<<en.first<<" "<<en.sec<<"\n";
//		flag=1;
//		break;
//         }
//         if(flag)
//         {
//         	break;
//		 }
    	sl++;
	}
	for(i=0;i<k;i++)
	{
		if(dist[mp(a[i],b[i])]==0)
		{
			cout<<"-1";
		}
		else
		{
			cout<<dist[mp(a[i],b[i])];
		}
		cout<<"\n";
	}

}


/*NAME: SANYAM GARG
INSTITUTION : DELHI TECHNOLOGICAL UNIVERSITY*/
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<bits/stdc++.h>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<bitset>
#define mod 1000000007
#define li long long int
#define ll long long int
#define readi(x) scanf("%d",&x)
#define  reads(x)  scanf("%s",&x)
#define readl(x) scanf("%I64d",&x)
#define rep(i,n) for(i=0;i<n;i++)
#define revp(i,n) for(i=(n-1);i>=0;i--)
#define myrep1(i,a,b) for(i=a;i<=b;i++)
#define myrep2(i,a,b) for(i=b;i>=a;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define sec second
#define MAXN 1000000000
#define MINN -1000000000000
#define pii pair<long long int,long long int>
#define N 100010
#define ran rank
using namespace std;
ll n,m,deb=1,flag=0;
map<pair<ll,ll> ,ll> dist;
vector<list<pair<ll,ll> > > diff(N);
vector<list<pair<ll,ll> > > sum(N);
pii up(pii cur,ll s)
{
	if(s==0)
	return mp(m-cur.sec+cur.fi,m);
	else
	return mp(cur.sec+cur.fi-m,m);
}
pii right(pii cur,ll s)
{
	if(s==0)
	return mp(n,n+cur.sec-cur.fi);
	else
	return mp(n,cur.sec+cur.fi-n);
}
pii down(pii cur,ll s)
{
	if(s==0)
	return mp(cur.fi-cur.sec,0);
	else
	return mp(cur.fi+cur.sec,0);
}
pii left(pii cur,ll s)
{
	if(s==0)
	return mp(0,cur.sec-cur.fi);
	else
	return mp(0,cur.fi+cur.sec);
}
int main()
{
	ll k,i;
	cin>>n>>m>>k;
	ll a[k],b[k];
    rep(i,k)
    {
    	cin>>a[i]>>b[i];
    	diff[abs(a[i]-b[i])].pb(mp(a[i],b[i]));
        sum[abs(a[i]+b[i])].pb(mp(a[i],b[i]));

	}
	ll sl=0;//slope!!!
	pair<ll,ll> st,en;
	st.fi=0;
	st.sec=0;
	char c='d';
	ll t=0;
	while(!(((st.fi==0)&&(st.sec==(m)))||((st.fi==n)&&(st.sec==(0)))||((st.fi==n)&&(st.sec==(m)))))
    {
//   	cout<<"i";
    	if(sl%2==0)
    	{
    		//increase both x and y whichever reaches n and m respectively first will give
    		if(c=='d')
    		{
    			if((n-st.fi)>=(m-st.sec))
    			{
    				en=up(st,sl%2);
    				c='u';
				}
				else
				{
					en=right(st,sl%2);
					c='r';
				}
//				cout<<c<<endl;
			}
			else if(c=='r')
    		{
    			if((st.fi)>=(st.sec))
    			{
    				en=down(st,sl%2);
    				c='d';
				}
				else
				{
					en=left(st,sl%2);
					c='l';
				}
			}
				else if(c=='u')
    		{
    			if((st.fi)>=(st.sec))
    			{
    				en=down(st,sl%2);
    				c='d';
				}
				else
				{
					en=left(st,sl%2);
					c='l';
				}
			}
				else if(c=='l')
    		{
    			if((n-st.fi)>=(m-st.sec))
    			{
    				en=up(st,sl%2);
    				c='u';
				}
				else
				{
					en=right(st,sl%2);
					c='r';
				}
			}
    	}
		else
		{
			if(c=='d')
    		{
    			if((st.fi)>=(m-st.sec))
    			{
    				en=up(st,sl%2);
    				c='u';
				}
				else
				{
					en=left(st,sl%2);
					c='l';
				}
			}
			else if(c=='r')
    		{
    			if((st.fi)>=(m-st.sec))
    			{
    				en=up(st,sl%2);
    				c='u';
				}
				else
				{
					en=left(st,sl%2);
					c='l';
				}
			}
				else if(c=='u')
    		{
    			if((n-st.fi)>=(st.sec))
    			{
    				en=down(st,sl%2);
    				c='d';
				}
				else
				{
					en=right(st,sl%2);
					c='r';
				}
			}
				else if(c=='l')
    		{
    			if((n-st.fi)>=(st.sec))
    			{
    				en=down(st,sl%2);
    				c='d';
				}
				else
				{
					en=right(st,sl%2);
					c='r';
				}
			}
		}
    	if(sl%2==0)
    	{
    		//diff
//    		cout<<"hi";
    		ll p=st.sec-st.fi;
    		for(list<pair<ll,ll> > ::iterator it=diff[abs(p)].begin();it!=diff[abs(p)].end();it++)
    		{
    			if((it->sec-it->fi)==p)
    			{

    			if(dist[mp(it->fi,it->sec)]==0)
    			{
    				dist[mp(it->fi,it->sec)]=t+abs(it->fi-st.fi);
				}

			    }
			}

		}
		else
		{
//			cout<<"hell";
				ll p=st.sec+st.fi;
    		for(list<pair<ll,ll> > ::iterator it=sum[abs(p)].begin();it!=sum[abs(p)].end();it++)
    		{
    			if((it->sec+it->fi)==p)
    			{

    			if(dist[mp(it->fi,it->sec)]==0)
    			{
    				dist[mp(it->fi,it->sec)]=t+abs(it->fi-st.fi);
				}

			    }
			}
		}
		t+=abs(en.first-st.fi);
//		cout<<t<<endl;
		st=en;
//		while(deb--)
//		{
//
//
//		cout<<en.first<<" "<<en.sec<<"\n";
//		flag=1;
//		break;
//         }
//         if(flag)
//         {
//         	break;
//		 }
    	sl++;
	}
	for(i=0;i<k;i++)
	{
		if(dist[mp(a[i],b[i])]==0)
		{
			cout<<"-1";
		}
		else
		{
			cout<<dist[mp(a[i],b[i])];
		}
		cout<<"\n";
	}

}


/*NAME: SANYAM GARG
INSTITUTION : DELHI TECHNOLOGICAL UNIVERSITY*/
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<bits/stdc++.h>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<bitset>
#define mod 1000000007
#define li long long int
#define ll int
#define readi(x) scanf("%d",&x)
#define  reads(x)  scanf("%s",&x)
#define readl(x) scanf("%I64d",&x)
#define rep(i,n) for(i=0;i<n;i++)
#define revp(i,n) for(i=(n-1);i>=0;i--)
#define myrep1(i,a,b) for(i=a;i<=b;i++)
#define myrep2(i,a,b) for(i=b;i>=a;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define sec second
#define MAXN 1000000000
#define MINN -1000000000000
#define pii pair<int,int>
#define N 100010
#define ran rank
using namespace std;
ll n,m,deb=1,flag=0;
map<pair<ll,ll> ,ll> dist;
vector<list<pair<ll,ll> > > diff(N);
vector<list<pair<ll,ll> > > sum(N);
pii up(pii cur,ll s)
{
	if(s==0)
	return mp(m-cur.sec+cur.fi,m);
	else
	return mp(cur.sec+cur.fi-m,m);

}
pii right(pii cur,ll s)
{
	if(s==0)
	return mp(n,n+cur.sec-cur.fi);
	else
	return mp(n,cur.sec+cur.fi-n);
}
pii down(pii cur,ll s)
{
	if(s==0)
	return mp(cur.fi-cur.sec,0);
	else
	return mp(cur.fi+cur.sec,0);
}
pii left(pii cur,ll s)
{
	if(s==0)
	return mp(0,cur.sec-cur.fi);
	else
	return mp(0,cur.fi+cur.sec);
}
int main()
{
	ll k,i;
	cin>>n>>m>>k;
	ll a[k],b[k];
    rep(i,k)
    {
    	cin>>a[i]>>b[i];
    	diff[abs(a[i]-b[i])].pb(mp(a[i],b[i]));
        sum[abs(a[i]+b[i])].pb(mp(a[i],b[i]));

	}
	ll sl=0;//slope!!!
	pair<ll,ll> st,en;
	st.fi=0;
	st.sec=0;
	char c='d';
	ll t=0;
	while(!(((st.fi==0)&&(st.sec==(m)))||((st.fi==n)&&(st.sec==(0)))||((st.fi==n)&&(st.sec==(m)))))
    {
//   	cout<<"i";
    	if(sl%2==0)
    	{
    		//increase both x and y whichever reaches n and m respectively first will give
    		if(c=='d')
    		{
    			if((n-st.fi)>=(m-st.sec))
    			{
    				en=up(st,sl%2);
    				c='u';
				}
				else
				{
					en=right(st,sl%2);
					c='r';
				}
//				cout<<c<<endl;
			}
			else if(c=='r')
    		{
    			if((st.fi)>=(st.sec))
    			{
    				en=down(st,sl%2);
    				c='d';
				}
				else
				{
					en=left(st,sl%2);
					c='l';
				}
			}
				else if(c=='u')
    		{
    			if((st.fi)>=(st.sec))
    			{
    				en=down(st,sl%2);
    				c='d';
				}
				else
				{
					en=left(st,sl%2);
					c='l';
				}
			}
				else if(c=='l')
    		{
    			if((n-st.fi)>=(m-st.sec))
    			{
    				en=up(st,sl%2);
    				c='u';
				}
				else
				{
					en=right(st,sl%2);
					c='r';
				}
			}
    	}
		else
		{
			if(c=='d')
    		{
    			if((st.fi)>=(m-st.sec))
    			{
    				en=up(st,sl%2);
    				c='u';
				}
				else
				{
					en=left(st,sl%2);
					c='l';
				}
			}
			else if(c=='r')
    		{
    			if((st.fi)>=(m-st.sec))
    			{
    				en=up(st,sl%2);
    				c='u';
				}
				else
				{
					en=left(st,sl%2);
					c='l';
				}
			}
				else if(c=='u')
    		{
    			if((n-st.fi)>=(st.sec))
    			{
    				en=down(st,sl%2);
    				c='d';
				}
				else
				{
					en=right(st,sl%2);
					c='r';
				}
			}
				else if(c=='l')
    		{
    			if((n-st.fi)>=(st.sec))
    			{
    				en=down(st,sl%2);
    				c='d';
				}
				else
				{
					en=right(st,sl%2);
					c='r';
				}
			}
		}
    	if(sl%2==0)
    	{
    		//diff
//    		cout<<"hi";
    		ll p=st.sec-st.fi;
    		for(list<pair<ll,ll> > ::iterator it=diff[abs(p)].begin();it!=diff[abs(p)].end();it++)
    		{
    			if((it->sec-it->fi)==p)
    			{

    			if(dist[mp(it->fi,it->sec)]==0)
    			{
    				dist[mp(it->fi,it->sec)]=t+abs(it->fi-st.fi);
				}

			    }
			}

		}
		else
		{
//			cout<<"hell";
				ll p=st.sec+st.fi;
    		for(list<pair<ll,ll> > ::iterator it=sum[abs(p)].begin();it!=sum[abs(p)].end();it++)
    		{
    			if((it->sec+it->fi)==p)
    			{

    			if(dist[mp(it->fi,it->sec)]==0)
    			{
    				dist[mp(it->fi,it->sec)]=t+abs(it->fi-st.fi);
				}

			    }
			}
		}
		t+=abs(en.first-st.fi);
//		cout<<t<<endl;
		st=en;
//		while(deb--)
//		{
//
//
//		cout<<en.first<<" "<<en.sec<<"\n";
//		flag=1;
//		break;
//         }
//         if(flag)
//         {
//         	break;
//		 }
    	sl++;
	}
	for(i=0;i<k;i++)
	{
		if(dist[mp(a[i],b[i])]==0)
		{
			cout<<"-1";
		}
		else
		{
			cout<<dist[mp(a[i],b[i])];
		}
		cout<<"\n";
	}

}


/*NAME: SANYAM GARG
INSTITUTION : DELHI TECHNOLOGICAL UNIVERSITY*/
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<bits/stdc++.h>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<bitset>
#define mod 1000000007
#define li long long int
#define ll long long int
#define readi(x) scanf("%d",&x)
#define  reads(x)  scanf("%s",&x)
#define readl(x) scanf("%I64d",&x)
#define rep(i,n) for(i=0;i<n;i++)
#define revp(i,n) for(i=(n-1);i>=0;i--)
#define myrep1(i,a,b) for(i=a;i<=b;i++)
#define myrep2(i,a,b) for(i=b;i>=a;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define sec second
#define MAXN 1000000000
#define MINN -1000000000000
#define pii pair<long long int,long long int>
#define N 100010
#define ran rank
using namespace std;
ll n,m,deb=1,flag=0;
map<pair<ll,ll> ,ll> dist;
vector<list<pair<ll,ll> > > diff(N);
vector<list<pair<ll,ll> > > sum(N);
pii up(pii cur,ll s)
{
	if(s==0)
	return mp(m-cur.sec+cur.fi,m);
	else
	return mp(cur.sec+cur.fi-m,m);
}
pii right(pii cur,ll s)
{
	if(s==0)
	return mp(n,n+cur.sec-cur.fi);
	else
	return mp(n,cur.sec+cur.fi-n);
}
pii down(pii cur,ll s)
{
	if(s==0)
	return mp(cur.fi-cur.sec,0);
	else
	return mp(cur.fi+cur.sec,0);
}
pii left(pii cur,ll s)
{
	if(s==0)
	return mp(0,cur.sec-cur.fi);
	else
	return mp(0,cur.fi+cur.sec);
}
int main()
{
	ll k,i;
	cin>>n>>m>>k;
	ll a[k],b[k];
    rep(i,k)
    {
    	cin>>a[i]>>b[i];
    	diff[abs(a[i]-b[i])].pb(mp(a[i],b[i]));
        sum[abs(a[i]+b[i])].pb(mp(a[i],b[i]));

	}
	ll sl=0;//slope!!!
	pair<ll,ll> st,en;
	st.fi=0;
	st.sec=0;
	char c='d';
	ll t=0;
	while(!(((st.fi==0)&&(st.sec==(m)))||((st.fi==n)&&(st.sec==(0)))||((st.fi==n)&&(st.sec==(m)))))
    {
//   	cout<<"i";
    	if(sl%2==0)
    	{
    		//increase both x and y whichever reaches n and m respectively first will give
    		if(c=='d')
    		{
    			if((n-st.fi)>=(m-st.sec))
    			{
    				en=up(st,sl%2);
    				c='u';
				}
				else
				{
					en=right(st,sl%2);
					c='r';
				}
//				cout<<c<<endl;
			}
			else if(c=='r')
    		{
    			if((st.fi)>=(st.sec))
    			{
    				en=down(st,sl%2);
    				c='d';
				}
				else
				{
					en=left(st,sl%2);
					c='l';
				}
			}
				else if(c=='u')
    		{
    			if((st.fi)>=(st.sec))
    			{
    				en=down(st,sl%2);
    				c='d';
				}
				else
				{
					en=left(st,sl%2);
					c='l';
				}
			}
				else if(c=='l')
    		{
    			if((n-st.fi)>=(m-st.sec))
    			{
    				en=up(st,sl%2);
    				c='u';
				}
				else
				{
					en=right(st,sl%2);
					c='r';
				}
			}
    	}
		else
		{
			if(c=='d')
    		{
    			if((st.fi)>=(m-st.sec))
    			{
    				en=up(st,sl%2);
    				c='u';
				}
				else
				{
					en=left(st,sl%2);
					c='l';
				}
			}
			else if(c=='r')
    		{
    			if((st.fi)>=(m-st.sec))
    			{
    				en=up(st,sl%2);
    				c='u';
				}
				else
				{
					en=left(st,sl%2);
					c='l';
				}
			}
				else if(c=='u')
    		{
    			if((n-st.fi)>=(st.sec))
    			{
    				en=down(st,sl%2);
    				c='d';
				}
				else
				{
					en=right(st,sl%2);
					c='r';
				}
			}
				else if(c=='l')
    		{
    			if((n-st.fi)>=(st.sec))
    			{
    				en=down(st,sl%2);
    				c='d';
				}
				else
				{
					en=right(st,sl%2);
					c='r';
				}
			}
		}
    	if(sl%2==0)
    	{
    		//diff
//    		cout<<"hi";
    		ll p=st.sec-st.fi;
    		for(list<pair<ll,ll> > ::iterator it=diff[abs(p)].begin();it!=diff[abs(p)].end();it++)
    		{
    			if((it->sec-it->fi)==p)
    			{

    			if(dist[mp(it->fi,it->sec)]==0)
    			{
    				dist[mp(it->fi,it->sec)]=t+abs(it->fi-st.fi);
				}

			    }
			}

		}
		else
		{
//			cout<<"hell";
				ll p=st.sec+st.fi;
    		for(list<pair<ll,ll> > ::iterator it=sum[abs(p)].begin();it!=sum[abs(p)].end();it++)
    		{
    			if((it->sec+it->fi)==p)
    			{

    			if(dist[mp(it->fi,it->sec)]==0)
    			{
    				dist[mp(it->fi,it->sec)]=t+abs(it->fi-st.fi);
				}

			    }
			}
		}
		t+=abs(en.first-st.fi);
//		cout<<t<<endl;
		st=en;
		if(st.first==0&&st.sec==0)
		{
			break;
		}
//		cout<<en.first<<" "<<en.sec<<endl;;
//		while(deb--)
//		{
//
//
//		cout<<en.first<<" "<<en.sec<<"\n";
//		flag=1;
//		break;
//         }
//         if(flag)
//         {
//         	break;
//		 }
    	sl++;
	}
	for(i=0;i<k;i++)
	{
		if(dist[mp(a[i],b[i])]==0)
		{
			cout<<"-1";
		}
		else
		{
			cout<<dist[mp(a[i],b[i])];
		}
		cout<<"\n";
	}

}


/*NAME: SANYAM GARG
INSTITUTION : DELHI TECHNOLOGICAL UNIVERSITY*/
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<bits/stdc++.h>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<bitset>
#define mod 1000000007
#define li long long int
#define ll long long int
#define readi(x) scanf("%d",&x)
#define  reads(x)  scanf("%s",&x)
#define readl(x) scanf("%I64d",&x)
#define rep(i,n) for(i=0;i<n;i++)
#define revp(i,n) for(i=(n-1);i>=0;i--)
#define myrep1(i,a,b) for(i=a;i<=b;i++)
#define myrep2(i,a,b) for(i=b;i>=a;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define sec second
#define MAXN 1000000000
#define MINN -1000000000000
#define pii pair<long long int,long long int>
#define N 100010
#define ran rank
using namespace std;
ll n,m,deb=1,flag=0;
map<pair<ll,ll> ,ll> dist;
vector<list<pair<ll,ll> > > diff(N);
vector<list<pair<ll,ll> > > sum(N);
pii up(pii cur,ll s)
{
	if(s==0)
	return mp(m-cur.sec+cur.fi,m);
	else
	return mp(cur.sec+cur.fi-m,m);
}
pii right(pii cur,ll s)
{
	if(s==0)
	return mp(n,n+cur.sec-cur.fi);
	else
	return mp(n,cur.sec+cur.fi-n);
}
pii down(pii cur,ll s)
{
	if(s==0)
	return mp(cur.fi-cur.sec,0);
	else
	return mp(cur.fi+cur.sec,0);
}
pii left(pii cur,ll s)
{
	if(s==0)
	return mp(0,cur.sec-cur.fi);
	else
	return mp(0,cur.fi+cur.sec);
}
int main()
{
	ll k,i;
	cin>>n>>m>>k;
	ll a[k],b[k];
    rep(i,k)
    {
    	cin>>a[i]>>b[i];
    	diff[abs(a[i]-b[i])].pb(mp(a[i],b[i]));
        sum[abs(a[i]+b[i])].pb(mp(a[i],b[i]));

	}
	ll sl=0;//slope!!!
	pair<ll,ll> st,en;
	st.fi=0;
	st.sec=0;
	char c='d';
	ll t=0;
	while(!(((st.fi==0)&&(st.sec==(m)))||((st.fi==n)&&(st.sec==(0)))||((st.fi==n)&&(st.sec==(m)))))
    {
//   	cout<<"i";
    	if(sl%2==0)
    	{
    		//increase both x and y whichever reaches n and m respectively first will give
    		if(c=='d')
    		{
    			if((n-st.fi)>=(m-st.sec))
    			{
    				en=up(st,sl%2);
    				c='u';
				}
				else
				{
					en=right(st,sl%2);
					c='r';
				}
//				cout<<c<<endl;
			}
			else if(c=='r')
    		{
    			if((st.fi)>=(st.sec))
    			{
    				en=down(st,sl%2);
    				c='d';
				}
				else
				{
					en=left(st,sl%2);
					c='l';
				}
			}
				else if(c=='u')
    		{
    			if((st.fi)>=(st.sec))
    			{
    				en=down(st,sl%2);
    				c='d';
				}
				else
				{
					en=left(st,sl%2);
					c='l';
				}
			}
				else if(c=='l')
    		{
    			if((n-st.fi)>=(m-st.sec))
    			{
    				en=up(st,sl%2);
    				c='u';
				}
				else
				{
					en=right(st,sl%2);
					c='r';
				}
			}
    	}
		else
		{
			if(c=='d')
    		{
    			if((st.fi)>=(m-st.sec))
    			{
    				en=up(st,sl%2);
    				c='u';
				}
				else
				{
					en=left(st,sl%2);
					c='l';
				}
			}
			else if(c=='r')
    		{
    			if((st.fi)>=(m-st.sec))
    			{
    				en=up(st,sl%2);
    				c='u';
				}
				else
				{
					en=left(st,sl%2);
					c='l';
				}
			}
				else if(c=='u')
    		{
    			if((n-st.fi)>=(st.sec))
    			{
    				en=down(st,sl%2);
    				c='d';
				}
				else
				{
					en=right(st,sl%2);
					c='r';
				}
			}
				else if(c=='l')
    		{
    			if((n-st.fi)>=(st.sec))
    			{
    				en=down(st,sl%2);
    				c='d';
				}
				else
				{
					en=right(st,sl%2);
					c='r';
				}
			}
		}
    	if(sl%2==0)
    	{
    		//diff
//    		cout<<"hi";
    		ll p=st.sec-st.fi;
    		for(list<pair<ll,ll> > ::iterator it=diff[abs(p)].begin();it!=diff[abs(p)].end();it++)
    		{
    			if((it->sec-it->fi)==p)
    			{

    			if(dist[mp(it->fi,it->sec)]==0)
    			{
    				dist[mp(it->fi,it->sec)]=t+abs(it->fi-st.fi);
				}

			    }
			}

		}
		else
		{
//			cout<<"hell";
				ll p=st.sec+st.fi;
    		for(list<pair<ll,ll> > ::iterator it=sum[abs(p)].begin();it!=sum[abs(p)].end();it++)
    		{
    			if((it->sec+it->fi)==p)
    			{

    			if(dist[mp(it->fi,it->sec)]==0)
    			{
    				dist[mp(it->fi,it->sec)]=t+abs(it->fi-st.fi);
				}

			    }
			}
		}
		t+=abs(en.first-st.fi);
//		cout<<t<<endl;
		st=en;
		if(st.first==0&&st.sec==0)
		{
			break;
		}
//		cout<<en.first<<" "<<en.sec<<endl;;
//		while(deb--)
//		{
//
//
//		cout<<en.first<<" "<<en.sec<<"\n";
//		flag=1;
//		break;
//         }
//         if(flag)
//         {
//         	break;
//		 }
//    	sl++;
	}
	for(i=0;i<k;i++)
	{
		if(dist[mp(a[i],b[i])]==0)
		{
			cout<<"-1";
		}
		else
		{
			cout<<dist[mp(a[i],b[i])];
		}
		cout<<"\n";
	}

}


#include <bits/stdc++.h>
using namespace std;
#define lp(i,n) for (int i = 0; i < int(n); ++i)
#define lp1(i,n) for (int i = 1; i <= int(n); ++i)
#define eprintf(...)
#ifdef ONLINE_JUDGE
#else
#define eprintf(...) fprintf(stderr,__VA_ARGS__);fflush(stderr);
#endif // ONLINE_JUDGE
typedef long long ll;
typedef double dbl;
const int inf = 1e9+7;
const ll llinf = 1e18+7;
const dbl eps = 1e-12;
int W,H,n;
typedef pair<int,int> P;
#define X first
#define Y second
const int N = 1e5+10;
map<pair<P,P>,vector<int> > f;

int ans[N];
P a[N];
enum{upright,upleft,downright,downleft};
int G[2][2] = {{downleft,upleft},{downright,upright}};
P go(P p,int dir){
    if(dir == downleft){
        int mn = min(p.X,p.Y);
        p.X -= mn,p.Y -= mn;
        return p;
    }
    if(dir == downright){
        int mn = min(W-p.X,p.Y);
        p.X += mn,p.Y -= mn;
        return p;
    }
    if(dir == upright){
        int mn = min(W-p.X,H-p.Y);
        p.X += mn,p.Y += mn;
        return p;
    }
    if(dir == upleft){
        int mn = min(p.X,H-p.Y);
        p.X -= mn,p.Y += mn;
        return p;
    }
    assert(0);
}

unordered_set<ll> vis;
ll h(P p,int dir){
    return p.X*1ll*N*4+p.Y*4+dir;
}
void dfs(P p,int dir,int tim,int first = 0){
//    cerr << p.X <<" "<<p.Y<<endl;
    if((p.X == 0 || p.X == W) && (p.Y == 0 || p.Y == H) && !first)return;
    if (vis.count(h(p,dir)))return;
    vis.insert(h(p,dir));
//    if(vis.count({p,dir}))return;
//    vis.insert({p,dir});
    P p2 = go(p,dir);
    pair<P,P> seg1 = {p,p2};
    if(p > p2)swap(seg1.first,seg1.second);
    if(f.count(seg1)){
        for (int idx: f[seg1]){
            int res  = tim + min(abs(a[idx].X-p.X),abs(a[idx].Y-p.Y));
            ans[idx] = min(ans[idx],res);
        }
        f.erase(seg1);
    }

    int isu = 0,isr = 0;
    if(dir == upright || dir == upleft)isu = 1;

    if(dir == downright || dir == upright)isr = 1;

    if(p2.X == 0 || p2.X == W)isr ^= 1;

    if(p2.Y == 0 || p2.Y == H)isu ^= 1;
    dfs(p2,G[isr][isu],tim + min(abs(p.X-p2.X),abs(p.Y-p2.Y)));

}
int main(){
    scanf("%d%d%d",&W,&H,&n);

//    for(P p;cin >> p.X >> p.Y;)
//    {
//        auto pp = go(p,upright);
//        cerr <<pp.X <<" "<<pp.Y<<endl;
//        pp = go(p,downleft);
//        cerr <<pp.X <<" "<<pp.Y<<endl;
//        pp = go(p,upleft);
//        cerr <<pp.X <<" "<<pp.Y<<endl;
//        pp = go(p,downright);
//        cerr <<pp.X <<" "<<pp.Y<<endl;
//    }

    lp(i,n){
        scanf("%d%d",&a[i].X,&a[i].Y);
        pair<P,P> major = {go(a[i],downleft),go(a[i],upright)};
        pair<P,P> minor = {go(a[i],upleft),go(a[i],downright)};
        f[major].push_back(i);
        f[minor].push_back(i);
        ans[i] = inf;
    }

    dfs({0,0},upright,0,1);
    lp(i,n)if(ans[i] == inf)printf("-1\n");else printf("%d\n",ans[i]);
}


#include <bits/stdc++.h>
using namespace std;
#define lp(i,n) for (int i = 0; i < int(n); ++i)
#define lp1(i,n) for (int i = 1; i <= int(n); ++i)
#define eprintf(...)
#ifdef ONLINE_JUDGE
#else
#define eprintf(...) fprintf(stderr,__VA_ARGS__);fflush(stderr);
#endif // ONLINE_JUDGE
typedef long long ll;
typedef double dbl;
const int inf = 1e9+7;
const ll llinf = 1e18+7;
const dbl eps = 1e-12;
int W,H,n;
typedef pair<int,int> P;
#define X first
#define Y second
const int N = 1e5+10;
map<pair<P,P>,set<int> > f;
set<pair<P,int> > vis;
int ans[N];
P a[N];
enum{upright,upleft,downright,downleft};
int G[2][2] = {{downleft,upleft},{downright,upright}};
P go(P p,int dir){
    if(dir == downleft){
        int mn = min(p.X,p.Y);
        p.X -= mn,p.Y -= mn;
        return p;
    }
    if(dir == downright){
        int mn = min(W-p.X,p.Y);
        p.X += mn,p.Y -= mn;
        return p;
    }
    if(dir == upright){
        int mn = min(W-p.X,H-p.Y);
        p.X += mn,p.Y += mn;
        return p;
    }
    if(dir == upleft){
        int mn = min(p.X,H-p.Y);
        p.X -= mn,p.Y += mn;
        return p;
    }
    assert(0);
}
void dfs(P p,int dir,int tim){
    cerr << p.X <<" "<<p.Y<<endl;
    if((p.X == 0 || p.X == W) && (p.Y == 0 || p.Y == H) && !(p == make_pair(0,0) && (dir == upright)) )return;
    if(vis.count({p,dir}))return;
    vis.insert({p,dir});
    P p2 = go(p,dir);
    pair<P,P> seg1 = {p,p2};
    pair<P,P> seg2 = {p2,p};
    if(f.count(seg1)){
        for (int idx: f[seg1]){
            int res  = tim + min(abs(a[idx].X-p.X),abs(a[idx].Y-p.Y));
            ans[idx] = min(ans[idx],res);
        }
        f.erase(seg1);
    }
    if(f.count(seg2)){
        for (int idx: f[seg2]){
            int res  = tim + min(abs(a[idx].X-p.X),abs(a[idx].Y-p.Y));
            ans[idx] = min(ans[idx],res);
        }
        f.erase(seg2);
    }

    int isu = 0,isr = 0;
    if(dir == upright || dir == upleft)isu = 1;

    if(dir == downright || dir == upright)isr = 1;

    if(p2.X == 0 || p2.X == W)isr ^= 1;

    if(p2.Y == 0 || p2.Y == H)isu ^= 1;
    dfs(p2,G[isr][isu],tim + min(abs(p.X-p2.X),abs(p.Y-p2.Y)));

}
int main(){
    scanf("%d%d%d",&W,&H,&n);

//    for(P p;cin >> p.X >> p.Y;)
//    {
//        auto pp = go(p,upright);
//        cerr <<pp.X <<" "<<pp.Y<<endl;
//        pp = go(p,downleft);
//        cerr <<pp.X <<" "<<pp.Y<<endl;
//        pp = go(p,upleft);
//        cerr <<pp.X <<" "<<pp.Y<<endl;
//        pp = go(p,downright);
//        cerr <<pp.X <<" "<<pp.Y<<endl;
//    }
    lp(i,n){
        scanf("%d%d",&a[i].X,&a[i].Y);
        pair<P,P> major = {go(a[i],downleft),go(a[i],upright)};
        pair<P,P> minor = {go(a[i],downright),go(a[i],upleft)};
        f[major].insert(i);
        f[minor].insert(i);
        ans[i] = inf;
//        cerr<<major.first.X << " "<<major.first.Y <<" "<<major.second.X <<" "<<major.second.Y<<endl;
//        cerr<<minor.first <<" "<<minor.second<<endl;
    }

    dfs({0,0},upright,0);
    lp(i,n)if(ans[i] == inf)printf("-1\n");else printf("%d\n",ans[i]);
}


#include <bits/stdc++.h>
using namespace std;
#define lp(i,n) for (int i = 0; i < int(n); ++i)
#define lp1(i,n) for (int i = 1; i <= int(n); ++i)
#define eprintf(...)
#ifdef ONLINE_JUDGE
#else
#define eprintf(...) fprintf(stderr,__VA_ARGS__);fflush(stderr);
#endif // ONLINE_JUDGE
typedef long long ll;
typedef double dbl;
const int inf = 1e9+7;
const ll llinf = 1e18+7;
const dbl eps = 1e-12;
int W,H,n;
typedef pair<int,int> P;
#define X first
#define Y second
const int N = 1e5+10;
map<pair<P,P>,vector<int> > f;

ll ans[N];
P a[N];
enum{upright,upleft,downright,downleft};
int G[2][2] = {{downleft,upleft},{downright,upright}};
P go(P p,int dir){
    if(dir == downleft){
        int mn = min(p.X,p.Y);
        p.X -= mn,p.Y -= mn;
        return p;
    }
    if(dir == downright){
        int mn = min(W-p.X,p.Y);
        p.X += mn,p.Y -= mn;
        return p;
    }
    if(dir == upright){
        int mn = min(W-p.X,H-p.Y);
        p.X += mn,p.Y += mn;
        return p;
    }
    if(dir == upleft){
        int mn = min(p.X,H-p.Y);
        p.X -= mn,p.Y += mn;
        return p;
    }
    assert(0);
}

unordered_set<ll> vis;
ll h(P p,int dir){
    return p.X*1ll*N*4+p.Y*4+dir;
}
void dfs(P p,int dir,ll tim,int first = 0){
    if((p.X == 0 || p.X == W) && (p.Y == 0 || p.Y == H) && !first)return;
    if (vis.count(h(p,dir)))return;
    vis.insert(h(p,dir));
    P p2 = go(p,dir);
    pair<P,P> seg1 = {p,p2};
    if(p > p2)swap(seg1.first,seg1.second);
    if(f.count(seg1)){
        for (int idx: f[seg1]){
            ll res  = tim + min(abs(a[idx].X-p.X),abs(a[idx].Y-p.Y));
            ans[idx] = min(ans[idx],res);
        }
        f.erase(seg1);
    }

    int isu = 0,isr = 0;
    if(dir == upright || dir == upleft)isu = 1;

    if(dir == downright || dir == upright)isr = 1;

    if(p2.X == 0 || p2.X == W)isr ^= 1;

    if(p2.Y == 0 || p2.Y == H)isu ^= 1;
    dfs(p2,G[isr][isu],tim + min(abs(p.X-p2.X),abs(p.Y-p2.Y)));

}
int main(){
    scanf("%d%d%d",&W,&H,&n);

//    for(P p;cin >> p.X >> p.Y;)
//    {
//        auto pp = go(p,upright);
//        cerr <<pp.X <<" "<<pp.Y<<endl;
//        pp = go(p,downleft);
//        cerr <<pp.X <<" "<<pp.Y<<endl;
//        pp = go(p,upleft);
//        cerr <<pp.X <<" "<<pp.Y<<endl;
//        pp = go(p,downright);
//        cerr <<pp.X <<" "<<pp.Y<<endl;
//    }

    lp(i,n){
        scanf("%d%d",&a[i].X,&a[i].Y);
        pair<P,P> major = {go(a[i],downleft),go(a[i],upright)};
        pair<P,P> minor = {go(a[i],upleft),go(a[i],downright)};
        f[major].push_back(i);
        f[minor].push_back(i);
        ans[i] = llinf;
    }

    dfs({0,0},upright,0,1);
    lp(i,n)if(ans[i] == llinf)printf("-1\n");else printf("%d\n",ans[i]);
}


#include <bits/stdc++.h>
using namespace std;
#define lp(i,n) for (int i = 0; i < int(n); ++i)
#define lp1(i,n) for (int i = 1; i <= int(n); ++i)
#define eprintf(...)
#ifdef ONLINE_JUDGE
#else
#define eprintf(...) fprintf(stderr,__VA_ARGS__);fflush(stderr);
#endif // ONLINE_JUDGE
typedef long long ll;
typedef double dbl;
const int inf = 1e9+7;
const ll llinf = 1e18+7;
const dbl eps = 1e-12;
int W,H,n;
typedef pair<int,int> P;
#define X first
#define Y second
const int N = 1e5+10;
map<pair<P,P>,vector<int> > f;
set<pair<P,int> > vis;
int ans[N];
P a[N];
enum{upright,upleft,downright,downleft};
int G[2][2] = {{downleft,upleft},{downright,upright}};
P go(P p,int dir){
    if(dir == downleft){
        int mn = min(p.X,p.Y);
        p.X -= mn,p.Y -= mn;
        return p;
    }
    if(dir == downright){
        int mn = min(W-p.X,p.Y);
        p.X += mn,p.Y -= mn;
        return p;
    }
    if(dir == upright){
        int mn = min(W-p.X,H-p.Y);
        p.X += mn,p.Y += mn;
        return p;
    }
    if(dir == upleft){
        int mn = min(p.X,H-p.Y);
        p.X -= mn,p.Y += mn;
        return p;
    }
    assert(0);
}

void dfs(P p,int dir,int tim,int first = 0){
//    cerr << p.X <<" "<<p.Y<<endl;
    if((p.X == 0 || p.X == W) && (p.Y == 0 || p.Y == H) && !first)return;

//    if(vis.count({p,dir}))return;
//    vis.insert({p,dir});
    P p2 = go(p,dir);
    pair<P,P> seg1 = {p,p2};
    if(p > p2)swap(seg1.first,seg1.second);
    if(f.count(seg1)){
        for (int idx: f[seg1]){
            int res  = tim + min(abs(a[idx].X-p.X),abs(a[idx].Y-p.Y));
            ans[idx] = min(ans[idx],res);
        }
        f.erase(seg1);
    }

    int isu = 0,isr = 0;
    if(dir == upright || dir == upleft)isu = 1;

    if(dir == downright || dir == upright)isr = 1;

    if(p2.X == 0 || p2.X == W)isr ^= 1;

    if(p2.Y == 0 || p2.Y == H)isu ^= 1;
    dfs(p2,G[isr][isu],tim + min(abs(p.X-p2.X),abs(p.Y-p2.Y)));

}
int main(){
    scanf("%d%d%d",&W,&H,&n);

//    for(P p;cin >> p.X >> p.Y;)
//    {
//        auto pp = go(p,upright);
//        cerr <<pp.X <<" "<<pp.Y<<endl;
//        pp = go(p,downleft);
//        cerr <<pp.X <<" "<<pp.Y<<endl;
//        pp = go(p,upleft);
//        cerr <<pp.X <<" "<<pp.Y<<endl;
//        pp = go(p,downright);
//        cerr <<pp.X <<" "<<pp.Y<<endl;
//    }

    lp(i,n){
        scanf("%d%d",&a[i].X,&a[i].Y);
        pair<P,P> major = {go(a[i],downleft),go(a[i],upright)};
        pair<P,P> minor = {go(a[i],upleft),go(a[i],downright)};
        f[major].push_back(i);
        f[minor].push_back(i);
        ans[i] = inf;
    }

    dfs({0,0},upright,0,1);
    lp(i,n)if(ans[i] == inf)printf("-1\n");else printf("%d\n",ans[i]);
}


#include <bits/stdc++.h>
using namespace std;
#define lp(i,n) for (int i = 0; i < int(n); ++i)
#define lp1(i,n) for (int i = 1; i <= int(n); ++i)
#define eprintf(...)
#ifdef ONLINE_JUDGE
#else
#define eprintf(...) fprintf(stderr,__VA_ARGS__);fflush(stderr);
#endif // ONLINE_JUDGE
typedef long long ll;
typedef double dbl;
const int inf = 1e9+7;
const ll llinf = 1e18+7;
const dbl eps = 1e-12;
int W,H,n;
typedef pair<int,int> P;
#define X first
#define Y second
const int N = 1e5+10;
map<pair<P,P>,vector<int> > f;

ll ans[N];
P a[N];
enum{upright,upleft,downright,downleft};
int G[2][2] = {{downleft,upleft},{downright,upright}};
P go(P p,int dir){
    if(dir == downleft){
        int mn = min(p.X,p.Y);
        p.X -= mn,p.Y -= mn;
        return p;
    }
    if(dir == downright){
        int mn = min(W-p.X,p.Y);
        p.X += mn,p.Y -= mn;
        return p;
    }
    if(dir == upright){
        int mn = min(W-p.X,H-p.Y);
        p.X += mn,p.Y += mn;
        return p;
    }
    if(dir == upleft){
        int mn = min(p.X,H-p.Y);
        p.X -= mn,p.Y += mn;
        return p;
    }
    assert(0);
}

unordered_set<ll> vis;
ll h(P p,int dir){
    return p.X*1ll*N*4+p.Y*4+dir;
}
void dfs(P p,int dir,ll tim,int first = 0){
    if((p.X == 0 || p.X == W) && (p.Y == 0 || p.Y == H) && !first)return;
    if (vis.count(h(p,dir)))return;
    vis.insert(h(p,dir));
    P p2 = go(p,dir);
    pair<P,P> seg1 = {p,p2};
    if(p > p2)swap(seg1.first,seg1.second);
    if(f.count(seg1)){
        for (int idx: f[seg1]){
            ll res  = tim + min(abs(a[idx].X-p.X),abs(a[idx].Y-p.Y));
            ans[idx] = min(ans[idx],res);
        }
        f.erase(seg1);
    }

    int isu = 0,isr = 0;
    if(dir == upright || dir == upleft)isu = 1;

    if(dir == downright || dir == upright)isr = 1;

    if(p2.X == 0 || p2.X == W)isr ^= 1;

    if(p2.Y == 0 || p2.Y == H)isu ^= 1;
    dfs(p2,G[isr][isu],tim + min(abs(p.X-p2.X),abs(p.Y-p2.Y)));

}
int main(){
    scanf("%d%d%d",&W,&H,&n);

//    for(P p;cin >> p.X >> p.Y;)
//    {
//        auto pp = go(p,upright);
//        cerr <<pp.X <<" "<<pp.Y<<endl;
//        pp = go(p,downleft);
//        cerr <<pp.X <<" "<<pp.Y<<endl;
//        pp = go(p,upleft);
//        cerr <<pp.X <<" "<<pp.Y<<endl;
//        pp = go(p,downright);
//        cerr <<pp.X <<" "<<pp.Y<<endl;
//    }

    lp(i,n){
        scanf("%d%d",&a[i].X,&a[i].Y);
        pair<P,P> major = {go(a[i],downleft),go(a[i],upright)};
        pair<P,P> minor = {go(a[i],upleft),go(a[i],downright)};
        f[major].push_back(i);
        f[minor].push_back(i);
        ans[i] = llinf;
    }

    dfs({0,0},upright,0,1);
    lp(i,n)if(ans[i] == llinf)printf("-1\n");else printf("%lld\n",ans[i]);
}


#include <bits/stdc++.h>
using namespace std;
#define lp(i,n) for (int i = 0; i < int(n); ++i)
#define lp1(i,n) for (int i = 1; i <= int(n); ++i)
#define eprintf(...)
#ifdef ONLINE_JUDGE
#else
#define eprintf(...) fprintf(stderr,__VA_ARGS__);fflush(stderr);
#endif // ONLINE_JUDGE
typedef long long ll;
typedef double dbl;
const int inf = 1e9+7;
const ll llinf = 1e18+7;
const dbl eps = 1e-12;
int W,H,n;
typedef pair<int,int> P;
#define X first
#define Y second
const int N = 1e5+10;
map<pair<P,P>,set<int> > f;
set<pair<P,int> > vis;
int ans[N];
P a[N];
enum{upright,upleft,downright,downleft};
int G[2][2] = {{downleft,upleft},{downright,upright}};
P go(P p,int dir){
    if(dir == downleft){
        int mn = min(p.X,p.Y);
        p.X -= mn,p.Y -= mn;
        return p;
    }
    if(dir == downright){
        int mn = min(p.X,H-p.Y);
        p.X -= mn,p.Y += mn;
        return p;
    }
    if(dir == upright){
        int mn = min(W-p.X,H-p.Y);
        p.X += mn,p.Y += mn;
        return p;
    }
    if(dir == upleft){
        int mn = min(p.X,H-p.Y);
        p.X -= mn,p.Y += mn;
        return p;
    }
    assert(0);
}
void dfs(P p,int dir,int tim){
    if(vis.count({p,dir}))return;
    vis.insert({p,dir});
    P p2 = go(p,dir);
    pair<P,P> seg1 = {p,p2};
    pair<P,P> seg2 = {p2,p};
    if(f.count(seg1)){
        for (int idx: f[seg1]){
            int res  = tim + min(abs(a[idx].X-p.X),abs(a[idx].Y-p.Y));
            ans[idx] = min(ans[idx],res);
        }
        f.erase(seg1);
    }
    if(f.count(seg2)){
        for (int idx: f[seg2]){
            int res  = tim + min(abs(a[idx].X-p.X),abs(a[idx].Y-p.Y));
            ans[idx] = min(ans[idx],res);
        }
        f.erase(seg2);
    }

    int isu = 0,isr = 0;
    if(dir == upright || dir == upleft)isu = 1;
    if(dir == downright || dir == upright)isr = 1;
    if(p2.X == 0 || p2.X == W)isr ^= 1;
    if(p2.Y == 0 || p2.Y == H)isu ^= 1;
    dfs(p2,G[isr][isu],tim + min(abs(p.X-p2.X),abs(p.Y-p2.Y)));

}
int main(){
    scanf("%d%d%d",&W,&H,&n);
    lp(i,n){
        scanf("%d%d",&a[i].X,&a[i].Y);
        pair<P,P> major = {go(a[i],downleft),go(a[i],upright)};
        pair<P,P> minor = {go(a[i],downright),go(a[i],upleft)};
        f[major].insert(i);
        f[minor].insert(i);
        ans[i] = inf;
    }

    dfs({0,0},upright,0);
    lp(i,n)if(ans[i] == inf)printf("-1\n");else printf("%d\n",ans[i]);
}


#include <stdio.h>
#include <iostream>
#include <vector>
#include <string.h>
#include <set>
#define LL long long int
#define MAXN 100005

using namespace std;

int N,M,K;
int dr,dc;
LL INF=(1LL<<62)-1LL;

struct poi
{
    LL t;int r,c;
    bool operator <(const poi &rhs)const
    {
        if(r<rhs.r)return true;
        if(r>rhs.r)return false;
        return c<rhs.c;
    }
};

vector<poi> s;
vector<poi> sp;
set<poi> lines;
LL ANS[MAXN];

inline bool atCorner(int r,int c)
{
    return (r==0&&c==M)||(r==N&&c==0)||(r==N&&c==M);
}

int main()
{
    while(scanf("%d%d%d",&N,&M,&K)==3)
    {
        lines.clear();
        s.clear();
        sp.clear();
        sp.push_back((poi){0,0,0});
        for(int i=0;i<K;i++)
        {
            ANS[i]=0;
            int r,c;
            scanf("%d%d",&r,&c);
            s.push_back((poi){i,r,c});
        }
        dr=1;dc=1;
        int nowr=0,nowc=0,nxtr=0,nxtc=0;
        int h=min(N,M),l=max(N,M),i=1;
        long long int t=0;
        while(!atCorner(nowr,nowc))
        {
            nxtr=nowr+dr*h;
            nxtc=nowc+dc*h;
            t+=(LL)h;
            sp.push_back((poi){t,nxtr,nxtc});
            lines.insert((poi){i++,dr*dc,(nxtr-dr*dc*nxtc)});
            if(nxtr==0||nxtr==N)
            {
                dr=-dr;
                l=dc==1 ? M-nxtc : nxtc;h=N;
                if(l<h)swap(h,l);
            }
            if(nxtc==0||nxtc==M)
            {
                dc=-dc;
                l=dr==1 ? N-nxtr : nxtr;h=M;
                if(l<h)swap(h,l);
            }
            nowr=nxtr,nowc=nxtc;
        }
        //printf("traversal done %d\n",sp.size());
        for(i=0;i<s.size();i++)
        {
            set<poi>::iterator it1=lines.find((poi){0,1,s[i].r-s[i].c});
            if(it1!=lines.end())ANS[i]=abs(s[i].r-sp[(*it1).t-1].r)+sp[(*it1).t-1].t;
            else ANS[i]=INF;
            set<poi>::iterator it2=lines.find((poi){0,-1,s[i].r+s[i].c});
            if(it2!=lines.end())ANS[i]=min(abs(s[i].r-sp[(*it2).t-1].r)+sp[(*it2).t-1].t , ANS[i]);
        }

        //dq(1,sp.size(),s);

        for(int i=0;i<K;i++)
        {
            printf("%I64d\n",ANS[i]!=INF ? ANS[i] : -1);
        }
    }
}


#include <stdio.h>
#include <iostream>
#include <vector>
#include <string.h>
#include <set>
#define LL long long int
#define MAXN 100005

using namespace std;

int N,M,K;
int dr,dc;
LL INF=(1<<62)-1;

struct poi
{
    LL t;int r,c;
    bool operator <(const poi &rhs)const
    {
        if(r<rhs.r)return true;
        if(r>rhs.r)return false;
        return c<rhs.c;
    }
};

vector<poi> s;
vector<poi> sp;
set<poi> lines;
LL ANS[MAXN];

inline bool atCorner(int r,int c)
{
    return (r==0&&c==M)||(r==N&&c==0)||(r==N&&c==M);
}

int main()
{
    while(scanf("%d%d%d",&N,&M,&K)==3)
    {
        lines.clear();
        s.clear();
        sp.clear();
        sp.push_back((poi){0,0,0});
        for(int i=0;i<K;i++)
        {
            ANS[i]=0;
            int r,c;
            scanf("%d%d",&r,&c);
            s.push_back((poi){i,r,c});
        }
        dr=1;dc=1;
        int nowr=0,nowc=0,nxtr=0,nxtc=0;
        int h=min(N,M),l=max(N,M),i=1;
        long long int t=0;
        while(!atCorner(nowr,nowc))
        {
            nxtr=nowr+dr*h;
            nxtc=nowc+dc*h;
            t+=(LL)h;
            sp.push_back((poi){t,nxtr,nxtc});
            lines.insert((poi){i++,dr*dc,(nxtr-dr*dc*nxtc)});
            if(nxtr==0||nxtr==N)
            {
                dr=-dr;
                l=dc==1 ? M-nxtc : nxtc;h=N;
                if(l<h)swap(h,l);
            }
            if(nxtc==0||nxtc==M)
            {
                dc=-dc;
                l=dr==1 ? N-nxtr : nxtr;h=M;
                if(l<h)swap(h,l);
            }
            nowr=nxtr,nowc=nxtc;
        }
        //printf("traversal done %d\n",sp.size());
        for(i=0;i<s.size();i++)
        {
            set<poi>::iterator it1=lines.find((poi){0,1,s[i].r-s[i].c});
            if(it1!=lines.end())ANS[i]=abs(s[i].r-sp[(*it1).t-1].r)+sp[(*it1).t-1].t;
            else ANS[i]=INF;
            set<poi>::iterator it2=lines.find((poi){0,-1,s[i].r+s[i].c});
            if(it2!=lines.end())ANS[i]=min(abs(s[i].r-sp[(*it2).t-1].r)+sp[(*it2).t-1].t , ANS[i]);
        }

        //dq(1,sp.size(),s);

        for(int i=0;i<K;i++)
        {
            printf("%I64d\n",ANS[i]!=INF ? ANS[i] : -1);
        }
    }
}


#include <stdio.h>
#include <iostream>
#include <vector>
#include <string.h>
#include <set>
#define LL long long int
#define MAXN 100005

using namespace std;

int N,M,K;
int dr,dc;
LL INF=(1<<62)-1;

struct poi
{
    LL t;int r,c;
    bool operator <(const poi &rhs)const
    {
        if(r<rhs.r)return true;
        if(r>rhs.r)return false;
        return c<rhs.c;
    }
};

vector<poi> s;
vector<poi> sp;
set<poi> lines;
LL ANS[MAXN];

inline bool atCorner(int r,int c)
{
    return (r==0&&c==M)||(r==N&&c==0)||(r==N&&c==M);
}

int main()
{
    while(scanf("%d%d%d",&N,&M,&K)==3)
    {
        lines.clear();
        s.clear();
        sp.clear();
        sp.push_back((poi){0,0,0});
        for(int i=0;i<K;i++)
        {
            ANS[i]=0;
            int r,c;
            scanf("%d%d",&r,&c);
            s.push_back((poi){i,r,c});
        }
        dr=1;dc=1;
        int nowr=0,nowc=0,nxtr=0,nxtc=0;
        int h=min(N,M),l=max(N,M),i=1;
        long long int t=0;
        while(!atCorner(nowr,nowc))
        {
            nxtr=nowr+dr*h;
            nxtc=nowc+dc*h;
            t+=(LL)h;
            sp.push_back((poi){t,nxtr,nxtc});
            lines.insert((poi){i++,dr*dc,(nxtr-dr*dc*nxtc)});
            if(nxtr==0||nxtr==N)
            {
                dr=-dr;
                l=dc==1 ? M-nxtc : nxtc;h=N;
                if(l<h)swap(h,l);
            }
            if(nxtc==0||nxtc==M)
            {
                dc=-dc;
                l=dr==1 ? N-nxtr : nxtr;h=M;
                if(l<h)swap(h,l);
            }
            nowr=nxtr,nowc=nxtc;
        }
        //printf("traversal done %d\n",sp.size());
        for(i=0;i<s.size();i++)
        {
            set<poi>::iterator it1=lines.find((poi){0,1,s[i].r-s[i].c});
            if(it1!=lines.end())ANS[i]=abs(s[i].r-sp[(*it1).t-1].r)+sp[(*it1).t-1].t;
            else ANS[i]=INF;
            set<poi>::iterator it2=lines.find((poi){0,-1,s[i].r+s[i].c});
            if(it2!=lines.end())ANS[i]=min(abs(s[i].r-sp[(*it2).t-1].r)+sp[(*it2).t-1].t , ANS[i]);
        }

        //dq(1,sp.size(),s);

        for(int i=0;i<K;i++)
        {
            printf("%d\n",ANS[i]!=INF ? ANS[i] : -1);
        }
    }
}


#include <stdio.h>
#include <iostream>
#include <vector>
#define MAXN 100005

using namespace std;

int N,M,K;
int dr,dc;

struct poi
{
    int t,r,c;
};

vector<poi> sp;
int ANS[MAXN];

inline bool atCorner(int r,int c)
{
    return (r==0&&c==M)||(r==N&&c==0)||(r==N&&c==M);
}

inline bool online(int r,int c,int tl,int tr)
{
    bool res=0;
    for(int i=tl;i<=tr;i++)
    {
        if(i==sp.size())continue;
        res|=((r-sp[i-1].r)*(sp[i].c-sp[i-1].c)==(c-sp[i-1].c)*(sp[i].r-sp[i-1].r));
    }
    return res;
}

void dq(int l,int r,vector<poi>& s)
{
    //printf("%d %d\n",l,r);
    if(s.size()==0)return ;
    vector<poi> ok;
    vector<poi> nok;
    int mid=(l+r)/2;
    for(int i=0;i<s.size();i++)
    {
        if(online(s[i].r,s[i].c,l,mid))ok.push_back(s[i]);
        else nok.push_back(s[i]);
    }
    if(l==r){
        for(int i=0;i<ok.size();i++)ANS[ok[i].t]=abs(ok[i].r-sp[l-1].r)+sp[l-1].t;
        return ;
    }
    vector<poi>().swap(s);
    dq(l,mid,ok);
    dq(mid+1,r,nok);
}

int main()
{
    while(scanf("%d%d%d",&N,&M,&K)==3)
    {
        vector<poi> s;
        sp.clear();
        sp.push_back((poi){0,0,0});
        for(int i=0;i<K;i++)
        {
            ANS[i]=0;
            int r,c;
            scanf("%d%d",&r,&c);
            s.push_back((poi){i,r,c});
        }
        dr=1;dc=1;
        int nowr=0,nowc=0,nxtr=0,nxtc=0;
        int h=min(N,M),l=max(N,M),t=0;
        while(!atCorner(nowr,nowc))
        {
            nxtr=nowr+dr*h;
            nxtc=nowc+dc*h;
            t+=h;
            sp.push_back((poi){t,nxtr,nxtc});
            if(nxtr==0||nxtr==N)
            {
                dr=-dr;
                l=dc==1 ? M-nxtc : nxtc;h=N;
                if(l<h)swap(h,l);
            }
            if(nxtc==0||nxtc==M)
            {
                dc=-dc;
                l=dr==1 ? N-nxtr : nxtr;h=M;
                if(l<h)swap(h,l);
            }
            nowr=nxtr,nowc=nxtc;
        }
        //printf("traversal done %d\n",sp.size());

        dq(1,sp.size(),s);
        for(int i=0;i<K;i++)
        {
            printf("%d\n",ANS[i] ? ANS[i] : -1);
        }
    }
}


#include <stdio.h>
#include <iostream>
#include <vector>
#include <string.h>
#include <set>
#define MAXN 100005

using namespace std;

int N,M,K;
int dr,dc;

struct poi
{
    int t,r,c;
    bool operator <(const poi &rhs)const
    {
        if(r<rhs.r)return true;
        if(r>rhs.r)return false;
        return c<rhs.c;
    }
};

vector<poi> s;
vector<poi> sp;
set<poi> lines;
int ANS[MAXN];

inline bool atCorner(int r,int c)
{
    return (r==0&&c==M)||(r==N&&c==0)||(r==N&&c==M);
}

inline bool online(int r,int c,int tl,int tr)
{
    bool res=0;
    for(int i=tl;i<=tr;i++)
    {
        if(i==sp.size())continue;
        res|=((r-sp[i-1].r)*(sp[i].c-sp[i-1].c)==(c-sp[i-1].c)*(sp[i].r-sp[i-1].r));
    }
    return res;
}

void dq(int l,int r,vector<poi>& s)
{
    //printf("%d %d\n",l,r);
    if(s.size()==0)return ;
    vector<poi> ok;
    vector<poi> nok;
    int mid=(l+r)/2;
    for(int i=0;i<s.size();i++)
    {
        if(online(s[i].r,s[i].c,l,mid))ok.push_back(s[i]);
        else nok.push_back(s[i]);
    }
    if(l==r){
        for(int i=0;i<ok.size();i++)ANS[ok[i].t]=abs(ok[i].r-sp[l-1].r)+sp[l-1].t;
        return ;
    }
    vector<poi>().swap(s);
    dq(l,mid,ok);
    dq(mid+1,r,nok);
}

int main()
{
    while(scanf("%d%d%d",&N,&M,&K)==3)
    {
        lines.clear();
        s.clear();
        sp.clear();
        sp.push_back((poi){0,0,0});
        for(int i=0;i<K;i++)
        {
            ANS[i]=0;
            int r,c;
            scanf("%d%d",&r,&c);
            s.push_back((poi){i,r,c});
        }
        dr=1;dc=1;
        int nowr=0,nowc=0,nxtr=0,nxtc=0;
        int h=min(N,M),l=max(N,M),t=0,i=1;
        while(!atCorner(nowr,nowc))
        {
            nxtr=nowr+dr*h;
            nxtc=nowc+dc*h;
            t+=h;
            sp.push_back((poi){t,nxtr,nxtc});
            lines.insert((poi){i++,dr*dc,(nxtr-dr*dc*nxtc)});
            if(nxtr==0||nxtr==N)
            {
                dr=-dr;
                l=dc==1 ? M-nxtc : nxtc;h=N;
                if(l<h)swap(h,l);
            }
            if(nxtc==0||nxtc==M)
            {
                dc=-dc;
                l=dr==1 ? N-nxtr : nxtr;h=M;
                if(l<h)swap(h,l);
            }
            nowr=nxtr,nowc=nxtc;
        }
        //printf("traversal done %d\n",sp.size());
        for(i=0;i<s.size();i++)
        {
            set<poi>::iterator it1=lines.find((poi){0,1,s[i].r-s[i].c});
            if(it1!=lines.end())ANS[i]=abs(s[i].r-sp[(*it1).t-1].r)+sp[(*it1).t-1].t;
            else ANS[i]=2147483647;
            set<poi>::iterator it2=lines.find((poi){0,-1,s[i].r+s[i].c});
            if(it2!=lines.end())ANS[i]=min(abs(s[i].r-sp[(*it2).t-1].r)+sp[(*it2).t-1].t , ANS[i]);
        }

        //dq(1,sp.size(),s);

        for(int i=0;i<K;i++)
        {
            printf("%d\n",ANS[i]!=2147483647 ? ANS[i] : -1);
        }
    }
}


#include <stdio.h>
#include <iostream>
#include <vector>
#define MAXN 100005

using namespace std;

int N,M,K;
int dr,dc;

struct poi
{
    int t,r,c;
};

vector<poi> sp;
int ANS[MAXN];

inline bool atCorner(int r,int c)
{
    return (r==0&&c==M)||(r==N&&c==0)||(r==N&&c==M);
}

inline bool online(int r,int c,int tl,int tr)
{
    bool res=0;
    for(int i=tl;i<=tr;i++)
    {
        if(i==sp.size())continue;
        res|=((r-sp[i-1].r)*(sp[i].c-sp[i-1].c)==(c-sp[i-1].c)*(sp[i].r-sp[i-1].r));
    }
    return res;
}

void dq(int l,int r,vector<poi>& s)
{
    //printf("%d %d\n",l,r);

    vector<poi> ok;
    vector<poi> nok;
    int mid=(l+r)/2;
    for(int i=0;i<s.size();i++)
    {
        if(online(s[i].r,s[i].c,l,mid))ok.push_back(s[i]);
        else nok.push_back(s[i]);
    }
    if(l==r){
        for(int i=0;i<ok.size();i++)ANS[ok[i].t]=abs(ok[i].r-sp[l-1].r)+sp[l-1].t;
        return ;
    }
    vector<poi>().swap(s);
    dq(l,mid,ok);
    dq(mid+1,r,nok);
}

int main()
{
    while(scanf("%d%d%d",&N,&M,&K)==3)
    {
        vector<poi> s;
        sp.clear();
        sp.push_back((poi){0,0,0});
        for(int i=0;i<K;i++)
        {
            ANS[i]=0;
            int r,c;
            scanf("%d%d",&r,&c);
            s.push_back((poi){i,r,c});
        }
        dr=1;dc=1;
        int nowr=0,nowc=0,nxtr=0,nxtc=0;
        int h=min(N,M),l=max(N,M),t=0;
        while(!atCorner(nowr,nowc))
        {
            nxtr=nowr+dr*h;
            nxtc=nowc+dc*h;
            t+=h;
            sp.push_back((poi){t,nxtr,nxtc});
            if(nxtr==0||nxtr==N)
            {
                dr=-dr;
                l=dc==1 ? M-nxtc : nxtc;h=N;
                if(l<h)swap(h,l);
            }
            if(nxtc==0||nxtc==M)
            {
                dc=-dc;
                l=dr==1 ? N-nxtr : nxtr;h=M;
                if(l<h)swap(h,l);
            }
            nowr=nxtr,nowc=nxtc;
        }
        //printf("traversal done %d\n",sp.size());

        dq(1,sp.size(),s);
        for(int i=0;i<K;i++)
        {
            printf("%d\n",ANS[i] ? ANS[i] : -1);
        }
    }
}


#include<bits/stdc++.h>
#include<stdio.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define ll long long
#define mp make_pair
#define pb push_back
#define pp pair<int,int>
#define vpp vector<pp>
#define f first
#define s second
#define mpp map<pp,ll>
#define INF (1LL<<58)
int n,m,k,x,y;
ll t;
mpp p;
vpp vp;
int dir[4][2]={{1,1},{1,-1},{-1,-1},{-1,1}};
int vw(int d)
{
   if(d==1)
   return 2;
   else if(d==2)
   return 1;
   else if(d==3)
   return 0;
   else if(d==0)
   return 3;
}
int hw(int d)
{ if(d==0)
   return 1;
  else if(d==1)
   return 0;
  else if(d==2)
   return 3;
  else if(d==3)
   return 2;
}

bool cp(int cx,int cy)
{
    if((cx==0 && cy==0) || (cx==0 && cy==m) || (cx==n && cy==0) || (cx==n && cy==m))
    return 1;
    else
    return 0;
}
bool val(int zx,int zy)
{
    if(zx>=0 && zx<=n && zy>=0 && zy<=m)
     return 1;
     else
     return 0;
}

 void newrp(int &d,int &rx, int &ry)
 {  int tx=rx,ty=ry;
    if(d==0 || d==3)    // reach top wall
    {if(val(rx+(dir[d][0])*(m-ry),m))
         {
             rx=rx+(dir[d][0])*(m-ry);
             ry=m;
             d=hw(d);
             t+=abs(tx-rx);
             return ;
         }
    }
    if (d==0 || d==1) //reach right wall
    {
        if(val(n,ry+(dir[d][1])*(n-rx)))
         {
             rx=n;
             ry=ry+(dir[d][1])*(n-tx);
             d=vw(d);
             t+=abs(tx-rx);
             return ;
         }

    }
    if(d==1 || d==2) //reach bottom wall
    { if(val(rx+(dir[d][0])*(ry),0))
         {
             rx=rx+(dir[d][0])*(ry);
             ry=0;
             d=hw(d);
             t+=abs(tx-rx);
             return ;
         }

    }
    if(d==2 ||d==3) //reach left wall
    {
        if(val(0,ry+(dir[d][1])*(rx)))
         {
             rx=0;
             ry=ry+(dir[d][1])*(tx);
             d=vw(d);
             t+=abs(tx-rx);
             return ;
         }

    }
 }

int main()
{scanf("%d%d%d",&n,&m,&k);
rep(i,1,k)
{scanf("%d%d",&x,&y);
vp.pb(mp(x,y)) ;
}
int d=0,rx=0,ry=0;
t=0;
p[mp(0,0)]=0;

do
{ newrp(d,rx,ry);
  p[mp(rx,ry)]=t;
}
while(cp(rx,ry)!=true) ;

for(auto l:vp)
{ tie(rx,ry)=l;
ll ans=-1,ds=INF;
pp np;bool b=0;
rep(j,0,3)
{ x=rx;y=ry,d=j;
  newrp(d,x,y);

 if(p.find(mp(x,y))!=end(p))
  {if(ds>p[mp(x,y)])
      { ds=p[mp(x,y)];
        np=mp(x,y);
        b=1;
      }
  }
}
if(b)
ans=p[np]+abs(rx-np.f);

printf("%ld\n",ans);

}
return 0;
}


#include<bits/stdc++.h>
#include<stdio.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define mp make_pair
#define pb push_back
#define pp pair<int,int>
#define vpp vector<pp>
#define f first
#define s second
#define mpp map<pp,int>
int t,n,m,k,x,y;
mpp p;
vpp vp;
int dir[4][2]={{1,1},{1,-1},{-1,-1},{-1,1}};
void vw(int& dx,int& dy)
{ if(dx==dir[0][0] && dy==dir[0][1])
   {dx=dir[3][0];dy=dir[3][1];}
  else if(dx==dir[3][0] && dy==dir[3][1])
   {dx=dir[0][0];dy=dir[0][1];}
  else if (dx==dir[1][0] && dy==dir[1][1])
   {dx=dir[2][0];dy=dir[2][1];}
   else if(dx==dir[2][0] && dy==dir[2][1])
   {dx=dir[1][0];dy=dir[1][1];}
}
void hw(int& dx,int& dy)
{ if(dx==dir[0][0] && dy==dir[0][1])
   {dx=dir[1][0];dy=dir[1][1];}
  else if(dx==dir[3][0] && dy==dir[3][1])
   {dx=dir[2][0];dy=dir[2][1];}
  else if (dx==dir[1][0] && dy==dir[1][1])
   {dx=dir[0][0];dy=dir[0][1];}
   else if(dx==dir[2][0] && dy==dir[2][1])
   {dx=dir[3][0];dy=dir[3][1];}
}

bool cp(int cx,int cy)
{
    if((cx==0 && cy==0) || (cx==0 && cy==m) || (cx==n && cy==0) || (cx==n && cy==m))
    return 1;
    else
    return 0;
}

int main()
{scanf("%d%d%d",&n,&m,&k);
rep(i,1,k)
{scanf("%d%d",&x,&y);
 p[mp(x,y)]=-1;
vp.pb(mp(x,y)) ;
}
int cdx=dir[0][0],cdy=dir[0][0],px=0,py=0;
do
{ if(p.find(mp(px,py))!=end(p) && p[mp(px,py)]==-1)
    p[mp(px,py)]=t;
 px=px+cdx;
 py=py+cdy;
 if(px==n || px==0)
  vw(cdx,cdy);
 else if(py==m || py==0)
  hw(cdx,cdy);
 t++;
}
while(cp(px,py)!=1);
for(auto l:vp)
printf("%d\n",p[l]);
return 0;
}


#include<bits/stdc++.h>
#include<stdio.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define ll long long
#define mp make_pair
#define pb push_back
#define pp pair<int,int>
#define vpp vector<pp>
#define f first
#define s second
#define mpp map<pp,int>
#define INF (1LL<<58)
int t,n,m,k,x,y;
mpp p;
vpp vp;
int dir[4][2]={{1,1},{1,-1},{-1,-1},{-1,1}};
int vw(int d)
{
   if(d==1)
   return 2;
   else if(d==2)
   return 1;
   else if(d==3)
   return 0;
   else if(d==0)
   return 3;
}
int hw(int d)
{ if(d==0)
   return 1;
  else if(d==1)
   return 0;
  else if(d==2)
   return 3;
  else if(d==3)
   return 2;
}

bool cp(int cx,int cy)
{
    if((cx==0 && cy==0) || (cx==0 && cy==m) || (cx==n && cy==0) || (cx==n && cy==m))
    return 1;
    else
    return 0;
}
bool val(int zx,int zy)
{
    if(zx>=0 && zx<=n && zy>=0 && zy<=m)
     return 1;
     else
     return 0;
}

 void newrp(int &d,int &rx, int &ry)
 {  int tx=rx,ty=ry;
    if(d==0 || d==3)    // reach top wall
    {if(val(rx+(dir[d][0])*(m-ry),m))
         {
             rx=rx+(dir[d][0])*(m-ry);
             ry=m;
             d=hw(d);
             t+=abs(tx-rx);
             return ;
         }
    }
    if (d==0 || d==1) //reach right wall
    {
        if(val(n,ry+(dir[d][1])*(n-rx)))
         {
             rx=n;
             ry=ry+(dir[d][1])*(n-tx);
             d=vw(d);
             t+=abs(tx-rx);
             return ;
         }

    }
    if(d==1 || d==2) //reach bottom wall
    { if(val(rx+(dir[d][0])*(ry),0))
         {
             rx=rx+(dir[d][0])*(ry);
             ry=0;
             d=hw(d);
             t+=abs(tx-rx);
             return ;
         }

    }
    if(d==2 ||d==3) //reach left wall
    {
        if(val(0,ry+(dir[d][1])*(rx)))
         {
             rx=0;
             ry=ry+(dir[d][1])*(tx);
             d=vw(d);
             t+=abs(tx-rx);
             return ;
         }

    }
 }

int main()
{scanf("%d%d%d",&n,&m,&k);
rep(i,1,k)
{scanf("%d%d",&x,&y);
vp.pb(mp(x,y)) ;
}
int d=0,rx=0,ry=0;
t=0;
p[mp(0,0)]=0;

do
{ newrp(d,rx,ry);
  p[mp(rx,ry)]=t;
}
while(cp(rx,ry)!=true) ;

for(auto l:vp)
{ tie(rx,ry)=l;
ll ans=-1,ds=INF;
pp np;bool b=0;
rep(j,0,3)
{ x=rx;y=ry,d=j;
  newrp(d,x,y);

 if(p.find(mp(x,y))!=end(p))
  {if(ds>p[mp(x,y)])
      { ds=p[mp(x,y)];
        np=mp(x,y);
        b=1;
      }
  }
}
if(b)
ans=p[np]+abs(rx-np.f);

printf("%ld\n",ans);

}
return 0;
}


#include<bits/stdc++.h>
#include<stdio.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define ll long long
#define mp make_pair
#define pb push_back
#define pp pair<int,int>
#define vpp vector<pp>
#define f first
#define s second
#define mpp map<pp,ll>
#define INF (1LL<<58)
int n,m,k,x,y;
ll t;
mpp p;
vpp vp;
int dir[4][2]={{1,1},{1,-1},{-1,-1},{-1,1}};
int vw(int d)
{
   if(d==1)
   return 2;
   else if(d==2)
   return 1;
   else if(d==3)
   return 0;
   else if(d==0)
   return 3;
}
int hw(int d)
{ if(d==0)
   return 1;
  else if(d==1)
   return 0;
  else if(d==2)
   return 3;
  else if(d==3)
   return 2;
}

bool cp(int cx,int cy)
{
    if((cx==0 && cy==0) || (cx==0 && cy==m) || (cx==n && cy==0) || (cx==n && cy==m))
    return 1;
    else
    return 0;
}
bool val(int zx,int zy)
{
    if(zx>=0 && zx<=n && zy>=0 && zy<=m)
     return 1;
     else
     return 0;
}
ll ab(ll a,ll b)
{
    if(a>b)
     return a-b;
     else
     return b-a;

}
 void newrp(int &d,int &rx, int &ry)
 {  int tx=rx,ty=ry;
    if(d==0 || d==3)    // reach top wall
    {if(val(rx+(dir[d][0])*(m-ry),m))
         {
             rx=rx+(dir[d][0])*(m-ry);
             ry=m;
             d=hw(d);
             t+=ab(tx,rx);
             return ;
         }
    }
    if (d==0 || d==1) //reach right wall
    {
        if(val(n,ry+(dir[d][1])*(n-rx)))
         {
             rx=n;
             ry=ry+(dir[d][1])*(n-tx);
             d=vw(d);
             t+=ab(tx,rx);
             return ;
         }

    }
    if(d==1 || d==2) //reach bottom wall
    { if(val(rx+(dir[d][0])*(ry),0))
         {
             rx=rx+(dir[d][0])*(ry);
             ry=0;
             d=hw(d);
             t+=ab(tx,rx);
             return ;
         }

    }
    if(d==2 ||d==3) //reach left wall
    {
        if(val(0,ry+(dir[d][1])*(rx)))
         {
             rx=0;
             ry=ry+(dir[d][1])*(tx);
             d=vw(d);
             t+=ab(tx,rx);
             return ;
         }

    }
 }

int main()
{scanf("%d%d%d",&n,&m,&k);
rep(i,1,k)
{scanf("%d%d",&x,&y);
vp.pb(mp(x,y)) ;
}
int d=0,rx=0,ry=0;
t=0;
p[mp(0,0)]=0;

do
{ newrp(d,rx,ry);
  p[mp(rx,ry)]=t;
}
while(cp(rx,ry)!=true) ;

for(auto l:vp)
{ tie(rx,ry)=l;
ll ans=-1,ds=INF;
pp np;bool b=0;
rep(j,0,3)
{ x=rx;y=ry,d=j;
  newrp(d,x,y);

 if(p.find(mp(x,y))!=end(p))
  {if(ds>p[mp(x,y)])
      { ds=p[mp(x,y)];
        np=mp(x,y);
        b=1;
      }
  }
}
if(b)
ans=p[np]+ ab(rx,np.f);
printf("%lld\n",ans);

}
return 0;
}


#include<bits/stdc++.h>
#include<stdio.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define mp make_pair
#define pb push_back
#define pp pair<int,int>
#define vpp vector<pp>
#define f first
#define s second
#define mpp map<pp,int>
int t,n,m,k,x,y;
mpp p;
vpp vp;
int dir[4][2]={{1,1},{1,-1},{-1,-1},{-1,1}};
void vw(int& dx,int& dy)
{ if(dx==dir[0][0] && dy==dir[0][1])
   {dx=dir[3][0];dy=dir[3][1];}
  else if(dx==dir[3][0] && dy==dir[3][1])
   {dx=dir[0][0];dy=dir[0][1];}
  else if (dx==dir[1][0] && dy==dir[1][1])
   {dx=dir[2][0];dy=dir[2][1];}
   else if(dx==dir[2][0] && dy==dir[2][1])
   {dx=dir[1][0];dy=dir[1][1];}
}
void hw(int& dx,int& dy)
{ if(dx==dir[0][0] && dy==dir[0][1])
   {dx=dir[1][0];dy=dir[1][1];}
  else if(dx==dir[3][0] && dy==dir[3][1])
   {dx=dir[2][0];dy=dir[2][1];}
  else if (dx==dir[1][0] && dy==dir[1][1])
   {dx=dir[0][0];dy=dir[0][1];}
   else if(dx==dir[2][0] && dy==dir[2][1])
   {dx=dir[3][0];dy=dir[3][1];}
}

bool cp(int cx,int cy)
{
    if((cx==0 && cy==0) || (cx==0 && cy==m) || (cx==n && cy==0) || (cx==n && cy==m))
    return 1;
    else
    return 0;
}

int main()
{cin>>n>>m>>k;
rep(i,1,k)
{cin>>x>>y;
 p[mp(x,y)]=-1;
vp.pb(mp(x,y)) ;
}
int cdx=dir[0][0],cdy=dir[0][0],px=0,py=0;
do
{ if(p.find(mp(px,py))!=end(p) && p[mp(px,py)]==-1)
    p[mp(px,py)]=t;
 px=px+cdx;
 py=py+cdy;
 if(px==n || px==0)
  vw(cdx,cdy);
 else if(py==m || py==0)
  hw(cdx,cdy);
 t++;
}
while(cp(px,py)!=1);
for(auto l:vp)
cout<<p[l]<<endl;
return 0;
}


#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

int n, m, k;

int now, x, y, timer;
bool corner;

vector< pair<int, int> > pos;
vector< pair<int, int> > up, down;
vector<int> wop;
bool visited[100005][4];

void calculate(){
	if(now == 0){
		if(n - x == m - y){
			now = 2;
			timer+=n-x;
			x = n; y = m;
			corner = true;
		}else if(n - x < m - y){
			now = 3;
			timer+=n-x;
			y+= n-x; x = n;
		}else{
			now = 1;
			timer+=m-y;
			x+= m-y; y = m;
		}
	}else if(now == 1){
		if(n - x == y){
			now = 3;
			timer+=y;
			x = n; y = 0;
			corner = true;
		}else if(n - x < y){
			now = 2;
			timer+=n-x;
			y-= n-x; x = n;
		}else{
			now = 0;
			timer+=y;
			x+= y; y = 0;
		}
	}else if(now == 2){
		if(x == y){
			now = 0;
			timer+=x;
			x = 0; y = 0;
			corner = true;
		}else if(x < y){
			now = 1;
			timer+=x;
			y-= x; x = 0;
		}else{
			now = 3;
			timer+=y;
			x-= y; y = 0;
		}
	}else{
		if(m - y == x){
			now = 1;
			timer+=x;
			x = 0; y = m;
			corner = true;
		}else if(x < m - y){
			now = 0;
			timer+=x;
			y+= x; x = 0;
		}else{
			now = 2;
			timer+=m-y;
			x-= m-y; y = m;
		}
	}
}

int main(){
	scanf("%i%i%i", &n, &m, &k);

	int a, b;

	for (int i = 0; i < k; i++){
		scanf("%i%i", &a, &b);
		pos.push_back(make_pair(a, b));
		wop.push_back(-1);
		if((a+b)%2) continue;
		up.push_back(make_pair(a - b, i));
		down.push_back(make_pair(a + b, i));
	}

	sort(up.begin(), up.end());
	sort(down.begin(), down.end());

	vector< pair<int, int> >::iterator it1, it2, it;

	now = 0; x = 0; y = 0;
	calculate();
	do{
//			printf("xy:%i %i\n",x, y);
		if((corner && (now == 0 || now == 2))||(now == 1 || now == 3)){
			it1 = lower_bound(up.begin(), up.end(), make_pair(x-y, 0));
			it2 = upper_bound(up.begin(), up.end(), make_pair(x-y, k));
			for(it = it1; it != it2; it++){
				if(wop[(*it).second] == -1){
//						printf("update %i: %i\n", (*it).second, timer);
					wop[(*it).second] = timer - abs(x-pos[(*it).second].first);
				}
			}
		}else{
			it1 = lower_bound(down.begin(), down.end(), make_pair(x+y, 0));
			it2 = upper_bound(down.begin(), down.end(), make_pair(x+y, k));
			for(it = it1; it != it2; it++){
				if(wop[(*it).second] == -1){
//						printf("update %i: %i\n", (*it).second, timer);
					wop[(*it).second] = timer - abs(x-pos[(*it).second].first);
				}
			}
		}
		calculate();

	}while(!corner);
	for (int i = 0; i < (int) wop.size(); i++){
		printf("%i\n", wop[i]);
	}

}


#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

int n, m, k;

int now, x, y, timer;
bool corner;

vector< pair<int, int> > pos;
vector< pair<int, int> > up, down;
vector<int> ans;

void calculate(){
	if(now == 0){
		if(n - x == m - y){
			now = 2;
			timer+=n-x;
			x = n; y = m;
			corner = true;
		}else if(n - x < m - y){
			now = 3;
			timer+=n-x;
			y+= n-x; x = n;
		}else{
			now = 1;
			timer+=m-y;
			x+= m-y; y = m;
		}
	}else if(now == 1){
		if(n - x == y){
			now = 3;
			timer+=y;
			x = n; y = 0;
			corner = true;
		}else if(n - x < y){
			now = 2;
			timer+=n-x;
			y-= n-x; x = n;
		}else{
			now = 0;
			timer+=y;
			x+= y; y = 0;
		}
	}else if(now == 2){
		if(x == y){
			now = 0;
			timer+=x;
			x = 0; y = 0;
			corner = true;
		}else if(x < y){
			now = 1;
			timer+=x;
			y-= x; x = 0;
		}else{
			now = 3;
			timer+=y;
			x-= y; y = 0;
		}
	}else{
		if(m - y == x){
			now = 1;
			timer+=x;
			x = 0; y = m;
			corner = true;
		}else if(x < m - y){
			now = 0;
			timer+=x;
			y+= x; x = 0;
		}else{
			now = 2;
			timer+=m-y;
			x-= m-y; y = m;
		}
	}
}

int main(){
	scanf("%i%i%i", &n, &m, &k);

	int a, b;

	for (int i = 0; i < k; i++){
		scanf("%i%i", &a, &b);
		pos.push_back(make_pair(a, b));
		ans.push_back(-1);
		if((a+b)%2) continue;
		up.push_back(make_pair(a - b, i));
		down.push_back(make_pair(a + b, i));
	}

	sort(up.begin(), up.end());
	sort(down.begin(), down.end());

	vector< pair<int, int> >::iterator it1, it2, it;

	now = 0; x = 0; y = 0;
	do{
		calculate();
//		printf("xy:%i %i\n",x, y);
//		printf("corner:%i\n", corner);
		if((corner && (now == 0 || now == 2))||(now == 1 || now == 3)){
			it1 = lower_bound(up.begin(), up.end(), make_pair(x-y, 0));
			it2 = upper_bound(up.begin(), up.end(), make_pair(x-y, k));
			for(it = it1; it != it2; it++){
				if(ans[(*it).second] == -1){
//						printf("update %i: %i\n", (*it).second, timer);
					ans[(*it).second] = timer - abs(x-pos[(*it).second].first);
				}
			}
		}else{
			it1 = lower_bound(down.begin(), down.end(), make_pair(x+y, 0));
			it2 = upper_bound(down.begin(), down.end(), make_pair(x+y, k));
			for(it = it1; it != it2; it++){
				if(ans[(*it).second] == -1){
//						printf("update %i: %i\n", (*it).second, timer);
					ans[(*it).second] = timer - abs(x-pos[(*it).second].first);
				}
			}
		}
	}while(!corner);
	for (int i = 0; i < (int) ans.size(); i++){
		printf("%i\n", ans[i]);
	}

}


#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

int n, m, k;

int now, x, y, timer;
bool corner;

vector< pair<int, int> > pos;
vector< pair<int, int> > up, down;
vector<int> ans;

void calculate(){
	if(now == 0){
		if(n - x == m - y){
			now = 2;
			timer+=n-x;
			x = n; y = m;
			corner = true;
		}else if(n - x < m - y){
			now = 3;
			timer+=n-x;
			y+= n-x; x = n;
		}else{
			now = 1;
			timer+=m-y;
			x+= m-y; y = m;
		}
	}else if(now == 1){
		if(n - x == y){
			now = 3;
			timer+=y;
			x = n; y = 0;
			corner = true;
		}else if(n - x < y){
			now = 2;
			timer+=n-x;
			y-= n-x; x = n;
		}else{
			now = 0;
			timer+=y;
			x+= y; y = 0;
		}
	}else if(now == 2){
		if(x == y){
			now = 0;
			timer+=x;
			x = 0; y = 0;
			corner = true;
		}else if(x < y){
			now = 1;
			timer+=x;
			y-= x; x = 0;
		}else{
			now = 3;
			timer+=y;
			x-= y; y = 0;
		}
	}else{
		if(m - y == x){
			now = 1;
			timer+=x;
			x = 0; y = m;
			corner = true;
		}else if(x < m - y){
			now = 0;
			timer+=x;
			y+= x; x = 0;
		}else{
			now = 2;
			timer+=m-y;
			x-= m-y; y = m;
		}
	}
}

int main(){
	scanf("%i%i%i", &n, &m, &k);

	int a, b;

	for (int i = 0; i < k; i++){
		scanf("%i%i", &a, &b);
		pos.push_back(make_pair(a, b));
		ans.push_back(-1);
		if((a+b)%2) continue;
		up.push_back(make_pair(a - b, i));
		down.push_back(make_pair(a + b, i));
	}

	sort(up.begin(), up.end());
	sort(down.begin(), down.end());

	vector< pair<int, int> >::iterator it1, it2, it;

	now = 0; x = 0; y = 0;
	do{
		calculate();
//		printf("xy:%i %i\n",x, y);
//		printf("corner:%i\n", corner);
		if((corner && (now == 0 || now == 2))||(!corner&&(now == 1 || now == 3))){
			it1 = lower_bound(up.begin(), up.end(), make_pair(x-y, 0));
			it2 = upper_bound(up.begin(), up.end(), make_pair(x-y, k));
			for(it = it1; it != it2; it++){
				if(ans[(*it).second] == -1){
//					printf("update %i: %i\n", (*it).second, timer);
					ans[(*it).second] = timer - abs(x-pos[(*it).second].first);
				}
			}
		}else{
			it1 = lower_bound(down.begin(), down.end(), make_pair(x+y, 0));
			it2 = upper_bound(down.begin(), down.end(), make_pair(x+y, k));
			for(it = it1; it != it2; it++){
				if(ans[(*it).second] == -1){
//					printf("update %i: %i\n", (*it).second, timer);
					ans[(*it).second] = timer - abs(x-pos[(*it).second].first);
				}
			}
		}
	}while(!corner);
	for (int i = 0; i < (int) ans.size(); i++){
		printf("%i\n", ans[i]);
	}

}


#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

int n, m, k;

int now, x, y;
long long timer;
bool corner;

vector< pair<int, int> > pos;
vector< pair<int, int> > up, down;
vector<long long> ans;

void calculate(){
	if(now == 0){
		if(n - x == m - y){
			now = 2;
			timer+=n-x;
			x = n; y = m;
			corner = true;
		}else if(n - x < m - y){
			now = 3;
			timer+=n-x;
			y+= n-x; x = n;
		}else{
			now = 1;
			timer+=m-y;
			x+= m-y; y = m;
		}
	}else if(now == 1){
		if(n - x == y){
			now = 3;
			timer+=y;
			x = n; y = 0;
			corner = true;
		}else if(n - x < y){
			now = 2;
			timer+=n-x;
			y-= n-x; x = n;
		}else{
			now = 0;
			timer+=y;
			x+= y; y = 0;
		}
	}else if(now == 2){
		if(x == y){
			now = 0;
			timer+=x;
			x = 0; y = 0;
			corner = true;
		}else if(x < y){
			now = 1;
			timer+=x;
			y-= x; x = 0;
		}else{
			now = 3;
			timer+=y;
			x-= y; y = 0;
		}
	}else{
		if(m - y == x){
			now = 1;
			timer+=x;
			x = 0; y = m;
			corner = true;
		}else if(x < m - y){
			now = 0;
			timer+=x;
			y+= x; x = 0;
		}else{
			now = 2;
			timer+=m-y;
			x-= m-y; y = m;
		}
	}
}

int main(){
	scanf("%i%i%i", &n, &m, &k);

	int a, b;

	for (int i = 0; i < k; i++){
		scanf("%i%i", &a, &b);
		pos.push_back(make_pair(a, b));
		ans.push_back(-1);
		if((a+b)%2) continue;
		up.push_back(make_pair(a - b, i));
		down.push_back(make_pair(a + b, i));
	}

	sort(up.begin(), up.end());
	sort(down.begin(), down.end());

	vector< pair<int, int> >::iterator it1, it2, it;

	now = 0; x = 0; y = 0;
	do{
		calculate();
//		printf("xy:%i %i\n",x, y);
//		printf("corner:%i\n", corner);
		if((corner && (now == 0 || now == 2))||(!corner&&(now == 1 || now == 3))){
			it1 = lower_bound(up.begin(), up.end(), make_pair(x-y, 0));
			it2 = upper_bound(up.begin(), up.end(), make_pair(x-y, k));
			for(it = it1; it != it2; it++){
				if(ans[(*it).second] == -1){
//					printf("update %i: %i\n", (*it).second, timer);
					ans[(*it).second] = timer - abs(x-pos[(*it).second].first);
				}
			}
		}else{
			it1 = lower_bound(down.begin(), down.end(), make_pair(x+y, 0));
			it2 = upper_bound(down.begin(), down.end(), make_pair(x+y, k));
			for(it = it1; it != it2; it++){
				if(ans[(*it).second] == -1){
//					printf("update %i: %i\n", (*it).second, timer);
					ans[(*it).second] = timer - abs(x-pos[(*it).second].first);
				}
			}
		}
	}while(!corner);
	for (int i = 0; i < (int) ans.size(); i++){
		printf("%lli\n", ans[i]);
	}

}


#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

int n, m, k;

int now, x, y, haha, side, timer;

vector< pair<int, int> > pos;
vector< pair<int, int> > up, down;
vector<int> wop;
bool visited[100005][4];

void calculate(){
	if(now == 0){
		if(n - x == m - y){
			now = 2;
			timer+=n-x;
			x = n; y = m;
			haha = n;
			side = 0;
		}else if(n - x < m - y){
			now = 3;
			timer+=n-x;
			y+= n-x; x = n;
			haha = y;
			side = 1;
		}else{
			now = 1;
			timer+=m-y;
			x+= m-y; y = m;
			haha = x;
			side = 0;
		}
	}else if(now == 1){
		if(n - x == y){
			now = 3;
			timer+=y;
			x = n; y = 0;
			haha = 0;
			side = 1;
		}else if(n - x < y){
			now = 2;
			timer+=n-x;
			y-= n-x; x = n;
			haha = y;
			side = 1;
		}else{
			now = 0;
			timer+=y;
			x+= y; y = 0;
			haha = x;
			side = 2;
		}
	}else if(now == 2){
		if(x == y){
			now = 0;
			timer+=x;
			x = 0; y = 0;
			haha = 0;
			side = 2;
		}else if(x < y){
			now = 1;
			timer+=x;
			y-= x; x = 0;
			haha = y;
			side = 3;
		}else{
			now = 3;
			timer+=y;
			x-= y; y = 0;
			haha = x;
			side = 2;
		}
	}else{
		if(m - y == x){
			now = 1;
			timer+=x;
			x = 0; y = m;
			haha = m;
			side = 3;
		}else if(x < m - y){
			now = 0;
			timer+=x;
			y+= x; x = 0;
			haha = y;
			side = 3;
		}else{
			now = 2;
			timer+=m-y;
			x-= m-y; y = m;
			haha = x;
			side = 0;
		}
	}
}

int main(){
	scanf("%i%i%i", &n, &m, &k);

	int a, b;

	for (int i = 0; i < k; i++){
		scanf("%i%i", &a, &b);
		pos.push_back(make_pair(a, b));
		up.push_back(make_pair(a - b, i));
		down.push_back(make_pair(a + b, i));
		wop.push_back(-1);
	}

	sort(up.begin(), up.end());
	sort(down.begin(), down.end());

	vector< pair<int, int> >::iterator it1, it2, it;

	if(n==m){
		calculate();
		it1 = lower_bound(up.begin(), up.end(), make_pair(0, 0));
		it2 = upper_bound(up.begin(), up.end(), make_pair(0, k));
		for(it = it1; it != it2; it++){
			wop[(*it).second] = timer - abs(x-pos[(*it).second].first);
		}
	}else{
		now = 0; x = 0; y = 0;
		calculate();
		while(!visited[haha][side]){
//			printf("xy:%i %i\n",x, y);
			visited[haha][side] = true;
			if(now == 1 || now == 3){
				it1 = lower_bound(up.begin(), up.end(), make_pair(x-y, 0));
				it2 = upper_bound(up.begin(), up.end(), make_pair(x-y, k));
				for(it = it1; it != it2; it++){
					if(wop[(*it).second] == -1){
//						printf("update %i: %i\n", (*it).second, timer);
						wop[(*it).second] = timer - abs(x-pos[(*it).second].first);
					}
				}
			}else{
				it1 = lower_bound(down.begin(), down.end(), make_pair(x+y, 0));
				it2 = upper_bound(down.begin(), down.end(), make_pair(x+y, k));
				for(it = it1; it != it2; it++){
					if(wop[(*it).second] == -1){
//						printf("update %i: %i\n", (*it).second, timer);
						wop[(*it).second] = timer - abs(x-pos[(*it).second].first);
					}
				}
			}
			calculate();

		}
	}
	for (int i = 0; i < (int) wop.size(); i++){
		printf("%i\n", wop[i]);
	}

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e5 + 10;

struct node {
    int x, y;
}a[MAXN];

map <int, set <int> > mp[2];
set <int> :: iterator it;
ll ans[MAXN];

int main() {
    //freopen("in", "r", stdin);
    int n, m, p;
    scanf("%d%d%d", &m, &n, &p);
    for (int i = 1; i <= p; i++) {
        scanf("%d%d", &a[i].x, &a[i].y);
        mp[0][a[i].y - a[i].x].insert(i);
        mp[1][a[i].x + a[i].y].insert(i);
    }
    int k = 0, b = 0, nx = 0, ny = 0;
    ll nt = 0;
    memset(ans, -1, sizeof(ans));
    while (true) {
        for (it = mp[k][b].begin(); it != mp[k][b].end(); it++) {
            //if (ans[*it] != -1) continue;
            int x = a[*it].x, y = a[*it].y;
            ans[*it] = nt + (ll)abs(x - nx);
            if (k == 0) mp[1][x + y].erase(*it);
            else mp[0][y - x].erase(*it);
        }
        int kk = k == 0 ? 1 : -1;
        int xx[4] = {0, -b / kk, m, (n - b) / kk};
        int yy[4] = {b, 0, kk * m + b, n};
        int tx = nx, ty = ny;
        for (int i = 0; i < 4; i++) {
            if (xx[i] >= 0 && xx[i] <= m && yy[i] >= 0 && yy[i] <= n && xx[i] != tx && yy[i] != ty) {
                nt += (ll)abs(xx[i] - tx);
                nx = xx[i];
                ny = yy[i];
            }
        }
        if ((nx == m && ny == n) || (nx == 0 && ny == 0) || (nx == m && ny == 0) || (nx == 0 && ny == n)) break;
        kk = -kk;
        b = ny - kk * nx;
        k = kk == 1 ? 0 : 1;
    }
    for (int i = 1; i <= p; i++)
        printf("%I64d\n", ans[i]);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;

struct node {
    int x, y;
}a[MAXN];

map <int, set <int> > mp[2];
set <int> :: iterator it;
bool vis[2][MAXN];
int ans[MAXN];

int main() {
    //freopen("in", "r", stdin);
    int n, m, p;
    scanf("%d%d%d", &m, &n, &p);
    for (int i = 1; i <= p; i++) {
        scanf("%d%d", &a[i].x, &a[i].y);
        mp[0][a[i].y - a[i].x].insert(i);
        mp[1][a[i].x + a[i].y].insert(i);
    }
    int k = 0, b = 0, nx = 0, ny = 0, nt = 0;
    vis[0][0] = true;
    memset(ans, -1, sizeof(ans));
    while (true) {
        for (it = mp[k][b].begin(); it != mp[k][b].end(); it++) {
            //if (ans[*it] != -1) continue;
            int x = a[*it].x, y = a[*it].y;
            ans[*it] = nt + abs(x - nx);
            if (k == 0) mp[1][x + y].erase(*it);
            else mp[0][y - x].erase(*it);
        }
        int kk = k == 0 ? 1 : -1;
        int xx[4] = {0, -b / kk, m, (n - b) / kk};
        int yy[4] = {b, 0, kk * m + b, n};
        int tx = nx, ty = ny;
        for (int i = 0; i < 4; i++) {
            if (xx[i] >= 0 && xx[i] <= m && yy[i] >= 0 && yy[i] <= n && xx[i] != tx && yy[i] != ty) {
                nt += abs(xx[i] - tx);
                nx = xx[i];
                ny = yy[i];
            }
        }
        //printf("%d %d\n", nx, ny);
        if ((nx == m && ny == n) || (nx == 0 && ny == 0) || (nx == m && ny == 0) || (nx == 0 && ny == n)) break;
        kk = -kk;
        b = ny - kk * nx;
        k = kk == 1 ? 0 : 1;
        if (vis[k][b]) break;
        vis[k][b] = true;
    }
    for (int i = 1; i <= p; i++)
        printf("%d\n", ans[i]);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;

struct node {
    int x, y, id;
}a[MAXN];

map <int, set <int> > mp[2];
set <int> :: iterator it;
bool vis[2][MAXN];
int ans[MAXN];

int main() {
    //freopen("in", "r", stdin);
    int n, m, p;
    scanf("%d%d%d", &m, &n, &p);
    for (int i = 1; i <= p; i++) {
        scanf("%d%d", &a[i].x, &a[i].y);
        mp[0][a[i].y - a[i].x].insert(i);
        mp[1][a[i].x + a[i].y].insert(i);
    }
    int k = 0, b = 0, nx = 0, ny = 0, nt = 0;
    vis[0][0] = true;
    memset(ans, -1, sizeof(ans));
    while (true) {
        for (it = mp[k][b].begin(); it != mp[k][b].end(); it++) {
            if (ans[*it] != -1) continue;
            int x = a[*it].x, y = a[*it].y;
            int t = sqrt(((x - nx) * (x - nx) + (y - ny) * (y - ny)) / 2);
            ans[*it] = nt + t;
        }
        int kk = k == 0 ? 1 : -1;
        int xx[4] = {0, -b / kk, m, (n - b) / kk};
        int yy[4] = {b, 0, kk * m + b, n};
        int tx = nx, ty = ny;
        for (int i = 0; i < 4; i++) {
            if (xx[i] >= 0 && xx[i] <= m && yy[i] >= 0 && yy[i] <= n && xx[i] != tx && yy[i] != ty) {
                nt += sqrt(((xx[i] - nx) * (xx[i] - nx) + (yy[i] - ny) * (yy[i] - ny)) / 2);
                nx = xx[i];
                ny = yy[i];
            }
        }
        //printf("%d %d\n", nx, ny);
        if ((nx == m && ny == n) || (nx == 0 && ny == 0) || (nx == m && ny == 0) || (nx == 0 && ny == n)) break;
        kk = -kk;
        b = ny - kk * nx;
        k = kk == 1 ? 0 : 1;
        if (vis[k][b]) break;
        vis[k][b] = true;
    }
    for (int i = 1; i <= p; i++)
        printf("%d\n", ans[i]);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e5 + 10;

struct node {
    int x, y;
}a[MAXN];

map <int, set <int> > mp[2];
set <int> :: iterator it;
ll ans[MAXN];

int main() {
    //freopen("in", "r", stdin);
    int n, m, p;
    scanf("%d%d%d", &m, &n, &p);
    for (int i = 1; i <= p; i++) {
        scanf("%d%d", &a[i].x, &a[i].y);
        mp[0][a[i].y - a[i].x].insert(i);
        mp[1][a[i].x + a[i].y].insert(i);
    }
    int k = 0, b = 0, nx = 0, ny = 0;
    ll nt = 0;
    memset(ans, -1, sizeof(ans));
    while (true) {
        for (it = mp[k][b].begin(); it != mp[k][b].end(); it++) {
            //if (ans[*it] != -1) continue;
            int x = a[*it].x, y = a[*it].y;
            ans[*it] = nt + (ll)abs(x - nx);
            if (k == 0) mp[1][x + y].erase(*it);
            else mp[0][y - x].erase(*it);
        }
        int kk = k == 0 ? 1 : -1;
        int xx[4] = {0, -b / kk, m, (n - b) / kk};
        int yy[4] = {b, 0, kk * m + b, n};
        int tx = nx, ty = ny;
        for (int i = 0; i < 4; i++) {
            if (xx[i] >= 0 && xx[i] <= m && yy[i] >= 0 && yy[i] <= n && xx[i] != tx && yy[i] != ty) {
                nt += (ll)abs(xx[i] - tx);
                nx = xx[i];
                ny = yy[i];
            }
        }
        if ((nx == m && ny == n) || (nx == 0 && ny == 0) || (nx == m && ny == 0) || (nx == 0 && ny == n)) break;
        kk = -kk;
        b = ny - kk * nx;
        k = kk == 1 ? 0 : 1;
    }
    for (int i = 1; i <= p; i++)
        printf("%d\n", ans[i]);
    return 0;
}