#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:60000000")
using namespace std;
template <class T>
T sqr(T x) {
  return x * x;
}
double const pi = 3.1415926535897932384626433832795;
long long const inf64 = (long long)4e18;
const string name = "c";
const int NMAX = 100010;
int num, n, m, k, tree[3 * NMAX];
pair<pair<int, int>, pair<int, int> > q[NMAX];
pair<int, pair<int, int> > p[NMAX];
int ans[NMAX], x[NMAX], a[NMAX], r[NMAX], zn[NMAX];
pair<int, int> tmp[NMAX];
int find_sum(int v, int l, int r, int ll, int rr) {
  if (x[l] > rr || x[r] < ll) return 0;
  if (x[l] >= ll && x[r] <= rr) return tree[v];
  return find_sum(2 * v, l, (l + r) >> 1, ll, rr) +
         find_sum(2 * v + 1, (l + r + 2) >> 1, r, ll, rr);
}
void update_sum(int v, int l, int r, int p, int zn) {
  if (x[l] > p || x[r] < p) return;
  if (x[l] == x[r]) {
    tree[v] += zn;
    return;
  }
  update_sum(2 * v, l, (l + r) >> 1, p, zn);
  update_sum(2 * v + 1, (l + r + 2) >> 1, r, p, zn);
  tree[v] = tree[2 * v] + tree[2 * v + 1];
}
int find(int v, int l, int r, int ll, int rr) {
  if (x[l] > rr || x[r] < ll) return -1;
  if (x[l] >= ll && x[r] <= rr) return tree[v];
  return max(find(2 * v, l, (l + r) >> 1, ll, rr),
             find(2 * v + 1, (l + r + 2) >> 1, r, ll, rr));
}
void update(int v, int l, int r, int p, int zn) {
  if (x[l] > p || x[r] < p) return;
  if (x[l] == x[r]) {
    tree[v] = max(tree[v], zn);
    return;
  }
  update(2 * v, l, (l + r) >> 1, p, zn);
  update(2 * v + 1, (l + r + 2) >> 1, r, p, zn);
  tree[v] = max(tree[2 * v], tree[2 * v + 1]);
}
void precalc() {
  for (int i = 0; i < (int)n; i++) tmp[i] = make_pair(r[i], i);
  sort(tmp, tmp + n);
  memset(tree, 0, sizeof(tree));
  for (int i = 0; i < (int)n; i++) {
    update_sum(1, 0, num - 1, a[tmp[i].second], 1);
    zn[tmp[i].second] =
        find_sum(1, 0, num - 1, a[tmp[i].second] - k, a[tmp[i].second] + k);
  }
}
int main() {
  cin >> n >> k;
  for (int i = 0; i < (int)n; i++) scanf("%d", &r[i]);
  for (int i = 0; i < (int)n; i++) scanf("%d", &a[i]);
  for (int i = 0; i < (int)n; i++) x[i] = a[i];
  sort(x, x + n);
  num = unique(x, x + n) - x;
  precalc();
  for (int i = 0; i < (int)n; i++)
    p[i] = make_pair(r[i], make_pair(a[i], zn[i]));
  sort(p, p + n);
  reverse(p, p + n);
  cin >> m;
  for (int i = 0; i < (int)m; i++) {
    scanf("%d%d", &q[i].second.first, &q[i].second.second);
    q[i].second.first--, q[i].second.second--;
    if (a[q[i].second.first] > a[q[i].second.second])
      swap(q[i].second.first, q[i].second.second);
    q[i].first = make_pair(max(r[q[i].second.first], r[q[i].second.second]), i);
  }
  sort(q, q + m);
  reverse(q, q + m);
  memset(tree, 255, sizeof(tree));
  int qqqqqqq = 0, y2 = 0;
  while (y2 < m) {
    if (qqqqqqq == n || q[y2].first.first > p[qqqqqqq].first) {
      ans[q[y2].first.second] = find(1, 0, num - 1, a[q[y2].second.second] - k,
                                     a[q[y2].second.first] + k);
      y2++;
    } else {
      update(1, 0, num - 1, p[qqqqqqq].second.first, p[qqqqqqq].second.second);
      qqqqqqq++;
    }
  }
  for (int i = 0; i < (int)m; i++) printf("%d\n", ans[i]);
  return 0;
}
