#include <bits/stdc++.h>
using namespace std;
const int MAXn = 100 * 1000 + 100;
int n, k;
pair<int, pair<int, int> > p[MAXn];
int rID[MAXn];
int ages[MAXn], ac;
int cnt[4 * MAXn];
int ldr[MAXn];
int Q;
pair<int, pair<int, int> > q[MAXn];
int val[4 * MAXn];
int ans[MAXn];
bool cmp(const pair<int, pair<int, int> > &A,
         const pair<int, pair<int, int> > &B) {
  return max(p[A.first].first, p[A.second.first].first) <
         max(p[B.first].first, p[B.second.first].first);
}
void add(int x, int l, int r, int pos) {
  if (pos < l || r <= pos) return;
  cnt[x]++;
  if (r - l == 1) return;
  int m = (l + r) / 2;
  add(2 * x + 1, l, m, pos);
  add(2 * x + 2, m, r, pos);
}
int req(int x, int l, int r, int p, int q) {
  if (q <= l || r <= p) return 0;
  if (p <= l && r <= q) return cnt[x];
  int m = (l + r) / 2;
  return req(2 * x + 1, l, m, p, q) + req(2 * x + 2, m, r, p, q);
}
void add2(int x, int l, int r, int pos, int v) {
  if (pos < l || r <= pos) return;
  (val[x] = max(val[x], v));
  if (r - l == 1) return;
  int m = (l + r) / 2;
  add2(2 * x + 1, l, m, pos, v);
  add2(2 * x + 2, m, r, pos, v);
}
int req2(int x, int l, int r, int p, int q) {
  if (q <= l || r <= p) return 0;
  if (p <= l && r <= q) return val[x];
  int m = (l + r) / 2;
  return max(req2(2 * x + 1, l, m, p, q), req2(2 * x + 2, m, r, p, q));
}
int main() {
  cin >> n >> k;
  for (int i = 0, _n = (n); i < _n; i++) cin >> p[i].first;
  for (int i = 0, _n = (n); i < _n; i++) cin >> p[i].second.first;
  for (int i = 0, _n = (n); i < _n; i++) p[i].second.second = i;
  sort(p, p + n);
  for (int i = 0, _n = (n); i < _n; i++) rID[p[i].second.second] = i;
  for (int i = 0, _n = (n); i < _n; i++) ages[i] = p[i].second.first;
  sort(ages, ages + n);
  ac = unique(ages, ages + n) - ages;
  for (int i = 0, _n = (n); i < _n; i++) {
    int j = i;
    while (j < n && p[j].first == p[i].first) {
      add(0, 0, ac, lower_bound(ages, ages + ac, p[j].second.first) - ages);
      j++;
    }
    for (; i < j; i++) {
      int L = lower_bound(ages, ages + ac, p[i].second.first - k) - ages,
          R = upper_bound(ages, ages + ac, p[i].second.first + k) - ages;
      ldr[i] = req(0, 0, ac, L, R);
    }
    i--;
  }
  cin >> Q;
  for (int i = 0, _n = (Q); i < _n; i++) {
    cin >> q[i].first >> q[i].second.first;
    q[i].first--, q[i].second.first--;
    q[i].first = rID[q[i].first], q[i].second.first = rID[q[i].second.first];
    if (p[q[i].first].second.first > p[q[i].second.first].second.first)
      swap(q[i].first, q[i].second.first);
    q[i].second.second = i;
  }
  sort(q, q + Q, cmp);
  int pos = n;
  for (int i = Q; i--;) {
    int mxr = max(p[q[i].first].first, p[q[i].second.first].first);
    while (pos && p[pos - 1].first >= mxr)
      --pos, add2(0, 0, ac,
                  lower_bound(ages, ages + ac, p[pos].second.first) - ages,
                  ldr[pos]);
    int L = lower_bound(ages, ages + ac,
                        p[q[i].second.first].second.first - k) -
            ages,
        R = upper_bound(ages, ages + ac, p[q[i].first].second.first + k) - ages;
    if (R - L > 0) ans[q[i].second.second] = req2(0, 0, ac, L, R);
    if (ans[q[i].second.second] == 0) ans[q[i].second.second] = -1;
  }
  for (int i = 0, _n = (Q); i < _n; i++) cout << ans[i] << endl;
  {
    int _;
    cin >> _;
  }
}
