#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <fstream>
#include <queue>
#define rep(i, l, r) for(int i = l; i <= r; i++)
#define down(i, l, r) for(int i = l; i >= r; i--)
#define MS 34567
#define MAX 1037471823
using namespace std;
struct node { int l, r, s, m, lch, rch; };
struct edge { int n, next; };
node nd[MS*5]; int t[MS], nc;
edge e[MS*2]; int fir[MS], ec;
int f[MS], s[MS], pt[MS], pd[MS], ps[MS], pc, be[MS], r[MS];
int n, w[MS], a, b, q;
char qt[123];
void Init()
{
int x, y;
scanf("%d", &n);
rep(i, 1, n-1)
{
scanf("%d%d", &x, &y);
ec++; e[ec].n = y; e[ec].next = fir[x]; fir[x] = ec;
ec++; e[ec].n = x; e[ec].next = fir[y]; fir[y] = ec;
}
rep(i, 1, n) scanf("%d", &w[i]);
}
void DFS(int x, int o)
{
o++; int p, a, b, m, ma;
p = fir[x]; s[x] = 1; m = 0;
while (p)
{
if (e[p].n != f[x])
{
a = e[p].n;
f[a] = x; DFS(a, o); s[x] += s[a];
if (s[a] > m) m = s[a], ma = a;
}
p = e[p].next;
}
p = fir[x]; be[x] = 0;
while (p)
{
if (e[p].n != f[x])
{
if (e[p].n == ma) be[x] = be[ma], r[x] = r[ma]+1; else
{
a = e[p].n; b = be[a];
pd[b] = o; ps[b] = r[a]; pt[b] = a;
}
}
p = e[p].next;
}
if (be[x] == 0) be[x] = ++pc, r[x] = 1;
}
void Build(int p, int l, int r)
{
nd[p].l = l, nd[p].r = r;
if (r-l > 0)
{
nd[p].lch = ++nc; Build(nd[p].lch, l, (l+r)/2);
nd[p].rch = ++nc; Build(nd[p].rch, (l+r)/2+1, r);
}
else nd[p].lch = nd[p].rch = 0;
}
void Change(int p, int x, int d)
{
if (nd[p].l == nd[p].r) nd[p].s = nd[p].m = d;
else
{
if (x <= (nd[p].l + nd[p].r) / 2) Change(nd[p].lch, x, d); else Change(nd[p].rch, x, d);
nd[p].m = max(nd[nd[p].lch].m, nd[nd[p].rch].m);
nd[p].s = nd[nd[p].lch].s + nd[nd[p].rch].s;
}
}
void Prepare()
{
pc = f[1] = 0;
DFS(1, 0);
pd[be[1]] = 0; ps[be[1]] = r[1]; pt[be[1]] = 1;
rep(i, 1, pc)
{
t[i] = ++nc;
Build(nc, 1, ps[i]);
}
rep(i, 1, n) Change(t[be[i]], r[i], w[i]);
}
int Asksum(int p, int x, int y)
{
if (x <= nd[p].l && nd[p].r <= y) return nd[p].s;
int ans = 0;
if (x <= (nd[p].l + nd[p].r) / 2) ans += Asksum(nd[p].lch, x, y);
if ((nd[p].l + nd[p].r) / 2+1 <= y) ans += Asksum(nd[p].rch, x, y);
return ans;
}
int Askmax(int p, int x, int y)
{
if (x <= nd[p].l && nd[p].r <= y) return nd[p].m;
int ans = -MAX;
if (x <= (nd[p].l + nd[p].r) / 2) ans = max(ans, Askmax(nd[p].lch, x, y));
if ((nd[p].l + nd[p].r) / 2+1 <= y) ans = max(ans, Askmax(nd[p].rch, x, y));
return ans;
}
int Qsum(int a, int b)
{
int ans = 0, x = be[a], y = be[b];
while (x != y)
{
if (pd[x] > pd[y])
{
ans += Asksum(t[x], r[a], ps[x]);
a = f[pt[x]]; x = be[a];
}
else
{
ans += Asksum(t[y], r[b], ps[y]);
b = f[pt[y]]; y = be[b];
}
}
ans += Asksum(t[x], min(r[a], r[b]), max(r[a], r[b]));
return ans;
}
int Qmax(int a, int b)
{
int ans = -MAX, x = be[a], y = be[b];
while (x != y)
{
if (pd[x] > pd[y])
{
ans = max(ans, Askmax(t[x], r[a], ps[x]));
a = f[pt[x]]; x = be[a];
}
else
{
ans = max(ans, Askmax(t[y], r[b], ps[y]));
b = f[pt[y]]; y = be[b];
}
}
ans = max(ans, Askmax(t[x], min(r[a], r[b]), max(r[a], r[b])));
return ans;
}
int main()
{
Init();
Prepare();
scanf("%d", &q);
rep(i, 1, q)
{
scanf("%s%d%d", qt, &a, &b);
if (qt[1] == 'M') printf("%d\n", Qmax(a, b));
else if (qt[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