#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 21;
int n, N, m, k, a[maxn], c[maxn], R[maxn], mx[4 * maxn], ans[maxn], mp[maxn];
pair<int, int> b[maxn], q[maxn];
vector<int> seg[4 * maxn];
vector<int> compress[maxn];
vector<pair<int, int> > Q;
void build(int s = 0, int e = N, int x = 1) {
  if (e - s == 1) {
    for (auto &u : compress[s]) seg[x].push_back(u);
    sort(((seg[x]).begin()), ((seg[x]).end()));
    return;
  }
  int mid = (s + e) / 2;
  build(s, mid, 2 * x);
  build(mid, e, 2 * x + 1);
  seg[x].resize(((int)(seg[2 * x]).size()) + ((int)(seg[2 * x + 1]).size()));
  merge(((seg[2 * x]).begin()), ((seg[2 * x]).end()),
        ((seg[2 * x + 1]).begin()), ((seg[2 * x + 1]).end()), seg[x].begin());
}
int get(int l, int r, int val, int s = 0, int e = N, int x = 1) {
  if (l <= s && e <= r) {
    int ret =
        upper_bound(((seg[x]).begin()), ((seg[x]).end()), val) - seg[x].begin();
    return ret;
  }
  int mid = (s + e) / 2, ret = 0;
  if (l < mid) ret += get(l, r, val, s, mid, 2 * x);
  if (r > mid) ret += get(l, r, val, mid, e, 2 * x + 1);
  return ret;
}
void add(int ind, int val, int s = 0, int e = N, int x = 1) {
  if (e - s == 1) {
    mx[x] = max(mx[x], val);
    return;
  }
  int mid = (s + e) / 2;
  if (ind < mid)
    add(ind, val, s, mid, 2 * x);
  else
    add(ind, val, mid, e, 2 * x + 1);
  mx[x] = max(mx[2 * x], mx[2 * x + 1]);
}
int get_max(int l, int r, int s = 0, int e = N, int x = 1) {
  if (l <= s && e <= r) return mx[x];
  int mid = (s + e) / 2, ret = 0;
  if (l < mid) ret = max(ret, get_max(l, r, s, mid, 2 * x));
  if (r > mid) ret = max(ret, get_max(l, r, mid, e, 2 * x + 1));
  return ret;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> k;
  N = n;
  for (int i = 0; i < n; i++) {
    cin >> R[i];
    b[i].first = R[i];
    b[i].second = i;
  }
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    mp[i] = a[i];
  }
  sort(mp, mp + N);
  N = unique(mp, mp + N) - mp;
  for (int i = 0; i < n; i++) {
    int ind = lower_bound(mp, mp + N, a[i]) - mp;
    compress[ind].push_back(R[i]);
  }
  build();
  cin >> m;
  for (int i = 0; i < n; i++) {
    int l = lower_bound(mp, mp + N, a[i] - k) - mp;
    int r = upper_bound(mp, mp + N, a[i] + k) - mp;
    c[i] = get(l, r, R[i]);
  }
  for (int i = 0; i < m; i++) {
    int &v = q[i].first, &u = q[i].second;
    cin >> v >> u;
    v--, u--;
    if (a[v] > a[u]) swap(v, u);
    Q.push_back({max(R[v], R[u]), i});
  }
  sort(((Q).begin()), ((Q).end()));
  sort(b, b + n);
  reverse(b, b + n);
  for (int i = 0; i < n; i++) {
    while (Q.size() && b[i].first < Q.back().first) {
      int ind = Q.back().second;
      int v = q[ind].first, u = q[ind].second;
      Q.pop_back();
      if (a[u] - a[v] > 2 * k) {
        ans[ind] = -1;
        continue;
      }
      int l = lower_bound(mp, mp + N, a[u] - k) - mp;
      int r = upper_bound(mp, mp + N, a[v] + k) - mp;
      ans[ind] = get_max(l, r);
      if (!ans[ind]) ans[ind] = -1;
    }
    int tmp = b[i].first;
    while (i < n && b[i].first == tmp) {
      int v = b[i].second;
      int ind = lower_bound(mp, mp + N, a[v]) - mp;
      add(ind, c[v]);
      i++;
    }
    i--;
  }
  while (Q.size()) {
    int ind = Q.back().second;
    int v = q[ind].first, u = q[ind].second;
    Q.pop_back();
    if (a[u] - a[v] > 2 * k) {
      ans[ind] = -1;
      continue;
    }
    int l = lower_bound(mp, mp + N, a[u] - k) - mp;
    int r = upper_bound(mp, mp + N, a[v] + k) - mp;
    ans[ind] = get_max(l, r);
    if (!ans[ind]) ans[ind] = -1;
  }
  for (int i = 0; i < m; i++) cout << ans[i] << "\n";
}
