#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100007;
const int MAXM = 107;
int c[MAXN];
vector<int> g[MAXN];
int d[MAXM][MAXN];
int que[MAXN];
int main()
{
//ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, m;
cin >> n >> m;
for(int i = 0; i < n; i++)
{
cin >> c[i];
c[i]--;
}
for(int i = 0; i < n - 1; i++)
{
int u, v;
cin >> u >> v;
u--;
v--;
g[u].push_back(v);
g[v].push_back(u);
}
memset(d, -1, sizeof d);
for(int j = 0; j < m; j++)
{
int qh = 0, qt = 0;
for(int i = 0; i < n; i++)
if(c[i] == j)
{
d[j][i] = 0;
que[qt++] = i;
}
while(qh != qt)
{
int v = que[qh++];
for(int to: g[v])
if(d[j][to] == -1)
{
d[j][to] = d[j][v] + 1;
que[qt++] = to;
}
}
}
int q;
cin >> q;
while(q--)
{
int company, server, distance;
cin >> company >> server >> distance;
company--;
server--;
cout << (d[company][server] <= distance);
}
cout << "\n";
return 0;
}