#include <bits/stdc++.h>
using namespace std;
struct edge {
  int next, now;
} d[100010];
struct node {
  int r, a, p;
} peo[100010];
struct question {
  int mr, l, r, p;
} que[100010];
int sum[100010], c[100010], tree[100010 * 4], pos[100010], ans[100010],
    id[100010];
int n, m, k;
bool compare1(const node &x, const node &y) { return x.r < y.r; }
bool compare2(const node &x, const node &y) { return x.a < y.a; }
bool compare3(const question &x, const question &y) { return x.mr < y.mr; }
void add_value(int u, int v, int i) {
  d[i].next = c[u];
  c[u] = i;
  d[i].now = v;
}
void build_tree(int l, int r, int i) {
  if (l == r) {
    pos[l] = i;
    return;
  }
  int mid = (l + r) / 2;
  build_tree(l, mid, i + i);
  build_tree(mid + 1, r, i + i + 1);
}
void change(int x) {
  while (x > 0) {
    tree[x] += 1;
    x /= 2;
  }
}
int calc_sum(int tl, int tr, int l, int r, int i) {
  if (l > tr || r < tl) return 0;
  if (tl <= l && r <= tr) return tree[i];
  int mid = (l + r) / 2;
  return calc_sum(tl, tr, l, mid, i + i) +
         calc_sum(tl, tr, mid + 1, r, i + i + 1);
}
void Change(int x, int s) {
  while (x > 0) {
    tree[x] = max(tree[x], s);
    x /= 2;
  }
}
int calc_max(int tl, int tr, int l, int r, int i) {
  if (l > tr || r < tl) return 0;
  if (tl <= l && r <= tr) return tree[i];
  int mid = (l + r) / 2;
  return max(calc_max(tl, tr, l, mid, i + i),
             calc_max(tl, tr, mid + 1, r, i + i + 1));
}
int binary_search_up(int l, int r, int x) {
  while (l <= r) {
    int mid = (l + r) / 2;
    if (peo[mid].a >= x)
      r = mid - 1;
    else
      l = mid + 1;
  }
  return l;
}
int binary_search_down(int l, int r, int x) {
  while (l <= r) {
    int mid = (l + r) / 2;
    if (peo[mid].a <= x)
      l = mid + 1;
    else
      r = mid - 1;
  }
  return r;
}
void init() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++) cin >> peo[i].r;
  for (int i = 1; i <= n; i++) cin >> peo[i].a;
  for (int i = 1; i <= n; i++) peo[i].p = i;
}
void solve() {
  sort(peo + 1, peo + n + 1, compare1);
  int t = 0;
  for (int i = 1; i <= n; i++) {
    if (peo[i].r != peo[i - 1].r) t++;
    peo[i].r = t;
  }
  sort(peo + 1, peo + n + 1, compare2);
  for (int i = 1; i <= n; i++) add_value(peo[i].r, i, i);
  memset(tree, 0, sizeof(tree));
  build_tree(1, n, 1);
  for (int j = 1; j <= n; j++) {
    for (int i = c[j]; i; i = d[i].next) change(pos[d[i].now]);
    for (int i = c[j]; i; i = d[i].next) {
      int now = peo[d[i].now].a;
      int l = binary_search_up(1, d[i].now, now - k);
      int r = binary_search_down(d[i].now, n, now + k);
      sum[d[i].now] = calc_sum(l, r, 1, n, 1);
    }
  }
  for (int i = 1; i <= n; i++) id[peo[i].p] = i;
  cin >> m;
  for (int i = 1; i <= m; i++) {
    int x, y;
    cin >> x >> y;
    que[i].mr = max(peo[id[x]].r, peo[id[y]].r);
    que[i].l = max(binary_search_up(1, id[x], peo[id[x]].a - k),
                   binary_search_up(1, id[y], peo[id[y]].a - k));
    que[i].r = min(binary_search_down(id[x], n, peo[id[x]].a + k),
                   binary_search_down(id[y], n, peo[id[y]].a + k));
    que[i].p = i;
  }
  sort(que + 1, que + m + 1, compare3);
  memset(tree, 0, sizeof(tree));
  int now = n + 1;
  for (int j = m; j >= 1; j--) {
    for (int t = que[j].mr; t <= now - 1; t++)
      for (int i = c[t]; i; i = d[i].next) Change(pos[d[i].now], sum[d[i].now]);
    now = que[j].mr;
    ans[que[j].p] = calc_max(que[j].l, que[j].r, 1, n, 1);
  }
  for (int i = 1; i <= m; i++)
    if (ans[i] == 0)
      cout << "-1" << endl;
    else
      cout << ans[i] << endl;
}
int main() {
  ios::sync_with_stdio(false);
  init();
  solve();
  return 0;
}
