#include <bits/stdc++.h>
#define fi first
#define se second
#define all(v) v.begin() , v.end()
#define sz(v) int(v.size())
#define unq(v) sort(all(v)); v.resize(unique(all(v)) - v.begin());
using namespace std;
typedef long long ll;
typedef pair<int , int> ii;
typedef pair<long long , int> lli;
const int maxN = int(2e5)+7;
const int LOG = 20;
int n , q , a[maxN] , lg2[maxN] , val[maxN][LOG + 1];
vector<int> g[maxN];
int h[maxN] , p[maxN] , sz[maxN];
void dfs(int u){
sz[u] = 1;
for (int v : g[u]){
if (v != p[u]){
p[v] = u;
h[v] = h[u] + 1;
dfs(v);
sz[u] += sz[v];
}
}
}
int head[maxN] , chain[maxN] , pos[maxN] , numChain = 1 , numPos = 0;
int lef[maxN] , rig[maxN] , pre[maxN] , nxt[maxN];
void hld(int u){
if (head[numChain] == 0) head[numChain] = u;
chain[u] = numChain;
pos[u] = ++numPos;
int heavy = -1;
for (int v : g[u]){
if (v != p[u] && (heavy == -1 || sz[heavy] < sz[v])){
heavy = v;
}
}
if (heavy != -1) hld(heavy);
for (int v : g[u]){
if (v != p[u] && v != heavy){
numChain++;
hld(v);
}
}
}
int lca(int u , int v){
while (chain[u] != chain[v]){
if (h[head[chain[u]]] > h[head[chain[v]]]) swap(u , v);
v = p[head[chain[v]]];
}
if (h[u] > h[v]) swap(u , v);
return u;
}
int getMin(int l , int r){
int k = lg2[r - l + 1];
return min(val[l][k] , val[r - (1<<k) + 1][k]);
}
vector<ii> range;
void get_range(int u , int root , int t){
range.clear();
while (chain[u] != chain[root]){
range.push_back({pos[head[chain[u]]] , pos[u]});
u = p[head[chain[u]]];
}
range.push_back({pos[root] + t , pos[u]});
}
void solve(){
cin >> n >> q;
for (int i = 1 ; i < n ; i++){
int u , v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 1 ; i <= n ; i++) cin >> a[i];
dfs(1);
hld(1);
for (int i = 1 ; i <= numChain ; i++){
lef[i] = INT_MAX;
rig[i] = INT_MIN;
}
for (int i = 1 ; i <= n ; i++){
lef[chain[i]] = min(lef[chain[i]] , pos[i]);
rig[chain[i]] = max(rig[chain[i]] , pos[i]);
}
for (int i = 1 ; i <= n ; i++){
val[pos[i]][0] = a[i];
lg2[i] = __lg(i);
}
for (int j = 1 ; j <= LOG ; j++){
for (int i = 1 ; i + (1<<j) - 1 <= n ; i++){
val[i][j] = min(val[i][j - 1] , val[i + (1<<(j - 1))][j - 1]);
}
}
while (q--){
int u , v , x;
cin >> u >> v >> x;
int root = lca(u , v);
get_range(u , root , 1);
for (auto it : range){
int curL = it.fi;
int curR = it.se;
while (curL <= curR){
int L = curL , R = curR , nxtR = -1;
while (L <= R){
int M = (L + R) / 2;
if (getMin(M , curR) <= x){
nxtR = M;
L = M + 1;
}
else{
R = M - 1;
}
}
if (nxtR == -1) break;
x %= val[nxtR][0];
curR = nxtR - 1;
}
}
//--------------------------------------------------
get_range(v , root , 0);
reverse(all(range));
for (auto it : range){
int curL = it.fi;
int curR = it.se;
while (curL <= curR){
int L = curL , R = curR , nxtL = -1;
while (L <= R){
int M = (L + R) / 2;
if (getMin(curL , M) <= x){
nxtL = M;
R = M - 1;
}
else{
L = M + 1;
}
}
if (nxtL == -1) break;
x %= val[nxtL][0];
curL = nxtL + 1;
}
}
cout << x << "\n";
}
}
#define name "J"
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen(name".INP" , "r")){
freopen(name".INP" , "r" , stdin);
freopen(name".OUT" , "w" , stdout);
}
int t = 1; //cin >> t;
while (t--) solve();
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgZmkgZmlyc3QKI2RlZmluZSBzZSBzZWNvbmQKI2RlZmluZSBhbGwodikgdi5iZWdpbigpICwgdi5lbmQoKQojZGVmaW5lIHN6KHYpIGludCh2LnNpemUoKSkKI2RlZmluZSB1bnEodikgc29ydChhbGwodikpOyB2LnJlc2l6ZSh1bmlxdWUoYWxsKHYpKSAtIHYuYmVnaW4oKSk7CnVz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ICAgIH0KICAgIH0KICAgIGlmIChoZWF2eSAhPSAtMSkgaGxkKGhlYXZ5KTsKICAgIGZvciAoaW50IHYgOiBnW3VdKXsKICAgICAgICBpZiAodiAhPSBwW3VdICYmIHYgIT0gaGVhdnkpewogICAgICAgICAgICBudW1DaGFpbisrOwogICAgICAgICAgICBobGQodik7CiAgICAgICAgfQogICAgfQp9CgppbnQgbGNhKGludCB1ICwgaW50IHYpewogICAgd2hpbGUgKGNoYWluW3VdICE9IGNoYWluW3ZdKXsKICAgICAgICBpZiAoaFtoZWFkW2NoYWluW3VdXV0gPiBoW2hlYWRbY2hhaW5bdl1dXSkgc3dhcCh1ICwgdik7CiAgICAgICAgdiA9IHBbaGVhZFtjaGFpblt2XV1dOwogICAgfQogICAgaWYgKGhbdV0gPiBoW3ZdKSBzd2FwKHUgLCB2KTsKICAgIHJldHVybiB1Owp9CgppbnQgZ2V0TWluKGludCBsICwgaW50IHIpewogICAgaW50IGsgPSBsZzJbciAtIGwgKyAxXTsKICAgIHJldHVybiBtaW4odmFsW2xdW2tdICwgdmFsW3IgLSAoMTw8aykgKyAxXVtrXSk7Cn0KCnZlY3RvcjxpaT4gcmFuZ2U7Cgp2b2lkIGdldF9yYW5nZShpbnQgdSAsIGludCByb290ICwgaW50IHQpewogICAgcmFuZ2UuY2xlYXIoKTsKICAgIHdoaWxlIChjaGFpblt1XSAhPSBjaGFpbltyb290XSl7CiAgICAgICAgcmFuZ2UucHVzaF9iYWNrKHtwb3NbaGVhZFtjaGFpblt1XV1dICwgcG9zW3VdfSk7CiAgICAgICAgdSA9IHBbaGVhZFtjaGFpblt1XV1dOwogICAgfQogICAgcmFuZ2UucHVzaF9iYWNrKHtwb3Nbcm9vdF0gKyB0ICwgcG9zW3VdfSk7Cn0KCnZvaWQgc29sdmUoKXsKICAgIGNpbiA+PiBuID4+IHE7CiAgICBmb3IgKGludCBpID0gMSA7IGkgPCBuIDsgaSsrKXsKICAgICAgICBpbnQgdSAsIHY7CiAgICAgICAgY2luID4+IHUgPj4gdjsKICAgICAgICBnW3VdLnB1c2hfYmFjayh2KTsKICAgICAgICBnW3ZdLnB1c2hfYmFjayh1KTsKICAgIH0KICAgIGZvciAoaW50IGkgPSAxIDsgaSA8PSBuIDsgaSsrKSBjaW4gPj4gYVtpXTsKICAgIGRmcygxKTsKICAgIGhsZCgxKTsKICAgIGZvciAoaW50IGkgPSAxIDsgaSA8PSBudW1DaGFpbiA7IGkrKyl7CiAgICAgICAgbGVmW2ldID0gSU5UX01BWDsKICAgICAgICByaWdbaV0gPSBJTlRfTUlOOwogICAgfQogICAgZm9yIChpbnQgaSA9IDEgOyBpIDw9IG4gOyBpKyspewogICAgICAgIGxlZltjaGFpbltpXV0gPSBtaW4obGVmW2NoYWluW2ldXSAsIHBvc1tpXSk7CiAgICAgICAgcmlnW2NoYWluW2ldXSA9IG1heChyaWdbY2hhaW5baV1dICwgcG9zW2ldKTsKICAgIH0KICAgIGZvciAoaW50IGkgPSAxIDsgaSA8PSBuIDsgaSsrKXsKICAgICAgICB2YWxbcG9zW2ldXVswXSA9IGFbaV07CiAgICAgICAgbGcyW2ldID0gX19sZyhpKTsKICAgIH0KICAgIGZvciAoaW50IGogPSAxIDsgaiA8PSBMT0cgOyBqKyspewogICAgICAgIGZvciAoaW50IGkgPSAxIDsgaSArICgxPDxqKSAtIDEgPD0gbiA7IGkrKyl7CiAgICAgICAgICAgIHZhbFtpXVtqXSA9IG1pbih2YWxbaV1baiAtIDFdICwgdmFsW2kgKyAoMTw8KGogLSAxKSldW2ogLSAxXSk7CiAgICAgICAgfQogICAgfQogICAgd2hpbGUgKHEtLSl7CiAgICAgICAgaW50IHUgLCB2ICwgeDsKICAgICAgICBjaW4gPj4gdSA+PiB2ID4+IHg7CiAgICAgICAgaW50IHJvb3QgPSBsY2EodSAsIHYpOwogICAgICAgIGdldF9yYW5nZSh1ICwgcm9vdCAsIDEpOwogICAgICAgIGZvciAoYXV0byBpdCA6IHJhbmdlKXsKICAgICAgICAgICAgaW50IGN1ckwgPSBpdC5maTsKICAgICAgICAgICAgaW50IGN1clIgPSBpdC5zZTsKICAgICAgICAgICAgd2hpbGUgKGN1ckwgPD0gY3VyUil7CiAgICAgICAgICAgICAgICBpbnQgTCA9IGN1ckwgLCBSID0gY3VyUiAsIG54dFIgPSAtMTsKICAgICAgICAgICAgICAgIHdoaWxlIChMIDw9IFIpewogICAgICAgICAgICAgICAgICAgIGludCBNID0gKEwgKyBSKSAvIDI7CiAgICAgICAgICAgICAgICAgICAgaWYgKGdldE1pbihNICwgY3VyUikgPD0geCl7CiAgICAgICAgICAgICAgICAgICAgICAgIG54dFIgPSBNOwogICAgICAgICAgICAgICAgICAgICAgICBMID0gTSArIDE7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGVsc2V7CiAgICAgICAgICAgICAgICAgICAgICAgIFIgPSBNIC0gMTsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAobnh0UiA9PSAtMSkgYnJlYWs7CiAgICAgICAgICAgICAgICB4ICU9IHZhbFtueHRSXVswXTsKICAgICAgICAgICAgICAgIGN1clIgPSBueHRSIC0gMTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICAvLy0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tCiAgICAgICAgZ2V0X3JhbmdlKHYgLCByb290ICwgMCk7CiAgICAgICAgcmV2ZXJzZShhbGwocmFuZ2UpKTsKICAgICAgICBmb3IgKGF1dG8gaXQgOiByYW5nZSl7CiAgICAgICAgICAgIGludCBjdXJMID0gaXQuZmk7CiAgICAgICAgICAgIGludCBjdXJSID0gaXQuc2U7CiAgICAgICAgICAgIHdoaWxlIChjdXJMIDw9IGN1clIpewogICAgICAgICAgICAgICAgaW50IEwgPSBjdXJMICwgUiA9IGN1clIgLCBueHRMID0gLTE7CiAgICAgICAgICAgICAgICB3aGlsZSAoTCA8PSBSKXsKICAgICAgICAgICAgICAgICAgICBpbnQgTSA9IChMICsgUikgLyAyOwogICAgICAgICAgICAgICAgICAgIGlmIChnZXRNaW4oY3VyTCAsIE0pIDw9IHgpewogICAgICAgICAgICAgICAgICAgICAgICBueHRMID0gTTsKICAgICAgICAgICAgICAgICAgICAgICAgUiA9IE0gLSAxOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBlbHNlewogICAgICAgICAgICAgICAgICAgICAgICBMID0gTSArIDE7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYgKG54dEwgPT0gLTEpIGJyZWFrOwogICAgICAgICAgICAgICAgeCAlPSB2YWxbbnh0TF1bMF07CiAgICAgICAgICAgICAgICBjdXJMID0gbnh0TCArIDE7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgY291dCA8PCB4IDw8ICJcbiI7CiAgICB9Cn0KCiNkZWZpbmUgbmFtZSAiSiIKCmludCBtYWluKCl7CiAgICBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKDApOyBjaW4udGllKDApOyBjb3V0LnRpZSgwKTsKICAgIGlmIChmb3BlbihuYW1lIi5JTlAiICwgInIiKSl7CiAgICAgICAgZnJlb3BlbihuYW1lIi5JTlAiICwgInIiICwgc3RkaW4pOwogICAgICAgIGZyZW9wZW4obmFtZSIuT1VUIiAsICJ3IiAsIHN0ZG91dCk7CiAgICB9CiAgICBpbnQgdCA9IDE7IC8vY2luID4+IHQ7CiAgICB3aGlsZSAodC0tKSBzb2x2ZSgpOwogICAgcmV0dXJuIDA7Cn0=