#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e6;
int a[maxn], b[maxn], n, q, k, bt[4 * maxn], fen[4 * maxn], ls[4 * maxn],
    rs[4 * maxn], cnt = 2, sz[maxn], ans[maxn];
map<int, int> m;
pair<int, int> p[maxn];
pair<pair<int, int>, pair<int, int> > qs[maxn];
vector<int> ps;
void chk(int u) {
  if (ls[u] == 0) {
    ls[u] = cnt++;
    rs[u] = cnt++;
  }
}
void add(int u, int l, int r, int v) {
  if (r - l == 1) {
    fen[u]++;
    return;
  }
  int md = (l + r) >> 1;
  chk(u);
  if (v < md) {
    add(ls[u], l, md, v);
  } else {
    add(rs[u], md, r, v);
  }
  fen[u] = fen[ls[u]] + fen[rs[u]];
}
int gt(int u, int l, int r, int f, int s) {
  if (f >= r || l >= s) {
    return 0;
  }
  if (f <= l && r <= s) {
    return fen[u];
  }
  int md = (l + r) >> 1;
  chk(u);
  return gt(ls[u], l, md, f, s) + gt(rs[u], md, r, f, s);
}
void up(int u, int l, int r, int v, int x) {
  if (r - l == 1) {
    bt[u] = max(bt[u], x);
    return;
  }
  chk(u);
  int md = (l + r) >> 1;
  if (v < md)
    up(ls[u], l, md, v, x);
  else
    up(rs[u], md, r, v, x);
  bt[u] = max(bt[ls[u]], bt[rs[u]]);
}
int query(int u, int l, int r, int f, int s) {
  if (f >= r || l >= s) {
    return -1;
  }
  if (f <= l && r <= s) {
    return bt[u];
  }
  int md = (l + r) >> 1;
  chk(u);
  return max(query(ls[u], l, md, f, s), query(rs[u], md, r, f, s));
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    cin >> b[i];
    ps.push_back(b[i]);
  }
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    p[i] = {b[i], i};
  }
  sort(p, p + n);
  for (int i = 0; i < n; ++i) {
    int y = i;
    while (p[y].first == p[i].first) {
      int x = p[y].second;
      add(1, 0, 1e9 + 1, a[x]);
      y++;
    }
    for (int j = i; j < y; ++j) {
      int x = p[j].second;
      sz[x] =
          gt(1, 0, 1e9 + 1, max(a[x] - k, 0), min(a[x] + k + 1, (int)1e9 + 1));
    }
    i = y - 1;
  }
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    if (a[x] > a[y]) {
      swap(x, y);
    }
    qs[i] = {{max(b[x], b[y]), i},
             {max(a[y] - k, 0), min(a[x] + k + 1, (int)1e9 + 1)}};
    ps.push_back(qs[i].first.first);
  }
  sort(ps.begin(), ps.end());
  sort(qs, qs + q);
  reverse(ps.begin(), ps.end());
  reverse(qs, qs + q);
  reverse(p, p + n);
  int g = 0, po = 0;
  memset(bt, -1, sizeof bt);
  for (int j = 0; j < ps.size(); ++j) {
    int i = ps[j];
    while (po != n && p[po].first == i) {
      int x = p[po].second;
      up(1, 0, 1e9 + 1, a[x], sz[x]);
      po++;
    }
    while (g != q && qs[g].first.first == i) {
      if (ans[qs[g].first.second] != -1) {
        int h = qs[g].first.first, l = qs[g].second.first,
            r = qs[g].second.second, ind = qs[g].first.second;
        if (r <= l) {
          ans[ind] = -1;
        } else
          ans[ind] = query(1, 0, 1e9 + 1, l, r);
      }
      g++;
    }
    if (g == q) {
      break;
    }
  }
  for (int i = 0; i < q; ++i) {
    cout << ans[i] << '\n';
  }
  return 0;
}
