#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10, MAXA = 1e9 + 10;
struct que {
  int x, y, ind;
};
vector<que> ques[MAXN];
pair<long long, long long> a[MAXN], b[MAXN];
long long fen[MAXN], H[MAXN], num[MAXN], n, k, q, ans[MAXN];
vector<long long> hi;
int geta(long long a) {
  int p = lower_bound(hi.begin(), hi.end(), a) - hi.begin();
  if (p == hi.size()) return p - 1;
  if (hi[p] > a) p--;
  return p;
}
int getau(long long a) {
  return lower_bound(hi.begin(), hi.end(), a) - hi.begin();
}
void addnum(long long i, long long val) {
  i = geta(i);
  for (i++; i <= hi.size(); i += i & (-i)) num[i] += val;
}
int getnum(long long i) {
  i = geta(i);
  int res = 0;
  for (i++; i > 0; i -= i & (-i)) res += num[i];
  return res;
}
void addfen(long long i, long long val) {
  i = geta(i);
  i++;
  H[i] = max(H[i], val);
  for (; i <= hi.size(); i += i & (-i)) fen[i] = max(fen[i], val);
}
int getfen(long long l, long long r) {
  if (r < l) return 0;
  l = getau(l) + 1, r = geta(r) + 1;
  long long res = 0;
  for (int i = r; i >= l;) {
    if (i - (i & (-i)) < l)
      res = max(res, H[i]), i--;
    else
      res = max(res, fen[i]), i -= i & (-i);
  }
  return res;
}
int getind(int a) {
  int s = 0, e = n + 1;
  while (s + 1 != e) {
    int mid = (s + e) / 2;
    if (b[mid].first < a)
      s = mid;
    else
      e = mid;
  }
  return e;
}
int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) cin >> a[i].first;
  for (int i = 0; i < n; i++) {
    cin >> a[i].second, b[i] = a[i];
    hi.push_back(a[i].second);
  }
  sort(hi.begin(), hi.end());
  hi.resize(unique(hi.begin(), hi.end()) - hi.begin());
  for (int i = 0; i < n; i++) addnum(a[i].second, 1);
  sort(b, b + n);
  cin >> q;
  for (int i = 0; i < q; i++) {
    que tmp;
    cin >> tmp.x >> tmp.y;
    tmp.x--, tmp.y--;
    tmp.ind = i;
    int r = max(a[tmp.x].first, a[tmp.y].first);
    ques[getind(r)].push_back(tmp);
  }
  vector<int> tmp;
  for (int i = n - 1; i >= 0; i--) {
    int g = getnum(b[i].second + k) - getnum(b[i].second - k - 1);
    addfen(b[i].second, g);
    for (int j = 0; j < ques[i].size(); j++) {
      int a1 = a[ques[i][j].x].second, a2 = a[ques[i][j].y].second;
      ans[ques[i][j].ind] = getfen(max(a1, a2) - k, min(a1, a2) + k);
    }
    if (i > 0 && b[i].first == b[i - 1].first)
      tmp.push_back(b[i].second);
    else {
      addnum(b[i].second, -1);
      for (int j = 0; j < tmp.size(); j++) addnum(tmp[j], -1);
      tmp.clear();
    }
  }
  for (int i = 0; i < q; i++) {
    if (ans[i] == 0) ans[i] = -1;
    cout << ans[i] << '\n';
  }
}
