#include<bits/stdc++.h>
using namespace std;
#define N 100010
int w[N];
int sn,n,m,u,v,tst;
int cnt[100000]={0};
vector<int> g[N];
vector<pair<int, pair<int,int > > > q;
int a[N];
int st[N];
int en[N];
int lvl[N];
int p[N];
int ansq[N]={0};
bool isvisited[N];
struct compare{
bool operator()(const pair<int, pair<int,int > > &a1, const pair<int, pair<int,int > > &a2)
{
if(((a1.second).first)/sn!=((a2.second).first)/sn)
return ((a1.second).first)/sn<((a2.second).first)/sn;
else
return ((a1.second).second)<((a2.second).second);
}
};
void dfs(int i,int l)
{
if(!isvisited[i])
{
if(l==0)
{
p[i]=0;
}
isvisited[i]=1;
lvl[i]=l;
st[i]=tst;
a[tst]=i;
tst++;
vector<int>::iterator it;
for(it=g[i].begin();it!=g[i].end();it++)
{
dfs(*it,l+1);
p[*it]=i;
}
en[i]=tst;
a[tst]=i;
tst++;
}
}
int par(int x,int z)
{
while(lvl[z]!=lvl[x])
{
z=p[z];
}
if(z==x)
return 1;
while(p[x]!=p[z]&&p[x]!=0&&p[z]!=0)
{
x=p[x];
z=p[z];
}
if(p[x]==0)
return x;
else
return p[x];
}
inline void check(int x, int& res){
if ( (isvisited[x]) and (--cnt[w[x]] == 0) ) res--;
else if ( (!isvisited[x]) and (cnt[w[x]]++ == 0) ) res++;
isvisited[x] ^= 1;
}
int main()
{
memset(isvisited,0,sizeof(isvisited));
for(int i=0;i<N;i++){
ansq[i]=0;
cnt[i]=0;
}
scanf("%d %d",&n,&m);
for(int i=1;i<n+1;i++)
scanf("%d",&w[i]);
for(int i=0;i<n-1;i++)
{
scanf("%d %d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
tst=0,a[tst]=0;
//cout<<"dfs start"<<endl;
for(int i=1;i<n;i++){
dfs(i,0);
}
//cout<<" dfs end"<<endl;
//for(int i=0;i<tst;i++)
// cout<<a[i]<<" ";
//cout<<endl;
//cout<<tst<<"\n";
sn=sqrt(tst);
//cout<<"query insertion time"<<endl;
for(int i=1;i<=m;i++)
{
scanf("%d %d",&u,&v);
if(lvl[u]>lvl[v])
{
int temp=u;
u=v;
v=temp;
}
//cout<<"query insertion processing"<<endl;
int k=par(u,v);
if(k==u)
{
q.push_back(make_pair(i,make_pair(st[u],st[v])));
}
else
{
q.push_back(make_pair(i,make_pair(en[u],st[v])));
q.push_back(make_pair(i,make_pair(st[k],st[k])));
}
// cout<<"a query is inserted"<<endl;
}
//cout<<"all query inserted an sorting start"<<endl;
sort(q.begin(),q.end(),compare());
//cout<<"query sorted"<<endl;
vector<pair<int, pair<int,int > > >::iterator it1;
int curl=0,cur=0,ans=0,val;
memset(isvisited,0,sizeof(isvisited));
for(it1=q.begin();it1!=q.end();it1++)
{
//cout<<"a query is given consideration"<<endl;
u=(it1->second).first;
v=(it1->second).second;
int ind=it1->first;
//cout<<"a query curl and u is checked for <"<<endl;
while(curl<u)
{
val=a[curl];
check(val,ans);
curl++;
}
//cout<<"a query curl and u is checked for >"<<endl;
while(curl>u)
{
curl--;
val=a[curl];
check(val,ans); }
//cout<<"a query cur and v is checked for >"<<endl;
while(cur>v)
{
val=a[cur];
check(val,ans);
curl--;
}
while(cur<v)
{
cur++;
val=a[cur];
check(val,ans);
}
ansq[ind]+=ans;
}
for(int i=1;i<=m;i++)
printf("%d\n",ansq[i]);
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSBOIDEwMDAxMAppbnQgd1tOXTsKaW50IHNuLG4sbSx1LHYsdHN0OwppbnQgY250WzEwMDAwMF09ezB9Owp2ZWN0b3I8aW50PiBnW05dOwp2ZWN0b3I8cGFpcjxpbnQsIHBhaXI8aW50LGludCA+ID4gPiBxOwppbnQgYVtOXTsKaW50IHN0W05dOwppbnQgZW5bTl07CmludCBsdmxbTl07CmludCBwW05dOwppbnQgYW5zcVtOXT17MH07CmJvb2wgaXN2aXNpdGVkW05dOwpzdHJ1Y3QgY29tcGFyZXsKYm9vbCBvcGVyYXRvcigpKGNvbnN0IHBhaXI8aW50LCBwYWlyPGludCxpbnQgPiA+ICZhMSwgY29uc3QgcGFpcjxpbnQsIHBhaXI8aW50LGludCA+ID4gJmEyKQp7CiAgICBpZigoKGExLnNlY29uZCkuZmlyc3QpL3NuIT0oKGEyLnNlY29uZCkuZmlyc3QpL3NuKQogICAgICAgIHJldHVybiAoKGExLnNlY29uZCkuZmlyc3QpL3NuPCgoYTIuc2Vjb25kKS5maXJzdCkvc247CiAgICBlbHNlCiAgICAgICAgcmV0dXJuICgoYTEuc2Vjb25kKS5zZWNvbmQpPCgoYTIuc2Vjb25kKS5zZWNvbmQpOwp9Cn07CnZvaWQgZGZzKGludCBpLGludCBsKQp7CiAgICBpZighaXN2aXNpdGVkW2ldKQogICAgewogICAgICAgIGlmKGw9PTApCiAgICAgICAgewogICAgICAgICBwW2ldPTA7CiAgICAgICAgfQogICAgICAgIGlzdmlzaXRlZFtpXT0xOwogICAgICAgIGx2bFtpXT1sOwogICAgICAgIHN0W2ldPXRzdDsKICAgICAgICBhW3RzdF09aTsKICAgICAgICB0c3QrKzsKICAgICAgICB2ZWN0b3I8aW50Pjo6aXRlcmF0b3IgaXQ7CiAgICAgICAgZm9yKGl0PWdbaV0uYmVnaW4oKTtpdCE9Z1tpXS5lbmQoKTtpdCsrKQogICAgICAgIHsKICAgICAgICAgICAgZGZzKCppdCxsKzEpOwogICAgICAgICAgICBwWyppdF09aTsKICAgICAgICB9CiAgICAgICAgZW5baV09dHN0OwogICAgICAgIGFbdHN0XT1pOwogICAgICAgIHRzdCsrOwogICAgfQp9CmludCBwYXIoaW50IHgsaW50IHopCnsKICAgIHdoaWxlKGx2bFt6XSE9bHZsW3hdKQogICAgewogICAgICAgIHo9cFt6XTsKICAgIH0KICAgIGlmKHo9PXgpCiAgICAgICAgcmV0dXJuIDE7CiAgICB3aGlsZShwW3hdIT1wW3pdJiZwW3hdIT0wJiZwW3pdIT0wKQogICAgewogICAgICAgIHg9cFt4XTsKICAgICAgICB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zaXRlZCkpOwogICAgZm9yKGl0MT1xLmJlZ2luKCk7aXQxIT1xLmVuZCgpO2l0MSsrKQogICAgewogICAgICAgIC8vY291dDw8ImEgcXVlcnkgaXMgZ2l2ZW4gY29uc2lkZXJhdGlvbiI8PGVuZGw7CiAgICAgICAgdT0oaXQxLT5zZWNvbmQpLmZpcnN0OwogICAgICAgIHY9KGl0MS0+c2Vjb25kKS5zZWNvbmQ7CiAgICAgICAgaW50IGluZD1pdDEtPmZpcnN0OwogICAgICAgIC8vY291dDw8ImEgcXVlcnkgY3VybCBhbmQgdSBpcyBjaGVja2VkIGZvciA8Ijw8ZW5kbDsKICAgICAgICB3aGlsZShjdXJsPHUpCiAgICAgICAgewogICAgICAgICAgICB2YWw9YVtjdXJsXTsKICAgICAgICAgICAgY2hlY2sodmFsLGFucyk7CiAgICAgICAgICAgIGN1cmwrKzsKICAgICAgICB9CiAgICAgICAgLy9jb3V0PDwiYSBxdWVyeSBjdXJsIGFuZCB1IGlzIGNoZWNrZWQgZm9yID4iPDxlbmRsOwogICAgICAgIHdoaWxlKGN1cmw+dSkKICAgICAgICB7CiAgICAgICAgICAgIGN1cmwtLTsKICAgICAgICAgICAgdmFsPWFbY3VybF07CiAgICAgICAgICAgIGNoZWNrKHZhbCxhbnMpOyAgICAgICAgfQogICAgICAgIC8vY291dDw8ImEgcXVlcnkgY3VyIGFuZCB2IGlzIGNoZWNrZWQgZm9yID4iPDxlbmRsOwogICAgICAgIHdoaWxlKGN1cj52KQogICAgICAgIHsKICAgICAgICAgICAgdmFsPWFbY3VyXTsKICAgICAgICAgICAgY2hlY2sodmFsLGFucyk7CiAgICAgICAgICAgIGN1cmwtLTsKICAgICAgICB9CiAgICAgICAgd2hpbGUoY3VyPHYpCiAgICAgICAgewogICAgICAgICAgICBjdXIrKzsKICAgICAgICAgICAgdmFsPWFbY3VyXTsKICAgICAgICAgICAgY2hlY2sodmFsLGFucyk7CiAgICAgICAgfQogICAgICAgIGFuc3FbaW5kXSs9YW5zOwogICAgfQogICAgZm9yKGludCBpPTE7aTw9bTtpKyspCiAgICAgICAgcHJpbnRmKCIlZFxuIixhbnNxW2ldKTsKICAgIHJldHVybiAwOwp9Cg==