#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5, LG = 17 + 1;
int n, k, r[N], a[N], L[N], R[N], sz[N];
int p[N], pos[N], fen[N], seg[LG][N], mx[LG][N];
vector<int> vec[N];
inline bool cmp1(int i, int j) { return r[i] < r[j]; }
inline bool cmp2(int i, int j) { return a[i] < a[j]; }
void build(int lvl = 0, int st = 0, int en = n) {
  if (en - st == 1) {
    mx[lvl][st] = sz[seg[lvl][st] = p[st]];
    return;
  }
  int mid = st + en >> 1;
  build(lvl + 1, st, mid);
  build(lvl + 1, mid, en);
  merge(seg[lvl + 1] + st, seg[lvl + 1] + mid, seg[lvl + 1] + mid,
        seg[lvl + 1] + en, seg[lvl] + st, cmp1);
  for (int i = en - 1, val = 0; i >= st; i--)
    mx[lvl][i] = val = max(val, sz[seg[lvl][i]]);
}
int get(int l, int r, int x, int lvl = 0, int st = 0, int en = n) {
  if (r <= st || en <= l) return 0;
  if (l <= st && en <= r) {
    int id = lower_bound(seg[lvl] + st, seg[lvl] + en, x, cmp1) - seg[lvl];
    return id < en ? mx[lvl][id] : 0;
  }
  int mid = st + en >> 1;
  return max(get(l, r, x, lvl + 1, st, mid), get(l, r, x, lvl + 1, mid, en));
}
inline void add(int p, int x) {
  for (p++; p < N; p += p & -p) fen[p] += x;
}
inline int get(int p) {
  int ans = 0;
  for (p++; p; p -= p & -p) ans += fen[p];
  return ans;
}
inline void compress(int x[N]) {
  vector<int> vec;
  for (int i = 0; i < n; i++) vec.push_back(x[i]);
  sort(vec.begin(), vec.end());
  vec.resize(unique(vec.begin(), vec.end()) - vec.begin());
  for (int i = 0; i < n; i++) {
    L[i] = lower_bound(vec.begin(), vec.end(), x[i] - k) - vec.begin();
    R[i] = upper_bound(vec.begin(), vec.end(), x[i] + k) - vec.begin();
    x[i] = lower_bound(vec.begin(), vec.end(), x[i]) - vec.begin();
  }
}
inline void read_input() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) cin >> r[i];
  for (int i = 0; i < n; i++) cin >> a[i];
}
inline void solve() {
  iota(p, p + n, 0);
  sort(p, p + n, cmp2);
  compress(r), compress(a);
  for (int i = 0; i < n; i++) {
    vec[r[i]].push_back(i);
    pos[a[i] + 1]++;
  }
  for (int i = 0; i < n; i++) {
    for (auto x : vec[i]) add(a[x], 1);
    for (auto x : vec[i]) sz[x] = get(R[x] - 1) - get(L[x] - 1);
    pos[i + 1] += pos[i];
  }
  build();
}
inline void write_output() {
  int q;
  cin >> q;
  while (q--) {
    int x, y;
    cin >> x >> y;
    int st = max(L[--x], L[--y]), en = min(R[x], R[y]);
    int ans = get(pos[st], pos[en], r[x] > r[y] ? x : y);
    cout << (ans ? ans : -1) << endl;
  }
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  read_input(), solve(), write_output();
  return 0;
}
