#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int n, k, Q, a[maxn], cnt, sum[maxn << 2], ans[maxn], R[maxn], A[maxn], f[maxn];
struct node {
  int r, a, id;
} e[maxn];
struct node2 {
  int r, a1, a2, id;
} q[maxn];
bool cmp(node a, node b) { return a.r < b.r; }
bool cmp2(node2 a, node2 b) { return a.r > b.r; }
void pushup(int o) { sum[o] = sum[o * 2] + sum[o * 2 + 1]; }
void update(int o, int l, int r, int pos, int v) {
  if (l == r) {
    sum[o] += v;
    return;
  }
  int mid = (l + r) >> 1;
  if (pos <= mid)
    update(o * 2, l, mid, pos, v);
  else
    update(o * 2 + 1, mid + 1, r, pos, v);
  pushup(o);
}
int query(int o, int l, int r, int x, int y) {
  if (x > y) return 0;
  if (x <= l && r <= y) return sum[o];
  int mid = (l + r) >> 1, res = 0;
  if (x <= mid) res += query(o * 2, l, mid, x, y);
  if (y > mid) res += query(o * 2 + 1, mid + 1, r, x, y);
  return res;
}
namespace seg2 {
int maxx[maxn << 2];
void pushup(int o) { maxx[o] = max(maxx[o * 2], maxx[o * 2 + 1]); }
void update(int o, int l, int r, int pos, int v) {
  if (l == r) {
    maxx[o] = max(maxx[o], v);
    return;
  }
  int mid = (l + r) >> 1;
  if (pos <= mid)
    update(o * 2, l, mid, pos, v);
  else
    update(o * 2 + 1, mid + 1, r, pos, v);
  pushup(o);
}
int query(int o, int l, int r, int x, int y) {
  if (x > y) return 0;
  if (x <= l && r <= y) return maxx[o];
  int mid = (l + r) >> 1, res = 0;
  if (x <= mid) res = max(res, query(o * 2, l, mid, x, y));
  if (y > mid) res = max(res, query(o * 2 + 1, mid + 1, r, x, y));
  return res;
}
}  // namespace seg2
int findl(int x) {
  int l = 1, r = cnt, res = cnt + 1;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (a[mid] >= x) {
      res = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }
  return res;
}
int findr(int x) {
  int l = 1, r = cnt, res = 0;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (a[mid] <= x) {
      res = mid;
      l = mid + 1;
    } else
      r = mid - 1;
  }
  return res;
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &e[i].r);
    R[i] = e[i].r;
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &e[i].a);
    A[i] = e[i].a;
    a[++cnt] = e[i].a;
  }
  sort(a + 1, a + 1 + cnt);
  cnt = unique(a + 1, a + 1 + cnt) - a - 1;
  for (int i = 1; i <= n; i++)
    e[i].a = lower_bound(a + 1, a + 1 + cnt, e[i].a) - a;
  sort(e + 1, e + 1 + n, cmp);
  int last = 0;
  for (int i = 1; i <= n; i = last + 1) {
    while (last + 1 <= n && e[last + 1].r == e[i].r) last++;
    for (int j = i; j <= last; j++) update(1, 1, cnt, e[j].a, 1);
    for (int j = i; j <= last; j++) {
      int temp = findl(a[e[j].a] - k), temp2 = findr(a[e[j].a] + k);
      f[j] = query(1, 1, cnt, temp, temp2);
    }
  }
  scanf("%d", &Q);
  for (int i = 1; i <= Q; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    q[i].r = max(R[x], R[y]);
    q[i].a1 = A[x];
    q[i].a2 = A[y];
    q[i].id = i;
  }
  sort(q + 1, q + 1 + Q, cmp2);
  int cur = n;
  for (int i = 1; i <= Q; i++) {
    while (cur >= 1 && e[cur].r >= q[i].r) {
      seg2::update(1, 1, cnt, e[cur].a, f[cur]);
      cur--;
    }
    int l1 = q[i].a1 - k, r1 = q[i].a1 + k;
    int l2 = q[i].a2 - k, r2 = q[i].a2 + k;
    int L = max(l1, l2), R = min(r1, r2);
    if (L > R) {
      ans[q[i].id] = -1;
      continue;
    }
    L = findl(L), R = findr(R);
    ans[q[i].id] = seg2::query(1, 1, cnt, L, R);
  }
  for (int i = 1; i <= Q; i++)
    if (ans[i] == 0) ans[i] = -1;
  for (int i = 1; i <= Q; i++) printf("%d\n", ans[i]);
  return 0;
}
