#include <bits/stdc++.h>
using namespace ::std;
const long long maxn = 1e5 + 500;
const long long mod = 1e9 + 7;
const long long inf = 1e9 + 500;
long long ans[maxn];
vector<pair<long long, pair<long long, pair<long long, long long> > > > que;
long long seg[maxn * 10];
long long segs[maxn * 10];
long long anss[maxn * 10];
long long ind[maxn * 10];
long long rr[maxn * 10];
long long aa[maxn * 10];
pair<long long, long long> z[maxn];
pair<long long, long long> w[maxn];
void inser(long long L, long long R, long long node, long long x,
           long long val) {
  seg[node] = max(seg[node], val);
  segs[node]++;
  if (L + 1 == R) {
    return;
  }
  long long mid = (L + R) / 2;
  if (x < mid) {
    inser(L, mid, 2 * node, x, val);
  } else {
    inser(mid, R, 2 * node + 1, x, val);
  }
}
long long find_max(long long L, long long R, long long node, long long l,
                   long long r) {
  if (l <= L && R <= r) {
    return seg[node];
  }
  if (R <= l || r <= L) {
    return 0;
  }
  long long mid = (L + R) / 2;
  return max(find_max(L, mid, 2 * node, l, r),
             find_max(mid, R, 2 * node + 1, l, r));
}
long long find_sum(long long L, long long R, long long node, long long l,
                   long long r) {
  if (l <= L && R <= r) {
    return segs[node];
  }
  if (R <= l || r <= L) {
    return 0;
  }
  long long mid = (L + R) / 2;
  return find_sum(L, mid, 2 * node, l, r) +
         find_sum(mid, R, 2 * node + 1, l, r);
}
void add(long long last, long long i, long long k, long long n) {
  for (long long j = last; j < i; j++) {
    inser(0, n, 1, lower_bound(w, w + n, make_pair(aa[z[j].second], 0LL)) - w,
          1);
  }
  for (long long j = last; j < i; j++) {
    ans[z[j].second] = find_sum(
        0, n, 1, lower_bound(w, w + n, make_pair(aa[z[j].second] - k, 0LL)) - w,
        lower_bound(w, w + n, make_pair(aa[z[j].second] + k + 1, 0LL)) - w);
  }
}
void mia(long long n, long long k) {
  long long last = 0;
  for (long long i = 1; i < n; i++) {
    if (z[i].first != z[i - 1].first) {
      add(last, i, k, n);
      last = i;
    }
  }
  add(last, n, k, n);
  fill(seg, seg + 10 * maxn, 0);
  fill(segs, segs + 10 * maxn, 0);
}
int main() {
  long long n, m, k;
  cin >> n >> k;
  for (long long i = 0; i < n; i++) {
    long long r;
    cin >> r;
    rr[i] = r;
    z[i] = make_pair(r, i);
  }
  for (long long i = 0; i < n; i++) {
    long long b;
    cin >> b;
    aa[i] = b;
    w[i] = make_pair(b, i);
  }
  sort(z, z + n);
  sort(w, w + n);
  for (long long i = 0; i < n; i++) {
    ind[w[i].second] = i;
  }
  mia(n, k);
  cin >> m;
  for (long long i = 0; i < m; i++) {
    long long v, u, ui, vi;
    cin >> u >> v;
    u--;
    v--;
    ui = u;
    vi = v;
    v = ind[v];
    u = ind[u];
    if (v < u) {
      swap(v, u);
    }
    long long uu =
        lower_bound(w, w + n, make_pair(w[u].first + k + 1, 0LL)) - w - 1;
    long long vv = lower_bound(w, w + n, make_pair(w[v].first - k, 0LL)) - w;
    if (vv <= uu) {
      que.push_back(
          make_pair(max(rr[ui], rr[vi]), make_pair(i, make_pair(vv, uu))));
    } else {
      anss[i] = -1;
    }
  }
  sort(que.begin(), que.end());
  long long p = n - 1;
  for (long long i = ((int)que.size()) - 1; i >= 0; i--) {
    while (p >= 0 && z[p].first >= que[i].first) {
      inser(0, n, 1, lower_bound(w, w + n, make_pair(aa[z[p].second], 0LL)) - w,
            ans[z[p].second]);
      p--;
    }
    anss[que[i].second.first] = find_max(0, n, 1, que[i].second.second.first,
                                         que[i].second.second.second + 1);
  }
  for (long long i = 0; i < m; i++) {
    if (anss[i] >= 2) {
      cout << anss[i] << endl;
    } else {
      cout << "-1\n";
    }
  }
}
