#include <bits/stdc++.h>
using namespace std;
using namespace std;
const int maxn = 1e5 + 100;
pair<long long, long long> b[maxn];
int a[maxn];
pair<long long, long long> ru[maxn];
int seg[maxn * 5];
long long sh[maxn];
int c[maxn];
long long ans[maxn];
long long f[maxn];
long long javab[maxn];
long long t[maxn];
pair<long long, long long> q[maxn];
pair<long long, long long> baq[maxn];
long long soq[maxn];
int n, k, m;
bool amx_3(int i, int j) { return t[i] < t[j]; }
void add_F(int i, int j) {
  for (; i < maxn; i += (-i & i)) f[i] += j;
}
long long get_F(int i) {
  int d = 0;
  for (; i != 0; i -= (i & (-i))) d += f[i];
  return d;
}
bool amx_1(int i, int j) { return b[i].second < b[j].second; }
bool amx_2(int i, int j) { return ru[i].first < ru[j].first; }
void add(int i, int j, int x, int l) {
  if (l < i || l >= j) return;
  if (i + 1 == j) {
    seg[x] = ans[l];
    return;
  }
  add(i, (i + j) / 2, (x * 2), l);
  add((i + j) / 2, j, (x * 2 + 1), l);
  seg[x] = max(seg[(x * 2)], seg[(x * 2 + 1)]);
}
long long get(int i, int j, int x, int l, int r) {
  if (i >= r || j <= l) return 0;
  if (i >= l && j <= r) return seg[x];
  return max(get(i, (i + j) / 2, (x * 2), l, r),
             get((i + j) / 2, j, (x * 2 + 1), l, r));
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  for (long long y = 1; y <= n; y++) cin >> b[y].first, c[y] = y;
  for (long long y = 1; y <= n; y++) cin >> b[y].second, a[y] = y;
  sort(a + 1, a + 1 + n, amx_1);
  for (long long y = 1; y <= n; y++) ru[y] = b[a[y]];
  for (long long y = 1; y <= n; y++) sh[a[y]] = y;
  sort(c + 1, c + 1 + n, amx_2);
  int po = 1;
  for (int y = 1; y <= n; y++) {
    int i, j;
    long long x = c[y];
    i = j = x;
    while (po < n + 1 && ru[c[po]].first <= ru[x].first) add_F(c[po], 1), po++;
    for (int d = 20; d > -1; d--) {
      if ((1 << d) < i && (ru[x].second - ru[i - (1 << d)].second <= k)) {
        i -= (1 << d);
      }
    }
    for (int d = 20; d > -1; d--) {
      if (j + (1 << d) <= n && ru[j + (1 << d)].second - ru[x].second <= k)
        j += (1 << d);
    }
    ans[x] = get_F(j) - get_F(i - 1);
  }
  po = n;
  cin >> m;
  for (int y = 1; y <= m; y++) {
    int i, j;
    cin >> i >> j;
    i = sh[i];
    j = sh[j];
    if (i > j) swap(i, j);
    q[y] = pair<long long, long long>(i, j);
    int l, r;
    l = i;
    r = j;
    for (int d = 20; d > -1; d--) {
      if (r - (1 << d) > 0 && ru[j].second - ru[r - (1 << d)].second <= k)
        r -= (1 << d);
    }
    for (int d = 20; d > -1; d--) {
      if (l + (1 << d) <= n && ru[l + (1 << d)].second - ru[i].second <= k)
        l += (1 << d);
    }
    baq[y] = pair<long long, long long>(r, l + 1);
    soq[y] = y;
    t[y] = max(ru[i].first, ru[j].first);
  }
  sort(soq + 1, soq + 1 + m, amx_3);
  for (long long e = m; e; e--) {
    int s = soq[e];
    int i = q[s].first;
    int j = q[s].second;
    while (po > 0 && max(ru[j].first, ru[i].first) <= ru[c[po]].first) {
      add(1, n + 1, 1, c[po]);
      po--;
    }
    javab[s] = get(1, n + 1, 1, baq[s].first, baq[s].second);
  }
  for (long long y = 1; y <= m; y++) {
    if (javab[y])
      cout << javab[y] << ' ';
    else
      cout << -1 << ' ';
  }
}
