#include <bits/stdc++.h>
//Complete code at ideone.com/Tkters
//This code has comments and is for understanding the blog
using namespace std;
#define gc getchar_unlocked
#define fo(i,n) for(i=0;i<n;i++)
#define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define ll long long
#define si(x) scanf("%d",&x)
#define sl(x) scanf("%lld",&x)
#define ss(s) scanf("%s",s)
#define pi(x) printf("%d\n",x)
#define pl(x) printf("%lld\n",x)
#define ps(s) printf("%s\n",s)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pl> vpl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
int mpow(int base, int exp);
void ipgraph(int m);
void dfs(int u, int par);
const int mod = 1000000007;
const int N = 3e5, M = N;
//=======================
vi g[N];
int a[N];
const int MAX = 1e6;
struct wavelet_tree{
#define vi vector<int>
#define pb push_back
int lo, hi;
wavelet_tree *l, *r;
vi b;
//nos are in range [x,y]
//array indices are [from, to)
wavelet_tree(int *from, int *to, int x, int y){
lo = x, hi = y;
if(lo == hi or from >= to) return;
int mid = (lo+hi)/2;
auto f = [mid](int x){
return x <= mid;
};
b.reserve(to-from+1);
b.pb(0);
//b[i] = no of elements from first "i" elements that go to left node
for(auto it = from; it != to; it++)
b.pb(b.back() + f(*it));
//see how lambda function is used here
auto pivot = stable_partition(from, to, f);
l = new wavelet_tree(from, pivot, lo, mid);
r = new wavelet_tree(pivot, to, mid+1, hi);
}
//kth smallest element in [l, r]
int kth(int l, int r, int k){
if(l > r) return 0;
if(lo == hi) return lo;
//how many nos are there in left node from [l, r]
int inleft = b[r] - b[l-1];
int lb = b[l-1]; //amt of nos from first (l-1) nos that go in left
int rb = b[r]; //amt of nos from first (r) nos that go in left
//so [lb+1, rb] represents nos from [l, r] that go to left
if(k <= inleft) return this->l->kth(lb+1, rb , k);
//(l-1-lb) is amt of nos from first (l-1) nos that go to right
//(r-rb) is amt of nos from first (r) nos that go to right
//so [l-lb, r-rb] represents nos from [l, r] that go to right
return this->r->kth(l-lb, r-rb, k-inleft);
}
~wavelet_tree(){
delete l;
delete r;
}
};
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
srand(time(NULL));
int i,n,k,j,q,l,r;
cin >> n;
fo(i, n) cin >> a[i+1];
wavelet_tree T(a+1, a+n+1, 1, MAX);
cin >> q;
while(q--){
cin >> l >> r >> k;
cout << T.kth(l, r, k) << endl;
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Ci8vQ29tcGxldGUgY29kZSBhdCBpZGVvbmUuY29tL1RrdGVycwovL1RoaXMgY29kZSBoYXMgY29tbWVudHMgYW5kIGlzIGZvciB1bmRlcnN0YW5kaW5nIHRoZSBibG9nIAp1c2luZyBuYW1lc3BhY2Ugc3RkOwojZGVmaW5lIGdjIGdldGNoYXJfdW5sb2NrZWQKI2RlZmluZSBmbyhpLG4pIGZvcihpPTA7aTxuO2krKykKI2RlZmluZSBGbyhpLGssbikgZm9yKGk9aztrPG4/aTxuOmk+bjtrPG4/aSs9MTppLT0xKQojZGVmaW5lIGxsIGxvbmcgbG9uZwojZGVmaW5lIHNpKHgpCXNjYW5mKCIlZCIsJngpCiNkZWZpbmUgc2woeCkJc2NhbmYoIiVsbGQiLCZ4KQojZGVmaW5lIHNzKHMpCXNjYW5mKCIlcyIscykKI2RlZmluZSBwaSh4KQlwcmludGYoIiVkXG4iLHgpCiNkZWZpbmUgcGwoeCkJcHJpbnRmKCIlbGxkXG4iLHgpCiNkZWZpbmUgcHMocykJcHJpbnRmKCIlc1xuIixzKQojZ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