/*
███████ ██████ ███ ██ ██ ██████ ██ ██ ██ ██ ██ ██
██ ██ ██ ████ ██ ██ ██ ██ ██ ██ ██ ██ ██
███████ ██ ██ ██ ██ ██ ██ ██ ███████ ███████ ███████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
███████ ██████ ██ ████ ██ ██████ ██ ██ ██
*/
#include <bits/stdc++.h>
using namespace std;
#define sonic ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl "\n"
#define gcd __gcd
#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define acc(x) accumulate(all(x),0LL)
#define fixed(n) fixed << setprecision(n)
#define ll long long
#define all(v) v.begin(),v.end()
#define mod 1000000007
#define mx(v) *max_element(all(v))
#define mn(v) *min_element(all(v))
#define mxi(v) max_element(all(v))-v.begin()
#define mni(v) min_element(all(v))-v.begin()
#define cin(vec) for(auto &i : vec) cin >> i
#define cout(vec) for(auto& i : vec) cout << i << " "; cout << '\n';
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
vector<ll> in, out, dffs;
struct query {
int l, r, k, q_idx, block_idx;
query() {}
int SQ = 320;
query(int _l, int _r, ll _k, int _q_idx) {
l = _l - 1, r = _r - 1, k = _k, q_idx = _q_idx, block_idx = _l / SQ;
}
bool operator<(const query &y) const {
if (block_idx != y.block_idx) {
return block_idx < y.block_idx;
}
return r < y.r;
}
};
vector<ll> col;
class MO {
public:
static const int N = 10000005;
static const int Q = 300005;
static const int SQ = 320;
vector<ll> v;
int mx = 0;
query queries[Q];
ll n, q, res = 0, q_ans[Q], f[3000005] = {0}, ff[N] = {0};
MO(vector<ll> &vv, vector<pair<pair<ll, ll>, ll>> &lr) { // 1 indexed l,r
q = lr.size(), n = vv.size();
v = vv;
for (int i = 0; i < lr.size(); ++i)queries[i] = query(lr[i].first.first, lr[i].first.second, lr[i].second, i);
mo_process();
}
void add(int i, ll k) {
// ff[f[col[v[i]]]]--;
f[col[v[i]]]++;
ff[f[col[v[i]]]]++;
}
void remove(int i, ll k) {
ff[f[col[v[i]]]]--;
f[col[v[i]]]--;
ff[f[col[v[i]]]]++;
}
void mo_process() {
sort(queries, queries + q);
int l = 1, r = 0;
for (int i = 0; i < q; i++) {
while (r < queries[i].r)add(++r, queries[i].k);
while (r > queries[i].r)remove(r--, queries[i].k);
while (l < queries[i].l)remove(l++, queries[i].k);
while (l > queries[i].l)add(--l, queries[i].k);
q_ans[queries[i].q_idx] = ff[f[queries[i].k]];
}
}
};
const int N = 1e5 + 5;
vector<ll> adj[N];
int timer;
void dfs(ll node, ll p) {
in[node] = timer++;
dffs[timer] = node;
for (auto i: adj[node]) {
if (i == p)continue;
dfs(i, node);
}
out[node] = timer;
}
void sonic444() {
ll n, q;
cin >> n >> q;
col.resize(n);
cin(col);
in.resize(n + 5);
out.resize(n + 5);
dffs.resize(n + 5);
for (int i = 0; i < n - 1; ++i) {
ll u, v;
cin >> u >> v;
adj[u].emplace_back(v);
adj[v].emplace_back(u);
}
dfs(1,0);
// cout(dffs);
vector<pair<pair<ll, ll>, ll>> lr;
for (int i = 0; i < q; ++i) {
ll s, k;
cin >> s >> k;
lr.push_back({{in[s]+1, out[s]}, k});
}
MO mo(dffs, lr);
for (int i = 0; i < q; ++i) {
cout << mo.q_ans[i] << endl;
}
}
int main() {
// freopen("input1.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
sonic
//////////////////////////////////////////////
int t = 1;
//cin>>t;
while (t--) {
sonic444();
}
return 0;
}
LyoK4paI4paI4paI4paI4paI4paI4paIICDilojilojilojilojilojiloggIOKWiOKWiOKWiCAgICDilojilogg4paI4paIICDilojilojilojilojilojilogg4paI4paIICAg4paI4paIIOKWiOKWiCAgIOKWiOKWiCDilojiloggICDilojilogK4paI4paIICAgICAg4paI4paIICAgIOKWiOKWiCDilojilojilojiloggICDilojilogg4paI4paIIOKWiOKWiCAgICAgIOKWiOKWiCAgIOKWiOKWiCDilojiloggICDilojilogg4paI4paIICAg4paI4paICuKWiOKWiOKWiOKWiOKWiOKWiOKWiCDilojiloggICAg4paI4paIIOKWiOKWiCDilojiloggIOKWiOKWiCDilojilogg4paI4paIICAgICAg4paI4paI4paI4paI4paI4paI4paIIOKWiOKWiOKWiOKWiOKWiOKWiOKWiCDilojilojilojilojilojilojilogKICAgICDilojilogg4paI4paIICAgIOKWiOKWiCDilojiloggIOKWiOKWiCDilojilogg4paI4paIIOKWiOKWiCAgICAgICAgICAg4paI4paIICAgICAg4paI4paIICAgICAg4paI4paICuKWiOKWiOKWiOKWiOKWiOKWiOKWiCAg4paI4paI4paI4paI4paI4paIICDilojiloggICDilojilojilojilogg4paI4paIICDilojilojilojilojilojiloggICAgICDilojiloggICAgICDilojiloggICAgICDilojilogKKi8KI2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwojZGVmaW5lIHNvbmljIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oMCk7Y2luLnRpZSgwKTtjb3V0LnRpZSgwKTsKI2RlZmluZSBlbmRsICAiXG4iCiNkZWZpbmUgZ2NkICBfX2djZAojZGVmaW5lIGFsbCh2KSB2LmJlZ2luKCksdi5lbmQoKQojZGVmaW5lIGFsbHIodikgdi5yYmVnaW4oKSx2LnJlbmQoKQojZGVmaW5lIGFjYyh4KSBhY2N1bXVsYXRlKGFsbCh4KSwwTEwpCiNkZWZpbmUgZml4ZWQobikgZml4ZWQgPDwgc2V0cHJlY2lzaW9uKG4pCiNkZWZpbmUgbGwgbG9uZyBsb25nCiNkZWZpbmUgYWxsKHYpIHYuYmVnaW4oKSx2LmVuZCgpCiNkZWZpbmUgbW9kIDEwMDAwMDAwMDcKI2RlZmluZSBteCh2KSAqbWF4X2VsZW1lbnQoYWxsKHYpKQojZGVmaW5lIG1uKHYpICptaW5fZWxlbWVudChhbGwodikpCiNkZWZpbmUgbXhpKHYpIG1heF9lbGVtZW50KGFsbCh2KSktdi5iZWdpbigpCiNkZWZpbmUgbW5pKHYpI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2ldLmwpcmVtb3ZlKGwrKywgcXVlcmllc1tpXS5rKTsKICAgICAgICAgICAgd2hpbGUgKGwgPiBxdWVyaWVzW2ldLmwpYWRkKC0tbCwgcXVlcmllc1tpXS5rKTsKICAgICAgICAgICAgcV9hbnNbcXVlcmllc1tpXS5xX2lkeF0gPSBmZltmW3F1ZXJpZXNbaV0ua11dOwogICAgICAgIH0KICAgIH0KCn07Cgpjb25zdCBpbnQgTiA9IDFlNSArIDU7Cgp2ZWN0b3I8bGw+IGFkaltOXTsKaW50IHRpbWVyOwoKdm9pZCBkZnMobGwgbm9kZSwgbGwgcCkgewogICAgaW5bbm9kZV0gPSB0aW1lcisrOwogICAgZGZmc1t0aW1lcl0gPSBub2RlOwogICAgZm9yIChhdXRvIGk6IGFkaltub2RlXSkgewogICAgICAgIGlmIChpID09IHApY29udGludWU7CiAgICAgICAgZGZzKGksIG5vZGUpOwogICAgfQogICAgb3V0W25vZGVdID0gdGltZXI7Cn0KCnZvaWQgc29uaWM0NDQoKSB7CiAgICBsbCBuLCBxOwogICAgY2luID4+IG4gPj4gcTsKICAgIGNvbC5yZXNpemUobik7CiAgICBjaW4oY29sKTsKCiAgICBpbi5yZXNpemUobiArIDUpOwogICAgb3V0LnJlc2l6ZShuICsgNSk7CiAgICBkZmZzLnJlc2l6ZShuICsgNSk7CiAgICBmb3IgKGludCBpID0gMDsgaSA8IG4gLSAxOyArK2kpIHsKICAgICAgICBsbCB1LCB2OwogICAgICAgIGNpbiA+PiB1ID4+IHY7CiAgICAgICAgYWRqW3VdLmVtcGxhY2VfYmFjayh2KTsKICAgICAgICBhZGpbdl0uZW1wbGFjZV9iYWNrKHUpOwogICAgfQogICAgZGZzKDEsMCk7CiAgICAvLyBjb3V0KGRmZnMpOwogICAgdmVjdG9yPHBhaXI8cGFpcjxsbCwgbGw+LCBsbD4+IGxyOwogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBxOyArK2kpIHsKICAgICAgICBsbCBzLCBrOwogICAgICAgIGNpbiA+PiBzID4+IGs7CiAgICAgICAgbHIucHVzaF9iYWNrKHt7aW5bc10rMSwgb3V0W3NdfSwga30pOwogICAgfQogICAgTU8gbW8oZGZmcywgbHIpOwoKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgcTsgKytpKSB7CiAgICAgICAgY291dCA8PCBtby5xX2Fuc1tpXSA8PCBlbmRsOwogICAgfQoKCn0KCmludCBtYWluKCkgewogICAgLy8gZnJlb3BlbigiaW5wdXQxLnR4dCIsICJyIiwgc3RkaW4pOwogICAgLy8gICAgZnJlb3Blbigib3V0cHV0LnR4dCIsICJ3Iiwgc3Rkb3V0KTsKICAgIHNvbmljCiAgICAvLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vCgogICAgaW50IHQgPSAxOwogICAgLy9jaW4+PnQ7CiAgICB3aGlsZSAodC0tKSB7CiAgICAgICAgc29uaWM0NDQoKTsKICAgIH0KICAgIHJldHVybiAwOwp9