#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5;
int n, k, i, j, x, y, z, siz[100005], tree[1200005], ans[100005];
pair<int, int> temp[100005], input[100005];
vector<int> dis;
map<int, int> id;
struct f {
  int r, age, id;
  bool operator<(const f& T) const { return r < T.r; }
} ppl[100005];
struct f1 {
  int from, to, val, id;
  bool operator<(const f1& T) const { return val > T.val; }
};
vector<f1> quest;
void update(int l, int r, int ind) {
  if (l == r) {
    tree[ind]++;
    return;
  }
  int mid = (l + r) / 2;
  if (x <= mid)
    update(l, mid, ind * 2);
  else
    update(mid + 1, r, ind * 2 + 1);
  tree[ind] = tree[ind * 2] + tree[ind * 2 + 1];
}
void query(int l, int r, int ind) {
  if (x <= l && r <= y) {
    z += tree[ind];
    return;
  }
  int mid = (l + r) / 2;
  if (x <= mid) query(l, mid, ind * 2);
  if (y > mid) query(mid + 1, r, ind * 2 + 1);
}
void update_1(int l, int r, int ind) {
  if (l == r) {
    tree[ind] = max(tree[ind], z);
    return;
  }
  int mid = (l + r) / 2;
  if (x <= mid)
    update_1(l, mid, ind * 2);
  else
    update_1(mid + 1, r, ind * 2 + 1);
  tree[ind] = max(tree[ind * 2], tree[ind * 2 + 1]);
}
void query_1(int l, int r, int ind) {
  if (x <= l && r <= y) {
    z = max(z, tree[ind]);
    return;
  }
  int mid = (l + r) / 2;
  if (x <= mid) query_1(l, mid, ind * 2);
  if (y > mid) query_1(mid + 1, r, ind * 2 + 1);
}
int main() {
  scanf("%d%d", &n, &k);
  for (i = 0; i < n; i++) {
    scanf("%d", &ppl[i].r);
    temp[i].first = ppl[i].r;
    input[i].first = ppl[i].r;
  }
  for (i = 0; i < n; i++) {
    scanf("%d", &ppl[i].age);
    ppl[i].id = i;
    temp[i].second = ppl[i].age;
    input[i].second = ppl[i].age;
    dis.push_back(ppl[i].age);
    dis.push_back(ppl[i].age - k);
    dis.push_back(ppl[i].age + k);
  }
  sort(dis.begin(), dis.end());
  dis.resize(unique(dis.begin(), dis.end()) - dis.begin());
  int nxt = 0;
  for (i = 0; i < dis.size(); i++) id[dis[i]] = ++nxt;
  sort(ppl, ppl + n);
  sort(temp, temp + n);
  int p = 0;
  for (i = 0; i < n; i++) {
    while (p < n && temp[p].first <= ppl[i].r) {
      x = id[temp[p++].second];
      update(1, nxt, 1);
    }
    x = id[ppl[i].age - k];
    y = id[ppl[i].age + k];
    z = 0;
    query(1, nxt, 1);
    siz[ppl[i].id] = z;
  }
  int q;
  scanf("%d", &q);
  for (i = 0; i < q; i++) {
    scanf("%d%d", &x, &y);
    if (input[x - 1].second > input[y - 1].second) swap(x, y);
    int ub = input[x - 1].second + k;
    int lb1 = input[y - 1].second - k;
    if (lb1 > ub)
      ans[i] = -1;
    else
      quest.push_back(
          {lb1, ub, max(input[x - 1].first, input[y - 1].first), i});
  }
  sort(quest.begin(), quest.end());
  reverse(ppl, ppl + n);
  p = 0;
  memset(tree, 0, sizeof(tree));
  for (i = 0; i < quest.size(); i++) {
    while (p < n && ppl[p].r >= quest[i].val) {
      x = id[ppl[p].age];
      z = siz[ppl[p].id];
      update_1(1, nxt, 1);
      p++;
    }
    x = id[quest[i].from];
    y = id[quest[i].to];
    z = 0;
    query_1(1, nxt, 1);
    if (z == 0)
      ans[quest[i].id] = -1;
    else
      ans[quest[i].id] = z;
  }
  for (i = 0; i < q; i++) printf("%d\n", ans[i]);
}
