#include <bits/stdc++.h>
using namespace std;
const int M = 100005;
struct people {
  int r, a, sz;
} p[M];
struct qes {
  int x, y, amin, amax, rmin, id;
} q[M];
int n, K, Q, ans[M];
int A[M], N;
bool cmpp(people a, people b) { return a.r < b.r; }
bool cmpq(qes a, qes b) { return a.rmin > b.rmin; }
int S[M];
int lowbit(int x) { return x & (-x); }
void add(int x, int y) {
  while (x <= N) {
    S[x] += y;
    x += lowbit(x);
  }
}
int getSum(int x) {
  int ret = 0;
  while (x) {
    ret += S[x];
    x -= lowbit(x);
  }
  return ret;
}
int T[4 * M];
void update(int st, int ed, int pos, int val, int f) {
  T[f] = max(T[f], val);
  if (st == ed) return;
  int mid = (st + ed) >> 1;
  if (pos <= mid)
    update(st, mid, pos, val, f << 1);
  else
    update(mid + 1, ed, pos, val, (f << 1) | 1);
}
int getMax(int st, int ed, int le, int ri, int f) {
  if (le > ri) return -1;
  if (st == le && ed == ri) return T[f];
  int mid = (st + ed) >> 1;
  if (ri <= mid)
    return getMax(st, mid, le, ri, f << 1);
  else if (le >= mid + 1)
    return getMax(mid + 1, ed, le, ri, (f << 1) | 1);
  else
    return max(getMax(st, mid, le, mid, f << 1),
               getMax(mid + 1, ed, mid + 1, ri, (f << 1) | 1));
}
void initRead() {
  for (int i = 0; i < n; i++) scanf("%d", &p[i].r);
  for (int i = 0; i < n; i++) {
    scanf("%d", &p[i].a);
    A[i] = p[i].a;
  }
  scanf("%d", &Q);
  for (int i = 0; i < Q; i++) {
    scanf("%d%d", &q[i].x, &q[i].y);
    q[i].amin = max(p[q[i].x - 1].a - K, p[q[i].y - 1].a - K);
    q[i].amax = min(p[q[i].x - 1].a + K, p[q[i].y - 1].a + K);
    q[i].rmin = max(p[q[i].x - 1].r, p[q[i].y - 1].r);
    q[i].id = i;
  }
  N = n;
  sort(A, A + N);
  N = unique(A, A + N) - A;
  for (int i = 0; i < n; i++) {
    p[i].a = lower_bound(A, A + N, p[i].a) - A;
  }
  sort(p, p + n, cmpp);
  sort(q, q + Q, cmpq);
}
void initLeader() {
  for (int i = 0; i < n;) {
    int j = i;
    while (j < n && p[j].r == p[i].r) j++;
    for (int k = i; k < j; k++) add(p[k].a + 1, 1);
    for (int k = i; k < j; k++) {
      int le = lower_bound(A, A + N, A[p[k].a] - K) - A + 1;
      int ri = upper_bound(A, A + N, A[p[k].a] + K) - A - 1 + 1;
      p[k].sz = getSum(ri) - getSum(le - 1);
    }
    i = j;
  }
}
void solve() {
  int ind = n - 1;
  memset(T, -1, sizeof(T));
  for (int i = 0; i < Q; i++) {
    while (ind >= 0 && p[ind].r >= q[i].rmin) {
      update(1, N, p[ind].a + 1, p[ind].sz, 1);
      ind--;
    }
    int le = lower_bound(A, A + N, q[i].amin) - A + 1;
    int ri = upper_bound(A, A + N, q[i].amax) - A - 1 + 1;
    ans[q[i].id] = getMax(1, N, le, ri, 1);
  }
  for (int i = 0; i < Q; i++) printf("%d\n", ans[i]);
}
int main() {
  while (scanf("%d%d", &n, &K) != EOF) {
    initRead();
    initLeader();
    solve();
  }
  return 0;
}
