#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <cstring>
#include <numeric>
#include <algorithm>
#include <functional>
#include <array>
#include <map>
#include <queue>
#include <limits.h>
#include <set>
#include <stack>
#include <random>
#include <complex>
#include <unordered_map>
#include <nmmintrin.h>
#include <chrono>
#define rep(i,s,n) for(int i = (s); (n) > i; i++)
#define REP(i,n) rep(i,0,n)
#define RANGE(x,a,b) ((a) <= (x) && (x) <= (b))
#define DUPLE(a,b,c,d) (RANGE(a,c,d) || RANGE(b,c,d) || RANGE(c,a,b) || RANGE(d,a,b))
#define INCLU(a,b,c,d) (RANGE(a,c,d) && (b,c,d))
#define PW(x) ((x)*(x))
#define ALL(x) (x).begin(), (x).end()
#define MODU 1000000007
#define bitcheck(a,b)   ((a >> b) & 1)
#define bitset(a,b)      ( a |= (1 << b))
#define bitunset(a,b)    (a &= ~(1 << b))
#define MP(a,b) make_pair((a),(b))
#define Manh(a,b) (abs((a).first-(b).first) + abs((a).second - ((b).second))
#define pritnf printf
#define scnaf scanf
#define itn int
#ifdef _MSC_VER
#define __builtin_popcount _mm_popcnt_u32
#define __builtin_popcountll _mm_popcnt_u64
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
ll gcd(ll a, ll b) {
	if (b == 0) return a;
	return gcd(b, a%b);
}
template<typename A, size_t N, typename T>
void Fill(A(&array)[N], const T &val) {
	std::fill((T*)array, (T*)(array + N), val);
}

int ccc = 0;
class Graph {
public:
	int vn;
	int sumcost = 0;
	vector<vector<pii>> g;

	Graph(int n) {
		vn = n;
		g.resize(n);
	}
	virtual void con(int a, int b, int w) = 0;
	int getWeight(int f, int t) {
		auto itr = lower_bound(ALL(g[f]), make_pair(t, INT_MIN));
		if (itr != g[f].end())
			return itr->second;
		return INT_MIN;
	}
	int Costsum() {
		return sumcost;
	}
	void scan(int edcount, bool oindexed, bool w) {
		REP(i, edcount) {
			int a, b, c = 1;
			scanf("%d %d", &a, &b);
			if (w)scanf("%d", &c);
			con(a - oindexed, b - oindexed, c);
		}
	}
};
class BiDGraph : public Graph {//??????
public:
	BiDGraph(int n) : Graph(n) {}

	void con(int a, int b, int w = 1) {
		g[a].push_back({ b,w });
		g[b].push_back({ a, w });
		sumcost++;
	}
};
class DGraph : public Graph {//??????
public:
	DGraph(int n) : Graph(n) {}
	void con(int a, int b, int w = 1) {
		g[a].push_back({ b,w });
		sumcost++;
	}
};
class Bitvector {
	//4byte???????????? ??¨??¨?????????0-index
	const int bsize = 8;
	static const int popc[256];
public:

	unsigned char* st;
	unsigned int* blockrank;
	int sz;
	Bitvector(int n) {
		sz = n;
		st = (unsigned char*)calloc((n + bsize - 1) / bsize, sizeof(char));
		blockrank = (unsigned int*)calloc((n + bsize - 1) / bsize + 1, sizeof(int));
		if (st == NULL || blockrank == NULL)
			abort();
	}
	inline void set(int a, bool f) {//????????????????????§?????????
		if (f)bitset(st[a / bsize], (bsize - 1 - a % bsize));
	}
	inline bool get(int a) { return bitcheck(st[a / bsize], (bsize - 1 - a % bsize)); }
	inline void calcblock() {
		int cou = 0;
		REP(i, sz + 1) {
			if (i && !(i % 8))blockrank[i / bsize] = cou;
			if (i<sz)cou += bitcheck(st[i / bsize], (bsize - 1 - i % bsize));
		}
	}
	int rank(int a, bool t) {
		int ret = blockrank[a / bsize] + (((bsize - a % bsize) >= bsize) ? 0 : popc[st[a / bsize] >> (bsize - a % bsize)]);
		return t ? ret : a - ret;
	}
	int select(int a, bool t) {
		int l = 0, r = sz + 1;

		while (r - l > 1) {
			int mid = (l + r) / 2;
			if (rank(mid, t) >= a) r = mid;
			else l = mid;
		}
		if (rank(r, t) < a) return -1;
		return r;
	}
	void clear() {
		free(st);
		free(blockrank);
	}
};
const int Bitvector::popc[256] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8 };


