#include <bits/stdc++.h>
using namespace std;
int N, K;
pair<pair<int, int>, int> A[100000];
int mx[100000];
vector<int> r, a;
bool cmp_a(pair<pair<int, int>, int> p1, pair<pair<int, int>, int> p2) {
  return p1.first.second < p2.first.second;
}
bool cmp_i(pair<pair<int, int>, int> p1, pair<pair<int, int>, int> p2) {
  return p1.second < p2.second;
}
struct BIT {
  int bit[114514];
  void add(int x, int a) {
    x++;
    while (x < 114514) bit[x] += a, x += x & -x;
  }
  int sum(int x) {
    x++;
    int r = 0;
    while (x > 0) r += bit[x], x -= x & -x;
    return r;
  }
} bit;
const int n = 1 << 17;
struct SegTree {
  int dat[n * 2 - 1];
  void update(int k, int s) {
    k += n - 1;
    dat[k] = max(dat[k], s);
    while (k) k = (k - 1) / 2, dat[k] = max(dat[k], s);
  }
  int query(int a, int b, int k, int l, int r) {
    if (a >= b) return 0;
    if (r <= a || b <= l) return 0;
    if (a <= l && r <= b) return dat[k];
    return max(query(a, b, k * 2 + 1, l, (l + r) / 2),
               query(a, b, k * 2 + 2, (l + r) / 2, r));
  }
} seg;
int Q, X, Y;
int ans[100000];
int main() {
  scanf("%d%d", &N, &K);
  for (int(i) = 0; (i) < (N); (i)++)
    scanf("%d", &A[i].first.first), r.push_back(A[i].first.first);
  for (int(i) = 0; (i) < (N); (i)++)
    scanf("%d", &A[i].first.second), A[i].second = i,
                                     a.push_back(A[i].first.second);
  sort(A, A + N, cmp_a);
  sort((r).begin(), (r).end());
  (r).erase(unique((r).begin(), (r).end()), (r).end());
  sort((a).begin(), (a).end());
  (a).erase(unique((a).begin(), (a).end()), (a).end());
  int p1 = 0, p2 = 0;
  for (int(i) = 0; (i) < (N); (i)++) {
    while (p1 < N && A[i].first.second - K > A[p1].first.second) {
      bit.add(lower_bound((r).begin(), (r).end(), (A[p1].first.first)) -
                  (r).begin(),
              -1);
      p1++;
    }
    while (p2 < N && A[i].first.second + K >= A[p2].first.second) {
      bit.add(lower_bound((r).begin(), (r).end(), (A[p2].first.first)) -
                  (r).begin(),
              1);
      p2++;
    }
    mx[A[i].second] = bit.sum(
        lower_bound((r).begin(), (r).end(), (A[i].first.first)) - (r).begin());
  }
  scanf("%d", &Q);
  sort(A, A + N, cmp_i);
  vector<pair<pair<int, int>, pair<int, int> > > v;
  for (int(i) = 0; (i) < (Q); (i)++) {
    scanf("%d%d", &X, &Y);
    X--, Y--;
    int r = max(A[X].first.first, A[Y].first.first);
    int al = max(A[X].first.second, A[Y].first.second) - K;
    int ar = min(A[X].first.second, A[Y].first.second) + K;
    al = lower_bound((a).begin(), (a).end(), (al)) - (a).begin();
    ar = lower_bound((a).begin(), (a).end(), (ar + 1)) - (a).begin();
    v.push_back(make_pair(make_pair(r, i), make_pair(al, ar)));
  }
  sort((v).begin(), (v).end());
  reverse((v).begin(), (v).end());
  sort(A, A + N);
  reverse(A, A + N);
  int ptr = 0;
  for (int(i) = 0; (i) < (Q); (i)++) {
    while (ptr < N && A[ptr].first.first >= v[i].first.first) {
      seg.update(lower_bound((a).begin(), (a).end(), (A[ptr].first.second)) -
                     (a).begin(),
                 mx[A[ptr].second]);
      ptr++;
    }
    ans[v[i].first.second] =
        seg.query(v[i].second.first, v[i].second.second, 0, 0, n);
    if (ans[v[i].first.second] <= 0) ans[v[i].first.second] = -1;
  }
  for (int(i) = 0; (i) < (Q); (i)++) printf("%d\n", ans[i]);
}
