#include <bits/stdc++.h>
int n, k;
std::vector<int> R, A;
struct SumTree {
  struct Node {
    Node *left, *right;
    int val;
    Node() {
      left = right = nullptr;
      val = 0;
    }
  };
  Node *root = new Node();
  int query(int a, int b, Node *node = nullptr, int lo = 0,
            int hi = (1 << 30) - 1) {
    if (node == nullptr) node = root;
    if (a > b) return 0;
    if (a == lo && b == hi) return node->val;
    int mid = (lo + hi) / 2;
    int ret = 0;
    if (node->left != nullptr)
      ret += query(a, std::min(b, mid), node->left, lo, mid);
    if (node->right != nullptr)
      ret += query(std::max(a, mid + 1), b, node->right, mid + 1, hi);
    return ret;
  }
  void add(int a, int val) {
    int lev = 30;
    Node *node = root;
    root->val += val;
    while (lev > 0) {
      lev--;
      Node *nw;
      if (a & (1 << lev))
        nw = node->right;
      else
        nw = node->left;
      if (nw == nullptr) {
        nw = new Node();
        if (a & (1 << lev))
          node->right = nw;
        else
          node->left = nw;
      }
      node = nw;
      node->val += val;
    }
  }
};
struct MaxTree {
  struct Node {
    Node *left, *right;
    int val;
    Node() {
      left = right = nullptr;
      val = 0;
    }
  };
  Node *root = new Node();
  int query(int a, int b, Node *node = nullptr, int lo = 0,
            int hi = (1 << 30) - 1) {
    if (node == nullptr) node = root;
    if (a > b) return 0;
    if (a == lo && b == hi) return node->val;
    int mid = (lo + hi) / 2;
    int ret = 0;
    if (node->left != nullptr)
      ret = std::max(ret, query(a, std::min(b, mid), node->left, lo, mid));
    if (node->right != nullptr)
      ret = std::max(ret,
                     query(std::max(a, mid + 1), b, node->right, mid + 1, hi));
    return ret;
  }
  void add(int a, int val) {
    int lev = 30;
    Node *node = root;
    root->val = std::max(root->val, val);
    while (lev > 0) {
      lev--;
      Node *nw;
      if (a & (1 << lev))
        nw = node->right;
      else
        nw = node->left;
      if (nw == nullptr) {
        nw = new Node();
        if (a & (1 << lev))
          node->right = nw;
        else
          node->left = nw;
      }
      node = nw;
      node->val = std::max(node->val, val);
    }
  }
};
int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(0);
  std::cin >> n >> k;
  R.resize(n);
  A.resize(n);
  for (int i = 0; i < (n); ++i) std::cin >> R[i];
  for (int i = 0; i < (n); ++i) std::cin >> A[i];
  SumTree tree;
  for (int i = 0; i < (n); ++i) tree.add(A[i], 1);
  std::vector<std::pair<int, int> > guys;
  for (int i = 0; i < (n); ++i) guys.push_back(std::make_pair(R[i], i));
  std::sort(guys.begin(), guys.end(), std::greater<std::pair<int, int> >());
  std::vector<int> de;
  de.push_back(guys[0].second);
  std::vector<int> score(n);
  score[guys[0].second] =
      tree.query(std::max(0, A[guys[0].second] - k),
                 std::min(1000000000, A[guys[0].second] + k));
  for (int i = (1); i < (n); ++i) {
    if (R[de.back()] != guys[i].first)
      while ((int)(de).size()) tree.add(A[de.back()], -1), de.pop_back();
    de.push_back(guys[i].second);
    score[guys[i].second] =
        tree.query(std::max(0, A[guys[i].second] - k),
                   std::min(1000000000, A[guys[i].second] + k));
  }
  int qi;
  std::cin >> qi;
  std::vector<std::pair<std::pair<int, int>, int> > que;
  for (int i = 0; i < (qi); ++i) {
    int a, b;
    std::cin >> a >> b;
    a--;
    b--;
    if (A[a] > A[b]) std::swap(a, b);
    que.push_back(std::make_pair(std::make_pair(a, b), i));
  }
  std::sort(que.begin(), que.end(),
            [](const std::pair<std::pair<int, int>, int> &a,
               const std::pair<std::pair<int, int>, int> &b) {
              return std::max(R[a.first.first], R[a.first.second]) <
                     std::max(R[b.first.first], R[b.first.second]);
            });
  std::reverse(que.begin(), que.end());
  MaxTree mt = MaxTree();
  auto it = guys.begin();
  std::vector<int> ans(qi);
  for (auto &q : que) {
    while (it != guys.end() &&
           it->first >= std::max(R[q.first.first], R[q.first.second]))
      mt.add(A[it->second], score[it->second]), it++;
    if (A[q.first.second] - k > A[q.first.first] + k)
      ans[q.second] = -1;
    else {
      ans[q.second] = mt.query(std::max(0, A[q.first.second] - k),
                               std::min(1000000000, A[q.first.first] + k));
      if (ans[q.second] == 0) ans[q.second] = -1;
    }
  }
  for (auto &i : ans) std::cout << i << "\n";
  return 0;
}
