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