#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
const int _ = 1000010;
int n, k, tot, b[_ << 1], r[_], a[_], h[_], c[_], cnt[_], ans[_];
inline int read() {
  int x = 0, f = 1;
  char ch;
  do {
    ch = getchar();
    if (ch == '-') f = -1;
  } while (ch > '9' || ch < '0');
  do {
    x = x * 10 + ch - '0';
    ch = getchar();
  } while (ch >= '0' && ch <= '9');
  return f * x;
}
struct segtree {
  int l, r, val;
} t[4000010];
int comp(int x, int y) { return r[x] < r[y] || (r[x] == r[y] && a[x] < a[y]); }
int lowbit(int x) { return x & -x; }
void add(int x, int k) {
  while (x <= tot) {
    c[x] += k;
    x += lowbit(x);
  }
}
void build(int p, int l, int r) {
  t[p].l = l;
  t[p].r = r;
  t[p].val = 0;
  if (l == r) return;
  int mid = (l + r) >> 1;
  build(p << 1, l, mid);
  build(p << 1 | 1, mid + 1, r);
}
int sum(int x) {
  int ans = 0;
  while (x) {
    ans += c[x];
    x -= lowbit(x);
  }
  return ans;
}
int zh(int x) { return lower_bound(b + 1, b + tot + 1, x) - b; }
struct xw {
  int x, y, c, num, as;
} qus[100010];
int cmp(const xw a, const xw b) { return a.c > b.c; }
void change(int p, int x, int d) {
  if (t[p].l == t[p].r) {
    t[p].val = max(t[p].val, d);
    return;
  }
  int mid = (t[p].l + t[p].r) >> 1;
  if (x <= mid)
    change(p << 1, x, d);
  else
    change(p << 1 | 1, x, d);
  t[p].val = max(t[p << 1].val, t[p << 1 | 1].val);
}
int ask(int p, int l, int r) {
  if (l <= t[p].l && t[p].r <= r) return t[p].val;
  int mid = (t[p].l + t[p].r) >> 1;
  int ans = 0;
  if (l <= mid) ans = max(ans, ask(p << 1, l, r));
  if (r > mid) ans = max(ans, ask(p << 1 | 1, l, r));
  return ans;
}
int cmp1(const xw a, const xw b) { return a.num < b.num; }
int main() {
  n = read();
  k = read();
  for (int i = 1; i <= n; i++) r[i] = read();
  for (int i = 1; i <= n; i++) {
    a[i] = read();
    b[++tot] = a[i];
    b[++tot] = a[i] + k;
    b[++tot] = a[i] - k;
    b[++tot] = a[i] - k - 1;
  }
  sort(b + 1, b + tot + 1);
  tot = unique(b + 1, b + tot + 1) - b - 1;
  for (int i = 1; i <= n; i++) h[i] = i;
  sort(h + 1, h + n + 1, comp);
  for (int i = 1; i <= n;) {
    int j = i;
    while (r[h[j + 1]] == r[h[i]]) {
      j++;
    }
    for (int q = i; q <= j; q++) {
      add(zh(a[h[q]]), 1);
    }
    for (int q = i; q <= j; q++) {
      int l = zh(a[h[q]] - k - 1);
      int r = zh(a[h[q]] + k);
      cnt[h[q]] = sum(r) - sum(l);
    }
    i = j + 1;
  }
  int Q = read();
  for (int i = 1; i <= Q; i++) {
    qus[i].num = i;
    qus[i].x = read();
    qus[i].y = read();
    qus[i].c = max(r[qus[i].x], r[qus[i].y]);
  }
  build(1, 1, tot);
  sort(qus + 1, qus + Q + 1, cmp);
  int sy = n;
  for (int i = 1; i <= Q; i++) {
    while (sy && r[h[sy]] >= qus[i].c) {
      change(1, zh(a[h[sy]]), cnt[h[sy]]);
      sy--;
    }
    int x = qus[i].x, y = qus[i].y;
    if (a[x] > a[y]) swap(x, y);
    int L1 = a[x] - k, L2 = a[y] - k, R1 = a[x] + k, R2 = a[y] + k, L, R;
    L = max(zh(L1), zh(L2));
    R = min(zh(R1), zh(R2));
    if (R < L) {
      ans[qus[i].num] = -1;
      continue;
    }
    int asn = ask(1, L, R);
    if (asn == 0)
      ans[qus[i].num] = -1;
    else
      ans[qus[i].num] = asn;
  }
  for (int i = 1; i <= Q; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}