//??????????????£??????????????????????????????(?????¬????????§????????¶????????§)
template<typename T>
class WaveletMatrix {
public:
	vector<T> data;
	vector<Bitvector> bitv;
	vector<int> border;
	int Max = 0;
	int bitsize;
	WaveletMatrix(vector<T>& dat) : bitsize(sizeof(T) * 8), data(dat) {
		bitv.reserve(bitsize);
		REP(i, bitsize + 1)
			bitv.push_back(Bitvector(data.size()));
		border.resize(bitsize + 1);
		REP(j, data.size())
			Max = max(Max, data[j]), bitv[0].set(j, bitcheck(data[j], bitsize - 1));
		bitv[0].calcblock();
		vector<T> buf[2];
		buf[0] = data;
		buf[1].resize(data.size());

		REP(i, bitsize - 1) {
			border[i + 1] = bitv[i].rank(data.size(), 0);
			int cou[2] = { 0, border[i + 1] };
			REP(j, data.size()) {
				bitv[i + 1].set(cou[bitv[i].get(j)], bitcheck(buf[i % 2][j], bitsize - i - 2));
				buf[1 - i % 2][cou[bitv[i].get(j)]] = buf[i % 2][j] << (i + 1) >> (i + 1);
				cou[bitv[i].get(j)]++;
			}
			bitv[i + 1].calcblock();
		}
		border[bitsize] = bitv[bitsize - 1].rank(data.size(), 0);
	}

	pii search(int s, int e, unsigned int t, int b = 0) {
		if (b == bitsize) return{ s,e };
		bool cur = bitcheck(t, bitsize - 1);
		return search(cur*border[b + 1] + bitv[b].rank(s, cur), cur*border[b + 1] + bitv[b].rank(e, cur), t << 1, b + 1);
	}
	int backward(int a, unsigned int t, int b = 0) {
		if (b == bitsize - 1) return a;
		backward(bitv[bitsize - b - 1].select(a + 1 - bitcheck(t, 0) * border[bitsize - b], bitcheck(t, 0)) - 1, t >> 1, b + 1);
	}

	int rank(int s, int e, T t) {
		pii r = search(s, e, t);
		return r.second - r.first;
	}

	int select(int a, T t) {
		pii r = search(0, data.size(), t);
		return  backward(r.first + a - 1, t);
	}

	T kthelement(int s, int e, int k, int b = 0) {
		int eo = bitv[b].rank(e, 0), so = bitv[b].rank(s, 0), cou = eo - so;
		if (b == bitsize - 1) return cou <= k;
		return (cou <= k ?
			kthelement(border[b + 1] + (s - so), border[b + 1] + (s - so) + (e - s - cou), k - cou, b + 1) :
			kthelement(so, so + cou, k, b + 1)) | ((cou <= k) << (bitsize - b - 1));
	}
	int lessthan(int s, int e, T t, int b = 0) {
		int eo = bitv[b].rank(e, 0), so = bitv[b].rank(s, 0), cou = (eo - so);
		if (b == bitsize) return e - s;
		return bitcheck(t, bitsize - b - 1) ?
			lessthan(border[b + 1] + (s - so), border[b + 1] + (s - so) + (e - s - cou), t, b + 1) + cou :
			lessthan(so, so + cou, t, b + 1);
	}
	void clear() {
		REP(i, bitsize) bitv[i].clear();
	}
};

class Heavy_Light_Decomposition {
public:
	BiDGraph g, compg;
	vector<int> sz, depth, hd, chain, par;

	int bn = 0;
	vector<int> bvn, inbn;
	vector<vector<int>> bv;

	Heavy_Light_Decomposition(BiDGraph graph, int root) :
		g(graph), compg(graph.vn), sz(graph.vn, 1), hd(graph.vn), depth(graph.vn), chain(graph.vn), par(graph.vn),
		bvn(graph.vn), inbn(graph.vn) {
		getsz(root, root);
		bv.push_back(vector<int>());
		HLD(root, -1, root, 0);
	}

	int getsz(int cur, int p) {
		par[cur] = p;
		for (auto itr : g.g[cur])
			if (p != itr.first)
				depth[itr.first] = depth[cur] + 1, sz[cur] += getsz(itr.first, cur), hd[cur] = max(hd[itr.first] + 1, hd[cur]);
		return sz[cur];
	}

