#include <bits/stdc++.h>
using namespace std;
inline int in() {
  int x;
  scanf("%d", &x);
  return x;
}
const int N = 1e5 + 10;
const int LOG = 30;
int n, k, age[N], res[N], q, gp[N], fen[3 * N], MX;
pair<pair<int, int>, int> arr[N];
vector<int> un;
vector<pair<int, int> > seg[4 * N];
vector<int> msg[4 * N];
vector<pair<int, int> > prep;
void upd(int ind, int val) {
  while (ind < MX) fen[ind] += val, ind += (ind & -ind);
}
int get(int ind) {
  int ret = 0;
  while (ind > 0) ret += fen[ind], ind ^= (ind & -ind);
  return ret;
}
int wut(int x) {
  return int(lower_bound(un.begin(), un.end(), x) - un.begin()) + 1;
}
void build(int l = 0, int r = n, int idx = 1) {
  int sz = r - l;
  seg[idx].resize(sz);
  msg[idx].resize(sz);
  if (r - l < 2) {
    seg[idx][0] = {arr[l].first.second, l};
    msg[idx][0] = gp[arr[l].second];
    return;
  }
  int mid = (l + r) >> 1;
  build(l, mid, idx * 2 + 0);
  build(mid, r, idx * 2 + 1);
  merge(seg[idx * 2 + 0].begin(), seg[idx * 2 + 0].end(),
        seg[idx * 2 + 1].begin(), seg[idx * 2 + 1].end(), seg[idx].begin());
  int proc = 0;
  for (int i = (int)seg[idx].size() - 1; i >= 0; i--)
    msg[idx][i] = max(proc, gp[arr[seg[idx][i].second].second]),
    proc = max(proc, gp[arr[seg[idx][i].second].second]);
}
int ans(int ql, int qr, int val, int l = 0, int r = n, int idx = 1) {
  if (qr <= l || r <= ql) return -1;
  if (ql <= l && r <= qr) {
    pair<int, int> sear = {val, -1};
    int point = int(lower_bound(seg[idx].begin(), seg[idx].end(), sear) -
                    seg[idx].begin());
    if (point >= r - l) return -1;
    return msg[idx][point];
  }
  int mid = (l + r) >> 1, ret = 0;
  ret = ans(ql, qr, val, l, mid, idx * 2 + 0);
  ret = max(ret, ans(ql, qr, val, mid, r, idx * 2 + 1));
  return ret;
}
void print(int l = 0, int r = n, int idx = 1) {
  cout << endl;
  cout << "IDX : " << idx << " : " << l << " " << r << endl;
  for (int i = 0; i < seg[idx].size(); i++) cout << seg[idx][i].first << " ";
  cout << endl;
  for (int i = 0; i < seg[idx].size(); i++) cout << seg[idx][i].second << " ";
  cout << endl;
  if (r - l < 2) return;
  int mid = (l + r) >> 1;
  print(l, mid, idx * 2 + 0);
  print(mid, r, idx * 2 + 1);
}
int main() {
  n = in();
  k = in();
  for (int i = 0; i < LOG; i++) seg[i].resize(n);
  for (int i = 0; i < n; i++) {
    res[i] = in();
    arr[i].first.second = res[i];
    prep.push_back({res[i], i});
  }
  for (int i = 0; i < n; i++) {
    age[i] = in();
    arr[i].first.first = age[i];
    arr[i].second = i;
    un.push_back(age[i]);
    un.push_back(age[i] + k);
    un.push_back(age[i] - k - 1);
  }
  sort(un.begin(), un.end());
  sort(prep.begin(), prep.end());
  un.resize(unique(un.begin(), un.end()) - un.begin());
  MX = (int)un.size() + 2;
  for (int i = 0; i < prep.size(); i++) {
    int x = prep[i].second;
    upd(wut(age[x]), +1);
    gp[x] = get(wut(age[x] + k)) - get(wut(age[x] - k - 1));
  }
  sort(arr, arr + n);
  build();
  q = in();
  while (q--) {
    int x, y;
    x = in();
    y = in();
    x--;
    y--;
    if (age[x] > age[y]) swap(x, y);
    if (age[y] - k > age[x] + k) {
      printf("%d\n", -1);
      continue;
    }
    pair<pair<int, int>, int> sl = {{age[y] - k, 0}, 0};
    pair<pair<int, int>, int> sr = {{age[x] + k + 1, 0}, 0};
    int L = int(lower_bound(arr, arr + n, sl) - arr);
    int R = int(lower_bound(arr, arr + n, sr) - arr);
    int oo = ans(L, R, max(res[x], res[y]));
    if (oo < 2) oo = -1;
    printf("%d\n", oo);
  }
  return 0;
}
