#include <bits/stdc++.h>
#define rep(i, l, r) for(int i = l; i <= r; i++)
#define rep2(i, l, r) for(int i = l; i >= r; i--)
#define fi first
#define se second
#define bit(i, x) (x >> i & 1)
const int N = 3e5 + 3;
using namespace std;
int n, m, a[N];
vector<int> G[N];
int parr[N];
int sz[N], big[N], depth[N];
int in[N], top[N], cnt;
struct node{
node *child[2];
int num;
node() {
child[0] = child[1] = nullptr;
num = 0;
}
};
void add(int u, node *cur) {
assert(cur != nullptr);
rep2(i, 16, 0) {
int x = bit(i, u);
if (cur -> child[x] != nullptr) {
node *old = cur -> child[x];
cur -> child[x] = new node;
cur = cur -> child[x];
cur -> child[0] = old -> child[0];
cur -> child[1] = old -> child[1];
cur -> num = old -> num + 1;
}
else {
cur -> child[x] = new node;
cur = cur -> child[x];
cur -> num++;
}
}
}
bool check(int u, node *cur) {
assert(cur != nullptr);
rep2(i, 16, 0) {
int x = bit(i, u);
if (cur -> child[x] == nullptr) return 0;
cur = cur -> child[x];
}
return 1;
}
void dfs(int u, int par, vector<node*> &root) {
root[u] = new node;
root[u] -> child[0] = root[par] -> child[0];
root[u] -> child[1] = root[par] -> child[1];
root[u] -> num = root[par] -> num;
add(a[u], root[u]);
parr[u] = par;
sz[u] = 1, big[u] = 0;
for(int v : G[u]) if (v != par) {
depth[v] = depth[u] + 1;
dfs(v, u, root);
sz[u] += sz[v];
if (sz[big[u]] < sz[v]) big[u] = v;
}
}
void rebuild(int u, int topp) {
in[u] = ++cnt;
top[u] = topp;
if (big[u]) rebuild(big[u], topp);
for(int v : G[u]) if (v != parr[u] && v != big[u]) {
rebuild(v, v);
}
}
int lca(int x, int y) {
while (top[x] != top[y]) {
if (depth[top[x]] < depth[top[y]]) swap(x, y);
x = parr[top[x]];
}
if (depth[x] > depth[y]) swap(x, y);
return x;
}
int get(node *cur, int x) {
if (cur == nullptr) return 0;
if (cur -> child[x] == nullptr) return 0;
return cur -> child[x] -> num;
}
void ask(node *x, node *y, node *p, node *pp, int val) {
int res = 0;
assert(x != nullptr);
assert(y != nullptr);
assert(p != nullptr);
assert(pp != nullptr);
rep2(i, 16, 0) {
int s[2];
s[0] = get(x, 0) + get(y, 0) - get(p, 0) - get(pp, 0);
s[1] = get(x, 1) + get(y, 1) - get(p, 1) - get(pp, 1);
int v = bit(i, val);
if (s[v ^ 1] > 0) {
res |= (1 << i);
if (x != nullptr) x = x -> child[v ^ 1];
if (y != nullptr) y = y -> child[v ^ 1];
if (p != nullptr) p = p -> child[v ^ 1];
if (pp != nullptr) pp = pp -> child[v ^ 1];
}
else {
if (x != nullptr) x = x -> child[v];
if (y != nullptr) y = y -> child[v];
if (p != nullptr) p = p -> child[v];
if (pp != nullptr) pp = pp -> child[v];
}
}
cout << res << "\n";
}
void inp() {
// cin >> n >> m;
rep(i, 1, n) cin >> a[i];
rep(i, 1, n - 1) {
int x, y; cin >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
}
void solve() {
vector<node*> root(n + 3);
root[0] = new node;
dfs(1, 0, root);
rebuild(1, 1);
rep(i, 1, m) {
int x, y, z; cin >> x >> y >> z;
int p = lca(x, y);
ask(root[x], root[y], root[p], root[parr[p]], z);
}
rep(i, 1, n) G[i].clear();
cnt = 0;
}
node *root[N];
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define task "bus"
// freopen(task".inp", "r", stdin);
// freopen(task".out", "w", stdout);
// freopen("testing.txt", "r", stdin);
while (cin >> n >> m) {
inp();
solve();
}
// root[0] = new node;
//
// root[1] = new node;
// root[1] -> child[0] = root[0] -> child[0];
// root[1] -> child[1] = root[0] -> child[1];
// root[1] -> num = root[0] -> num;
//
// add(2, root[1]);
// root[2] = new node;
// root[2] -> child[0] = root[1] -> child[0];
// root[2] -> child[1] = root[1] -> child[1];
// root[2] -> num = root[1] -> num;
//
// add(3, root[2]);
//
// cout << check(3, root[1]);
return 0 ^ 0;
}
// Identify Thief O_O
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgcmVwKGksIGwsIHIpIGZvcihpbnQgaSA9IGw7IGkgPD0gcjsgaSsrKQojZGVmaW5lIHJlcDIoaSwgbCwgcikgZm9yKGludCBpID0gbDsgaSA+PSByOyBpLS0pCiNkZWZpbmUgZmkgZmlyc3QKI2RlZmluZSBzZSBzZWNvbmQKI2RlZmluZSBiaXQoaSwgeCkgKHggPj4gaSAmIDEpCmNvbnN0IGludCBOID0gM2U1ICsgMzsKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKaW50IG4sIG0sIGFbTl07CnZlY3RvcjxpbnQ+IEdbTl07CmludCBwYXJyW05dOwppbnQgc3pbTl0sIGJpZ1tOXSwgZGVwdGhbTl07CmludCBpbltOXSwgdG9wW05dLCBjbnQ7CnN0cnVjdCBub2RlewogICAgbm9kZSAqY2hpbGRbMl07CiAgICBpbnQgbnVtOwogICAgbm9kZSgpIHsKICAgICAgICBjaGlsZFswXSA9IGNoaWxkWzFdID0gbnVsbHB0cjsKICAgICAgICBudW0gPSAwOwogICAgfQp9Owp2b2lkIGFkZChpbnQgdSwgbm9kZSAqY3VyKSB7CiAgICBhc3NlcnQoY3VyICE9IG51bGxwdHIpOwogICAgcmVwMihpLCAxNiwgMCkgewogICAgICAgIGludCB4ID0gYml0KGksIHUpOwogICAgICAgIGlmIChjdXIgLT4gY2hpbGRbeF0gIT0gbnVsbHB0cikgewogICAgICAgICAgICBub2RlICpvbGQgPSBjdXIgLT4gY2hpbGRbeF07CiAgICAgICAgICAgIGN1ciAtPiBjaGlsZFt4XSA9IG5ldyBub2RlOwogICAgICAgICAgICBjdXIgPSBjdXIgLT4gY2hpbGRbeF07CiAgICAgICAgICAgIGN1ciAtPiBjaGlsZFswXSA9IG9sZCAtPiBjaGlsZFswXTsKICAgICAgICAgICAgY3VyIC0+IGNoaWxkWzFdID0gb2xkIC0+IGNoaWxkWzFdOwogICAgICAgICAgICBjdXIgLT4gbnVtID0gb2xkIC0+IG51bSArIDE7CiAgICAgICAgfQogICAgICAgIGVsc2Ugew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KICAgICAgICBpbnQgdiA9IGJpdChpLCB2YWwpOwogICAgICAgIGlmIChzW3YgXiAxXSA+IDApIHsKICAgICAgICAgICAgcmVzIHw9ICgxIDw8IGkpOwogICAgICAgICAgICBpZiAoeCAhPSBudWxscHRyKSB4ID0geCAtPiBjaGlsZFt2IF4gMV07CiAgICAgICAgICAgIGlmICh5ICE9IG51bGxwdHIpIHkgPSB5IC0+IGNoaWxkW3YgXiAxXTsKICAgICAgICAgICAgaWYgKHAgIT0gbnVsbHB0cikgcCA9IHAgLT4gY2hpbGRbdiBeIDFdOwogICAgICAgICAgICBpZiAocHAgIT0gbnVsbHB0cikgcHAgPSBwcCAtPiBjaGlsZFt2IF4gMV07CiAgICAgICAgfQogICAgICAgIGVsc2UgewogICAgICAgICAgICBpZiAoeCAhPSBudWxscHRyKSB4ID0geCAtPiBjaGlsZFt2XTsKICAgICAgICAgICAgaWYgKHkgIT0gbnVsbHB0cikgeSA9IHkgLT4gY2hpbGRbdl07CiAgICAgICAgICAgIGlmIChwICE9IG51bGxwdHIpIHAgPSBwIC0+IGNoaWxkW3ZdOwogICAgICAgICAgICBpZiAocHAgIT0gbnVsbHB0cikgcHAgPSBwcCAtPiBjaGlsZFt2XTsKICAgICAgICB9CiAgICB9CiAgICBjb3V0IDw8IHJlcyA8PCAiXG4iOwp9CnZvaWQgaW5wKCkgewovLyAgICBjaW4gPj4gbiA+PiBtOwogICAgcmVwKGksIDEsIG4pIGNpbiA+PiBhW2ldOwogICAgcmVwKGksIDEsIG4gLSAxKSB7CiAgICAgICAgaW50IHgsIHk7IGNpbiA+PiB4ID4+IHk7CiAgICAgICAgR1t4XS5wdXNoX2JhY2soeSk7CiAgICAgICAgR1t5XS5wdXNoX2JhY2soeCk7CiAgICB9Cn0Kdm9pZCBzb2x2ZSgpIHsKICAgIHZlY3Rvcjxub2RlKj4gcm9vdChuICsgMyk7CiAgICByb290WzBdID0gbmV3IG5vZGU7CiAgICBkZnMoMSwgMCwgcm9vdCk7CiAgICByZWJ1aWxkKDEsIDEpOwogICAgcmVwKGksIDEsIG0pIHsKICAgICAgICBpbnQgeCwgeSwgejsgY2luID4+IHggPj4geSA+PiB6OwogICAgICAgIGludCBwID0gbGNhKHgsIHkpOwogICAgICAgIGFzayhyb290W3hdLCByb290W3ldLCByb290W3BdLCByb290W3BhcnJbcF1dLCB6KTsKICAgIH0KICAgIHJlcChpLCAxLCBuKSBHW2ldLmNsZWFyKCk7CiAgICBjbnQgPSAwOwp9Cm5vZGUgKnJvb3RbTl07CmludCBtYWluKCkKewogICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbygwKTsKICAgIGNpbi50aWUoMCk7IGNvdXQudGllKDApOwoKICAgICNkZWZpbmUgdGFzayAiYnVzIgovLyAgICBmcmVvcGVuKHRhc2siLmlucCIsICJyIiwgc3RkaW4pOwovLyAgICBmcmVvcGVuKHRhc2siLm91dCIsICJ3Iiwgc3Rkb3V0KTsKCi8vICAgIGZyZW9wZW4oInRlc3RpbmcudHh0IiwgInIiLCBzdGRpbik7CgogICAgd2hpbGUgKGNpbiA+PiBuID4+IG0pIHsKICAgICAgICBpbnAoKTsKICAgICAgICBzb2x2ZSgpOwogICAgfQoKLy8gICAgcm9vdFswXSA9IG5ldyBub2RlOwovLwovLyAgICByb290WzFdID0gbmV3IG5vZGU7Ci8vICAgIHJvb3RbMV0gLT4gY2hpbGRbMF0gPSByb290WzBdIC0+IGNoaWxkWzBdOwovLyAgICByb290WzFdIC0+IGNoaWxkWzFdID0gcm9vdFswXSAtPiBjaGlsZFsxXTsKLy8gICAgcm9vdFsxXSAtPiBudW0gPSByb290WzBdIC0+IG51bTsKLy8KLy8gICAgYWRkKDIsIHJvb3RbMV0pOwovLyAgICByb290WzJdID0gbmV3IG5vZGU7Ci8vICAgIHJvb3RbMl0gLT4gY2hpbGRbMF0gPSByb290WzFdIC0+IGNoaWxkWzBdOwovLyAgICByb290WzJdIC0+IGNoaWxkWzFdID0gcm9vdFsxXSAtPiBjaGls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