#include <bits/stdc++.h>
using namespace std;
inline void pisz(int n) { printf("%d\n", n); }
template <typename T, typename TT>
ostream &operator<<(ostream &s, pair<T, TT> t) {
  return s << "(" << t.first << "," << t.second << ")";
}
template <typename T>
ostream &operator<<(ostream &s, vector<T> t) {
  for (int(i) = 0; (i) < (((int)(t.size()))); ++(i)) s << t[i] << " ";
  return s;
}
int *macroCompArray;
inline bool macrocomp(int i, int j) {
  return macroCompArray[i] < macroCompArray[j];
}
int OVERWRITE = 0;
template <typename T, typename Aug>
struct AugTreap {
  struct Node {
    T v;
    int pr;
    Node *le, *ri;
    Aug al, ag;
  };
  Node *root;
  AugTreap() : root(0) {}
  inline static void recalc(Node *&n) {
    Aug left = n->le ? n->le->ag + n->al : n->al;
    n->ag = n->ri ? left + n->ri->ag : left;
  }
  inline void rotle(Node *&n) {
    Node *syn = n->le;
    n->le = syn->ri;
    syn->ri = n;
    n = syn;
    recalc(n->ri);
    recalc(n);
  }
  inline void rotri(Node *&n) {
    Node *syn = n->ri;
    n->ri = syn->le;
    syn->le = n;
    n = syn;
    recalc(n->le);
    recalc(n);
  }
  void insert_aux(Node *&n, const T &x, const Aug &aug) {
    if (!n) {
      n = new Node;
      n->v = x;
      n->pr = rand();
      n->le = n->ri = 0;
      n->al = n->ag = aug;
    } else if (x < n->v) {
      insert_aux(n->le, x, aug);
      if (n->pr > n->le->pr)
        rotle(n);
      else
        recalc(n);
    } else if (x > n->v) {
      insert_aux(n->ri, x, aug);
      if (n->pr > n->ri->pr)
        rotri(n);
      else
        recalc(n);
    } else {
      n->al = OVERWRITE ? aug : n->al + aug;
      recalc(n);
    }
  }
  void insert(const T &x, const Aug &aug) { insert_aux(root, x, aug); }
  Aug sumgeq_aux(const T &x, Node *n) {
    if (!n) return Aug();
    if (x > n->v) return sumgeq_aux(x, n->ri);
    if (x < n->v) {
      Aug left = sumgeq_aux(x, n->le) + n->al;
      return n->ri ? left + n->ri->ag : left;
    }
    return n->ri ? n->al + n->ri->ag : n->al;
  }
  Aug sumgeq(const T &x) { return sumgeq_aux(x, root); }
  Aug sumleq_aux(const T &x, Node *n) {
    if (!n) return Aug();
    if (x < n->v) return sumleq_aux(x, n->le);
    if (x > n->v) {
      Aug right = n->al + sumleq_aux(x, n->ri);
      return n->le ? n->le->ag + right : right;
    }
    return n->le ? n->le->ag + n->al : n->al;
  }
  Aug sumleq(const T &x) { return sumleq_aux(x, root); }
  Aug sumbetween_aux(const T &x, const T &y, Node *n) {
    if (!n) return Aug();
    if (y < n->v) return sumbetween_aux(x, y, n->le);
    if (x > n->v) return sumbetween_aux(x, y, n->ri);
    return sumgeq_aux(x, n->le) + n->al + sumleq_aux(y, n->ri);
  }
  Aug sumbetween(const T &x, const T &y) { return sumbetween_aux(x, y, root); }
};
struct IntMax {
  int x;
  IntMax(int y = -2010000000) : x(y) {}
  inline IntMax operator+(const IntMax &s) const { return IntMax(max(x, s.x)); }
};
int r[100007], a[100007], sz[100007], aU[100007], aL[100007], rL[100007],
    res[100007];
int main() {
  int(n), (k);
  scanf("%d %d", &(n), &(k));
  for (int(i) = 1; (i) <= (n); ++(i)) scanf("%d", r + i);
  for (int(i) = 1; (i) <= (n); ++(i)) scanf("%d", a + i);
  vector<int> temp;
  for (int(i) = 1; (i) <= (n); ++(i)) temp.push_back(i);
  {
    macroCompArray = r;
    sort(temp.begin(), temp.end(), macrocomp);
  };
  AugTreap<int, int> tr;
  for (int i = 0; i < n;) {
    int start = i;
    while (i < n && r[temp[i]] == r[temp[start]]) {
      tr.insert(a[temp[i]], 1);
      i++;
    }
    for (int s = start; s < i; ++s) {
      sz[temp[s]] = tr.sumbetween(a[temp[s]] - k, a[temp[s]] + k);
    }
  }
  int(queries);
  scanf("%d", &(queries));
  for (int(q) = 0; (q) < (queries); ++(q)) {
    int(x), (y);
    scanf("%d %d", &(x), &(y));
    aU[q] = min(a[x], a[y]) + k;
    aL[q] = max(a[x], a[y]) - k;
    rL[q] = max(r[x], r[y]);
  }
  vector<int> temp2;
  for (int(q) = 0; (q) < (queries); ++(q)) temp2.push_back(q);
  {
    macroCompArray = rL;
    sort(temp2.begin(), temp2.end(), macrocomp);
  };
  reverse(temp2.begin(), temp2.end());
  reverse(temp.begin(), temp.end());
  int nowToAdd = 0;
  AugTreap<int, IntMax> tr2;
  for (int(kkk) = 0; (kkk) < (queries); ++(kkk)) {
    int q = temp2[kkk];
    while (nowToAdd < n && r[temp[nowToAdd]] >= rL[q]) {
      tr2.insert(a[temp[nowToAdd]], sz[temp[nowToAdd]]);
      ++nowToAdd;
    }
    res[q] = tr2.sumbetween(aL[q], aU[q]).x;
  }
  for (int(q) = 0; (q) < (queries); ++(q))
    pisz(res[q] == -2010000000 ? -1 : res[q]);
}
