#include <bits/stdc++.h>
using namespace std;
int n, k, back[100005];
pair<pair<int, int>, int> sm[100005];
pair<int, int> sm2[100005];
int a[100005], r[100005];
int lo[100005], hi[100005];
int ans[100005];
int out[100005];
int bit[100005];
void enable(int i) {
  for (; i < 100005; i += (i & -i)) bit[i]++;
}
int query(int i) {
  int r = 0;
  for (; i > 0; i -= (i & -i)) r += bit[i];
  return r;
}
int query(int l, int r) { return query(r) - query(l - 1); }
struct asdf {
  int l, r, rspon, i;
} qry[100005];
bool cmp(const asdf &a, const asdf &b) { return a.rspon > b.rspon; }
int tree[4 * 100005];
void sset(int i, int l, int r, int si, int v) {
  if (l == r) {
    tree[i] = v;
    return;
  }
  if (si <= (r + l) / 2)
    sset((2 * (i)), l, (l + r) / 2, si, v);
  else
    sset(((2 * (i)) + 1), (l + r) / 2 + 1, r, si, v);
  tree[i] = max(tree[(2 * (i))], tree[((2 * (i)) + 1)]);
}
int squery(int i, int l, int r, int sl, int sr) {
  if (l > sr || r < sl) return -1;
  if (l >= sl && r <= sr) return tree[i];
  return max(squery((2 * (i)), l, (l + r) / 2, sl, sr),
             squery(((2 * (i)) + 1), (l + r) / 2 + 1, r, sl, sr));
}
int main() {
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &sm[i].first.second), sm[i].second = i;
  for (int i = 1; i <= n; i++) scanf("%d", &sm[i].first.first);
  sort(sm + 1, sm + 1 + n);
  for (int i = 1; i <= n; i++) {
    back[sm[i].second] = i;
    a[i] = sm[i].first.first;
    r[i] = sm[i].first.second;
  }
  for (int i = 1; i <= n; i++) {
    int l = 1, h = i, m;
    while (l < h) {
      m = (l + h) / 2;
      if (a[i] - a[m] <= k)
        h = m;
      else
        l = m + 1;
    }
    lo[i] = l;
    l = i, h = n;
    while (l < h) {
      m = (l + h + 1) / 2;
      if (a[m] - a[i] <= k)
        l = m;
      else
        h = m - 1;
    }
    hi[i] = l;
    sm2[i] = make_pair(r[i], i);
  }
  sort(sm2 + 1, sm2 + 1 + n);
  for (int i = 1; i <= n;) {
    int j = i;
    while (j <= n && sm2[i].first == sm2[j].first) {
      enable(sm2[j].second);
      j++;
    }
    j = i;
    while (j <= n && sm2[i].first == sm2[j].first) {
      int x = sm2[j].second;
      ans[x] = query(lo[x], hi[x]);
      j++;
    }
    i = j;
  }
  int q;
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    x = back[x], y = back[y];
    pair<int, int> i1 = make_pair(a[x] - k, a[x] + k);
    pair<int, int> i2 = make_pair(a[y] - k, a[y] + k);
    if (i2.first <= i2.second && i2.first <= i1.second) {
      pair<int, int> ival =
          make_pair(max(i1.first, i2.first), min(i1.second, i2.second));
      int qr = max(r[x], r[y]);
      int lo = lower_bound(a + 1, a + 1 + n, ival.first) - a;
      int hi = n;
      if (a[n] > ival.second)
        hi = upper_bound(a + 1, a + 1 + n, ival.second) - a - 1;
      qry[i].i = i;
      qry[i].rspon = qr;
      qry[i].l = lo;
      qry[i].r = hi;
    } else
      out[i] = -1;
  }
  sort(qry + 1, qry + 1 + q, cmp);
  for (int i = 1; i <= n; i++) sset(1, 1, n, i, -1);
  int ptr = n;
  for (int i = 1; i <= q; i++) {
    while (ptr > 0 && sm2[ptr].first >= qry[i].rspon) {
      sset(1, 1, n, sm2[ptr].second, ans[sm2[ptr].second]);
      ptr--;
    }
    out[qry[i].i] = squery(1, 1, n, qry[i].l, qry[i].r);
  }
  for (int i = 1; i <= q; i++) printf("%d\n", out[i]);
  return 0;
}
