#include <bits/stdc++.h>   using namespace std;   const int N = 4e5;   int T, n, m, k, f[N + 5], ans[N + 5];   struct People {     int a, r, d, u, id;   } p[N + 5];   struct Info {     int opt, l, r;     Info() {}     Info(int opt, int l, int r) : opt(opt), l(l), r(r) {}   };   template <int MAX>   struct BIT {     static const int N = MAX;     int b[N + 5];     void clear() { fill(b + 1, b + N + 1, 0); }     void modify(int x, int v) {       for (; x <= N; x += x & -x) b[x] += v;     }     int query(int x) {       int ans = 0;       for (; x > 0; x ^= x & -x) ans += b[x];       return ans;     }     int query(int l, int r) { return query(r) - query(l - 1); }   };   template <int MAX>   struct SegmentTree {     static const int N = MAX << 2;     int val[N + 5];     void clear() { fill(val + 1, val + N + 1, -1); }     void modify(int p, int l, int r, int x, int v) {       if (l == r) {         val[p] = max(val[p], v);         return;       }       int mid = (l + r) >> 1;       if (x <= mid) {         modify((p << 1), l, mid, x, v);       } else {         modify((p << 1 | 1), mid + 1, r, x, v);       }       val[p] = max(val[(p << 1)], val[(p << 1 | 1)]);     }     int query(int p, int l, int r, int x, int y) {       if (x == l && r == y) return val[p];       int mid = (l + r) >> 1;       if (y <= mid) {         return query((p << 1), l, mid, x, y);       } else if (x > mid) {         return query((p << 1 | 1), mid + 1, r, x, y);       } else {         return max(query((p << 1), l, mid, x, mid),                    query((p << 1 | 1), mid + 1, r, mid + 1, y));       }     }   };   BIT<N> bit;   SegmentTree<N> seg;   vector<Info> que[N + 5];   void disc() {     vector<int> u, v;     for (int i = 1; i <= n; i++) {       u.push_back(p[i].r);       v.push_back(p[i].a);       v.push_back(p[i].d);       v.push_back(p[i].u);     }     sort(u.begin(), u.end());     sort(v.begin(), v.end());     u.erase(unique(u.begin(), u.end()), u.end());     v.erase(unique(v.begin(), v.end()), v.end());     for (int i = 1; i <= n; i++) {       p[i].r = lower_bound(u.begin(), u.end(), p[i].r) - u.begin() + 1;       p[i].a = lower_bound(v.begin(), v.end(), p[i].a) - v.begin() + 1;       p[i].d = lower_bound(v.begin(), v.end(), p[i].d) - v.begin() + 1;       p[i].u = lower_bound(v.begin(), v.end(), p[i].u) - v.begin() + 1;     }   }   void calc() {     sort(p + 1, p + n + 1,          [](const People &x, const People &y) { return x.r < y.r; });     bit.clear();     for (int i = 1; i <= n;) {       int j = i - 1;       for (; j < n && p[j + 1].r == p[i].r; j++, bit.modify(p[j].a, 1))         ;       for (int k = i; k <= j; k++) {         f[p[k].id] = bit.query(p[k].d, p[k].u);       }       i = j + 1;     }     sort(p + 1, p + n + 1,          [](const People &x, const People &y) { return x.id < y.id; });   }   void solve() {     scanf( %d%d , &n, &k);     for (int i = 1; i <= n; i++) scanf( %d , &p[i].r);     for (int i = 1; i <= n; i++) {       scanf( %d , &p[i].a);       p[i].d = p[i].a - k;       p[i].u = p[i].a + k;       p[i].id = i;     }     disc();     calc();     for (int i = 1; i <= n; i++) {       que[p[i].r].push_back(Info(0, p[i].a, f[i]));     }     scanf( %d , &m);     for (int u, v, i = 1; i <= m; i++) {       scanf( %d%d , &u, &v);       int l = max(p[u].d, p[v].d);       int r = min(p[u].u, p[v].u);       if (l > r) {         ans[i] = -1;       } else {         que[max(p[u].r, p[v].r)].push_back(Info(i, l, r));       }     }     seg.clear();     for (int i = n; i >= 1; i--) {       for (const auto &t : que[i]) {         if (t.opt == 0) {           seg.modify(1, 1, 3 * n, t.l, t.r);         } else {           ans[t.opt] = seg.query(1, 1, 3 * n, t.l, t.r);         }       }       que[i].clear();     }     for (int i = 1; i <= m; i++) printf( %d n , ans[i]);   }   int main() {     solve();     return 0;   }