#include <bits/stdc++.h>
using namespace std;
const char IO_MODE = 0;
inline long long ReadInt() {
  long long x = 0, s = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') s = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return s * x;
}
inline void WriteInt(long long x) {
  char c[20];
  if (!x) {
    putchar('0');
    return;
  }
  if (x < 0) putchar('-'), x = -x;
  int i = 0;
  while (x > 0) c[++i] = x % 10, x /= 10;
  while (i) putchar(c[i--] + 48);
}
template <typename T>
inline void out(T x) {
  if (IO_MODE & 1)
    WriteInt(x);
  else if (typeid(x) == typeid(int))
    printf("%i", x);
  else
    printf("%lld", (long long)x);
}
template <typename T, typename... Args>
inline void out(T x, Args... args) {
  out(x);
  putc(' ', stdout);
  out(args...);
}
template <typename T>
inline void in(T& x) {
  if (IO_MODE & 2)
    x = ReadInt();
  else if (typeid(x) == typeid(int))
    scanf("%i", &x);
  else if (typeid(x) == typeid(long long))
    scanf("%lld", &x);
}
template <typename T, typename... Args>
inline void in(T& x, Args&... args) {
  in(x);
  in(args...);
}
int aa, bb, cc, dd, ee, ff, gg, hh, ii, jj, kk, mm, nn, oo, pp, qq, rr, ss, tt,
    uu, vv, ww, xx, yy, zz;
int tc;
template <class c>
struct rge {
  c b, e;
};
template <class c>
rge<c> range(c i, c j) {
  return rge<c>{i, j};
}
template <class c>
auto dud(c* x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct debug {
  template <class c>
  debug& operator<<(const c&) {
    return *this;
  }
};
int n, q, k, A[100100], B[100100], C[100100], R[100100], I[100100], M[100100];
vector<pair<pair<int, int>, int>> Q[300100];
int ans[300100];
struct node {
  vector<int> idx, wt;
  int mx;
  node *lft, *rgt;
}* root = new node();
struct DynamicWaveLetTree {
  void build(int b, int e, int Lo = 0, int Hi = 1e9, node* p = root) {
    if (b == e) return;
    if (Lo == Hi) return;
    if (!p->lft) p->lft = new node();
    if (!p->rgt) p->rgt = new node();
    p->wt.reserve(e - b + 1);
    p->wt.push_back(0);
    int Mid = Lo + Hi >> 1;
    for (int i = b; i < e; i++)
      p->wt.push_back(p->wt.back() + (B[M[i]] <= Mid));
    int m =
        stable_partition(M + b, M + e, [=](int i) { return B[i] <= Mid; }) - M;
    build(b, m, Lo, Mid, p->lft);
    build(m, e, Mid + 1, Hi, p->rgt);
  }
  void push_back(int val, int cnt, int Lo = 0, int Hi = 1e9, node* p = root) {
    p->mx = max(p->mx, cnt);
    if (Lo == Hi) return;
    int Mid = Lo + Hi >> 1;
    if (val <= Mid)
      push_back(val, cnt, Lo, Mid, p->lft);
    else
      push_back(val, cnt, Mid + 1, Hi, p->rgt);
  }
  int count(int i, int j, int a, int b, int Lo = 0, int Hi = 1e9,
            node* p = root) {
    if (j - i == 0) return 0;
    if (Lo > b || Hi < a) return 0;
    if (Lo >= a && Hi <= b) return j - i;
    int Mid = Lo + Hi >> 1;
    return count(p->wt[i], p->wt[j], a, b, Lo, Mid, p->lft) +
           count(i - p->wt[i], j - p->wt[j], a, b, Mid + 1, Hi, p->rgt);
  }
  int getMax(int a, int b, int Lo = 0, int Hi = 1e9, node* p = root) {
    if (Lo > b || Hi < a) return 0;
    if (Lo >= a && Hi <= b) return p->mx;
    int Mid = Lo + Hi >> 1;
    return max(p->lft ? getMax(a, b, Lo, Mid, p->lft) : 0,
               p->rgt ? getMax(a, b, Mid + 1, Hi, p->rgt) : 0);
  }
} wt;
int32_t main() {
  in(n, k);
  for (int i = 0; i < n; i++) in(R[i]);
  for (int i = 0; i < n; i++) in(A[i]);
  for (int i = 0; i < n; i++) M[i] = I[i] = i;
  sort(I, I + n, [](int a, int b) { return R[a] < R[b]; });
  for (int i = 0; i < n; i++) B[i] = A[I[i]];
  wt.build(0, n);
  for (int i = 0, j = 0; i < n; i++) {
    while (j < n && R[I[i]] == R[I[j]]) j++;
    C[i] = wt.count(0, j, B[i] - k, B[i] + k);
  }
  for (int i = 0; i < n; i++) R[I[i]] = i;
  in(q);
  for (int i = 0; i < q; i++) {
    int x = R[ReadInt() - 1];
    int y = R[ReadInt() - 1];
    int a = max(B[x] - k, B[y] - k);
    int b = min(B[x] + k, B[y] + k);
    if (a <= b) {
      if (x > y) swap(x, y);
      Q[y].emplace_back(make_pair(a, b), i);
    }
  }
  for (int i = n; i--;) {
    wt.push_back(B[i], C[i]);
    for (auto q : Q[i])
      ans[q.second] = wt.getMax(q.first.first, q.first.second);
  }
  for (int i = 0; i < q; i++)
    if (ans[i] < 2)
      printf("-1\n");
    else
      printf("%d\n", ans[i]);
}
