1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  4. #define endl "\n"
  5. #define int long long
  6. const int N = 111115;
  7. const int LG = log2(N) + 1;
  8. int n, q, ct = 0, num = 0;
  9. int a[N], root[N], level[N];
  10. int st[21*N], lc[21*N], rc[21*N];
  11. int tim=0;
  12. int parent[LG][N];
  13. map<int, int> comp, rcomp;
  14. vector<int> g[N];
  15. void dfs0(int k, int par, int lvl)
  16. {
  17. parent[0][k] = par;
  18. level[k] = lvl;
  19. for(auto it:g[k])
  20. {
  21. if(it==par)
  22. continue;
  23. dfs0(it, k, lvl+1);
  24. }
  25. }
  26. void precompute()
  27. {
  28. for(int i=1;i<LG;i++)
  29. for(int j=1;j<=n;j++)
  30. if(parent[i-1][j])
  31. parent[i][j]=parent[i-1][parent[i-1][j]];
  32. }
  33. int LCA(int u, int v)
  34. {
  35. if(level[u]<level[v])
  36. swap(u,v);
  37. int diff=level[u]-level[v];
  38. for(int i=LG-1;i>=0;i--)
  39. {
  40. if((1<<i) & diff)
  41. {
  42. u=parent[i][u];
  43. }
  44. }
  45. if(u==v)
  46. return u;
  47. for(int i=LG-1;i>=0;i--)
  48. {
  49. if(parent[i][u] && parent[i][u]!=parent[i][v])
  50. {
  51. u=parent[i][u];
  52. v=parent[i][v];
  53. }
  54. }
  55. return parent[0][u];
  56. }
  57. int build(int L, int R)
  58. {
  59. int node = ++ct;
  60. if(L==R)
  61. return node;
  62. int M = (L+R)/2;
  63. lc[node] = build(L, M);
  64. rc[node] = build(M+1, R);
  65. return node;
  66. }
  67. int update(int onode, int L, int R, int pos)
  68. {
  69. int node = ++ct;
  70. if(L==R)
  71. {
  72. st[node] = st[onode] + 1;
  73. return node;
  74. }
  75. int M = (L+R)/2;
  76. lc[node] = lc[onode];
  77. rc[node] = rc[onode];
  78. if(pos <= M)
  79. lc[node] = update(lc[onode], L, M, pos);
  80. else
  81. rc[node] = update(rc[onode], M+1, R, pos);
  82. st[node] = st[lc[node]] + st[rc[node]];
  83. return node;
  84. }
  85. int query(int nodeu, int nodev, int nodelca, int nodelcapar, int L, int R, int pos)
  86. {
  87. if(L==R)
  88. return L;
  89. int M = (L+R)/2;
  90. int leftval = st[lc[nodeu]] + st[lc[nodev]] - st[lc[nodelca]] - st[lc[nodelcapar]];
  91. int rightval = st[rc[nodeu]] + st[rc[nodev]] - st[rc[nodelca]] - st[rc[nodelcapar]];
  92. if(leftval>=pos)
  93. return query(lc[nodeu], lc[nodev], lc[nodelca], lc[nodelcapar], L, M, pos);
  94. else
  95. return query(rc[nodeu], rc[nodev], rc[nodelca], rc[nodelcapar], M+1, R, pos-leftval);
  96. }
  97. void dfs(int u, int par)
  98. {
  99. int onode = root[par];
  100. root[u] = update(onode, 1, num, a[u]);
  101. for(auto &it:g[u])
  102. {
  103. if(it==par)
  104. continue;
  105. dfs(it, u);
  106. }
  107. }
  108. int32_t main()
  109. {
  110. IOS;
  111. cin>>n>>q;
  112. for(int i=1;i<=n;i++)
  113. {
  114. cin>>a[i];
  115. comp[a[i]];
  116. }
  117. for(auto &it:comp)
  118. {
  119. it.second = ++num;
  120. rcomp[num] = it.first;
  121. }
  122. for(int i=1;i<=n;i++)
  123. a[i] = comp[a[i]];
  124. for(int i=1;i<=n-1;i++)
  125. {
  126. int u, v;
  127. cin>>u>>v;
  128. g[u].push_back(v);
  129. g[v].push_back(u);
  130. }
  131. root[0] = build(1, num);
  132. dfs0(1, 0, 1);
  133. precompute();
  134. dfs(1, 0);
  135. while(q--)
  136. {
  137. int u, v, k;
  138. cin>>u>>v>>k;
  139. int lca = LCA(u, v);
  140. int parlca = parent[0][lca];
  141. int value = query(root[u], root[v], root[lca], root[parlca], 1, num, k);
  142. int ans = rcomp[value];
  143. cout<<ans<<endl;
  144. }
  145. return 0;
  146. }