#include <bits/stdc++.h>   using namespace std;   vector<int> domain;   int domain_index(int x) {     int id = lower_bound(domain.begin(), domain.end(), x) - domain.begin();     assert(domain[id] == x);     return id;   }   struct Seg {     Seg *lc, *rc;     int L, M, R;     int sum, val;     Seg(int l, int r) : L(l), R(r) {       lc = rc = NULL;       M = (L + R) / 2;       sum = 0;       val = -1;       if (L < R) {         lc = new Seg(L, M);         rc = new Seg(M + 1, R);       }     }     void upd() {       sum = lc->sum + rc->sum;       val = max(lc->val, rc->val);     }     void add(int p, int v) {       if (L == R) {         sum += v;         return;       }       if (p <= M)         lc->add(p, v);       else         rc->add(p, v);       upd();     }     void update_maximum(int p, int v) {       if (L == R) {         val = max(val, v);         return;       }       if (p <= M)         lc->update_maximum(p, v);       else         rc->update_maximum(p, v);       upd();     }     int range_sum(int l, int r) {       if (l <= L && R <= r) return sum;       int ans = 0;       if (l <= M) ans += lc->range_sum(l, r);       if (r > M) ans += rc->range_sum(l, r);       return ans;     }     int range_max(int l, int r) {       if (l > r) return -1;       if (l <= L && R <= r) return val;       int ans = -1;       if (l <= M) ans = max(ans, lc->range_max(l, r));       if (r > M) ans = max(ans, rc->range_max(l, r));       return ans;     }   };   struct Leader {     bool is_query;     int lo, hi, id, ans;     int a, r, v;     bool operator<(const Leader &t) const {       if (r != t.r) return r < t.r;       return is_query > t.is_query;     }   };   bool comp(const Leader &a, const Leader &b) {     if (a.is_query != b.is_query) return a.is_query > b.is_query;     return a.id < b.id;   }   int main() {     int n, k;     cin >> n >> k;     vector<pair<int, int> > input(n);     vector<Leader> leaders(n);     for (int i = 0; i < n; ++i) scanf( %d , &leaders[i].r);     for (int i = 0; i < n; ++i) scanf( %d , &leaders[i].a);     for (int i = 0; i < n; ++i) {       domain.push_back(leaders[i].a);       domain.push_back(leaders[i].a - k);       domain.push_back(leaders[i].a + k);       input[i].first = leaders[i].r;       input[i].second = leaders[i].a;     }     sort(domain.begin(), domain.end());     domain.erase(unique(domain.begin(), domain.end()), domain.end());     sort(leaders.begin(), leaders.end());     Seg evaluate_leaders(0, domain.size() - 1);     int l = 0;     for (Leader &leader : leaders) {       while (l < n && leaders[l].r <= leader.r) {         evaluate_leaders.add(domain_index(leaders[l].a), 1);         ++l;       }       leader.v = evaluate_leaders.range_sum(domain_index(leader.a - k),                                             domain_index(leader.a + k));       leader.a = domain_index(leader.a);       leader.is_query = false;     }     int q;     cin >> q;     vector<Leader> queries(q);     for (int i = 0; i < q; ++i) {       int a, b;       scanf( %d %d , &a, &b);       --a, --b;       int min_age = domain_index(max(input[a].second, input[b].second) - k);       int max_age = domain_index(min(input[a].second, input[b].second) + k);       int min_resp = max(input[a].first, input[b].first);       queries[i].lo = min_age;       queries[i].hi = max_age;       queries[i].r = min_resp;       queries[i].id = i;       queries[i].is_query = true;       leaders.push_back(queries[i]);     }     sort(leaders.rbegin(), leaders.rend());     Seg find_leader(0, domain.size() - 1);     for (auto &leader : leaders) {       if (leader.is_query)         leader.ans = find_leader.range_max(leader.lo, leader.hi);       else         find_leader.update_maximum(leader.a, leader.v);     }     sort(leaders.begin(), leaders.end(), comp);     for (int i = 0; i < q; ++i) printf( %d n , leaders[i].ans);   }