#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 100;
const int INF = 1e9 + 100;
const int MINF = -INF;
int n, k, q;
int resp[MAXN];
int age[MAXN];
vector<pair<int, int> > v;
int maxgroup[MAXN];
struct SEG_VEC_RESP {
  vector<pair<int, int> > seg[4 * MAXN];
  vector<int> segm[4 * MAXN];
  void build(int x, int xl, int xr) {
    if (xr - xl == 1) {
      seg[x].push_back(make_pair(v[xl].second, xl));
      return;
    }
    int mid = (xl + xr) / 2;
    build(2 * x + 1, xl, mid);
    build(2 * x + 2, mid, xr);
    seg[x].resize(xr - xl);
    merge(seg[2 * x + 1].begin(), seg[2 * x + 1].end(), seg[2 * x + 2].begin(),
          seg[2 * x + 2].end(), seg[x].begin());
  }
  int get_num(int x, int xl, int xr, int ql, int qr, int qv) {
    if (qr <= xl || xr <= ql) return 0;
    if (ql <= xl && xr <= qr)
      return (upper_bound(seg[x].begin(), seg[x].end(), make_pair(qv, INF)) -
              seg[x].begin());
    int mid = (xl + xr) / 2;
    return (get_num(2 * x + 1, xl, mid, ql, qr, qv) +
            get_num(2 * x + 2, mid, xr, ql, qr, qv));
  }
  void phase2(int x, int xl, int xr) {
    if (xr - xl == 1) {
      segm[x].push_back(maxgroup[xl]);
      return;
    }
    int mid = (xl + xr) / 2;
    phase2(2 * x + 1, xl, mid);
    phase2(2 * x + 2, mid, xr);
    segm[x].resize(xr - xl);
    int maxtmp = MINF;
    for (int i = seg[x].size() - 1; i >= 0; i--) {
      maxtmp = max(maxtmp, maxgroup[seg[x][i].second]);
      segm[x][i] = maxtmp;
    }
  }
  int get_ans(int x, int xl, int xr, int ql, int qr, int qv) {
    if (qr <= xl || xr <= ql) return MINF;
    if (ql <= xl && xr <= qr) {
      int place =
          lower_bound(seg[x].begin(), seg[x].end(), make_pair(qv, MINF)) -
          seg[x].begin();
      if (place == seg[x].size()) return MINF;
      return segm[x][place];
    }
    int mid = (xl + xr) / 2;
    return max(get_ans(2 * x + 1, xl, mid, ql, qr, qv),
               get_ans(2 * x + 2, mid, xr, ql, qr, qv));
  }
} seg_resp;
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> k;
  for (int i = 0; i < n; i++) cin >> resp[i];
  for (int i = 0; i < n; i++)
    cin >> age[i], v.push_back(make_pair(age[i], resp[i]));
  sort(v.begin(), v.end());
  seg_resp.build(0, 0, n);
  for (int i = 0; i < n; i++) {
    int l = lower_bound(v.begin(), v.end(), make_pair(v[i].first - k, MINF)) -
            v.begin();
    int r = upper_bound(v.begin(), v.end(), make_pair(v[i].first + k, INF)) -
            v.begin();
    maxgroup[i] = seg_resp.get_num(0, 0, n, l, r, v[i].second);
  }
  seg_resp.phase2(0, 0, n);
  cin >> q;
  for (int i = 0; i < q; i++) {
    int x, y;
    cin >> x >> y;
    x--, y--;
    int a1 = age[x];
    int a2 = age[y];
    if (a1 > a2) swap(a1, a2);
    int l =
        lower_bound(v.begin(), v.end(), make_pair(a2 - k, MINF)) - v.begin();
    int r = upper_bound(v.begin(), v.end(), make_pair(a1 + k, INF)) - v.begin();
    if (r <= l) {
      cout << -1 << '\n';
      continue;
    }
    int rsp = max(resp[x], resp[y]);
    int ans = seg_resp.get_ans(0, 0, n, l, r, rsp);
    if (ans == MINF)
      cout << -1 << '\n';
    else
      cout << ans << '\n';
  }
  return 0;
}
