#include <bits/stdc++.h>
using namespace std;
struct node {
  int x, y, id, flag, ans;
} p[1300000], p2[1300000], p3[1300000];
int n, k;
vector<int> V;
map<int, int> H;
struct treenode {
  int L, R;
  int sum, lazy;
};
struct Question {
  int l, r, lead, flag, ans;
};
vector<Question> T;
treenode tree[2300000];
inline void push_up(int o) {
  tree[o].sum = max(tree[2 * o].sum, tree[2 * o + 1].sum);
}
inline void build_tree(int L, int R, int o) {
  tree[o].L = L, tree[o].R = R, tree[o].sum = tree[o].lazy = 0;
  if (R > L) {
    int mid = (L + R) >> 1;
    build_tree(L, mid, o * 2);
    build_tree(mid + 1, R, o * 2 + 1);
  }
}
inline void updata(int QL, int QR, int v, int o) {
  int L = tree[o].L, R = tree[o].R;
  if (QL <= L && R <= QR)
    tree[o].sum = max(tree[o].sum, v);
  else {
    int mid = (L + R) >> 1;
    if (QL <= mid) updata(QL, QR, v, o * 2);
    if (QR > mid) updata(QL, QR, v, o * 2 + 1);
    push_up(o);
  }
}
inline int query(int QL, int QR, int o) {
  int L = tree[o].L, R = tree[o].R;
  if (QL <= L && R <= QR)
    return tree[o].sum;
  else {
    int mid = (L + R) >> 1;
    int res = 0;
    if (QL <= mid) res = max(res, query(QL, QR, 2 * o));
    if (QR > mid) res = max(res, query(QL, QR, 2 * o + 1));
    push_up(o);
    return res;
  }
}
struct Bit {
  vector<int> a;
  int sz;
  void init(int n) {
    sz = n;
    for (int i = 1; i <= n + 5; i++) a.push_back(0);
  }
  int lowbit(int x) { return x & (-x); }
  int query(int x) {
    if (x == 0) return 0;
    int ans = 0;
    for (; x; x -= lowbit(x)) ans += a[x];
    return ans;
  }
  void updata(int x, int v) {
    if (x == 0) return;
    for (; x < sz; x += lowbit(x)) a[x] += v;
  }
} bit;
bool cmp(node a, node b) {
  if (a.x == b.x) return a.flag > b.flag;
  return a.x < b.x;
}
bool cmp2(Question a, Question b) {
  if (a.lead == b.lead) return a.flag < b.flag;
  return a.lead > b.lead;
}
int ans[1300000];
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) scanf("%d", &p[i].x);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &p[i].y);
    V.push_back(p[i].y);
    V.push_back(p[i].y - k);
    V.push_back(p[i].y + k);
    p[i].id = i;
    p[i].flag = 1;
  }
  sort(V.begin(), V.end());
  V.erase(unique(V.begin(), V.end()), V.end());
  for (int i = 0; i < V.size(); i++) H[V[i]] = i + 1;
  for (int i = 1; i <= n; i++) p2[i] = p[i];
  for (int i = 1; i <= n; i++) {
    p2[i + n] = p[i];
    p2[i + n].flag = -1;
  }
  sort(p2 + 1, p2 + 2 * n + 1, cmp);
  bit.init(10 * n);
  for (int i = 1; i <= 2 * n; i++) {
    if (p2[i].flag == 1)
      bit.updata(H[p2[i].y], 1);
    else {
      int sum = bit.query(H[p2[i].y + k]) - bit.query(H[p2[i].y - k] - 1);
      p[p2[i].id].ans = sum;
    }
  }
  for (int i = 1; i <= n; i++) {
    Question now;
    now.l = p[i].y;
    now.r = p[i].y;
    now.lead = p[i].x;
    now.ans = p[i].ans;
    now.flag = 0;
    T.push_back(now);
  }
  int q;
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    if (p[x].y > p[y].y) swap(x, y);
    Question now;
    now.l = p[y].y - k;
    now.r = p[x].y + k;
    now.lead = max(p[x].x, p[y].x);
    now.ans = 0;
    now.flag = i;
    T.push_back(now);
  }
  sort(T.begin(), T.end(), cmp2);
  build_tree(1, 5 * n + 5 * q, 1);
  for (int i = 0; i < T.size(); i++) {
    if (T[i].flag == 0)
      updata(H[T[i].l], H[T[i].r], T[i].ans, 1);
    else {
      if (T[i].l > T[i].r)
        ans[T[i].flag] = -1;
      else {
        int ans1 = query(H[T[i].l], H[T[i].r], 1);
        ans[T[i].flag] = ans1;
      }
    }
  }
  for (int i = 1; i <= q; i++)
    if (ans[i] < 2)
      printf("-1\n");
    else
      printf("%d\n", ans[i]);
  return 0;
}
