#include <bits/stdc++.h>
using namespace std;
const int N = 1010101;
int n, m, i, j, q, q1, q2, mx[N], l, r, c[N], flag[N], L[N], R[N], size[N];
int root[N], ls[N], rs[N], s[N], cnt, ans[N];
struct g {
  int r, v, id;
} a[N], o[N];
vector<int> num, vec[N];
vector<pair<int, int> > Q[N];
void cc(int x, int w) {
  while (x <= n) {
    c[x] += w;
    x += (x & -x);
  }
}
int sum(int x) {
  int ans = 0;
  while (x) {
    ans += c[x];
    x -= (x & -x);
  }
  return ans;
}
bool cmp(g a, g b) { return a.v < b.v; }
bool Cmp(g a, g b) { return a.v > b.v; }
void change(int x, int a, int b, int l, int r, int c) {
  if ((a <= l) && (r <= b)) {
    s[x] = max(s[x], c);
    return;
  }
  int m = (l + r) >> 1;
  if (a < m) change(2 * x, a, b, l, m, c);
  if (m < b) change(2 * x + 1, a, b, m, r, c);
  s[x] = max(s[2 * x], s[2 * x + 1]);
}
int query(int x, int a, int b, int l, int r) {
  int m = (l + r) >> 1, ans = 0;
  if ((a <= l) && (r <= b)) {
    return s[x];
  }
  if (a < m) ans = max(ans, query(2 * x, a, b, l, m));
  if (m < b) ans = max(ans, query(2 * x + 1, a, b, m, r));
  return ans;
}
int main() {
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++) {
    scanf("%d", &a[i].r);
    a[i].id = i;
    num.push_back(a[i].r);
  }
  for (i = 1; i <= n; i++) scanf("%d", &a[i].v);
  sort(num.begin(), num.end());
  num.erase(unique(num.begin(), num.end()), num.end());
  for (i = 1; i <= n; i++)
    a[i].r = lower_bound(num.begin(), num.end(), a[i].r) - num.begin() + 1;
  scanf("%d", &q);
  for (i = 1; i <= q; i++) {
    scanf("%d%d", &q1, &q2);
    Q[q1].push_back(make_pair(q2, i));
    Q[q2].push_back(make_pair(q1, i));
    mx[i] = max(a[q1].r, a[q2].r);
    vec[mx[i]].push_back(i);
  }
  sort(a + 1, a + 1 + n, cmp);
  l = r = 1;
  for (i = 1; i <= n; i++) {
    while (a[i].v - a[l].v > m) {
      cc(a[l].r, -1);
      for (j = 0; j < Q[a[l].id].size(); j++)
        if (flag[Q[a[l].id][j].first]) R[Q[a[l].id][j].second] = i - 1;
      flag[a[l].id] = 0;
      l++;
    }
    while ((r <= n) && (a[r].v - a[i].v <= m)) {
      cc(a[r].r, 1);
      for (j = 0; j < Q[a[r].id].size(); j++)
        if (flag[Q[a[r].id][j].first]) L[Q[a[r].id][j].second] = i;
      flag[a[r].id] = 1;
      r++;
    }
    o[i].r = sum(a[i].r);
    o[i].v = a[i].r;
    o[i].id = i;
  }
  for (i = 1; i <= q; i++)
    if ((L[i]) && (!R[i])) R[i] = n;
  sort(o + 1, o + 1 + n, Cmp);
  for (i = 1; i <= n; i++) {
    change(1, o[i].id - 1, o[i].id, 0, n, o[i].r);
    if (o[i].v != o[i + 1].v) {
      for (j = 0; j < vec[o[i].v].size(); j++) {
        int t = vec[o[i].v][j];
        if ((L[t] <= R[t]) && (L[t]) && (R[t]))
          ans[t] = query(1, L[t] - 1, R[t], 0, n);
      }
    }
  }
  for (i = 1; i <= q; i++)
    if (ans[i])
      printf("%d\n", ans[i]);
    else
      printf("-1\n");
}
