#include <stdio.h>
#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <stdlib.h>
#include <math.h>
#include <cstring>
#include <algorithm>
#include <map>
#include <set>
using namespace std;
#define fi first
#define sc second
#define ff first.first
#define fs first.second
#define sf second.first
#define ss second.second
#define pb push_back
#define mp make_pair
#define ll long long
#define dl double
#define ison(a,b) (a&(1<<b))
#define bitcnt __builtin_popcount
#define MOD 1000000007
typedef pair<ll,ll> ii;
typedef pair<ll,ii> iii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef vector<iii> wadj;
const ll N=1e5+5;
const ll LN=18;
ll parent[N],sub_size[N],depth[N];
ll chead[N],cind[N];
ll cno,lazy[4*N],ptr,pib[N],bsa[N],a[N];
iii st[4*N];
vi adj[N];
ll gcd(ll a,ll b)
{
a=abs(a),b=abs(b);
if(b>a)
swap(a,b);
if(b==0)
return a;
else
return gcd(b,a%b);
}
ll nlca(ll x,ll y)
{
while(chead[cind[x]]!=chead[cind[y]])
{
if(depth[chead[cind[x]]] > depth[chead[cind[y]]])
x=parent[chead[cind[x]]];
else
y=parent[chead[cind[y]]];
}
if(depth[x]>depth[y])
return y;
else
return x;
}
iii build(ll p,ll s,ll e)
{
if(s==e)
{
return st[p]=iii(0,ii(bsa[s],bsa[s]));
}
build(2*p,s,(s+e)>>1),build(2*p+1,((s+e)>>1)+1,e);
st[p]=iii(abs(gcd(st[2*p].fi,gcd(st[2*p+1].sf-st[2*p].ss,st[2*p+1].fi))),ii(st[2*p].sf,st[2*p+1].ss));
return st[p];
}
void push(ll p,ll l,ll r)
{
if(lazy[p])
{
st[p].sf+=lazy[p];
st[p].ss+=lazy[p];
if(l!=r)
{
lazy[2*p]+=lazy[p];
lazy[2*p+1]+=lazy[p];
}
lazy[p]=0;
}
}
void update(ll p,ll s,ll e,ll L,ll R,ll val)
{
push(p,L,R);
if(L>e||s>R)
return;
if(L>=s&&e>=R)
{
lazy[p]+=val;
push(p,L,R);
return;
}
update(2*p,s,e,L,(L+R)>>1,val);
update(2*p+1,s,e,((L+R)>>1)+1,R,val);
st[p]=iii(gcd(st[2*p].fi,gcd(st[2*p+1].sf-st[2*p].ss,st[2*p+1].fi)),ii(st[2*p].sf,st[2*p+1].ss));
}
ll query_tree(ll p,ll s,ll e,ll L,ll R)
{
push(p,L,R);
if(L>e||R<s)
return 0;
if(L>=s&&R<=e)
return gcd(st[p].fi,st[p].sf);
return gcd(query_tree(2*p,s,e,L,((L+R)>>1)),query_tree(2*p+1,s,e,((L+R)>>1)+1,R));
}
ll query_up(ll u,ll v)
{
ll uch,vch=cind[v],ans=0;
while(1)
{
uch=cind[u];
if(uch==vch)
{
ll t=query_tree(1,pib[v],pib[u],1,ptr);
ans=gcd(ans,t);
break;
}
ll t=query_tree(1,pib[chead[uch]],pib[u],1,ptr);
ans=gcd(ans,t);
u=chead[uch];
u=parent[u];
}
return ans;
}
void change_up(ll u,ll v,ll val)
{
ll uch,vch=cind[v];
while(1)
{
uch=cind[u];
if(uch==vch)
{
update(1,pib[v],pib[u],1,ptr,val);
break;
}
update(1,pib[chead[uch]],pib[u],1,ptr,val);
u=chead[uch];
u=parent[u]; }
}
void query(ll u,ll v)
{
ll l=nlca(u,v);
ll q1=query_up(u,l);
ll q2=query_up(v,l);
printf("%lld\n",gcd(q1,q2));
}
void change(ll u,ll v,ll val)
{
//printf("change\n" );
ll l=nlca(u,v);
change_up(u,l,val);
change_up(v,l,val);
change_up(l,l,-val);
}
void dfs(ll u1,ll p)
{
parent[u1]=p;
sub_size[u1]=1;
for(ll i=0;i<adj[u1].size();i++)
{
ll v=adj[u1][i];
if(v!=p)
{
depth[v]=depth[u1]+1;
dfs(v,u1);
sub_size[u1]+=sub_size[v];
}
}
}
void hld(ll u,ll p)
{
if(chead[cno]==0)
chead[cno]=u;
cind[u]=cno;
pib[u]=ptr;
bsa[ptr]=a[u];
ptr++;
ll sc=-1,nc;
for(ll i=0;i<adj[u].size();i++)
if(adj[u][i]!=p)
{
if(sc==-1||sub_size[sc]<sub_size[adj[u][i]])
{
sc=adj[u][i];
}
}
if(sc!=-1)
hld(sc,u);
for(ll i=0;i<adj[u].size();i++)
if(adj[u][i]!=sc&&adj[u][i]!=p)
{
cno++;
hld(adj[u][i],u);
}
}
void pre()
{
depth[1]=0;
dfs(1,0);
cno=ptr=1;
hld(1,0);
ptr--;
build(1,1,ptr);
}
int main(int argc, char const *argv[])
{
//freopen("inp.txt","r",stdin);
ll n;
cin>>n;
for(ll i=1;i<n;i++)
{
ll a,b;
scanf("%lld%lld",&a,&b);
adj[a+1].pb(b+1);
adj[b+1].pb(a+1);
}
for(ll i=1;i<=n;i++)
scanf("%lld",&a[i]);
pre();
ll q;
cin>>q;
while(q--)
{
char c;
ll u,v,d;
cin>>c;
scanf("%lld%lld",&u,&v);
u++,v++;
if(c=='F')
query(u,v);
else
{
scanf("%lld",&d);
change(u,v,d);
}
}
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPHN0YWNrPgojaW5jbHVkZSA8cXVldWU+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPG1hdGguaD4KI2luY2x1ZGUgPGNzdHJpbmc+CiNpbmNsdWRlIDxhbGdvcml0aG0+CiNpbmNsdWRlIDxtYXA+CiNpbmNsdWRlIDxzZXQ+CiAKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKIAojZGVmaW5lIGZpIGZpcnN0CiNkZWZpbmUgc2Mgc2Vjb25kCiNkZWZpbmUgZmYgZmlyc3QuZmlyc3QKI2RlZmluZSBmcyBmaXJzdC5zZWNvbmQKI2RlZmluZSBzZiBzZWNvbmQuZmlyc3QKI2RlZmluZSBzcyBzZWNvbmQuc2Vjb25kCiNkZWZpbmUgcGIgcHVzaF9iYWNrCiNkZWZpbmUgbXAgbWFrZV9wYWlyCiNkZWZpbmUgbGwgbG9uZyBsb25nCiNkZWZpbmUgZGwgZG91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