#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
int N, K, Q, n;
int R[maxn], A[maxn], Ord[maxn], B[maxn << 2], C[maxn], cnt[maxn],
    MAX[maxn << 4], ans[maxn];
struct node {
  int x, y, c, id;
  node(int xx = 0, int yy = 0, int iid = 0) {
    x = xx, y = yy, id = iid;
    c = max(R[xx], R[yy]);
  }
  friend bool operator<(node a, node b) { return a.c > b.c; }
} query[maxn];
inline bool cmp1(int a, int b) {
  return R[a] < R[b] || (R[a] == R[b] && A[a] < A[b]);
}
inline void Add(int x, int k) {
  while (x <= n) {
    C[x] += k;
    x += x & (-x);
  }
}
inline int Sum(int x) {
  int ans = 0;
  while (x) {
    ans += C[x];
    x -= x & (-x);
  }
  return ans;
}
inline int Hash(int x) { return lower_bound(B + 1, B + n + 1, x) - B; }
void Change(int x, int l, int r, int id, int k) {
  if (l == r) {
    MAX[id] = max(MAX[id], k);
    return;
  }
  int m = (l + r) >> 1;
  if (x <= m)
    Change(x, l, m, id << 1, k);
  else
    Change(x, m + 1, r, id << 1 | 1, k);
  MAX[id] = max(MAX[id << 1], MAX[id << 1 | 1]);
}
int Query(int x, int y, int l, int r, int id) {
  if (x <= l && r <= y) return MAX[id];
  int m = (l + r) >> 1;
  if (x <= m && y > m)
    return max(Query(x, y, l, m, id << 1), Query(x, y, m + 1, r, id << 1 | 1));
  else if (x <= m)
    return Query(x, y, l, m, id << 1);
  else
    return Query(x, y, m + 1, r, id << 1 | 1);
}
int main() {
  ios::sync_with_stdio(false);
  cin >> N >> K;
  for (register int i = 1; i <= N; ++i) cin >> R[i];
  for (register int i = 1; i <= N; ++i) {
    cin >> A[i];
    B[++n] = A[i];
    B[++n] = A[i] + K;
    B[++n] = A[i] - K - 1;
    B[++n] = A[i] - K;
  }
  sort(B + 1, B + n + 1);
  n = unique(B + 1, B + n + 1) - B - 1;
  for (register int i = 1; i <= N; ++i) Ord[i] = i;
  sort(Ord + 1, Ord + N + 1, cmp1);
  for (register int i = 1; i <= N;) {
    int j = i;
    while (R[Ord[j + 1]] == R[Ord[i]]) ++j;
    for (register int k = i; k <= j; ++k) Add(Hash(A[Ord[k]]), 1);
    for (register int k = i; k <= j; ++k) {
      int l = Hash(A[Ord[k]] - K - 1);
      int r = Hash(A[Ord[k]] + K);
      cnt[Ord[k]] = Sum(r) - Sum(l);
    }
    i = j + 1;
  }
  cin >> Q;
  for (register int i = 1; i <= Q; ++i) {
    int x, y;
    cin >> x >> y;
    query[i] = node(x, y, i);
  }
  sort(query + 1, query + Q + 1);
  int cur = N;
  for (int i = 1; i <= Q; i++) {
    while (cur && R[Ord[cur]] >= query[i].c) {
      Change(Hash(A[Ord[cur]]), 1, n, 1, cnt[Ord[cur]]);
      --cur;
    }
    int x = query[i].x, y = query[i].y;
    if (A[x] > A[y]) swap(x, y);
    int l = max(Hash(A[x] - K), Hash(A[y] - K));
    int r = min(Hash(A[x] + K), Hash(A[y] + K));
    if (l > r)
      ans[query[i].id] = -1;
    else {
      int ass = Query(l, r, 1, n, 1);
      if (!ass)
        ans[query[i].id] = -1;
      else
        ans[query[i].id] = ass;
    }
  }
  for (int i = 1; i <= Q; i++) printf("%d\n", ans[i]);
  return 0;
}
