#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
const int inf = 1e9 + 10;
int n, k;
int r[maxn], a[maxn], seg[maxn << 2], dp[maxn], ans[maxn];
vector<pair<int, int>> qrs;
vector<int> ansqrs[maxn];
void update1(int pos, int x, int id = 1, int l = 0, int r = n) {
  if (r - l <= 1) {
    seg[id] += x;
    return;
  }
  int mid = (l + r) >> 1;
  if (pos < mid)
    update1(pos, x, id << 1, l, mid);
  else
    update1(pos, x, id << 1 | 1, mid, r);
  seg[id] = seg[id << 1] + seg[id << 1 | 1];
}
int query1(int L, int R, int id = 1, int l = 0, int r = n) {
  if (r <= L || l >= R) return 0;
  if (r <= R && l >= L) return seg[id];
  int mid = (l + r) >> 1;
  return query1(L, R, id << 1, l, mid) + query1(L, R, id << 1 | 1, mid, r);
}
void update2(int pos, int x, int id = 1, int l = 0, int r = n) {
  if (r - l <= 1) {
    seg[id] = max(seg[id], x);
    return;
  }
  int mid = (r + l) >> 1;
  if (pos < mid)
    update2(pos, x, id << 1, l, mid);
  else
    update2(pos, x, id << 1 | 1, mid, r);
  seg[id] = max(seg[id << 1], seg[id << 1 | 1]);
}
int query2(int L, int R, int id = 1, int l = 0, int r = n) {
  if (l >= R || r <= L) return -1;
  if (l >= L && r <= R) return seg[id];
  int mid = (l + r) >> 1;
  return max(query2(L, R, id << 1, l, mid), query2(L, R, id << 1 | 1, mid, r));
}
int main() {
  cin >> n >> k;
  vector<int> t;
  for (int i = 0; i < n; i++) {
    scanf("%d", &r[i]);
    t.push_back(r[i]);
  }
  sort(t.begin(), t.end());
  auto last = unique(t.begin(), t.end());
  for (int i = 0; i < n; i++) {
    r[i] = lower_bound(t.begin(), last, r[i]) - t.begin();
  }
  t.clear();
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    t.push_back(a[i]);
  }
  sort(t.begin(), t.end());
  last = unique(t.begin(), t.end());
  vector<pair<int, int>> prs;
  for (int i = 0; i < n; i++) {
    prs.push_back(make_pair(r[i], i));
  }
  sort(prs.begin(), prs.end());
  for (int i = 0; i < prs.size(); i++) {
    int j = i;
    while (j < n - 1 && prs[j].first == prs[j + 1].first) {
      update1(lower_bound(t.begin(), last, a[prs[j].second]) - t.begin(), 1);
      j++;
    }
    update1(lower_bound(t.begin(), last, a[prs[j].second]) - t.begin(), 1);
    while (i < n - 1 && prs[i].first == prs[i + 1].first) {
      dp[prs[i].second] = query1(
          lower_bound(t.begin(), last, a[prs[i].second] - k) - t.begin(),
          upper_bound(t.begin(), last, a[prs[i].second] + k) - t.begin());
      i++;
    }
    dp[prs[i].second] =
        query1(lower_bound(t.begin(), last, a[prs[i].second] - k) - t.begin(),
               upper_bound(t.begin(), last, a[prs[i].second] + k) - t.begin());
  }
  memset(seg, -1, sizeof seg);
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    x--;
    y--;
    int st = lower_bound(t.begin(), last, max(a[x] - k, a[y] - k)) - t.begin();
    int en = upper_bound(t.begin(), last, min(a[x] + k, a[y] + k)) - t.begin();
    qrs.push_back(make_pair(st, en));
    ansqrs[max(r[x], r[y])].push_back(i);
  }
  reverse(prs.begin(), prs.end());
  for (int i = 0; i < n; i++) {
    while (i < n - 1 && prs[i].first == prs[i + 1].first) {
      update2(lower_bound(t.begin(), last, a[prs[i].second]) - t.begin(),
              dp[prs[i].second]);
      i++;
    }
    update2(lower_bound(t.begin(), last, a[prs[i].second]) - t.begin(),
            dp[prs[i].second]);
    for (int id : ansqrs[prs[i].first]) {
      if (qrs[id].first >= qrs[id].second)
        ans[id] = -1;
      else
        ans[id] = query2(qrs[id].first, qrs[id].second);
    }
  }
  for (int i = 0; i < q; i++) cout << ans[i] << endl;
  return 0;
}
