#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9, mod = 1e9 + 7;
const int N = 4e5 + 5;
int w[N], x, y, z, size[N], ST[4 * N], F[N], i, j, k, n, m, ans[N];
pair<pair<int, int>, int> a[N];
map<int, vector<pair<int, int> > > U;
map<int, vector<pair<pair<int, int>, int> > > q;
map<int, int> h;
int que(int x, int y) {
  int sum = 0;
  x--;
  for (; y > 0; y -= y & -y) sum += F[y];
  for (; x > 0; x -= x & -x) sum -= F[x];
  return sum;
}
void upd(int x) {
  for (; x && x < N; x += x & -x) F[x]++;
}
int query(int k, int bas, int son, int x, int y) {
  if (bas > y || son < x) return -1;
  if (x <= bas && son <= y) return ST[k];
  return max(query((k + k), bas, (bas + son >> 1), x, y),
             query((k + k + 1), (bas + son >> 1) + 1, son, x, y));
}
int update(int k, int bas, int son, int x, int t) {
  if (bas > x || son < x) return ST[k];
  if (x <= bas && son <= x) return ST[k] = max(ST[k], t);
  return ST[k] = max(update((k + k), bas, (bas + son >> 1), x, t),
                     update((k + k + 1), (bas + son >> 1) + 1, son, x, t));
}
int main() {
  memset(ST, -1, sizeof ST);
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i].first.first);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i].first.second);
    a[i].second = i;
    h[a[i].first.second] = h[a[i].first.second - k] = h[a[i].first.second + k] =
        1;
  }
  int s = 0;
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n; i++) w[a[i].second] = i;
  for (__typeof(h.begin()) it = h.begin(); it != h.end(); it++)
    it->second = ++s;
  int j = 1;
  for (int i = 1; i <= n; i++) {
    while (j <= n && a[j].first.first <= a[i].first.first) {
      upd(h[a[j].first.second]);
      j++;
    }
    size[i] = que(h[a[i].first.second - k], h[a[i].first.second + k]);
    U[a[i].first.first].push_back(make_pair(a[i].first.second, size[i]));
  }
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d %d", &x, &y);
    if (w[x] < w[y]) swap(x, y);
    if (a[w[x]].first.second < a[w[y]].first.second)
      q[a[w[x]].first.first].push_back(
          make_pair(make_pair(a[w[x]].first.second, a[w[y]].first.second), i));
    else
      q[a[w[x]].first.first].push_back(
          make_pair(make_pair(a[w[y]].first.second, a[w[x]].first.second), i));
  }
  for (map<int, vector<pair<int, int> > >::reverse_iterator it = U.rbegin();
       it != U.rend(); it++) {
    for (__typeof(it->second.begin()) it2 = it->second.begin();
         it2 != it->second.end(); it2++)
      update(1, 1, N, h[it2->first], it2->second);
    for (__typeof(q[it->first].begin()) it2 = q[it->first].begin();
         it2 != q[it->first].end(); it2++)
      ans[it2->second] =
          query(1, 1, N, h[it2->first.second - k], h[it2->first.first + k]);
  }
  for (int i = 1; i <= m; i++) printf("%d\n", ans[i]);
  return 0;
}
