#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
const int MAX_AGE = 3 * N;
struct person {
  int res, age, orgRes, orgAge, id, dp;
};
struct node {
  int l, r, mx;
  int size();
  int unity(int, int = -1);
  node(int _l = 0, int _r = 0) : l(_l), r(_r), mx(-1) {}
};
struct query {
  int l, r, id, maxRes;
};
query q[N];
person p[N];
node seg[MAX_AGE << 2];
vector<int> resVec, ageVec;
int n, k, fen[MAX_AGE], pos[N], ans[N];
int comid(vector<int> &, int);
void compress(vector<int> &, int &);
void addIndex(int);
int getSum(int, int);
int getPrefixSum(int);
void build(int, int, int = 1);
int getMax(int, int, int = 1);
void makeMax(int, int, int = 1);
int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> p[i].res;
    resVec.push_back(p[i].res);
  }
  for (int i = 0; i < n; i++) {
    int age;
    cin >> age;
    for (auto val : {age, age - k, age + k + 1}) ageVec.push_back(val);
    p[i].age = age;
  }
  for (int i = 0; i < n; i++) p[i].id = i;
  auto uniqueSort = [&](vector<int> &v) {
    sort(v.begin(), v.end());
    v.resize(unique(v.begin(), v.end()) - v.begin());
  };
  uniqueSort(ageVec);
  uniqueSort(resVec);
  for (int i = 0; i < n; i++) {
    p[i].orgAge = p[i].age;
    p[i].orgRes = p[i].res;
    compress(resVec, p[i].res);
    compress(ageVec, p[i].age);
  }
  sort(p, p + n, [&](person &i, person &j) { return i.res < j.res; });
  int pnt = 0;
  for (int i = 0; i < n; i++) {
    int ageLo = p[i].orgAge - k;
    int ageHi = ageLo + k + k + 1;
    compress(ageVec, ageLo);
    compress(ageVec, ageHi);
    while (pnt < n && p[pnt].res <= p[i].res) addIndex(p[pnt++].age);
    p[i].dp = getSum(ageLo, ageHi);
  }
  for (int i = 0; i < n; i++) pos[p[i].id] = i;
  build(0, ageVec.size() + 1);
  int q_cnt;
  cin >> q_cnt;
  for (int i = 0; i < q_cnt; i++) {
    int v, u;
    cin >> v >> u;
    v = pos[v - 1];
    u = pos[u - 1];
    q[i].l = max(p[v].orgAge, p[u].orgAge) - k;
    q[i].r = min(p[v].orgAge, p[u].orgAge) + k + 1;
    q[i].maxRes = max(p[v].res, p[u].res);
    q[i].id = i;
  }
  sort(q, q + q_cnt, [&](query &i, query &j) { return i.maxRes > j.maxRes; });
  pnt = n - 1;
  for (int i = 0; i < q_cnt; i++) {
    while (pnt >= 0 && p[pnt].res >= q[i].maxRes) {
      makeMax(p[pnt].age, p[pnt].dp);
      pnt--;
    }
    compress(ageVec, q[i].l);
    compress(ageVec, q[i].r);
    if (q[i].l >= q[i].r)
      ans[q[i].id] = -1;
    else
      ans[q[i].id] = getMax(q[i].l, q[i].r);
  }
  for (int i = 0; i < q_cnt; i++) cout << ans[i] << endl;
}
int comid(vector<int> &vec, int val) {
  return lower_bound(vec.begin(), vec.end(), val) - vec.begin();
}
void compress(vector<int> &vec, int &var) { var = comid(vec, var); }
int getSum(int l, int r) { return getPrefixSum(r) - getPrefixSum(l); }
int getPrefixSum(int r) {
  int res = 0;
  for (; r; r ^= r & -r) res += fen[r];
  return res;
}
void addIndex(int i) {
  for (i++; i <= ageVec.size(); i += i & -i) fen[i]++;
}
int node::size() { return r - l; }
int node::unity(int _l, int _r) {
  _r = (_r < 0) ? _l + 1 : _r;
  return max(0, min(_r, r) - max(_l, l));
}
void build(int l, int r, int v) {
  seg[v] = node(l, r);
  if (seg[v].size() == 1) return;
  int med = (l + r) / 2;
  build(l, med, v << 1);
  build(med, r, v << 1 | 1);
}
void makeMax(int ind, int val, int v) {
  if (seg[v].unity(ind) == 0) return;
  seg[v].mx = max(seg[v].mx, val);
  if (seg[v].size() == 1) return;
  makeMax(ind, val, v << 1);
  makeMax(ind, val, v << 1 | 1);
}
int getMax(int l, int r, int v) {
  if (seg[v].unity(l, r) == 0) return -1;
  if (seg[v].unity(l, r) == seg[v].size()) return seg[v].mx;
  return max(getMax(l, r, v << 1), getMax(l, r, v << 1 | 1));
}
