#include <bits/stdc++.h>
using namespace std;
const int N = 100005, M = 1e9;
struct P {
  int a, r, id, ans;
} a[N];
int tot1, tot2;
int n, m, q, K;
struct T1 {
  int l, r, num;
} tree1[N << 6];
struct T2 {
  int l, r, num;
} tree2[N << 6];
struct Ask {
  int x, y, l, r, val, id, ans;
} Q[N];
bool cmp1(P a, P b) { return a.r < b.r; }
bool cmp2(Ask a, Ask b) { return a.val < b.val; }
bool cmp3(Ask a, Ask b) { return a.id < b.id; }
bool cmp4(P a, P b) { return a.id < b.id; }
void ins1(int &i, int l, int r, int pos) {
  if (i == 0) i = ++tot1;
  if (l == r) {
    tree1[i].num++;
    return;
  }
  int mid = (l + r) / 2;
  if (pos <= mid)
    ins1(tree1[i].l, l, mid, pos);
  else
    ins1(tree1[i].r, mid + 1, r, pos);
  tree1[i].num = tree1[tree1[i].l].num + tree1[tree1[i].r].num;
}
int query1(int i, int l, int r, int L, int R) {
  if (L > r || R < l || i == 0) return 0;
  if (L <= l && R >= r) return tree1[i].num;
  int mid = (l + r) / 2;
  return query1(tree1[i].l, l, mid, L, R) +
         query1(tree1[i].r, mid + 1, r, L, R);
}
void ins2(int &i, int l, int r, int pos, int num) {
  if (i == 0) i = ++tot2;
  if (l == r) {
    tree2[i].num = max(tree2[i].num, num);
    return;
  }
  int mid = (l + r) / 2;
  if (pos <= mid)
    ins2(tree2[i].l, l, mid, pos, num);
  else
    ins2(tree2[i].r, mid + 1, r, pos, num);
  tree2[i].num = max(tree2[tree2[i].l].num, tree2[tree2[i].r].num);
}
int query2(int i, int l, int r, int L, int R) {
  if (L > r || R < l || i == 0) return -1;
  if (L <= l && R >= r) return tree2[i].num;
  int mid = (l + r) / 2;
  return max(query2(tree2[i].l, l, mid, L, R),
             query2(tree2[i].r, mid + 1, r, L, R));
}
int main() {
  int i, j, k, o, p, l;
  int root = 0;
  scanf("%d %d", &n, &K);
  for (i = 1; i <= n; i++) scanf("%d", &a[i].r), a[i].id = i;
  for (i = 1; i <= n; i++) scanf("%d", &a[i].a);
  sort(a + 1, a + 1 + n, cmp1);
  for (i = 1; i <= n; i++) {
    j = i;
    while (a[j].r == a[i].r) j++;
    j--;
    for (k = i; k <= j; k++) ins1(root, 1, M, a[k].a);
    for (k = i; k <= j; k++) {
      a[k].ans = query1(root, 1, M, max(1, a[k].a - K), min(a[k].a + K, M));
    }
    i = j;
  }
  scanf("%d", &m);
  sort(a + 1, a + 1 + n, cmp4);
  for (i = 1; i <= m; i++) {
    Q[i].id = i;
    scanf("%d %d", &Q[i].x, &Q[i].y);
    if (a[Q[i].x].a > a[Q[i].y].a) swap(Q[i].x, Q[i].y);
    Q[i].l = a[Q[i].y].a - K, Q[i].r = a[Q[i].x].a + K,
    Q[i].val = max(a[Q[i].x].r, a[Q[i].y].r);
  }
  sort(Q + 1, Q + 1 + m, cmp2);
  sort(a + 1, a + 1 + n, cmp1);
  int now1 = n, now2 = m;
  root = 0;
  while (now2 > 0) {
    while (now1 > 0 && a[now1].r >= Q[now2].val) {
      ins2(root, 1, M, a[now1].a, a[now1].ans);
      now1--;
    }
    while (now2 > 0 && (Q[now2].val > a[now1].r || !now1)) {
      if (Q[now2].l <= Q[now2].r)
        Q[now2].ans = query2(root, 1, M, Q[now2].l, Q[now2].r);
      now2--;
    }
  }
  sort(Q + 1, Q + 1 + m, cmp3);
  for (i = 1; i <= m; i++) printf("%d\n", Q[i].ans == 0 ? -1 : Q[i].ans);
  return 0;
}
