#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
const int maxn5 = 1e5 + 10;
const int maxnt = 5e5 + 10;
const int maxn3 = 1e3 + 10;
const int mod = 1e9 + 7;
const int ssq = 460;
const long long inf = 2e18;
map<int, int> st, ft;
int val[maxn5], out[maxn5], mx[maxnt], sum[maxnt];
pair<int, int> a[maxn5], per[maxn5];
vector<pair<int, int>> av;
vector<pair<pair<int, int>, int>> sar;
vector<pair<pair<int, int>, pair<int, int>>> req;
void build(int l, int r, int v) {
  sum[v] = mx[v] = 0;
  if (r - l == 1) return;
  int mid = (l + r) >> 1;
  build(l, mid, v * 2);
  build(mid, r, v * 2 + 1);
  return;
}
void update(int l, int r, int ind, int val, int v) {
  if (r - l == 1) {
    sum[v] = mx[v] = val;
    return;
  }
  int mid = (l + r) >> 1;
  if (ind < mid)
    update(l, mid, ind, val, v * 2);
  else
    update(mid, r, ind, val, v * 2 + 1);
  sum[v] = sum[v * 2] + sum[v * 2 + 1];
  mx[v] = max(mx[v * 2], mx[v * 2 + 1]);
  return;
}
int get_sum(int l, int r, int lq, int rq, int v) {
  if (lq >= rq) return 0;
  if (lq == l && r == rq) return sum[v];
  int mid = (l + r) >> 1;
  return get_sum(l, mid, lq, min(rq, mid), v * 2) +
         get_sum(mid, r, max(lq, mid), rq, v * 2 + 1);
}
int get_max(int l, int r, int lq, int rq, int v) {
  if (lq >= rq) return 0;
  if (lq == l && r == rq) return mx[v];
  int mid = (l + r) >> 1;
  return max(get_max(l, mid, lq, min(rq, mid), v * 2),
             get_max(mid, r, max(lq, mid), rq, v * 2 + 1));
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) cin >> a[i].second;
  for (int i = 0; i < n; i++) cin >> a[i].first;
  for (int i = 0; i < n; i++) per[i] = a[i];
  sort(a, a + n);
  for (int i = 0; i < n; i++) ft[a[i].first] = i;
  for (int i = n - 1; i >= 0; i--) st[a[i].first] = i;
  for (int i = 0; i < n; i++) av.push_back({a[i].second, i});
  sort(av.begin(), av.end());
  int ind = 0;
  for (auto [res, i] : av) {
    while (ind < n && av[ind].first == res) {
      update(0, n, av[ind].second, 1, 1);
      ind++;
    }
    int from = st.lower_bound(a[i].first - k)->second;
    auto it = ft.upper_bound(a[i].first + k);
    it--;
    val[i] = get_sum(0, n, from, (it->second) + 1, 1);
    sar.push_back({{a[i].second, val[i]}, i});
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    if (per[a].second < per[b].second) swap(a, b);
    req.push_back(
        {{per[a].second, i},
         {min(per[a].first, per[b].first), max(per[a].first, per[b].first)}});
  }
  sort(req.begin(), req.end());
  sort(sar.begin(), sar.end());
  reverse(sar.begin(), sar.end());
  reverse(req.begin(), req.end());
  ind = 0;
  build(0, n, 1);
  for (auto [p1, p2] : req) {
    while (ind < n && sar[ind].first.first >= p1.first) {
      update(0, n, sar[ind].second, sar[ind].first.second, 1);
      ind++;
    }
    int from = st.lower_bound(p2.second - k)->second;
    auto it = ft.upper_bound(p2.first + k);
    it--;
    out[p1.second] = get_max(0, n, from, (it->second) + 1, 1);
    if (out[p1.second] == 0) out[p1.second] = -1;
  }
  for (int i = 0; i < q; i++) cout << out[i] << '\n';
}
