#include <bits/stdc++.h>
using namespace std;
constexpr int N = 1e5 + 10;
constexpr int LG = 20;
constexpr int MOD = 1e9 + 7;
constexpr int MOD2 = 1e9 + 9;
int n, k, r[N], a[N], fen[N], gp[N], tree[4 * N], q, x, y, ans[N];
vector<int> v, v2, vec[N];
vector<pair<int, pair<int, int>>> qu[N];
void add(int x, int val) {
  for (; x < N; x += x & (-x)) fen[x] += val;
}
int get(int x) {
  int ret = 0;
  for (; x; x -= x & (-x)) ret += fen[x];
  return ret;
}
void update(int b, int e, int p, int val, int idx) {
  if (b + 1 == e) {
    tree[idx] = max(tree[idx], val);
    return;
  }
  int mid = (b + e) >> 1, lidx = idx << 1, ridx = lidx | 1;
  if (p >= mid)
    update(mid, e, p, val, ridx);
  else
    update(b, mid, p, val, lidx);
  tree[idx] = max(tree[lidx], tree[ridx]);
}
int query(int b, int e, int l, int r, int idx) {
  if (b >= r || e <= l || e <= b || r <= l) return -1;
  if (b >= l && e <= r) return tree[idx];
  int mid = (b + e) >> 1, lidx = idx << 1, ridx = lidx | 1;
  return max(query(b, mid, l, r, lidx), query(mid, e, l, r, ridx));
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> r[i];
    v.push_back(r[i]);
  }
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    v2.push_back(a[i]);
  }
  v.push_back(-MOD);
  v2.push_back(-MOD);
  sort(v.begin(), v.end());
  sort(v2.begin(), v2.end());
  v.resize(unique(v.begin(), v.end()) - v.begin());
  v2.resize(unique(v2.begin(), v2.end()) - v2.begin());
  for (int i = 1; i <= n; i++)
    vec[lower_bound(v.begin(), v.end(), r[i]) - v.begin()].push_back(i);
  for (int i = 1; i <= n; i++) {
    for (auto j : vec[i])
      add(lower_bound(v2.begin(), v2.end(), a[j]) - v2.begin(), 1);
    for (auto j : vec[i]) {
      int p1 = upper_bound(v2.begin(), v2.end(), a[j] + k) - v2.begin() - 1;
      int p2 = lower_bound(v2.begin(), v2.end(), a[j] - k) - v2.begin() - 1;
      gp[j] = get(p1) - get(p2);
    }
  }
  cin >> q;
  memset(tree, -1, sizeof(tree));
  for (int i = 0; i < q; i++) {
    cin >> x >> y;
    int maharat = max(r[x], r[y]);
    maharat = lower_bound(v.begin(), v.end(), maharat) - v.begin();
    qu[maharat].push_back({i, {x, y}});
  }
  for (int i = n; i; i--) {
    for (auto j : vec[i])
      update(0, (int)v2.size(),
             lower_bound(v2.begin(), v2.end(), a[j]) - v2.begin(), gp[j], 1);
    for (auto j : qu[i]) {
      int p1 = upper_bound(v2.begin(), v2.end(),
                           min(a[j.second.first], a[j.second.second]) + k) -
               v2.begin();
      int p2 = lower_bound(v2.begin(), v2.end(),
                           max(a[j.second.first], a[j.second.second]) - k) -
               v2.begin();
      ans[j.first] = query(0, (int)v2.size(), p2, p1, 1);
    }
  }
  for (int i = 0; i < q; i++) cout << ans[i] << '\n';
  return 0;
}
