#include <bits/stdc++.h>
#pragma comment(linker, "/stack:64000000")
using namespace std;
template <typename first>
inline first abs(const first& a) {
  return a < 0 ? -a : a;
}
template <typename first>
inline first sqr(const first& a) {
  return a * a;
}
const long double PI = 3.1415926535897932384626433832795;
const long double EPS = 1e-9;
const int INF = 1000 * 1000 * 1000;
const int N = 100 * 1000 + 3;
int n, dy, m;
int xs[N], ys[N];
vector<pair<pair<int, int>, int> > p;
vector<int> yy;
int maxg[N];
vector<pair<pair<int, int>, int> > qs[N];
int ans[N];
int t[4 * N];
void updSum(int idx, int lf, int rg, int pos, int val) {
  if (lf == rg) return void(t[idx] += val);
  int mid = (lf + rg) >> 1;
  if (pos <= mid)
    updSum(2 * idx + 1, lf, mid, pos, val);
  else
    updSum(2 * idx + 2, mid + 1, rg, pos, val);
  t[idx] = t[2 * idx + 1] + t[2 * idx + 2];
}
int sum(int idx, int lf, int rg, int l, int r) {
  if (lf == l && rg == r) return t[idx];
  int mid = (lf + rg) >> 1;
  int res = 0;
  if (l <= mid) res += sum(2 * idx + 1, lf, mid, l, min(mid, r));
  if (r > mid) res += sum(2 * idx + 2, mid + 1, rg, max(mid + 1, l), r);
  return res;
}
void updMax(int idx, int lf, int rg, int pos, int val) {
  if (lf == rg) return void(t[idx] = max(t[idx], val));
  int mid = (lf + rg) >> 1;
  if (pos <= mid)
    updMax(2 * idx + 1, lf, mid, pos, val);
  else
    updMax(2 * idx + 2, mid + 1, rg, pos, val);
  t[idx] = max(t[2 * idx + 1], t[2 * idx + 2]);
}
int max(int idx, int lf, int rg, int l, int r) {
  if (lf == l && rg == r) return t[idx];
  int mid = (lf + rg) >> 1;
  int res = -1;
  if (l <= mid) res = max(res, max(2 * idx + 1, lf, mid, l, min(r, mid)));
  if (r > mid)
    res = max(res, max(2 * idx + 2, mid + 1, rg, max(mid + 1, l), r));
  return res;
}
int main() {
  cin >> n >> dy;
  for (int i = 0; i < int(n); i++) scanf("%d", &xs[i]);
  for (int i = 0; i < int(n); i++) {
    scanf("%d", &ys[i]);
    yy.push_back(ys[i]);
  }
  sort((yy).begin(), (yy).end());
  yy.erase(unique((yy).begin(), (yy).end()), yy.end());
  for (int i = 0; i < int(n); i++)
    p.push_back(make_pair((make_pair((xs[i]), (ys[i]))), (i)));
  sort((p).begin(), (p).end());
  for (int i = 0; i < int(n); i++) {
    int idx = int(lower_bound((yy).begin(), (yy).end(), p[i].first.second) -
                  yy.begin());
    updSum(0, 0, int((yy).size()) - 1, idx, 1);
    int lf = int(lower_bound((yy).begin(), (yy).end(), p[i].first.second - dy) -
                 yy.begin());
    int rg =
        int(lower_bound((yy).begin(), (yy).end(), p[i].first.second + dy + 1) -
            yy.begin()) -
        1;
    maxg[p[i].second] = sum(0, 0, int((yy).size()) - 1, lf, rg);
  }
  cin >> m;
  memset(ans, -1, sizeof(ans));
  for (int i = 0; i < int(m); i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    --a, --b;
    if (abs(ys[a] - ys[b]) > 2 * dy) continue;
    if (xs[a] < xs[b]) swap(a, b);
    qs[a].push_back(make_pair(
        (make_pair((max(ys[a], ys[b]) - dy), (min(ys[a], ys[b]) + dy))), (i)));
    if (xs[a] == xs[b])
      qs[b].push_back(make_pair(
          (make_pair((max(ys[a], ys[b]) - dy), (min(ys[a], ys[b]) + dy))),
          (i)));
  }
  memset(t, -1, sizeof(t));
  for (int i = int(n) - 1; i >= 0; i--) {
    int idx = int(lower_bound((yy).begin(), (yy).end(), p[i].first.second) -
                  yy.begin());
    updMax(0, 0, int((yy).size()) - 1, idx, maxg[p[i].second]);
    for (int j = 0; j < int(int((qs[p[i].second]).size())); j++) {
      pair<pair<int, int>, int> curq = qs[p[i].second][j];
      int lf = int(lower_bound((yy).begin(), (yy).end(), curq.first.first) -
                   yy.begin());
      int rg =
          int(lower_bound((yy).begin(), (yy).end(), curq.first.second + 1) -
              yy.begin()) -
          1;
      if (lf <= rg)
        ans[curq.second] =
            max(ans[curq.second], max(0, 0, int((yy).size()) - 1, lf, rg));
    }
  }
  for (int i = 0; i < int(m); i++) printf("%d\n", ans[i]);
  return 0;
}
