#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 5e1, M = 3e5 + 5e3;
struct fen {
  long long D[M];
  inline fen() { memset(D, 0, sizeof(D)); }
  void add(long long ind, long long d) {
    for (ind++; ind < M; ind += (ind & (-ind))) D[ind] += d;
  }
  long long get(long long ind) {
    if (ind < 0) return (0);
    long long sm = 0;
    ind = max(ind, 0LL);
    for (ind++; ind; ind -= (ind & (-ind))) sm += D[ind];
    return (sm);
  }
} f1;
struct node {
  long long a, r, i;
};
struct QU {
  long long l, r, mx, i;
};
map<long long, vector<node> > mp;
vector<node> v;
vector<QU> qs;
vector<long long> X, Y;
long long n, k, q, A[N], R[N], ANSnode[N], ANS[N], D[M << 2], ll[N], rr[N];
bool cmp(const node &a, const node &b) { return (a.r > b.r); }
bool cmp2(const QU &a, const QU &b) { return (a.mx < b.mx); }
void add(long long ind, long long d, long long id = 1, long long l = 0,
         long long r = M) {
  D[id] = max(D[id], d);
  if (r - l < 2) return;
  long long mid = ((l + r) >> 1);
  if (ind < mid)
    add(ind, d, (id << 1), l, mid);
  else
    add(ind, d, (id << 1 | 1), mid, r);
}
long long get(long long s, long long e, long long id = 1, long long l = 0,
              long long r = M) {
  if (s >= e) return (-1);
  if (l >= e || r <= s) return (-1);
  if (l >= s && r <= e) return (D[id]);
  long long mid = ((l + r) >> 1);
  return (max(get(s, e, (id << 1), l, mid), get(s, e, (id << 1 | 1), mid, r)));
}
int32_t main() {
  scanf("%lld %lld", &n, &k);
  Y.push_back(k);
  for (long long i = 0; i < n; i++) {
    scanf("%lld", &R[i]);
    X.push_back(R[i]);
  }
  for (long long i = 0; i < n; i++) {
    scanf("%lld", &A[i]);
    Y.push_back(A[i]);
    Y.push_back(A[i] - k);
    Y.push_back(A[i] + k);
  }
  for (long long i = 0; i < n; i++) {
    ll[i] = A[i] - k;
    rr[i] = A[i] + k;
  }
  set<long long> st1(X.begin(), X.end());
  X.assign(st1.begin(), st1.end());
  for (long long i = 0; i < n; i++)
    R[i] = lower_bound(X.begin(), X.end(), R[i]) - X.begin() + 1;
  set<long long> st2(Y.begin(), Y.end());
  Y.assign(st2.begin(), st2.end());
  for (long long i = 0; i < n; i++)
    A[i] = lower_bound(Y.begin(), Y.end(), A[i]) - Y.begin() + 1;
  for (long long i = 0; i < n; i++)
    ll[i] = lower_bound(Y.begin(), Y.end(), ll[i]) - Y.begin() + 1;
  for (long long i = 0; i < n; i++)
    rr[i] = lower_bound(Y.begin(), Y.end(), rr[i]) - Y.begin() + 1;
  for (long long i = 0; i < n; i++) {
    node nd = {A[i], R[i], i};
    v.push_back(nd);
  }
  for (node nd : v) mp[nd.r].push_back(nd);
  for (auto x : mp) {
    for (node nd : x.second) f1.add(nd.a, 1);
    for (node nd : x.second)
      ANSnode[nd.i] = f1.get(rr[nd.i]) - f1.get(ll[nd.i] - 1);
  }
  sort(v.begin(), v.end(), cmp);
  scanf("%lld", &q);
  for (long long i = 0; i < q; i++) {
    long long a, b;
    scanf("%lld %lld", &a, &b);
    a--;
    b--;
    QU qu = {max(ll[a], ll[b]), min(rr[a], rr[b]), max(R[a], R[b]), i};
    qs.push_back(qu);
  }
  memset(ANS, -1, sizeof(ANS));
  memset(D, -1, sizeof(D));
  sort(qs.begin(), qs.end(), cmp2);
  reverse(v.begin(), v.end());
  long long cnt = 0;
  while (v.size() && qs.size()) {
    while (v.size() && qs.size() && v.back().r >= qs.back().mx) {
      add(v.back().a, ANSnode[v.back().i]);
      v.pop_back();
    }
    while ((v.empty() && qs.size()) ||
           (v.size() && qs.size() && qs.back().mx > v.back().r)) {
      ANS[qs.back().i] = get(qs.back().l, qs.back().r + 1);
      qs.pop_back();
    }
  }
  for (long long i = 0; i < q; i++) printf("%lld\n", ANS[i]);
}
