#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10, LOG = 21, INF = 1e9;
int seg[LOG][N], a[N], ans[N], s_mx[LOG][4 * N];
pair<int, int> seg2[LOG][N];
int tr[N];
pair<int, int> r[N], ls[N];
void build_1(int lg, int s, int e) {
  if (e - s < 2) {
    int id = r[s].second;
    seg[lg][s] = a[id];
    return;
  }
  int mid = (s + e) / 2;
  build_1(lg + 1, s, mid);
  build_1(lg + 1, mid, e);
  merge(seg[lg + 1] + s, seg[lg + 1] + mid, seg[lg + 1] + mid, seg[lg + 1] + e,
        seg[lg] + s);
  return;
}
int get_1(int lg, int L, int R, int s, int e, int val) {
  if (L <= s && e <= R) {
    int ind = lower_bound(seg[lg] + s, seg[lg] + e, val) - seg[lg] - s;
    return (e - s) - ind;
  }
  if (L >= e || s >= R) return 0;
  int mid = (s + e) / 2;
  return get_1(lg + 1, L, R, s, mid, val) + get_1(lg + 1, L, R, mid, e, val);
}
void build_2(int lg, int s, int e) {
  if (e - s < 2) {
    int id = ls[s].second;
    seg2[lg][s] = {tr[id], ans[id]};
    s_mx[lg][s] = ans[id];
    return;
  }
  int mid = (s + e) / 2;
  build_2(lg + 1, s, mid);
  build_2(lg + 1, mid, e);
  merge(seg2[lg + 1] + s, seg2[lg + 1] + mid, seg2[lg + 1] + mid,
        seg2[lg + 1] + e, seg2[lg] + s);
  for (int i = e - 1; i >= s; i--) {
    if (i == e - 1)
      s_mx[lg][e - 1] = seg2[lg][e - 1].second;
    else
      s_mx[lg][i] = max(s_mx[lg][i + 1], seg2[lg][i].second);
  }
  return;
}
int get_2(int lg, int L, int R, int s, int e, int val) {
  if (L <= s && e <= R) {
    int ind = lower_bound(seg2[lg] + s, seg2[lg] + e, pair<int, int>(val, -1)) -
              seg2[lg];
    if (ind == e) return -1;
    return s_mx[lg][ind];
  }
  if (L >= e || s >= R) return -1;
  int mid = (s + e) / 2;
  return max(get_2(lg + 1, L, R, s, mid, val),
             get_2(lg + 1, L, R, mid, e, val));
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> r[i].first;
    r[i].second = i;
    tr[i] = r[i].first;
  }
  sort(r, r + n);
  for (int i = 0; i < n; i++) cin >> a[i];
  build_1(0, 0, n);
  for (int i = n - 1; i >= 0; i--) {
    int id = r[i].second;
    int hi = upper_bound(r, r + n, pair<int, int>(r[i].first, INF)) - r;
    ans[id] =
        get_1(0, 0, hi, 0, n, a[id] - k) - get_1(0, 0, hi, 0, n, a[id] + k + 1);
    ls[i] = {a[i], i};
  }
  sort(ls, ls + n);
  build_2(0, 0, n);
  int q;
  cin >> q;
  for (int __ = 0; __ < q; __++) {
    int i, j;
    cin >> i >> j;
    i--;
    j--;
    if (a[i] < a[j]) swap(i, j);
    int mxR = a[j] + k, mnL = a[i] - k;
    if (mxR < mnL) {
      cout << -1 << '\n';
      continue;
    }
    int indl = lower_bound(ls, ls + n, pair<int, int>(mnL, -1)) - ls;
    int indr = upper_bound(ls, ls + n, pair<int, int>(mxR, INF)) - ls;
    cout << get_2(0, indl, indr, 0, n, max(tr[i], tr[j])) << '\n';
  }
}
