#include <bits/stdc++.h>
using namespace std;
const long long inf = ~0ull / 4;
const long long maxn = 210000;
long long n, m, k;
pair<long long, long long> rel[maxn];
long long age[maxn];
long long low[maxn];
long long high[maxn];
pair<long long, long long> a[maxn];
long long val[maxn * 3];
long long SZ = 1;
long long ans[maxn];
long long sum[maxn * 5];
long long out[maxn];
pair<pair<long long, long long>, long long> q[maxn];
long long h[maxn * 5];
long long rb[maxn];
void ad(long long id) {
  id += SZ;
  while (id) {
    val[id]++;
    id /= 2;
  }
}
long long fn(long long L, long long R, long long id, long long l, long long r) {
  if (R < l or r < L) return 0;
  if (l <= L and R <= r) return val[id];
  long long mid = (L + R) / 2;
  return fn(L, mid, id * 2, l, r) + fn(mid + 1, R, id * 2 + 1, l, r);
}
bool cmp(pair<pair<long long, long long>, long long> p1,
         pair<pair<long long, long long>, long long> p2) {
  return max(rb[p1.first.first], rb[p1.first.second]) >
         max(rb[p2.first.first], rb[p2.first.second]);
}
void add(long long id, long long v) {
  id += SZ;
  while (id) {
    h[id] = max(h[id], v);
    id /= 2;
  }
}
long long fnd(long long L, long long R, long long id, long long l,
              long long r) {
  if (r < L or R < l) return 0;
  if (l <= L and R <= r) {
    return h[id];
  }
  long long mid = (L + R) / 2;
  return max(fnd(L, mid, id * 2, l, r), fnd(mid + 1, R, id * 2 + 1, l, r));
}
int32_t main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> k;
  while (SZ < n) SZ *= 2;
  for (long long i = 0; i < n; i++) {
    cin >> rel[i].first;
    rb[i] = rel[i].first;
    rel[i].second = i;
  }
  for (long long i = 0; i < n; i++) {
    cin >> a[i].first;
    a[i].second = i;
  }
  sort(a, a + n);
  long long least = 0;
  for (long long i = 0; i < n; i++) {
    while (a[least].first < a[i].first - k) least++;
    low[a[i].second] = least;
    age[a[i].second] = i;
  }
  long long last = n - 1;
  for (long long i = n - 1; i >= 0; i--) {
    while (a[last].first > a[i].first + k) last--;
    high[a[i].second] = last;
  }
  sort(rel, rel + n);
  for (long long i = 0; i < n; i++) {
    if (!i or rel[i].first != rel[i - 1].first) {
      long long t = i;
      while (t < n and rel[t].first == rel[i].first) {
        ad(age[rel[t].second]);
        t++;
      }
    }
    long long id = rel[i].second;
    ans[id] = fn(0, SZ - 1, 1, low[id], high[id]);
  }
  cin >> m;
  for (long long i = 0; i < m; i++) {
    long long t1, t2;
    cin >> t1 >> t2;
    t1--, t2--;
    q[i] = make_pair(make_pair(t1, t2), i);
  }
  sort(q, q + m, cmp);
  long long t = n - 1;
  for (long long i = 0; i < m; i++) {
    long long v = q[i].first.first;
    long long u = q[i].first.second;
    if (age[v] > age[u]) swap(v, u);
    long long ind = q[i].second;
    while (t >= 0 and rel[t].first >= max(rb[v], rb[u])) {
      add(age[rel[t].second], ans[rel[t].second]);
      t--;
    }
    out[ind] = -1;
    if (high[v] < low[u]) continue;
    out[ind] = fnd(0, SZ - 1, 1, low[u], high[v]);
    if (!out[ind]) out[ind] = -1;
  }
  for (long long i = 0; i < m; i++) cout << out[i] << "\n";
  return 0;
}
