#include <bits/stdc++.h>
using namespace std;
pair<int, int> a[100005];
pair<pair<int, int>, int> c[100005];
pair<pair<int, pair<int, int> >, int> q[100005];
int b[100005], bn, t[400020], s[100005];
void upd(int x, int c, int l, int r, int rt, bool ty) {
  if (l >= r) {
    t[rt] += c;
    return;
  }
  int m = (l + r) / 2;
  if (x <= m)
    upd(x, c, l, m, rt * 2, ty);
  else
    upd(x, c, m + 1, r, rt * 2 + 1, ty);
  if (ty)
    t[rt] = max(t[rt * 2], t[rt * 2 + 1]);
  else
    t[rt] = t[rt * 2] + t[rt * 2 + 1];
}
int qy(int L, int R, int l, int r, int rt, bool ty) {
  if (l >= L && r <= R) return t[rt];
  int m = (l + r) / 2, s1 = 0, s2 = 0;
  if (L <= m) s1 = qy(L, R, l, m, rt * 2, ty);
  if (R > m) s2 = qy(L, R, m + 1, r, rt * 2 + 1, ty);
  if (ty)
    return max(s1, s2);
  else
    return s1 + s2;
}
int main() {
  int n, m, i, j, k, x, y;
  scanf("%d%d", &n, &k);
  for (i = 0; i < n; i++) scanf("%d", &a[i].first);
  for (i = 0; i < n; i++) scanf("%d", &b[i]), a[i].second = b[i];
  sort(b, b + n);
  bn = unique(b, b + n) - b;
  b[bn] = (1 << 31) - 1;
  for (scanf("%d", &m), i = 0; i < m; i++) {
    scanf("%d%d", &x, &y);
    x--, y--;
    if (a[x].second > a[y].second) swap(x, y);
    q[i] = make_pair(
        make_pair(max(a[x].first, a[y].first),
                  make_pair(lower_bound(b, b + bn, a[y].second - k) - b,
                            upper_bound(b, b + bn, a[x].second + k) - b - 1)),
        i);
  }
  sort(a, a + n);
  sort(q, q + m);
  for (i = 0; i < bn; i++) c[i].second = i;
  for (j = i = 0; i < n; i++) {
    while (j < n && a[j].first <= a[i].first)
      upd(a[j].second = lower_bound(b, b + bn, a[j].second) - b, 1, 0, bn - 1,
          1, 0),
          j++;
    c[a[i].second].first.first = a[i].first;
    c[a[i].second].first.second =
        max(c[a[i].second].first.second,
            qy(lower_bound(b, b + bn, b[a[i].second] - k) - b,
               upper_bound(b, b + bn, b[a[i].second] + k) - b - 1, 0, bn - 1, 1,
               0));
  }
  sort(c, c + bn);
  memset(t, 0, sizeof(t));
  for (i = m - 1, j = bn - 1; i >= 0; i--) {
    while (j >= 0 && c[j].first.first >= q[i].first.first)
      upd(c[j].second, c[j].first.second, 0, bn - 1, 1, 1), j--;
    s[q[i].second] = q[i].first.second.first > q[i].first.second.second
                         ? -1
                         : qy(q[i].first.second.first, q[i].first.second.second,
                              0, bn - 1, 1, 1);
  }
  for (i = 0; i < m; i++) printf("%d\n", s[i] > 0 ? s[i] : -1);
  return 0;
}
