#include <bits/stdc++.h>
using namespace std;
inline int lowbit(int x) { return x & (-x); }
int N, K, r[100010], a[100010], Q, ans[100010], n, B[1000010], ord[100010],
    cnt[100010];
inline int cmp(int x, int y) {
  return r[x] < r[y] || (r[x] == r[y] && a[x] < a[y]);
}
inline int Hash(int x) { return lower_bound(B + 1, B + n + 1, x) - B; }
class tree {
 public:
  int c[1000010];
  void modify(int p, int v) {
    for (; p <= n; p += lowbit(p)) c[p] += v;
  }
  int query(int p) {
    int ret = 0;
    for (; p; p -= lowbit(p)) ret += c[p];
    return ret;
  }
} T;
class query {
 public:
  int x, y, id;
  bool operator<(const query &q) const {
    return max(r[x], r[y]) > max(r[q.x], r[q.y]);
  }
} q[100010];
class sgt {
 public:
  int mxv[2000010];
  void init() { fill(mxv, mxv + 2000001, -1e9); }
  void modify(int p, int l, int r, int pos, int v) {
    if (l == r) {
      mxv[p] = max(mxv[p], v);
      return;
    }
    int mid = (l + r) >> 1;
    if (pos <= mid)
      modify((p << 1), l, mid, pos, v);
    else
      modify(((p << 1) | 1), mid + 1, r, pos, v);
    mxv[p] = max(mxv[(p << 1)], mxv[((p << 1) | 1)]);
  }
  inline int query(int p, int l, int r, int ql, int qr) {
    if (ql <= l && r <= qr) return mxv[p];
    int mid = (l + r) >> 1, ret = -1e9;
    if (ql <= mid) ret = query((p << 1), l, mid, ql, qr);
    if (qr > mid) ret = max(ret, query(((p << 1) | 1), mid + 1, r, ql, qr));
    return ret;
  }
} SGT;
int main() {
  scanf("%d%d", &N, &K);
  for (int i = 1; i <= N; i++) scanf("%d", &r[i]);
  for (int i = 1; i <= N; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= N; i++) {
    B[++n] = a[i];
    B[++n] = a[i] + K;
    B[++n] = a[i] - K - 1;
    B[++n] = a[i] - K;
  }
  sort(B + 1, B + n + 1);
  n = unique(B + 1, B + n + 1) - B - 1;
  for (int i = 1; i <= N; i++) ord[i] = i;
  sort(ord + 1, ord + N + 1, cmp);
  for (int i = 1; i <= N;) {
    int j = i;
    while (r[ord[j + 1]] == r[ord[i]]) ++j;
    for (int k = i; k <= j; ++k) T.modify(Hash(a[ord[k]]), 1);
    for (int k = i; k <= j; ++k) {
      int l = Hash(a[ord[k]] - K - 1);
      int r = Hash(a[ord[k]] + K);
      cnt[ord[k]] = T.query(r) - T.query(l);
    }
    i = j + 1;
  }
  scanf("%d", &Q);
  for (int i = 1; i <= Q; i++) {
    scanf("%d%d", &q[i].x, &q[i].y);
    q[i].id = i;
  }
  sort(q + 1, q + Q + 1);
  SGT.init();
  for (int i = 1, j = N; i <= Q; i++) {
    while (j && r[ord[j]] >= max(r[q[i].x], r[q[i].y]))
      SGT.modify(1, 1, n, Hash(a[ord[j]]), cnt[ord[j]]), j--;
    if (a[q[i].x] > a[q[i].y]) swap(q[i].x, q[i].y);
    int tl = max(Hash(a[q[i].x] - K), Hash(a[q[i].y] - K)),
        tr = min(Hash(a[q[i].x] + K), Hash(a[q[i].y] + K));
    if (tl > tr)
      ans[q[i].id] = -1;
    else {
      int tmp = SGT.query(1, 1, n, tl, tr);
      if (tmp == -1e9)
        ans[q[i].id] = -1;
      else
        ans[q[i].id] = tmp;
    }
  }
  for (int i = 1; i <= Q; i++) printf("%d\n", ans[i]);
  return 0;
}
