#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int n, m, k;
int cnt_a, cnt_r;
struct data_type {
  int index, A, R, a, r;
};
data_type data[maxn];
struct query_type {
  int index, l, r;
};
query_type query[maxn];
int order[maxn];
int cap[maxn];
int ADD[maxn];
int CUT[maxn];
int Tarr[maxn];
int maxs[maxn * 8];
int ans[maxn];
void inplt() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) scanf("%d", &data[i].R);
  for (int i = 1; i <= n; i++) scanf("%d", &data[i].A);
  for (int i = 1; i <= n; i++) data[i].index = i;
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) scanf("%d%d", &query[i].l, &query[i].r);
}
bool cmp_A(const data_type &A, const data_type &B) { return A.A < B.A; }
bool cmp_R(const data_type &A, const data_type &B) { return A.R < B.R; }
bool cmp_Q(const query_type &A, const query_type &B) { return A.r < B.r; }
void discretize() {
  sort(data + 1, data + 1 + n, cmp_A);
  for (int i = 1; i <= n; i++) {
    if (i == 1 || data[i].A > data[i - 1].A) cnt_a++;
    data[i].a = cnt_a;
  }
  for (int i = 1, j = 1; i <= n; i++) {
    while (data[i].A - data[j].A > k) j++;
    CUT[data[i].a] = data[j].a;
  }
  for (int i = n, j = n; i >= 1; i--) {
    while (data[j].A - data[i].A > k) j--;
    ADD[data[i].a] = data[j].a;
  }
  sort(data + 1, data + 1 + n, cmp_R);
  for (int i = 1; i <= n; i++) {
    if (i == 1 || data[i].R > data[i - 1].R) cnt_r++;
    data[i].r = cnt_r;
  }
  for (int i = 1; i <= n; i++) order[data[i].index] = i;
}
void add(int t) {
  while (t <= cnt_a) {
    Tarr[t]++;
    t += t & -t;
  }
}
int caln(int t) {
  int ret = 0;
  while (t >= 1) {
    ret += Tarr[t];
    t -= t & -t;
  }
  return ret;
}
void preprocess() {
  for (int i = 1; i <= n; i++) {
    if (i == 1 || data[i].r != data[i - 1].r) {
      for (int j = i; j <= n && data[j].r == data[i].r; j++) add(data[j].a);
    }
    cap[i] = caln(ADD[data[i].a]) - caln(CUT[data[i].a] - 1);
  }
}
void update(int t) { maxs[t] = max(maxs[t * 2 + 0], maxs[t * 2 + 1]); }
void insert(int t, int l, int r, int x, int p) {
  if (l == r) {
    maxs[t] = max(maxs[t], p);
    return;
  }
  int mid = (l + r) >> 1;
  if (x <= mid)
    insert(t * 2 + 0, l, mid, x, p);
  else
    insert(t * 2 + 1, mid + 1, r, x, p);
  update(t);
}
int search(int t, int l, int r, int x, int y) {
  if (x > y) return 0;
  if (l == x && y == r) return maxs[t];
  int mid = (l + r) >> 1;
  if (y <= mid) return search(t * 2 + 0, l, mid, x, y);
  if (x >= mid + 1) return search(t * 2 + 1, mid + 1, r, x, y);
  return max(search(t * 2 + 0, l, mid, x, mid),
             search(t * 2 + 1, mid + 1, r, mid + 1, y));
}
void solve() {
  discretize();
  preprocess();
  for (int i = 1; i <= m; i++) {
    query[i].index = i;
    query[i].l = order[query[i].l];
    query[i].r = order[query[i].r];
    if (query[i].l > query[i].r) swap(query[i].l, query[i].r);
  }
  sort(query + 1, query + 1 + m, cmp_Q);
  for (int i = m, j = n + 1; i >= 1; i--) {
    int l = query[i].l, r = query[i].r;
    while (j - 1 >= 1 && data[j - 1].r >= data[r].r) {
      j--;
      insert(1, 1, cnt_a, data[j].a, cap[j]);
    }
    int age1 = data[l].a, age2 = data[r].a;
    if (age1 > age2) swap(age1, age2);
    age1 = ADD[age1], age2 = CUT[age2];
    swap(age1, age2);
    ans[query[i].index] = search(1, 1, cnt_a, age1, age2);
  }
  for (int i = 1; i <= m; i++)
    if (ans[i] == 0)
      printf("-1\n");
    else
      printf("%d\n", ans[i]);
}
int main() {
  inplt();
  solve();
  return 0;
}
