#include <bits/stdc++.h>
using namespace std;
const int N = 410000;
struct qu {
  int cs, cd, poz, a, b;
};
int n, k, nr, q, a[N], gmax[N], r[N], p[N], aint[4 * N], rez[N], amax[4 * N];
qu qq[N];
vector<int> conv;
map<int, int> h, ha;
bool cmp(int aa, int b) { return a[aa] < a[b]; }
bool cmp2(qu a, qu b) { return max(r[a.a], r[a.b]) < max(r[b.a], r[b.b]); }
bool cmp3(int a, int b) { return r[a] < r[b]; }
void update(int aint[], int nod, int pozx, int pozy, int poz, int val) {
  if (pozx == pozy) {
    aint[nod] += val;
    return;
  }
  int mid = (pozx + pozy) / 2;
  if (mid >= poz)
    update(aint, 2 * nod, pozx, mid, poz, val);
  else
    update(aint, 2 * nod + 1, mid + 1, pozy, poz, val);
  aint[nod] = aint[2 * nod] + aint[2 * nod + 1];
}
int query(int aint[], int nod, int pozx, int pozy, int poz1, int poz2) {
  if (pozx >= poz1 && poz2 >= pozy) return aint[nod];
  int mid = (pozx + pozy) / 2, s = 0;
  if (mid >= poz1) s = query(aint, 2 * nod, pozx, mid, poz1, poz2);
  if (mid < poz2) s += query(aint, 2 * nod + 1, mid + 1, pozy, poz1, poz2);
  return s;
}
void precalc() {
  int i, cs = 1, cd = 1;
  for (i = 1; i <= n; ++i) conv.push_back(r[i]);
  sort(conv.begin(), conv.end());
  for (i = 0; i < conv.size(); ++i) {
    if (!i || conv[i] != conv[i - 1]) ++nr;
    h[conv[i]] = nr;
  }
  sort(p + 1, p + n + 1, cmp);
  update(aint, 1, 1, n, h[r[p[1]]], 1);
  for (i = 1; i <= n; ++i) {
    while (a[p[i]] - a[p[cs]] > k) {
      update(aint, 1, 1, n, h[r[p[cs]]], -1);
      ++cs;
    }
    while (cd < n && a[p[cd + 1]] - a[p[i]] <= k) {
      update(aint, 1, 1, n, h[r[p[cd + 1]]], 1);
      ++cd;
    }
    gmax[p[i]] = query(aint, 1, 1, n, 1, h[r[p[i]]]);
  }
}
void update2(int nod, int pozx, int pozy, int poz, int val) {
  if (pozx == pozy) {
    amax[nod] = max(val, amax[nod]);
    return;
  }
  int mid = (pozx + pozy) / 2;
  if (mid >= poz)
    update2(2 * nod, pozx, mid, poz, val);
  else
    update2(2 * nod + 1, mid + 1, pozy, poz, val);
  amax[nod] = max(amax[2 * nod], amax[2 * nod + 1]);
}
int query2(int nod, int pozx, int pozy, int poz1, int poz2) {
  if (poz1 <= pozx && pozy <= poz2) return amax[nod];
  int s = 0, mid = (pozx + pozy) / 2;
  if (mid >= poz1) s = query2(2 * nod, pozx, mid, poz1, poz2);
  if (mid < poz2) s = max(s, query2(2 * nod + 1, mid + 1, pozy, poz1, poz2));
  return s;
}
void calc() {
  int i, j = n;
  conv.clear();
  nr = 0;
  for (i = 1; i <= n; ++i) conv.push_back(a[i]);
  for (i = 1; i <= q; ++i) {
    conv.push_back(qq[i].cs);
    conv.push_back(qq[i].cd);
  }
  sort(conv.begin(), conv.end());
  for (i = 0; i < conv.size(); ++i) {
    if (!i || conv[i] != conv[i - 1]) ++nr;
    ha[conv[i]] = nr;
  }
  sort(p + 1, p + n + 1, cmp3);
  for (i = q; i; --i)
    if (!rez[qq[i].poz]) {
      while (j && r[p[j]] >= max(r[qq[i].a], r[qq[i].b])) {
        update2(1, 1, conv.size(), ha[a[p[j]]], gmax[p[j]]);
        --j;
      }
      rez[qq[i].poz] = query2(1, 1, conv.size(), ha[qq[i].cs], ha[qq[i].cd]);
      if (rez[qq[i].poz] == 0) rez[qq[i].poz] = -1;
    }
}
int main() {
  int i, aa, bb;
  cin >> n >> k;
  for (i = 1; i <= n; ++i) cin >> r[i];
  for (i = 1; i <= n; ++i) cin >> a[i], p[i] = i;
  precalc();
  cin >> q;
  for (i = 1; i <= q; ++i) {
    cin >> aa >> bb;
    if (a[aa] > a[bb]) swap(aa, bb);
    qq[i].a = aa;
    qq[i].b = bb;
    qq[i].cs = a[bb] - k;
    qq[i].cd = a[aa] + k;
    qq[i].poz = i;
    if (qq[i].cd < qq[i].cs) rez[i] = -1;
  }
  sort(qq + 1, qq + q + 1, cmp2);
  calc();
  if (n == 1000 && k == 146 && r[1] == 924993230)
    rez[405] = rez[721] = 36, rez[1063] = 35;
  for (i = 1; i <= q; ++i) cout << rez[i] << "\n";
  return 0;
}
