#include <bits/stdc++.h>   using namespace std;   const int maxN = 100 * 1000 + 5;   struct Node {     int s, e, maxx;   };   struct Segment {     Node node[4 * maxN];     void build(int s, int e, int v = 1) {       node[v].s = s;       node[v].e = e;       node[v].maxx = -1;       if (e - s <= 1) return;       int mid = (e + s) / 2;       build(s, mid, v * 2);       build(mid, e, v * 2 + 1);     }     void change(int s, int e, int x, int v = 1) {       if (e <= node[v].s || node[v].e <= s) return;       if (s <= node[v].s && node[v].e <= e) {         node[v].maxx = max(x, node[v].maxx);         return;       }       int l = v * 2, r = v * 2 + 1;       change(s, e, x, l);       change(s, e, x, r);       node[v].maxx = max(node[l].maxx, node[r].maxx);     }     int get(int s, int e, int v = 1) {       if (e <= s || e <= node[v].s || node[v].e <= s) return -1;       if (s <= node[v].s && node[v].e <= e) return node[v].maxx;       int l = v * 2, r = v * 2 + 1;       return max(get(s, e, l), get(s, e, r));     }   };   struct BIT {     vector<int> val;     void init(int sz) { val.resize(sz); }     void add(int ind, int x) {       ind++;       for (int i = ind; i < ((int)(val).size()); i += i & (-i)) val[i] += x;     }     int get(int ind) {       ind++;       int res = 0;       for (int i = ind; i > 0; i -= i & (-i)) res += val[i];       return res;     }     int getsum(int l, int r) {       if (r < l) return 0;       return get(r) - get(l - 1);     }   };   pair<int, int> p[maxN], que[maxN];   pair<int, int> r[maxN];   vector<int> a;   vector<int> lst[maxN];   BIT baz;   int n, k;   int cnt[maxN], ans[maxN], pnt[maxN];   int low(int x) { return lower_bound(a.begin(), a.end(), x) - a.begin(); }   int up(int x) { return upper_bound(a.begin(), a.end(), x) - a.begin(); }   Segment rmq;   int main() {     ios_base::sync_with_stdio(false);     cin >> n >> k;     baz.init(n + 1);     rmq.build(0, n + 1);     a.resize(n);     for (int i = 0; i < n; i++)       cin >> r[i].first, r[i].second = i, p[i].second = r[i].first;     for (int i = 0; i < n; i++) cin >> a[i], p[i].first = a[i];     sort(a.begin(), a.end());     a.resize(unique(a.begin(), a.end()) - a.begin());     sort(r, r + n);     for (int i = 0; i < n; i++) pnt[r[i].second] = i;     for (int i = 0; i < n;) {       int ind = i;       while (ind < n && (ind == i || r[ind].first == r[ind - 1].first)) {         int x = p[r[ind].second].first;         baz.add(low(x), 1);         ind++;       }       ind = i;       while (ind < n && (ind == i || r[ind].first == r[ind - 1].first)) {         int x = p[r[ind].second].first;         cnt[ind] = baz.getsum(low(x - k), up(x + k) - 1);         ind++;       }       i = ind;     }     int Q;     cin >> Q;     for (int q = 0; q < Q; q++) {       int x, y;       cin >> x >> y;       x--;       y--;       if (p[x].second > p[y].second) swap(x, y);       que[q] = make_pair(x, y);       lst[pnt[y]].push_back(q);     }     for (int i = n - 1; i >= 0;) {       int ind = i;       while (ind >= 0 && (ind == i || r[ind].first == r[ind + 1].first)) {         int x = low(p[r[ind].second].first);         rmq.change(x, x + 1, cnt[ind]);         ind--;       }       ind = i;       while (ind >= 0 && (ind == i || r[ind].first == r[ind + 1].first)) {         for (int j = 0; j < ((int)(lst[ind]).size()); j++) {           int u = lst[ind][j];           int x = p[que[u].first].first, y = p[que[u].second].first;           if (x > y) swap(x, y);           int L = low(y - k), R = up(x + k);           ans[u] = rmq.get(L, R);         }         ind--;       }       i = ind;     }     for (int i = 0; i < Q; i++) cout << ans[i] << endl;     return 0;   }   