	void HLD(int cur, int p, int head, int num) {
		chain[cur] = head, bvn[cur] = num;
		inbn[cur] = bv[num].size(), bv[num].push_back(cur);
		pii Maxc = { -1,-1 };
		for (auto itr : g.g[cur])
			if (itr.first != p)
				if (Maxc.first< sz[itr.first])
					Maxc = max(Maxc, { sz[itr.first], itr.first });


		for (auto itr : g.g[cur])
			if (itr.first != p) {
				int nb = num;
				if (itr.first != Maxc.second) {
					bv.push_back(vector<int>());
					nb = ++bn;
				}
				HLD(itr.first, cur, itr.first == Maxc.second ? head : itr.first, nb);
			}
	}

	int lca(int a, int b) {
		if (chain[a] == chain[b]) return depth[a] < depth[b] ? a : b;
		if (depth[chain[a]] > depth[chain[b]])
			return lca(par[chain[a]], b);
		return lca(a, par[chain[b]]);
	}

	void for_each_edge(int a, int b, function<void(int, int)> func) {
		if (chain[a] == chain[b]) return func(inbn[a] < inbn[b] ? a : b, inbn[a] < inbn[b] ? b : a);
		if (depth[chain[a]] < depth[chain[b]]) swap(a, b);
		for_each_edge(a, chain[a], func), for_each_edge(par[chain[a]], b, func);
	}
};
signed main() {
	int n, q;
	scnaf("%d %d", &n, &q);
	int mx = 0;
	vector<int> num(n);
	REP(i, n)
		scanf("%d", &num[i]),mx = max(mx, num[i]);

	BiDGraph g(n);
	g.scan(n - 1, 1, false);
	Heavy_Light_Decomposition hld(g, 0);
	vector<int> in(n*2, 0),out(n * 2, 0);
	vector<int> ord(n);
	int cou = 0;
	function<void(int, int)> dfs = [&](int cur, int p) {
		in[cou] = num[cur];
		ord[cur] = cou;
		cou++;
		for(auto itr : g.g[cur])
			if(itr.first != p)
				dfs(itr.first, cur);

		out[cou] = num[cur];
		cou++;
	};
	dfs(0,-1);

	WaveletMatrix<int> wi(in),wo(out);

	REP(i, q) {
		int a, b, c;
		scanf("%d %d %d", &a, &b, &c);
		int lca = hld.lca(--a, --b);
		c = hld.depth[a] + hld.depth[b] - 2 * hld.depth[lca]+1-c+1;
		pii li = { ord[lca],ord[a] +1}, ri = {ord[lca]+1,ord[b] + 1 }, lo = { ord[lca],ord[a] + 1 },ro = { ord[lca] + 1,ord[b] + 1 };
		int ans = 0;

		REP(j, 32) {
			ans <<= 1;
			int cou = 0;
			int licf = wi.bitv[j].rank(li.first, 1),
				lics = wi.bitv[j].rank(li.second, 1),
				ricf = wi.bitv[j].rank(ri.first, 1),
				rics = wi.bitv[j].rank(ri.second, 1),
				locf = wo.bitv[j].rank(lo.first, 1),
				locs = wo.bitv[j].rank(lo.second, 1),
				rocf = wo.bitv[j].rank(ro.first, 1),
				rocs = wo.bitv[j].rank(ro.second, 1);

			int lc = (lics - licf) - (locs - locf), rc = (rics - ricf) - (rocs - rocf);

			if (rc + lc >= c) {
				li.first = wi.border[j + 1] + licf;
				li.second = li.first + (lics - licf);
				ri.first = wi.border[j + 1] + ricf;
				ri.second = ri.first +(rics - ricf);
				lo.first = wo.border[j + 1] + locf;
				lo.second = lo.first + (locs - locf);
				ro.first = wo.border[j + 1] + rocf;
				ro.second = ro.first + (rocs - rocf);
				ans |= 1;
			}
			else {
				li.first = li.first - licf;
				li.second = li.first - licf + (li.second - li.first - (lics - licf));
				ri.first = ri.first - ricf;
				ri.second = ri.first - ricf + (ri.second - ri.first - (rics - ricf));
				lo.first = lo.first - locf;
				lo.second = lo.first - locf + (lo.second - lo.first - (locs - locf));
				ro.first = ro.first - rocf;
				ro.second = ro.first - rocf + (ro.second - ro.first - (rocs - rocf));
				c -= rc + lc;
			}
		}

		printf("%d\n", ans);
	} 
	return 0;
}

Measured memory_bytes: 37652