/*
ye mera template hai
apna khud likho bc :P
*/
/*
Author : Sarvagya Agarwal
*/
#include<bits/stdc++.h>
using namespace std;
//defines
#define openin freopen("input.txt","r",stdin)
#define openout freopen("output.txt","w",stdout)
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define ll long long
#define int long long
#define mod 1000000007
#define rep(i,x,y) for (__typeof(x) i=x;(x<=y?i<=y:i>=y);i=(x<=y?i+1:i-1))
#define all(c) (c).begin(),(c).end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
int power(int a , int b)
{
int res = 1 ;
while(b)
{
if(b%2) {
res = (res * a)%mod ;
}
b/=2 ;
a = (a*a)%mod ;
}
return res ;
}
//debug
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif
const int MAX_N = 40005 ;
const int MAX_M = 100005 ;
int weight[MAX_N] , answers[MAX_M] , N , M ,arr[3*MAX_N];
vector<int> graph[MAX_N] ;
int start[MAX_N] , endd[MAX_N] ;
int P[MAX_N][23] , depth[MAX_N] , vis[MAX_N] ,timer = 1 ;
void DFS(int node = 1 , int par = 0 , int d = 0)
{
start[node] = timer ;
arr[timer++] = node ;
depth[node] = d ;
P[node][0] = par ;
for(auto child : graph[node]) {
if(child != par) {
DFS(child,node,d+1) ;
}
}
endd[node] = timer ;
arr[timer++] = node ;
}
void pre(int n)
{
for(int j=1;(1<<j)<n;j++)
{
rep(i,1,n)
{
if(P[i][j-1]!=-1)
{
P[i][j] = P[P[i][j-1]][j-1];
}
}
}
}
int LCA(int p,int q)
{
int temp ;
if(depth[p] < depth[q])
temp=p,p=q,q=temp ;
int log ;
for(log=1;(1<<log)<=depth[p];log++) ;
log--;
for(int i=log;i>=0;i--)
{
if(depth[p] - (1<<i) >= depth[q])
p=P[p][i];
}
if(p==q)return p;
//now compute lca using dp table(P)
for(int i=log;i>=0;i--)
{
if(P[p][i]!=-1&&P[p][i]!=P[q][i])
p=P[p][i],q=P[q][i];
}
return P[p][0];
}
void compress()
{
set<int> s ;
map<int,int> m ;
rep(i,1,N)s.insert(weight[i]) ;
int index = 1 ;
for(auto it : s) m[it]=index++ ;
rep(i,1,N)
{
weight[i] = m[weight[i]] ;
}
return ;
}
int BLOCK ,ans = 0;
struct query {
int index,L,R,flag ;
};
query queries[MAX_M] ;
bool compare(query A , query B)
{
if(A.L / BLOCK != B.L/BLOCK)
return A.L/BLOCK < B.L/BLOCK ;
return A.R < B.R ;
}
int freq[MAX_N] , cnt[MAX_N] ;
void check(int index)
{
int node = arr[index] ;
if(freq[node] && (--cnt[weight[node]] == 0))
ans-- ;
else if(freq[node]==0 && (cnt[weight[node]]++ == 0))
ans++;
freq[node] ^= 1 ;
}
int32_t main()
{
fast;
//openin;
cin >> N >> M ;
BLOCK = sqrt(N) ;
rep(i,1,N)cin >> weight[i] ;
compress() ;
rep(i,2,N)
{
int u,v ;
cin >> u >> v ;
graph[u].pb(v) ;
graph[v].pb(u) ;
}
memset(P,-1,sizeof(P));
DFS() ;
pre(N) ;
rep(i,1,M)
{
int u,v ;
cin >> u >> v ;
queries[i].index = i ;
int lca = LCA(u,v) ;
// u should be upper
if(depth[u] > depth[v])swap(u,v) ;
if(lca==u || lca==v)
{
// first case
queries[i].L = start[u] ;
queries[i].R = start[v] ;
queries[i].flag = -1 ;
}
else
{
queries[i].L = endd[u] ;
queries[i].R = start[v] ;
queries[i].flag = start[lca] ;
}
}
sort(queries+1 , queries+1+M , compare) ;
ans = 0 ;
int curl = queries[1].L , curr = queries[1].R ;
//trace(curl,curr);
rep(i,curl,curr)
check(i);
answers[queries[1].index] = ans ;
if(queries[1].flag != -1) {
check(queries[1].flag) ;
answers[queries[1].index] = ans ;
check(queries[1].flag) ;
}
rep(i,2,M)
{
while(curl < queries[i].L)
check(curl++) ;
while(curl > queries[i].L)
check(--curl) ;
while(curr < queries[i].R)
check(++curr) ;
while(curr > queries[i].R)
check(curr--) ;
answers[queries[i].index] = ans ;
if(queries[i].flag != -1) {
check(queries[i].flag) ;
answers[queries[i].index] = ans ;
check(queries[i].flag) ;
}
}
rep(i,1,M)cout << answers[i] << '\n' ;
return 0;
}
LyoKeWUgbWVyYSB0ZW1wbGF0ZSBoYWkKYXBuYSBraHVkIGxpa2hvIGJjIDpQCiovCgovKgpBdXRob3IgOiBTYXJ2YWd5YSBBZ2Fyd2FsCiovCgojaW5jbHVkZTxiaXRzL3N0ZGMrKy5oPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKLy9kZWZpbmVzCiNkZWZpbmUgb3BlbmluIGZyZW9wZW4oImlucHV0LnR4dCIsInIiLHN0ZGluKQojZGVmaW5lIG9wZW5vdXQgZnJlb3Blbigib3V0cHV0LnR4dCIsInciLHN0ZG91dCkKI2RlZmluZSBmYXN0IGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oZmFsc2UpO2Npbi50aWUoMCk7Y291dC50aWUoMCkKI2RlZmluZSBsbCBsb25nIGxvbmcKI2RlZmluZSBpbnQgbG9uZyBsb25nCiNkZWZpbmUgbW9kIDEwMDAwMDAwMDcKI2RlZmluZSByZXAoaSx4LHkpIGZvciAoX190eXBlb2YoeCkgaT14Oyh4PD15P2k8PXk6aT49eSk7aT0oeDw9eT9pKzE6aS0xKSkKI2RlZmluZSBhbGwoYykgKGMpLmJlZ2luKCksKGMpLmVuZCgpCiNkZWZpbmUgZmYgZmlyc3QKI2RlZmluZSBzcyBzZWNvbmQKI2RlZmluZSBwYiBwdXNoX2JhY2sKI2RlZmluZSBtcCBtYWtlX3BhaXIKCmludCBwb3dlcihpbnQgYSAsIGludCBiKQp7CiAgICBpbnQgcmVzID0gMSA7CiAgICB3aGlsZShiKQogICAgewogIC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IERGUyhpbnQgbm9kZSA9IDEgLCBpbnQgcGFyID0gMCAsIGludCBkID0gMCkKewogICAgc3RhcnRbbm9kZV0gPSB0aW1lciA7CiAgICBhcnJbdGltZXIrK10gPSBub2RlIDsKICAgIGRlcHRoW25vZGVdID0gZCA7CiAgICBQW25vZGVdWzBdID0gcGFyIDsKICAgIGZvcihhdXRvIGNoaWxkIDogZ3JhcGhbbm9kZV0pIHsKICAgICAgICBpZihjaGlsZCAhPSBwYXIpIHsKICAgICAgICAgICAgREZTKGNoaWxkLG5vZGUsZCsxKSA7CiAgICAgICAgfQogICAgfQogICAgZW5kZFtub2RlXSA9IHRpbWVyIDsKICAgIGFyclt0aW1lcisrXSA9IG5vZGUgOwp9CnZvaWQgcHJlKGludCBuKQp7CiAgICBmb3IoaW50IGo9MTsoMTw8aik8bjtqKyspCiAgICB7CiAgICAgICAgcmVwKGksMSxuKQogICAgICAgIHsKICAgICAgICAgICAgaWYoUFtpXVtqLTFdIT0tMSkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgUFtpXVtqXSA9IFBbUFtpXVtqLTFdXVtqLTFdOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQp9CmludCBMQ0EoaW50IHAsaW50IHEpCnsKICAgIGludCB0ZW1wIDsKICAgIGlmKGRlcHRoW3BdIDwgZGVwdGhbcV0pCiAgICAgICAgdGVtcD1wLHA9cSxxPXRlbXAgOwogICAgaW50IGxvZyA7CiAgICBmb3IobG9nPTE7KDE8PGxvZyk8PWRlcHRoW3BdO2xvZysrKSA7CiAgICBsb2ctLTsKICAgIGZvcihpbnQgaT1sb2c7aT49MDtpLS0pCiAgICB7CiAgICAgICAgaWYoZGVwdGhbcF0gLSAoMTw8aSkgPj0gZGVwdGhbcV0pCiAgICAgICAgICAgIHA9UFtwXVtpXTsKICAgIH0KICAgIGlmKHA9PXEpcmV0dXJuIHA7CiAgICAvL25vdyBjb21wdXRlIGxjYSB1c2luZyBkcCB0YWJsZShQKQogICAgZm9yKG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ICAgIGZhc3Q7CiAgICAvL29wZW5pbjsKICAgIGNpbiA+PiBOID4+IE0gOwogICAgQkxPQ0sgPSBzcXJ0KE4pIDsKICAgIHJlcChpLDEsTiljaW4gPj4gd2VpZ2h0W2ldIDsKICAgIGNvbXByZXNzKCkgOwogICAgcmVwKGksMixOKQogICAgewogICAgICAgIGludCB1LHYgOwogICAgICAgIGNpbiA+PiB1ID4+IHYgOwogICAgICAgIGdyYXBoW3VdLnBiKHYpIDsKICAgICAgICBncmFwaFt2XS5wYih1KSA7CiAgICB9CiAgICBtZW1zZXQoUCwtMSxzaXplb2YoUCkpOwogICAgREZTKCkgOwogICAgcHJlKE4pIDsKICAgIHJlcChpLDEsTSkKICAgIHsKICAgICAgICBpbnQgdSx2IDsKICAgICAgICBjaW4gPj4gdSA+PiB2IDsKICAgICAgICBxdWVyaWVzW2ldLmluZGV4ID0gaSA7CiAgICAgICAgaW50IGxjYSA9IExDQSh1LHYpIDsKICAgICAgICAvLyB1IHNob3VsZCBiZSB1cHBlcgogICAgICAgIGlmKGRlcHRoW3VdID4gZGVwdGhbdl0pc3dhcCh1LHYpIDsKICAgICAgICBpZihsY2E9PXUgfHwgbGNhPT12KQogICAgICAgIHsKICAgICAgICAgICAgLy8gZmlyc3QgY2FzZQogICAgICAgICAgICBxdWVyaWVzW2ldLkwgPSBzdGFydFt1XSA7CiAgICAgICAgICAgIHF1ZXJpZXNbaV0uUiA9IHN0YXJ0W3ZdIDsKICAgICAgICAgICAgcXVlcmllc1tpXS5mbGFnID0gLTEgOwogICAgICAgIH0KICAgICAgICBlbHNlCiAgICAgICAgewogICAgICAgICAgICBxdWVyaWVzW2ldLkwgPSBlbmRkW3VdIDsKICAgICAgICAgICAgcXVlcmllc1tpXS5SID0gc3RhcnRbdl0gOwogICAgICAgICAgICBxdWVyaWVzW2ldLmZsYWcgPSBzdGFydFtsY2FdIDsKICAgICAgICB9CiAgICB9CiAgICBzb3J0KHF1ZXJpZXMrMSAsIHF1ZXJpZXMrMStNICwgY29tcGFyZSkgOwogICAgYW5zID0gMCA7CiAgICBpbnQgY3VybCA9IHF1ZXJpZXNbMV0uTCAsIGN1cnIgPSBxdWVyaWVzWzFdLlIgOwogICAgLy90cmFjZShjdXJsLGN1cnIpOwogICAgcmVwKGksY3VybCxjdXJyKQogICAgICAgIGNoZWNrKGkpOwogICAgYW5zd2Vyc1txdWVyaWVzWzFdLmluZGV4XSA9IGFucyA7CiAgICBpZihxdWVyaWVzWzFdLmZsYWcgIT0gLTEpIHsKICAgICAgICBjaGVjayhxdWVyaWVzWzFdLmZsYWcpIDsKICAgICAgICBhbnN3ZXJzW3F1ZXJpZXNbMV0uaW5kZXhdID0gYW5zIDsKICAgICAgICBjaGVjayhxdWVyaWVzWzFdLmZsYWcpIDsKICAgIH0KICAgIHJlcChpLDIsTSkKICAgIHsKICAgICAgICB3aGlsZShjdXJsIDwgcXVlcmllc1tpXS5MKQogICAgICAgICAgICBjaGVjayhjdXJsKyspIDsKICAgICAgICB3aGlsZShjdXJsID4gcXVlcmllc1tpXS5MKQogICAgICAgICAgICBjaGVjaygtLWN1cmwpIDsKICAgICAgICB3aGlsZShjdXJyIDwgcXVlcmllc1tpXS5SKQogICAgICAgICAgICBjaGVjaygrK2N1cnIpIDsKICAgICAgICB3aGlsZShjdXJyID4gcXVlcmllc1tpXS5SKQogICAgICAgICAgICBjaGVjayhjdXJyLS0pIDsKICAgICAgICBhbnN3ZXJzW3F1ZXJpZXNbaV0uaW5kZXhdID0gYW5zIDsKICAgICAgICBpZihxdWVyaWVzW2ldLmZsYWcgIT0gLTEpIHsKICAgICAgICAgICAgY2hlY2socXVlcmllc1tpXS5mbGFnKSA7CiAgICAgICAgICAgIGFuc3dlcnNbcXVlcmllc1tpXS5pbmRleF0gPSBhbnMgOwogICAgICAgICAgICBjaGVjayhxdWVyaWVzW2ldLmZsYWcpIDsKICAgICAgICB9CiAgICB9CiAgICByZXAoaSwxLE0pY291dCA8PCBhbnN3ZXJzW2ldIDw8ICdcbicgOwogICAgcmV0dXJuIDA7Cn0K