#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, k;
int age[N];
int tmp[N];
int res[N];
int cnt[N];
int ans[N];
vector<int> ind[N];
vector<int> seg1[4 * N], vec;
vector<pair<int, int> > seg2[4 * N];
void build1(int v = 1, int s = 0, int e = N) {
  if (e - s == 1) {
    for (auto u : ind[s]) {
      seg1[v].push_back(u);
    }
    sort(seg1[v].begin(), seg1[v].end());
    return;
  }
  int mid = (s + e) / 2;
  int lc = v * 2;
  int rc = lc + 1;
  build1(lc, s, mid);
  build1(rc, mid, e);
  seg1[v] = seg1[lc];
  for (auto u : seg1[rc]) {
    seg1[v].push_back(u);
  }
  sort(seg1[v].begin(), seg1[v].end());
}
int get1(int l, int r, int val, int v = 1, int s = 0, int e = N) {
  if (e <= l || r <= s) {
    return 0;
  }
  if (l <= s && e <= r) {
    return upper_bound(seg1[v].begin(), seg1[v].end(), val) - seg1[v].begin();
  }
  int mid = (s + e) / 2;
  int lc = v * 2;
  int rc = lc + 1;
  return get1(l, r, val, lc, s, mid) + get1(l, r, val, rc, mid, e);
}
void build2(int v = 1, int s = 0, int e = N) {
  if (e - s == 1) {
    seg2[v].push_back({cnt[s], ans[s]});
    return;
  }
  int mid = (s + e) / 2;
  int lc = v * 2;
  int rc = lc + 1;
  build2(lc, s, mid);
  build2(rc, mid, e);
  seg2[v] = seg2[lc];
  for (auto p : seg2[rc]) {
    seg2[v].push_back(p);
  }
  sort(seg2[v].begin(), seg2[v].end());
  int tmp = seg2[v].back().second;
  for (int i = seg2[v].size() - 1; i >= 0; i--) {
    tmp = max(tmp, seg2[v][i].second);
    seg2[v][i].second = tmp;
  }
}
int get2(int l, int r, int val, int v = 1, int s = 0, int e = N) {
  if (e <= l || r <= s) {
    return -1;
  }
  if (l <= s && e <= r) {
    pair<int, int> t = {val, -1};
    int plc = lower_bound(seg2[v].begin(), seg2[v].end(), t) - seg2[v].begin();
    if (plc == seg2[v].size()) {
      return -1;
    } else {
      return seg2[v][plc].second;
    }
  }
  int mid = (s + e) / 2;
  int lc = v * 2;
  int rc = lc + 1;
  return max(get2(l, r, val, lc, s, mid), get2(l, r, val, rc, mid, e));
}
void pre() {
  sort(vec.begin(), vec.end());
  vec.resize(unique(vec.begin(), vec.end()) - vec.begin());
  for (int i = 0; i < n; i++) {
    tmp[i] = lower_bound(vec.begin(), vec.end(), age[i]) - vec.begin();
    cnt[tmp[i]] = max(cnt[tmp[i]], res[i]);
    ind[tmp[i]].push_back(res[i]);
  }
}
void pre2() {
  for (int i = 0; i < vec.size(); i++) {
    int ind1 = upper_bound(vec.begin(), vec.end(), vec[i] + k) - vec.begin();
    int ind2 = lower_bound(vec.begin(), vec.end(), vec[i] - k) - vec.begin();
    ans[i] = get1(ind2, ind1, cnt[i]);
  }
}
void solver() {
  int x, y;
  cin >> x >> y;
  x--;
  y--;
  if (age[x] > age[y]) {
    swap(x, y);
  }
  int ind2 = lower_bound(vec.begin(), vec.end(), (age[y] - k)) - vec.begin();
  int ind1 = upper_bound(vec.begin(), vec.end(), age[x] + k) - vec.begin();
  if (ind2 >= ind1) {
    cout << -1 << endl;
    return;
  }
  cout << get2(ind2, ind1, max(res[x], res[y])) << endl;
}
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> res[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> age[i];
    vec.push_back(age[i]);
  }
  pre();
  build1();
  pre2();
  build2();
  int q;
  cin >> q;
  for (int t = 0; t < q; t++) {
    solver();
  }
}
