#include <bits/stdc++.h>   using namespace std;   long long int mod = 998244353;   long double eps = 1e-12;   long long int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};   long long int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};   long long int n, res = 0, k = 0, h, ans = 0, m;   long long int gcd(long long int a, long long int b) {     if (b == 0) return a;     return gcd(b, a % b);   }   long long int lcm(long long int x, long long int y) {     return (x * y) / gcd(x, y);   }   long long int pot(long long int a, long long int b) {     long long int r = 1;     while (b) {       if (b % 2) r = (r * a) % mod;       a = (a * a) % mod;       b /= 2;     }     return r % mod;   }   void pr(long long int a[], long long int n) {     for (long long int i = 1; i <= n; i++) cout << a[i] <<   n ;     cout <<   n ;   }   void read(long long int a[], long long int n) {     for (long long int i = 1; i <= n; i++) cin >> a[i];   }   void yes() { cout <<  Yes n ; }   void no() { cout <<  No n ; }   vector<long long int> v[300015];   void add_edge(long long int x, long long int y) {     v[x].push_back(y);     v[y].push_back(x);   }   string s2, ch =   , s, s1 =   , t;   vector<pair<string, long long int> > vs;   vector<pair<pair<long long int, long long int>, long long int> > vvv, vvv2;   vector<pair<long long int, long long int> > vv, vv2;   long long int a[300015], b[300015] = {0};   long long int bit[300015];   void update(long long int x, long long int val) {     for (; x <= n; x += x & -x) bit[x] += val;   }   long long int query(long long int x) {     long long int sum = 0;     for (; x > 0; x -= x & -x) {       sum += bit[x];     }     return sum;   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);     long long int i = 0, x = 0, z = 0, y = 0, j = 0, q, mx = 0, mm, idx = 0,                   ok = 0, l = 0, r, negatif = 0, positif = 0;     set<pair<long long int, long long int> >::iterator itw, ith;     multiset<long long int>::iterator it;     multiset<long long int> mst, mst2;     cin >> n;     read(a, n);     for (i = 1; i <= (long long int)(n); i++) {       vv.push_back({a[i], -i});     }     sort(vv.rbegin(), vv.rend());     for (i = 0; i < vv.size(); i++) vv[i].second = abs(vv[i].second);     cin >> q;     for (i = 1; i <= (long long int)(q); i++) {       long long int k, pos;       cin >> k >> pos;       vvv.push_back({{k, pos}, i});     }     sort(vvv.begin(), vvv.end());     j = 0;     for (i = 0; i < vvv.size(); i++) {       long long int k = vvv[i].first.first;       long long int pos = vvv[i].first.second;       long long int qidx = vvv[i].second;       while (j < k) {         long long int val = vv[j].first;         long long int idx = vv[j].second;         update(idx, 1);         j++;       }       l = 1;       r = n;       res = 0;       while (l <= r) {         long long int mid = (l + r) / 2;         long long int sum = query(mid);         if (sum < pos) {           res = mid;           l = mid + 1;         } else {           r = mid - 1;         }       }       res++;       b[qidx] = a[res];     }     pr(b, q);     return 0;   }   