#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:256000000")
using namespace std;
inline int mymax(int a, int b) { return a < b ? b : a; }
int sum[1000000];
int mx[1000000];
void zero(int a, int b, int k) {
  sum[k] = mx[k] = 0;
  if (b == a + 1) return;
  int c = (a + b) / 2;
  zero(a, c, k * 2 + 1);
  zero(c, b, k * 2 + 2);
}
void setval(int p, int v, int a, int b, int k) {
  if (b == a + 1) {
    sum[k] = mx[k] = v;
    return;
  }
  int c = (a + b) / 2;
  if (p < c)
    setval(p, v, a, c, k * 2 + 1);
  else
    setval(p, v, c, b, k * 2 + 2);
  sum[k] = sum[k * 2 + 1] + sum[k * 2 + 2];
  mx[k] = mymax(mx[k * 2 + 1], mx[k * 2 + 2]);
}
int getsum(int l, int r, int a, int b, int k) {
  if (a == l && b == r) return sum[k];
  int c = (a + b) / 2;
  if (r <= c)
    return getsum(l, r, a, c, k * 2 + 1);
  else if (l >= c)
    return getsum(l, r, c, b, k * 2 + 2);
  else
    return getsum(l, c, a, c, k * 2 + 1) + getsum(c, r, c, b, k * 2 + 2);
}
int getmax(int l, int r, int a, int b, int k) {
  if (a == l && b == r) return mx[k];
  int c = (a + b) / 2;
  if (r <= c)
    return getmax(l, r, a, c, k * 2 + 1);
  else if (l >= c)
    return getmax(l, r, c, b, k * 2 + 2);
  else
    return mymax(getmax(l, c, a, c, k * 2 + 1), getmax(c, r, c, b, k * 2 + 2));
}
struct event {
  bool query;
  int y;
  int x1, x2;
  int id;
  bool operator<(const event &b) const { return y > b.y; }
};
int n, K;
int xs[110000];
int xcnt;
int team[110000];
int res[110000];
pair<pair<int, int>, pair<int, int> > queries[110000];
pair<pair<int, int>, int> pts[110000];
int iperm[110000];
event evs[210000];
int evcnt;
int main() {
  cin >> n >> K;
  for (int i = 0; i < (n); ++i) {
    scanf("%d", &pts[i].first.first);
    pts[i].second = i;
  }
  for (int i = 0; i < (n); ++i) {
    scanf("%d", &pts[i].first.second);
    xs[xcnt++] = pts[i].first.second;
  }
  sort(xs, xs + xcnt);
  xcnt = unique(xs, xs + xcnt) - xs;
  for (int i = 0; i < (n); ++i) {
    pts[i].first.second = lower_bound(xs, xs + xcnt, pts[i].first.second) - xs;
  }
  sort(pts, pts + n);
  for (int i = 0; i < (n); ++i) {
    iperm[pts[i].second] = i;
  }
  for (int i = 0; i < n;) {
    int j = i;
    while (j < n && pts[j].first.first == pts[i].first.first) {
      setval(
          pts[j].first.second,
          getsum(pts[j].first.second, pts[j].first.second + 1, 0, xcnt, 0) + 1,
          0, xcnt, 0);
      evs[evcnt].query = false;
      evs[evcnt].id = pts[j].second;
      evs[evcnt].x1 = pts[j].first.second;
      evs[evcnt].y = pts[j].first.first;
      ++evcnt;
      ++j;
    }
    while (i < j) {
      int a = lower_bound(xs, xs + xcnt, xs[pts[i].first.second] - K) - xs;
      int b = upper_bound(xs, xs + xcnt, xs[pts[i].first.second] + K) - xs;
      if (b <= a) exit(321);
      team[pts[i].second] = getsum(a, b, 0, xcnt, 0);
      ++i;
    }
  }
  zero(0, xcnt, 0);
  int tc;
  cin >> tc;
  for (int qqq = 0; qqq < (tc); ++qqq) {
    int a, b;
    scanf("%d%d", &a, &b);
    --a;
    --b;
    a = iperm[a];
    b = iperm[b];
    evs[evcnt].id = qqq;
    evs[evcnt].query = true;
    evs[evcnt].y = mymax(pts[a].first.first, pts[b].first.first);
    int x1 = pts[a].first.second;
    int x2 = pts[b].first.second;
    if (x2 < x1) swap(x1, x2);
    evs[evcnt].x1 = lower_bound(xs, xs + xcnt, xs[x2] - K) - xs;
    evs[evcnt].x2 = upper_bound(xs, xs + xcnt, xs[x1] + K) - xs;
    ++evcnt;
  }
  sort(evs, evs + evcnt);
  for (int i = 0; i < evcnt;) {
    int j = i;
    while (j < evcnt && evs[j].y == evs[i].y) {
      if (!evs[j].query)
        setval(evs[j].x1,
               mymax(getmax(evs[j].x1, evs[j].x1 + 1, 0, xcnt, 0),
                     team[evs[j].id]),
               0, xcnt, 0);
      ++j;
    }
    while (i < j) {
      if (evs[i].query) {
        if (evs[i].x1 < evs[i].x2)
          res[evs[i].id] = getmax(evs[i].x1, evs[i].x2, 0, xcnt, 0);
      }
      ++i;
    }
  }
  for (int i = 0; i < (tc); ++i) {
    if (!res[i]) res[i] = -1;
    printf("%d\n", res[i]);
  }
  return 0;
}
