#include <bits/stdc++.h>
using namespace std;
const int maxn = 200100;
long long n, k, q;
class Tmem {
 public:
  long long age, res;
  bool operator<(const Tmem &m) const { return age < m.age; }
} mem[maxn];
long long age[maxn], res[maxn], lead[maxn], invage[maxn * 2];
vector<long long> con;
map<long long, long long> idx;
map<long long, long long> invidx;
class Tsum {
 public:
  long long stor[maxn * 4];
  void modify(int mod, int l, int r, int ll = 0, int rr = maxn, int idx = 1) {
    if (rr <= l || r <= ll) {
      return;
    } else if (l <= ll && rr <= r) {
      stor[idx] += mod;
    } else {
      int mid = (ll + rr) / 2;
      modify(mod, l, r, ll, mid, idx * 2);
      modify(mod, l, r, mid, rr, (idx * 2) + 1);
      stor[idx] = stor[idx * 2] + stor[(idx * 2) + 1];
    }
  }
  long long sigma(int l, int r, int ll = 0, int rr = maxn, int idx = 1) {
    if (rr <= l || r <= ll) {
      return 0;
    } else if (l <= ll && rr <= r) {
      return stor[idx];
    } else {
      long long mid = (ll + rr) / 2, ret = 0;
      ret += sigma(l, r, ll, mid, idx * 2);
      ret += sigma(l, r, mid, rr, (idx * 2) + 1);
      return ret;
    }
  }
} sum;
class Tstor {
 public:
  long long r, idx;
  bool operator<(const Tstor s) const { return r < s.r; }
  Tstor() {}
  Tstor(int _r) : r(_r) {}
  Tstor(int _r, int _i) : r(_r), idx(_i) {}
};
void pushOn(vector<Tstor> &at, vector<Tstor> &a, int i) {
  if (at.empty() || (at[at.size() - 1].r != a[i].r ||
                     lead[at[at.size() - 1].idx] < lead[a[i].idx])) {
    if (at.empty() == false && at[at.size() - 1].r == a[i].r) {
      at.pop_back();
    }
    at.push_back(a[i]);
  }
}
class Tseg {
 public:
  vector<Tstor> stor[maxn * 4];
  vector<int> best[maxn * 4];
  void insert(int ll = 0, int rr = maxn, int idx = 1) {
    if (rr - ll == 1) {
      if (invage[ll] != -1 &&
          (stor[idx].empty() == true || mem[invage[ll]].res > stor[idx][0].r)) {
        stor[idx].clear();
        stor[idx].push_back(Tstor(mem[invage[ll]].res, invage[ll]));
        best[idx].clear();
        best[idx].push_back(stor[idx][0].idx);
      }
    } else {
      int mid = (ll + rr) / 2;
      insert(ll, mid, idx * 2);
      insert(mid, rr, (idx * 2) + 1);
      int i = 0, j = 0;
      vector<Tstor> &a = stor[idx * 2], &b = stor[(idx * 2) + 1];
      while (i < a.size() && j < b.size()) {
        if (a[i].r < b[j].r) {
          pushOn(stor[idx], a, i++);
        } else {
          pushOn(stor[idx], b, j++);
        }
      }
      while (i < a.size()) {
        pushOn(stor[idx], a, i++);
      }
      while (j < b.size()) {
        pushOn(stor[idx], b, j++);
      }
      int cmp = -1;
      for (int i = stor[idx].size() - 1; i >= 0; i--) {
        if (cmp == -1 || lead[cmp] < lead[stor[idx][i].idx]) {
          cmp = stor[idx][i].idx;
        }
        best[idx].push_back(cmp);
      }
      reverse(best[idx].begin(), best[idx].end());
    }
  }
  int find(int res, int l, int r, int ll = 0, int rr = maxn, int idx = 1) {
    if (rr <= l || r <= ll) {
      return -1;
    } else if (l <= ll && rr <= r) {
      int lidx = lower_bound(stor[idx].begin(), stor[idx].end(), Tstor(res)) -
                 stor[idx].begin();
      if (lidx != stor[idx].size()) {
        return best[idx][lidx];
      }
      return -1;
    } else {
      int mid = (ll + rr) / 2, cmpa, cmpb;
      cmpa = find(res, l, r, ll, mid, idx * 2);
      cmpb = find(res, l, r, mid, rr, (idx * 2) + 1);
      int ret = cmpa;
      if (ret == -1 || (cmpb != -1 && lead[cmpb] > lead[cmpa])) {
        ret = cmpb;
      }
      return ret;
    }
  }
} root;
int main() {
  scanf("%lld%lld", &n, &k);
  for (int i = 0; i < n; i++) {
    scanf("%lld", &mem[i].res);
    con.push_back(mem[i].res);
    res[i] = mem[i].res;
  }
  for (int i = 0; i < n; i++) {
    scanf("%lld", &mem[i].age);
    con.push_back(mem[i].age);
    age[i] = mem[i].age;
  }
  scanf("%lld", &q);
  sort(con.begin(), con.end());
  con.resize(unique(con.begin(), con.end()) - con.begin());
  for (int i = 0; i < con.size(); i++) {
    idx[con[i]] = i;
    invidx[i] = con[i];
  }
  for (int i = 0; i < n; i++) {
    mem[i].age = idx[mem[i].age];
    mem[i].res = idx[mem[i].res];
  }
  sort(mem, mem + n);
  int lp = 0, hp = 0;
  for (int i = 0; i < n; i++) {
    while (invidx[mem[lp].age] < invidx[mem[i].age] - k) {
      sum.modify(-1, mem[lp].res, mem[lp].res + 1);
      lp++;
    }
    while (hp < n && invidx[mem[hp].age] <= invidx[mem[i].age] + k) {
      sum.modify(1, mem[hp].res, mem[hp].res + 1);
      hp++;
    }
    lead[i] = sum.sigma(0, mem[i].res + 1);
  }
  memset(invage, -1, sizeof(invage));
  for (int i = 0; i < n; i++) {
    if (invage[mem[i].age] == -1 || mem[i].res > mem[invage[mem[i].age]].res) {
      invage[mem[i].age] = i;
    }
  }
  root.insert();
  for (int i = 0; i < q; i++) {
    long long a, b;
    scanf("%lld%lld", &a, &b);
    a--;
    b--;
    long long ai = a, bi = b;
    a = age[a];
    b = age[b];
    long long low = max(a, b) - k, high = min(a, b) + k + 1;
    if (low >= high) {
      printf("-1\n");
    } else {
      low = idx.lower_bound(low)->second;
      high = (--idx.lower_bound(high))->second + 1;
      long long rlim = max(res[ai], res[bi]);
      rlim = idx.lower_bound(rlim)->second;
      long long ret = root.find(rlim, low, high);
      if (ret != -1) {
        printf("%lld\n", lead[ret]);
      } else {
        printf("-1\n");
      }
    }
  }
  return 0;
}
