#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int N = 1e6 + 5;
// Cây Fenwick dùng để cập nhật và tính tổng nhanh trong O(log)
struct FenwickTree
{
int n, a[N];
FenwickTree()
{
memset(a, 0, sizeof a);
}
void Update(int p, int v)
{
for (; p <= n; p += p & -p)
a[p] += v;
}
int Get(int p)
{
int ans = 0;
for (; p; p -= p & -p)
ans += a[p];
return ans;
}
int Get(int l, int r)
{
return Get(r) - Get(l - 1);
}
} f;
int n, q;
vector<int> adj[N];
int a[N];
int in[N], out[N], l;
int par[N][20], ranks[N];
struct com
{
bool operator()(const int &x, const int &y) const
{
return in[x] < in[y];
}
};
multiset<int, com> s[N]; // s[x] chứa các đỉnh có bông hoa có màu x,
// dùng multiset vì một đỉnh có thể xuất hiện nhiều lần
void Read()
{
cin >> n;
for (int i = 1; i <= n; ++i)
cin >> a[i];
for (int i = 1; i < n; ++i)
{
int u, v;
cin >> u >> v;
adj[u].emplace_back(v);
adj[v].emplace_back(u);
}
cin >> q;
}
// dfs để tạo ra dfs order (dfs decomposition)
void dfs(int v, int p = -1)
{
in[v] = ++l;
for (int i = 1; i < 20; ++i)
par[v][i] = par[par[v][i - 1]][i - 1];
for (auto i : adj[v])
if (i != p)
{
par[i][0] = v;
ranks[i] = ranks[v] + 1;
dfs(i, v);
}
out[v] = l;
}
int LCA(int u, int v)
{
if (ranks[u] < ranks[v])
swap(u, v);
for (int i = 19; ~i; --i)
if (ranks[par[u][i]] >= ranks[v])
u = par[u][i];
for (int i = 19; ~i; --i)
if (par[u][i] != par[v][i])
{
u = par[u][i];
v = par[v][i];
}
return u == v ? u : par[u][0];
}
// Mọc giữa đỉnh thứ v
// Một bông hoa màu x
void Add(int x, int v)
{
auto i = s[x].lower_bound(v);
auto j = prev(i);
f.Update(in[LCA(*i, *j)], 1);
f.Update(in[v], 1);
f.Update(in[LCA(*i, v)], -1);
f.Update(in[LCA(v, *j)], -1);
s[x].insert(v);
}
// Hoa v-màu hoa chóng tàn phái
// Chớm nở đỉnh v, hoa tàn hoa để cho ai...
void Erase(int x, int v)
{
auto i = s[x].lower_bound(v);
assert(i != s[x].end() && *i == v);
auto t = prev(i),
j = next(i);
f.Update(in[v], -1);
f.Update(in[LCA(v, *j)], 1);
f.Update(in[LCA(*t, v)], 1);
f.Update(in[LCA(*j, *t)], -1);
s[x].erase(i);
}
void Solve()
{
adj[n + 1].emplace_back(1);
adj[n + 1].emplace_back(n + 2);
ranks[n + 1] = 1;
dfs(n + 1);
f.n = l;
for (int i = 0; i < N; ++i)
{
s[i].insert(n + 2);
s[i].insert(n + 1);
}
for (int i = 1; i <= n; ++i)
if (a[i] != -1)
Add(a[i], i);
int ans = 0;
q *= 2;
while (q--)
{
string s;
int u, v;
cin >> s >> u >> v;
if (s == "BLOOM")
Add(u ^ ans, v ^ ans);
else if (s == "DISSOLVE")
Erase(u ^ ans, v ^ ans);
else
{
if (ranks[u] < ranks[v])
swap(u, v);
cout << (ans = f.Get(in[u], out[u])) << "\n";
}
}
}
int32_t main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
Read();
Solve();
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiAKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKdXNpbmcgbGwgPSBsb25nIGxvbmc7CiAKY29uc3RleHByIGludCBOID0gMWU2ICsgNTsKCi8vIEPDonkgRmVud2ljayBkw7luZyDEkeG7gyBj4bqtcCBuaOG6rXQgdsOgIHTDrW5oIHThu5VuZyBuaGFuaCB0cm9uZyBPKGxvZykKc3RydWN0IEZlbndpY2tUcmVlCnsKICAgIGludCBuLCBhW05dOwogICAgRmVud2lja1RyZWUoKQogICAgewogICAgICAgIG1lbXNldChhLCAwLCBzaXplb2YgYSk7CiAgICB9CiAKICAgIHZvaWQgVXBkYXRlKGludCBwLCBpbnQgdikKICAgIHsKICAgICAgICBmb3IgKDsgcCA8PSBuOyBwICs9IHAgJiAtcCkKICAgICAgICAgICAgYVtwXSArPSB2OwogICAgfQogCiAgICBpbnQgR2V0KGludCBwKQogICAgewogICAgICAgIGludCBhbnMgPSAwOwogICAgICAgIGZvciAoOyBwOyBwIC09IHAgJiAtcCkKICAgICAgICAgICAgYW5zICs9IGFbcF07CiAgICAgICAgcmV0dXJuIGFuczsKICAgIH0KIAogICAgaW50IEdldChpbnQgbCwgaW50IHIpCiAgICB7CiAgICAgICAgcmV0dXJuIEdldChyKSAtIEdldChsIC0gMSk7CiAgICB9Cn0gZjsKIAogCmludCBuLCBxOwp2ZWN0b3I8aW50PiBhZGpbTl07CmludCBhW05dOwppbnQgaW5bTl0sIG91dFtOXSwgbDsKaW50IHBhcltOXVsyMF0sIHJhbmtzW05dOwogCnN0cnVjdCBjb20KewogICAgYm9vbCBvcGVyYXRvcigpKGNvbnN0IGludCAmeCwgY29uc3QgaW50ICZ5KSBjb25zdAogICAgewogICAgICAgIHJldHVybiBpblt4XSA8IGluW3ldOwogICAgfQp9OwogCm11bHRpc2V0PGludCwgY29tPiBzW05dOyAvLyBzW3hdIGNo4bupYSBjw6FjIMSR4buJbmggY8OzIGLDtG5nIGhvYSBjw7MgbcOgdSB4LCAKICAgICAgICAgICAgICAgICAgICAgICAgIC8vIGTDuW5nIG11bHRpc2V0IHbDrCBt4buZdCDEkeG7iW5oIGPDsyB0aOG7gyB4deG6pXQgaGnhu4duIG5oaeG7gXUgbOG6p24KIAp2b2lkIFJlYWQoKQp7CiAgICBjaW4gPj4gbjsKIAogICAgZm9yIChpbnQgaSA9IDE7IGkgPD0gbjsgKytpKQogICAgICAgIGNpbiA+PiBhW2ldOwogCiAgICBmb3IgKGludCBpID0gMTsgaSA8IG47ICsraSkKICAgIHsKICAgICAgICBpbnQgdSwgdjsKICAgICAgICBjaW4gPj4gdSA+PiB2OwogICAgICAgIGFkalt1XS5lbXBsYWNlX2JhY2sodik7CiAgICAgICAgYWRqW3ZdLmVtcGxhY2VfYmFjayh1KTsKICAgIH0KIAogICAgY2luID4+IHE7Cn0KCi8vIGRmcyDEkeG7gyB04bqhbyByYSBkZnMgb3JkZXIgKGRmcyBkZWNvbXBvc2l0aW9uKQp2b2lkIGRmcyhpbnQgdiwgaW50IHAgPSAtMSkKewogICAgaW5bdl0gPSArK2w7CiAKICAgIGZvciAoaW50IGkgPSAxOyBpIDwgMjA7ICsraSkKICAgICAgICBwYXJbdl1baV0gPSBwYXJbcGFyW3ZdW2kgLSAxXV1baSAtIDFdOwogCiAgICBmb3IgKGF1dG8gaSA6IGFkalt2XSkKICAgICAgICBpZiAoaSAhPSBwKQogICAgICAgIHsKICAgICAgICAgICAgcGFyW2ldWzBdID0gdjsKICAgICAgICAgICAgcmFua3NbaV0gPSByYW5rc1t2XSArIDE7CiAgICAgICAgICAgIGRmcyhpLCB2KTsKICAgICAgICB9CiAKICAgIG91dFt2XSA9IGw7Cn0KIAppbnQgTENBKGludCB1LCBpbnQgdikKewogICAgaWYgKHJhbmtzW3VdIDwgcmFua3Nbdl0pCiAgICAgICAgc3dhcCh1LCB2KTsKIAogICAgZm9yIChpbnQgaSA9IDE5OyB+aTsgLS1pKQogICAgICAgIGlmIChyYW5rc1twYXJbdV1baV1dID49IHJhbmtzW3ZdKQogICAgICAgICAgICB1ID0gcGFyW3VdW2ldOwogCiAgICBmb3IgKGludCBpID0gMTk7IH5pOyAtLWkpCiAgICAgICAgaWYgKHBhclt1XVtpXSAhPSBwYXJbdl1baV0pCiAgICAgICAgewogICAgICAgICAgICB1ID0gcGFyW3VdW2ldOwogICAgICAgICAgICB2ID0gcGFyW3ZdW2ldOwogICAgICAgIH0KIAogICAgcmV0dXJuIHUgPT0gdiA/IHUgOiBwYXJbdV1bMF07Cn0KCi8vIE3hu41jIGdp4buvYSDEkeG7iW5oIHRo4bupIHYKLy8gTeG7mXQgYsO0bmcgaG9hIG3DoHUgeAp2b2lkIEFkZChpbnQgeCwgaW50IHYpCnsKICAgIGF1dG8gaSA9IHNbeF0ubG93ZXJfYm91bmQodik7CiAgICBhdXRvIGogPSBwcmV2KGkpOwogICAg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C0xKQogICAgICAgICAgICBBZGQoYVtpXSwgaSk7CiAKICAgIGludCBhbnMgPSAwOwogCiAgICBxICo9IDI7CiAKICAgIHdoaWxlIChxLS0pCiAgICB7CiAgICAgICAgc3RyaW5nIHM7CiAgICAgICAgaW50IHUsIHY7CiAgICAgICAgY2luID4+IHMgPj4gdSA+PiB2OwogCiAgICAgICAgaWYgKHMgPT0gIkJMT09NIikKICAgICAgICAgICAgQWRkKHUgXiBhbnMsIHYgXiBhbnMpOwogICAgICAgIGVsc2UgaWYgKHMgPT0gIkRJU1NPTFZFIikKICAgICAgICAgICAgRXJhc2UodSBeIGFucywgdiBeIGFucyk7CiAgICAgICAgZWxzZQogICAgICAgIHsKICAgICAgICAgICAgaWYgKHJhbmtzW3VdIDwgcmFua3Nbdl0pCiAgICAgICAgICAgICAgICBzd2FwKHUsIHYpOwogCiAgICAgICAgICAgIGNvdXQgPDwgKGFucyA9IGYuR2V0KGluW3VdLCBvdXRbdV0pKSA8PCAiXG4iOwogICAgICAgIH0KICAgIH0KfQogCmludDMyX3QgbWFpbigpCnsKICAgIGlvczo6c3luY193aXRoX3N0ZGlvKDApOwogICAgY2luLnRpZSgwKTsKICAgIGNvdXQudGllKDApOwogICAgUmVhZCgpOwogICAgU29sdmUoKTsKfQ==