#include <bits/stdc++.h>
#define rep(i, a, b) for(int i = (a); i <= (b); i++)
#define pre(i, a, b) for(int i = (a); i >= (b); i--)
#define Ede(i, u) for(int i = h[u]; i; i = ne[i])
#define go(i, a) for(auto i : a)
#define LL long long
#define ULL unsigned long long
#define PII pair<int, int>
#define PIL pair<int, long long>
#define PLI pair<long long, int>
#define PLL pair<long long, long long>
#define mp make_pair
#define eb emplace_back
#define opb pop_back
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define sf scanf
#define prf printf
#define el putchar('\n')
#define mms(arr, n) memset(arr, n, sizeof(arr))
#define mmc(arr1, arr2) memcpy(arr1, arr2, sizeof(arr2))
const int inf = 0x3f3f3f3f;
template <typename T> inline void rd(T &x){
x = 0; bool f = true; char ch = getchar();
while(ch < '0' || ch > '9'){ if(ch == '-') f = false; ch = getchar();}
while(ch >= '0' && ch <= '9'){ x = (x << 1) + (x << 3) + (ch ^ '0'); ch = getchar();}
if(!f) x = -x;
}
template <typename T, typename ...Args> inline void rd(T &x, Args &...args){ rd(x); rd(args...);}
using namespace std;
const int N = 100010, M = N << 1;
int h[N], e[N], ne[N], idx;
void add(int a, int b){
e[++idx] = b, ne[idx] = h[a], h[a] = idx++;
}
int n, m;
int siz;
int a[N];
int ds[M], cnt;
int s[N], t[N];
int fa[N][20], dep[N];
int ans[M], res;
void dfs(int u){
dep[u] = dep[fa[u][0]] + 1;
ds[++cnt] = u, s[u] = cnt;
for(int i = 1; i < 20; i++) fa[u][i] = fa[fa[u][i - 1]][i - 1];
Ede(i, u){
int v = e[i];
if(v != fa[u][0]) fa[v][0] = u, dfs(v);
}
ds[++cnt] = u, t[u] = cnt;
}
int LCA(int x, int y){
if(dep[x] < dep[y]) swap(x, y);
for(int i = 19; i >= 0; i--){
if(dep[fa[x][i]] >= dep[y]) x = fa[x][i];
}
if(x == y) return x;
for(int i = 19; i >= 0; i++){
if(fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];
}
return fa[x][0];
}
struct Qu{
int l, r;
int id, x;
}qu[N];
bool cmp(Qu a, Qu b){
if(a.l / siz != b.l / siz) return a.l < b.l;
else return a.r < b.r;
}
int vis[N];
int ccnt[N];
void modify(int x){
if(!vis[x]){
vis[x]++;
ccnt[a[x]]++;
if(ccnt[a[x]] == 1) res++;
}else{
vis[x]--;
ccnt[a[x]]--;
if(!ccnt[a[x]]) res--;
}
}
int main(){
rd(n, m);
siz = sqrt(m);
rep(i, 1, n) rd(a[i]);
for(int i = 1, x, y; i <= m; i++){
rd(x, y);
add(x, y), add(y, x);
}
dfs(1);
for(int i = 1, u, v, lca; i <= m; i++){
rd(u, v); lca = LCA(u, v);
if(u == lca || v == lca){
if(s[u] > s[v]) swap(u, v);
qu[i].l = s[u], qu[i].r = s[v];
}else{
if(s[v] > t[u]) swap(u, v);
qu[i].l = t[u], qu[i].r = s[v];
qu[i].x = lca;
}
}
sort(qu + 1, qu + m + 1, cmp);
for(int i = 1, l = qu[1].l, r = qu[1].l - 1; i <= m; i++){
while(l < qu[i].l) modify(ds[l++]);
while(r > qu[i].r) modify(ds[r--]);
while(l > qu[i].l) modify(ds[--l]);
while(r < qu[i].r) modify(ds[++r]);
if(qu[i].x) modify(qu[i].x);
ans[qu[i].id] = res;
if(qu[i].x) modify(qu[i].x);
}
rep(i, 1, m) prf("%d\n", ans[i]);
return 0;
}