#include<bits/stdc++.h>
using namespace std;
#define PI pair<int,int>
#define MP make_pair
#define PB push_back
#define FI first
#define SE second
#define ll long long
#define ALL(DATAST) DATAST.begin(), DATAST.end()
#define SFD(d) scanf("%d",&d)
#define SFLD(d) scanf("%lld",&d)
#define FOR(i,a,b) for(int i=(a); i<(b); i++)
#define FORL(i,a,b) for(ll i=(a); i<(b); i++)
#define DBG cout<<"here"<<endl
#define MOD 1000000007
#define SIZE 111111
#define MEM 35000
// PERSISTENT SEGTREE
struct node
{
int val;
node *l;
node *r;
node()
{
val = 0;
l = r = NULL;
}
};
void build(int s, int e, node *nd)
{
if (s == e)
{
return;
}
int m = (s + e) / 2;
nd->l = new node();
nd->r = new node();
build(s, m, nd->l);
build(m + 1, e, nd->r);
}
vector<int>adj[SIZE];
int dep[SIZE];
int par[25][SIZE];
int n;
int wt[SIZE];
int temp[SIZE];
node *vrsn[SIZE];
int comp[SIZE];
int k;
int rm[SIZE];
void update(node *prev, node *cur, int s, int e, int i, int v)
{
if (s > i || e < i)
return;
if (s == e)
{
cur->val = prev->val + v;
return;
}
int m = (s + e) / 2;
if (i <= m)
{
cur->r = prev->r;
cur->l = new node();
update(prev->l, cur->l, s, m, i, v);
}
else
{
cur->l = prev->l;
cur->r = new node();
update(prev->r, cur->r, m + 1, e, i, v);
}
cur->val = cur->l->val + cur->r->val;
}
int query(node *u, node *v, node *lc, node *lcp, int cnt, int s, int e)
{
if (s == e)
return s;
int m = (s + e) / 2;
int cc = u->l->val + v->l->val - lc->l->val - lcp->l->val;
if (cc >= cnt)
return query(u->l, v->l, lc->l, lcp->l, cnt, s, m);
else
return query(u->r, v->r, lc->r, lcp->r, cnt - cc, m + 1, e);
}
// LCA
void dfs(int s, int p)
{
if (p == -1)
dep[s] = 0;
else
dep[s] = dep[p] + 1;
par[0][s] = p;
vrsn[s + 1] = new node();
update(vrsn[p + 1], vrsn[s + 1], 0, k - 1, comp[s], 1);
for (auto it : adj[s])
{
if (it != p)
dfs(it, s);
}
}
void pre()
{
for (int i = 1; i < 21; i++)
{
for (int j = 0; j < n; j++)
{
if (par[i - 1][j] != -1)
par[i][j] = par[i - 1][par[i - 1][j]];
}
}
}
int lca(int u, int v)
{
if (dep[v] > dep[u])
swap(u, v);
if (dep[u] > dep[v])
{
for (int i = 0; i < 22; i++)
{
if ((dep[u] >> i) & 1)
u = par[i][u];
}
}
if (u == v) return v;
for (int i = 21; i > -1; i--)
{
if (par[i][u] != par[i][v])
{
u = par[i][u];
v = par[i][v];
}
}
return par[0][u];
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int q, l, r, c, lc, lcp;
SFD(n); SFD(q);
FOR(i, 0, n)
{
SFD(wt[i]);
temp[i] = wt[i];
}
sort(temp, temp + n);
k = unique(temp, temp + n) - temp;
FOR(i, 0, n)
{
comp[i] = lower_bound(temp, temp + n, wt[i]) - temp;
rm[comp[i]] = wt[i];
}
FOR(i, 0, n - 1)
{
SFD(l); SFD(r);
l--; r--;
adj[l].PB(r);
adj[r].PB(l);
}
memset(par, -1, sizeof(par));
vrsn[0] = new node();
build(0, k - 1, vrsn[0]);
dfs(0, -1);
pre();
while (q--)
{
SFD(l); SFD(r); SFD(c);
l--; r--;
lc = lca(l, r);
lcp = par[0][lc];
cout << rm[query(vrsn[l + 1], vrsn[r + 1], vrsn[lc + 1], vrsn[lcp + 1], c, 0, k - 1)] << endl;
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSBQSSBwYWlyPGludCxpbnQ+CiNkZWZpbmUgTVAgbWFrZV9wYWlyCiNkZWZpbmUgUEIgcHVzaF9iYWNrCiNkZWZpbmUgRkkgZmlyc3QKI2RlZmluZSBTRSBzZWNvbmQKI2RlZmluZSBsbCBsb25nIGxvbmcKI2RlZmluZSBBTEwoREFUQVNUKSBE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2KQp7CiAgICBpZiAocyA+IGkgfHwgZSA8IGkpCiAgICAgICAgcmV0dXJuOwogICAgaWYgKHMgPT0gZSkKICAgIHsKICAgICAgICBjdXItPnZhbCA9IHByZXYtPnZhbCArIHY7CiAgICAgICAgcmV0dXJuOwogICAgfQogICAgaW50IG0gPSAocyArIGUpIC8gMjsKICAgIGlmIChpIDw9IG0pCiAgICB7CiAgICAgICAgY3VyLT5yID0gcHJldi0+cjsKICAgICAgICBjdXItPmwgPSBuZXcgbm9kZSgpOwogICAgICAgIHVwZGF0ZShwcmV2LT5sLCBjdXItPmwsIHMsIG0sIGksIHYpOwogICAgfQogICAgZWxzZQogICAgewogICAgICAgIGN1ci0+bCA9IHByZXYtPmw7CiAgICAgICAgY3VyLT5yID0gbmV3IG5vZGUoKTsKICAgICAgICB1cGRhdGUocHJldi0+ciwgY3VyLT5yLCBtICsgMSwgZSwgaSwgdik7CiAgICB9CiAgICBjdXItPnZhbCA9IGN1ci0+bC0+dmFsICsgY3VyLT5yLT52YWw7Cn0KCmludCBxdWVyeShub2RlICp1LCBub2RlICp2LCBub2RlICpsYywgbm9kZSAqbGNwLCBpbnQgY250LCBpbnQgcywgaW50IGUpCnsKICAgIGlmIChzID09IGUpCiAgICAgICAgcmV0dXJuIHM7CiAgICBpbnQgbSA9IChzICsgZSkgLyAyOwogICAgaW50IGNjID0gdS0+bC0+dmFsICsgdi0+bC0+dmFsIC0gbGMtPmwtPnZhbCAtIGxjcC0+bC0+dmFsOwogICAgaWYgKGNjID49IGNudCkKICAgICAgICByZXR1cm4gcXVlcnkodS0+bCwgdi0+bCwgbGMtPmwsIGxjcC0+bCwgY250LCBzLCBtKTsKICAgIGVsc2UKICAgICAgICByZXR1cm4gcXVlcnkodS0+ciwgdi0+ciwgbGMtPnIsIGxjcC0+ciwgY250IC0gY2MsIG0gKyAxLCBlKTsKfQoKLy8gTENBCgp2b2lkIGRmcyhpbnQgcywgaW50IHApCnsKCiAgICBpZiAocCA9PSAtMSkKICAgICAgICBkZXBbc10gPSAwOwogICAgZWxzZQogICAgICAgIGRlcFtzXSA9IGRlcFtwXSArIDE7CgogICAgcGFyWzBdW3NdID0gcDsKCgogICAgdnJzbltzICsgMV0gPSBuZXcgbm9kZSgpOwogICAgdXBkYXRlKHZyc25bcCArIDFdLCB2cnNuW3MgKyAxXSwgMCwgayAtIDEsIGNvbXBbc10sIDEpOwogICAgZm9yIChhdXRvIGl0IDogYWRqW3NdKQogICAgewogICAgICAgIGlmIChpdCAhPSBwKQogICAgICAgICAgICBkZnMoaXQsIHMpOwogICAgfQoKfQoKdm9pZCBwcmUoKQp7CiAgICBmb3IgKGludCBpID0gMTsgaSA8IDIxOyBpKyspCiAgICB7CiAgICAgICAgZm9yIChpbnQgaiA9IDA7IGogPCBuOyBqKyspCiAgICAgICAgewogICAgICAgICAgICBpZiAocGFyW2kgLSAxXVtqXSAhPSAtMSkKICAgICAgICAgICAgICAgIHBhcltpXVtqXSA9IHBhcltpIC0gMV1bcGFyW2kgLSAxXVtqXV07CiAgICAgICAgfQogICAgfQp9CgppbnQgbGNhKGludCB1LCBpbnQgdikKewoKICAgIGlmIChkZXBbdl0gPiBkZXBbdV0pCiAgICAgICAgc3dhcCh1LCB2KTsKCiAgICBpZiAoZGVwW3VdID4gZGVwW3ZdKQogICAgewogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgMjI7IGkrKykKICAgICAgICB7CiAgICAgICAgICAgIGlmICgoZGVwW3VdID4+IGkpICYgMSkKICAgICAgICAgICAgICAgIHUgPSBwYXJbaV1bdV07CiAgICAgICAgfQogICAgfQoKICAgIGlmICh1ID09IHYpICAgIHJldHVybiB2OwoKICAgIGZvciAoaW50IGkgPSAyMTsgaSA+IC0xOyBpLS0pCiAgICB7CiAgICAgICAgaWYgKHBhcltpXVt1XSAhPSBwYXJbaV1bdl0pCiAgICAgICAgewogICAgICAgICAgICB1ID0gcGFyW2ldW3VdOwogICAgICAgICAgICB2ID0gcGFyW2ldW3ZdOwogICAgICAgIH0KICAgIH0KCiAgICByZXR1cm4gcGFyWzBdW3VdOwoKfQoKaW50IG1haW4oKQp7CiAgICBpb3M6OnN5bmNfd2l0aF9zdGRpbygwKTsKICAgIGNpbi50aWUoMCk7IGNvdXQudGllKDApOwojaWZuZGVmIE9OTElORV9KVURHRQogICAgZnJlb3BlbigiaW5wdXQudHh0IiwgInIiLCBzdGRpbik7CiAgICBmcmVvcGVuKCJvdXRwdXQudHh0IiwgInciLCBzdGRvdXQpOwojZW5kaWYKCiAgICBpbnQgcSwgbCwgciwgYywgbGMsIGxjcDsKCiAgICBTRkQobik7IFNGRChxKTsKCiAgICBGT1IoaSwgMCwgbikKICAgIHsKICAgICAgICBTRkQod3RbaV0pOwogICAgICAgIHRlbXBbaV0gPSB3dFtpXTsKICAgIH0KCiAgICBzb3J0KHRlbXAsIHRlbXAgKyBuKTsKICAgIGsgPSB1bmlxdWUodGVtcCwgdGVtcCArIG4pIC0gdGVtcDsKCiAgICBGT1IoaSwgMCwgbikKICAgIHsKICAgICAgICBjb21wW2ldID0gbG93ZXJfYm91bmQodGVtcCwgdGVtcCArIG4sIHd0W2ldKSAtIHRlbXA7CiAgICAgICAgcm1bY29tcFtpXV0gPSB3dFtpXTsKICAgIH0KCiAgICBGT1IoaSwgMCwgbiAtIDEpCiAgICB7CiAgICAgICAgU0ZEKGwpOyBTRkQocik7CiAgICAgICAgbC0tOyByLS07CiAgICAgICAgYWRqW2xdLlBCKHIpOwogICAgICAgIGFkaltyXS5QQihsKTsKICAgIH0KCiAgICBtZW1zZXQocGFyLCAtMSwgc2l6ZW9mKHBhcikpOwoKICAgIHZyc25bMF0gPSBuZXcgbm9kZSgpOwogICAgYnVpbGQoMCwgayAtIDEsIHZyc25bMF0pOwoKICAgIGRmcygwLCAtMSk7CgogICAgcHJlKCk7CgogICAgd2hpbGUgKHEtLSkKICAgIHsKICAgICAgICBTRkQobCk7IFNGRChyKTsgU0ZEKGMpOwogICAgICAgIGwtLTsgci0tOwogICAgICAgIGxjID0gbGNhKGwsIHIpOwogICAgICAgIGxjcCA9IHBhclswXVtsY107CiAgICAgICAgY291dCA8PCBybVtxdWVyeSh2cnNuW2wgKyAxXSwgdnJzbltyICsgMV0sIHZyc25bbGMgKyAxXSwgdnJzbltsY3AgKyAxXSwgYywgMCwgayAtIDEpXSA8PCBlbmRsOwogICAgfQoKCgogICAgcmV0dXJuIDA7Cn0=