#include <bits/stdc++.h>
using namespace std;
#define MAX_N 150005
#define MAX_M 100005
#define LG_MAX_N 20
int N, M, time_counter, block_size, distinct;
int wt[MAX_N], entry_time[MAX_N], exit_time[MAX_N], depth[MAX_N], freq[MAX_N];
bool inrange[MAX_N];
int time_log[MAX_N<<1];
int anc[MAX_N][LG_MAX_N]; // ancestor table; anc[u][i] is the 2^i th ancestor of u
vector<int> g[MAX_N];
struct query{
int id, l, r, lca;
}q[MAX_M];
int ans[MAX_M];
void dfs(int u, int par){
time_counter++;
entry_time[u] = time_counter;
time_log[time_counter] = u;
for(int i=1; i<LG_MAX_N; i++)
anc[u][i] = anc[anc[u][i-1]][i-1];
for(int i=0; i<g[u].size(); i++){
int v = g[u][i];
if(v==par) continue;
depth[v] = depth[u] + 1;
anc[v][0] = u;
dfs(v, u);
}
time_counter++;
exit_time[u] = time_counter;
time_log[time_counter] = u;
}
int lca(int u, int v){
if(depth[u]>depth[v])
swap(u, v);
for(int i=LG_MAX_N-1; i>=0; i--)
if(depth[v]-depth[u]>=(1<<i))
v = anc[v][i];
if(u==v) return u;
for(int i=LG_MAX_N-1; i>=0; i--){
if(anc[u][i]!=anc[v][i]){
u = anc[u][i];
v = anc[v][i];
}
}
return anc[u][0];
}
bool qcomp(query &q1, query &q2){
int b1 = q1.l/block_size;
int b2 = q2.l/block_size;
return b1==b2 ? q1.r<q2.r : b1<b2;
}
void add_rem(int i){
int u = time_log[i];
if(inrange[u]){
freq[wt[u]]--;
if(freq[wt[u]]==0)
distinct--;
}
else{
if(freq[wt[u]]==0)
distinct++;
freq[wt[u]]++;
}
inrange[u] ^= 1;
}
void mo(){
block_size = (int)(sqrt(N<<1)+0.5);
sort(q, q+M, qcomp);
int curr_l = 0, curr_r = -1;
distinct = 0;
for(int i=0; i<M; i++){
while(curr_l<q[i].l){
add_rem(curr_l);
curr_l++;
}
while(curr_l>q[i].l){
curr_l--;
add_rem(curr_l);
}
while(curr_r<q[i].r){
curr_r++;
add_rem(curr_r);
}
while(curr_r>q[i].r){
add_rem(curr_r);
curr_r--;
}
if(time_log[q[i].l]!=q[i].lca)
add_rem(entry_time[q[i].lca]);
ans[q[i].id] = distinct;
if(time_log[q[i].l]!=q[i].lca)
add_rem(entry_time[q[i].lca]);
}
}
int main(){
scanf("%d%d", &N, &M);
for(int i=1; i<=N; i++)
scanf("%d", &wt[i]);
map<int, int> cc;
int ccval = 1;
for(int i=1; i<=N; i++){
if(cc.count(wt[i])==0)
cc[wt[i]] = ccval++;
wt[i] = cc[wt[i]];
}
for(int i=1; i<=N-1; i++){
int u, v;
scanf("%d%d", &u, &v);
g[u].push_back(v);
g[v].push_back(u);
}
anc[1][0] = 1;
time_counter = 0;
dfs(1, -1);
for(int i=0; i<M; i++){
int l, r;
scanf("%d%d", &l, &r);
if(entry_time[l]>entry_time[r])
swap(l, r);
q[i].id = i;
q[i].lca = lca(l, r);
if(q[i].lca==l){
q[i].l = entry_time[l];
q[i].r = entry_time[r];
}
else{
q[i].l = exit_time[l];
q[i].r = entry_time[r];
}
}
mo();
for(int i=0; i<M; i++)
printf("%d\n", ans[i]);
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgojZGVmaW5lIE1BWF9OIDE1MDAwNQojZGVmaW5lIE1BWF9NIDEwMDAwNQojZGVmaW5lIExHX01BWF9OIDIwCgppbnQgTiwgTSwgdGltZV9jb3VudGVyLCBibG9ja19zaXplLCBkaXN0aW5jdDsKaW50IHd0W01BWF9OXSwgZW50cnlfdGltZVtNQVhfTl0sIGV4aXRfdGltZVtNQVhfTl0sIGRlcHRoW01BWF9OXSwgZnJlcVtNQVhfTl07CmJvb2wgaW5yYW5nZVtNQVhfTl07CmludCB0aW1lX2xvZ1tNQVhfTjw8MV07CmludCBhbmNbTUFYX05dW0xHX01BWF9OXTsgIC8vIGFuY2VzdG9yIHRhYmxlOyBhbmNbdV1baV0gaXMgdGhlIDJeaSB0aCBhbmNlc3RvciBv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FuY1t1XVtpXTsKICAgICAgICAgICAgdiA9IGFuY1t2XVtpXTsKICAgICAgICB9CiAgICB9CiAgICByZXR1cm4gYW5jW3VdWzBdOwp9Cgpib29sIHFjb21wKHF1ZXJ5ICZxMSwgcXVlcnkgJnEyKXsKICAgIGludCBiMSA9IHExLmwvYmxvY2tfc2l6ZTsKICAgIGludCBiMiA9IHEyLmwvYmxvY2tfc2l6ZTsKICAgIHJldHVybiBiMT09YjIgPyBxMS5yPHEyLnIgOiBiMTxiMjsKfQoKdm9pZCBhZGRfcmVtKGludCBpKXsKICAgIGludCB1ID0gdGltZV9sb2dbaV07CiAgICBpZihpbnJhbmdlW3VdKXsKICAgICAgICBmcmVxW3d0W3VdXS0tOwogICAgICAgIGlmKGZyZXFbd3RbdV1dPT0wKQogICAgICAgICAgICBkaXN0aW5jdC0tOwogICAgfQogICAgZWxzZXsKICAgICAgICBpZihmcmVxW3d0W3VdXT09MCkKICAgICAgICAgICAgZGlzdGluY3QrKzsKICAgICAgICBmcmVxW3d0W3VdXSsrOwogICAgfQogICAgaW5yYW5nZVt1XSBePSAxOwp9Cgp2b2lkIG1vKCl7CgogICAgYmxvY2tfc2l6ZSA9IChpbnQpKHNxcnQoTjw8MSkrMC41KTsKICAgIHNvcnQocSwgcStNLCBxY29tcCk7CgogICAgaW50IGN1cnJfbCA9IDAsIGN1cnJfciA9IC0xOwogICAgZGlzdGluY3QgPSAwOwogICAgZm9yKGludCBpPTA7IGk8TTsgaSsrKXsKCiAgICAgICAgd2hpbGUoY3Vycl9sPHFbaV0ubCl7CiAgICAgICAgICAgIGFkZF9yZW0oY3Vycl9sKTsKICAgICAgICAgICAgY3Vycl9sKys7CiAgICAgICAgfQogICAgICAgIHdoaWxlKGN1cnJfbD5xW2ldLmwpewogICAgICAgICAgICBjdXJyX2wtLTsKICAgICAgICAgICAgYWRkX3JlbShjdXJyX2wpOwogICAgICAgIH0KCiAgICAgICAgd2hpbGUoY3Vycl9yPHFbaV0ucil7CiAgICAgICAgICAgIGN1cnJfcisrOwogICAgICAgICAgICBhZGRfcmVtKGN1cnJfcik7CiAgICAgICAgfQogICAgICAgIHdoaWxlKGN1cnJfcj5xW2ldLnIpewogICAgICAgICAgICBhZGRfcmVtKGN1cnJfcik7CiAgICAgICAgICAgIGN1cnJfci0tOwogICAgICAgIH0KCiAgICAgICAgaWYodGltZV9sb2dbcVtpXS5sXSE9cVtpXS5sY2EpCiAgICAgICAgICAgIGFkZF9yZW0oZW50cnlfdGltZVtxW2ldLmxjYV0pOwogICAgICAgIGFuc1txW2ldLmlkXSA9IGRpc3RpbmN0OwogICAgICAgIGlmKHRpbWVfbG9nW3FbaV0ubF0hPXFbaV0ubGNhKQogICAgICAgICAgICBhZGRfcmVtKGVudHJ5X3RpbWVbcVtpXS5sY2FdKTsKICAgIH0KfQoKCmludCBtYWluKCl7CgogICAgc2NhbmYoIiVkJWQiLCAmTiwgJk0pOwogICAgZm9yKGludCBpPTE7IGk8PU47IGkrKykKICAgICAgICBzY2FuZigiJWQiLCAmd3RbaV0pOwogICAgCiAgICBtYXA8aW50LCBpbnQ+IGNjOwogICAgaW50IGNjdmFsID0gMTsKICAgIGZvcihpbnQgaT0xOyBpPD1OOyBpKyspewogICAgICAgIGlmKGNjLmNvdW50KHd0W2ldKT09MCkKICAgICAgICAgICAgY2Nbd3RbaV1dID0gY2N2YWwrKzsKICAgICAgICB3dFtpXSA9IGNjW3d0W2ldXTsKICAgIH0KCiAgICBmb3IoaW50IGk9MTsgaTw9Ti0xOyBpKyspewogICAgICAgIGludCB1LCB2OwogICAgICAgIHNjYW5mKCIlZCVkIiwgJnUsICZ2KTsKICAgICAgICBnW3VdLnB1c2hfYmFjayh2KTsKICAgICAgICBnW3ZdLnB1c2hfYmFjayh1KTsKICAgIH0KCiAgICBhbmNbMV1bMF0gPSAxOwogICAgdGltZV9jb3VudGVyID0gMDsKICAgIGRmcygxLCAtMSk7CgogICAgZm9yKGludCBpPTA7IGk8TTsgaSsrKXsKICAgICAgICBpbnQgbCwgcjsKICAgICAgICBzY2FuZigiJWQlZCIsICZsLCAmcik7CiAgICAgICAgaWYoZW50cnlfdGltZVtsXT5lbnRyeV90aW1lW3JdKQogICAgICAgICAgICBzd2FwKGwsIHIpOwogICAgICAgIHFbaV0uaWQgPSBpOwogICAgICAgIHFbaV0ubGNhID0gbGNhKGwsIHIpOwogICAgICAgIGlmKHFbaV0ubGNhPT1sKXsKICAgICAgICAgICAgcVtpXS5sID0gZW50cnlfdGltZVtsXTsKICAgICAgICAgICAgcVtpXS5yID0gZW50cnlfdGltZVtyXTsKICAgICAgICB9CiAgICAgICAgZWxzZXsKICAgICAgICAgICAgcVtpXS5sID0gZXhpdF90aW1lW2xdOwogICAgICAgICAgICBxW2ldLnIgPSBlbnRyeV90aW1lW3JdOwogICAgICAgIH0KICAgIH0KCiAgICBtbygpOwoKICAgIGZvcihpbnQgaT0wOyBpPE07IGkrKykKICAgICAgICBwcmludGYoIiVkXG4iLCBhbnNbaV0pOwoKICAgIHJldHVybiAwOwp9