#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:100000000000000")
using namespace std;
const long long int INF = 2e9;
int tage[3000000], tlevel[3000000], z[3000000], posage[3000000];
vector<pair<int, int> > age, level;
struct node {
  int x, y, val, q, sz;
  node *l, *r;
  node() {}
  node(int x, int vali) : x(x), l(NULL), r(NULL) {
    val = q = vali;
    sz = 1;
    srand(clock() ^ rand() * rand() * rand());
    y = rand() % (rand() + 1) % INF;
  }
};
int get_q(node *res) {
  if (!res) return 0;
  return res->q;
}
int get_sz(node *res) {
  if (!res) return 0;
  return res->sz;
}
void upd(node *res) {
  if (!res) return;
  res->q = max(res->val, max(get_q(res->l), get_q(res->r)));
  res->sz = 1 + get_sz(res->l) + get_sz(res->r);
}
void split(node *res, node *&l, node *&r, int key) {
  if (!res)
    l = r = NULL;
  else {
    if (res->x < key) {
      split(res->r, res->r, r, key);
      l = res;
    } else {
      split(res->l, l, res->l, key);
      r = res;
    }
  }
  upd(l);
  upd(r);
}
void merge(node *&res, node *l, node *r) {
  if (!l || !r)
    res = l ? l : r;
  else {
    if (l->y > r->y) {
      merge(l->r, l->r, r);
      res = l;
    } else {
      merge(r->l, l, r->l);
      res = r;
    }
  }
  upd(res);
}
void ins(node *&res, int key, int val) {
  node *l, *r, *l2, *r2;
  split(res, l, r, key);
  split(r, l2, r2, key + 1);
  merge(l2, l2, new node(key, val));
  merge(r, l2, r2);
  merge(res, l, r);
}
int query(node *&res, int lk, int rk) {
  node *l, *r, *l2, *r2;
  split(res, l, r, lk);
  split(r, l2, r2, rk + 1);
  int out = get_q(l2);
  merge(r, l2, r2);
  merge(res, l, r);
  return out;
}
int query_sz(node *&res, int lk, int rk) {
  node *l, *r, *l2, *r2;
  split(res, l, r, lk);
  split(r, l2, r2, rk + 1);
  int out = get_sz(l2);
  merge(r, l2, r2);
  merge(res, l, r);
  return out;
}
int main() {
  int n, k;
  cin >> n >> k;
  age.resize(n);
  level.resize(n);
  for (int i = 0; i < (n); i++) {
    int a;
    scanf("%d", &a);
    level[i] = make_pair(a, i);
    tlevel[i] = a;
  }
  for (int i = 0; i < (n); i++) {
    int a;
    scanf("%d", &a);
    age[i] = make_pair(a, i);
    tage[i] = a;
  }
  sort(age.begin(), age.end());
  sort(level.begin(), level.end());
  node *temp = NULL;
  for (int i = 0; i < (n); i++) {
    posage[age[i].second] = i;
    int id = level[i].second;
    int cur_age = tage[id];
    ins(temp, cur_age, 0);
    z[id] = query_sz(temp, cur_age - k, cur_age + k);
  }
  temp = NULL;
  int Q;
  cin >> Q;
  int ptr = n - 1;
  vector<pair<pair<int, int>, pair<int, int> > > qz(Q);
  vector<int> answer(Q);
  for (int qq = 0; qq < (Q); qq++) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--;
    b--;
    qz[qq] =
        make_pair(make_pair(max(tlevel[a], tlevel[b]), qq), make_pair(a, b));
  }
  sort(qz.begin(), qz.end());
  reverse(qz.begin(), qz.end());
  for (int i = 0; i < (Q); i++) {
    int a, b, bound, ind;
    bound = qz[i].first.first, ind = qz[i].first.second;
    a = qz[i].second.first, b = qz[i].second.second;
    while (ptr >= 0 && level[ptr].first >= bound) {
      int in_id = level[ptr].second;
      int x = posage[in_id];
      int val = z[in_id];
      ins(temp, x, val);
      ptr--;
    }
    int wha = posage[a], whb = posage[b];
    if (wha > whb) {
      swap(a, b);
      swap(wha, whb);
    }
    int L, R, l = 0, r = whb, print;
    while (l != r) {
      int m = (l + r) / 2;
      if (tage[b] - age[m].first > k)
        l = m + 1;
      else
        r = m;
    }
    L = l;
    l = wha, r = n - 1;
    while (l != r) {
      int m = (l + r + 1) / 2;
      if (age[m].first - tage[a] > k)
        r = m - 1;
      else
        l = m;
    }
    R = l;
    print = query(temp, L, R);
    if (print == 0) print = -1;
    answer[ind] = print;
  }
  for (int i = 0; i < (Q); i++) printf("%d\n", answer[i]);
}
