/*To The End Of The Galaxy*/
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<complex>
#include<iomanip>
#include<stack>
#include<map>
#include<set>
#include<cmath>
#define debug(x) cerr<<#x<<"="<<x<<endl
#define INF 0x7f7f7f7f
#define llINF 0x7fffffffffffll
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
inline int init()
{
int now=0,ju=1;char c;bool flag=false;
while(1)
{
c=getchar();
if(c==‘-‘)ju=-1;
else if(c>=‘0‘&&c<=‘9‘)
{
now=now*10+c-‘0‘;
flag=true;
}
else if(flag)return now*ju;
}
}
inline long long llinit()
{
long long now=0,ju=1;char c;bool flag=false;
while(1)
{
c=getchar();
if(c==‘-‘)ju=-1;
else if(c>=‘0‘&&c<=‘9‘)
{
now=now*10+c-‘0‘;
flag=true;
}
else if(flag)return now*ju;
}
}
int n,q;
struct edge
{
int from,to,val,pre;
}Edge[400005];
int head[200005],fa[200005],top[200005],son[200005],depth[200005],dfn[200005],cnt,dfs_time,size[200005];
void predfs(int now,int f,int d)
{
size[now]++;
fa[now]=f;
depth[now]=d;
for(int j=head[now];j;j=Edge[j].pre)
{
if(f!=Edge[j].to)
{
predfs(Edge[j].to,now,d+1);
size[now]+=size[Edge[j].to];
if(!son[now]||size[son[now]]<size[Edge[j].to])
{
son[now]=Edge[j].to;
}
}
}
return;
}
void nextdfs(int now,int chain)
{
top[now]=chain;
dfn[now]=++dfs_time;
if(son[now])nextdfs(son[now],chain);
for(int j=head[now];j;j=Edge[j].pre)
{
if(Edge[j].to!=son[now]&&Edge[j].to!=fa[now])
{
nextdfs(Edge[j].to,Edge[j].to);
}
}
return;
}
inline void addedge(int from,int to,int val)
{
++cnt;
Edge[cnt]=((edge){from,to,val,head[from]});
head[from]=cnt;
}
//----------------tree cut--------------------------
struct segment
{
int l,r;
int sum,max,min,tag;
}tree[1000005];
#define lson (now<<1)
#define rson (now<<1|1)
#define mid ((l+r)>>1)
void calc(int now,int delta)
{
int tmpmax,tmpmin;
tree[now].sum=-tree[now].sum;
tmpmax=tree[now].max;tmpmin=tree[now].min;
tree[now].min=-tmpmax;
tree[now].max=-tmpmin;
tree[now].tag^=delta;
}
void push_down(int now)
{
if(!tree[now].tag)return;
calc(lson,tree[now].tag);
calc(rson,tree[now].tag);
tree[now].tag=0;
}
void update(int now)
{
tree[now].sum=tree[lson].sum+tree[rson].sum;
tree[now].max=max(tree[lson].max,tree[rson].max);
tree[now].min=min(tree[lson].min,tree[rson].min);
}
void build(int l,int r,int now)
{
tree[now].l=l;tree[now].r=r;
if(l==r)return;
else
{
build(l,mid,lson);
build(mid+1,r,rson);
}
return;
}
void Modify(int l,int r,int pos,int now,int x)
{
push_down(now);
if(l==r)
{
tree[now].sum=x;
tree[now].min=x;
tree[now].max=x;
if(l==1)
{
tree[now].sum=0;tree[now].min=INF;tree[now].max=-INF;
}
return;
}
else
{
if(pos<=mid)Modify(l,mid,pos,lson,x);
else Modify(mid+1,r,pos,rson,x);
update(now);
}
return;
}
int QueryMax(int l,int r,int x,int y,int now)
{
push_down(now);
if(l==x&&r==y)
{
return tree[now].max;
}
else
{
if(y<=mid)return QueryMax(l,mid,x,y,lson);
else if(x>mid)return QueryMax(mid+1,r,x,y,rson);
else return max(QueryMax(l,mid,x,mid,lson),QueryMax(mid+1,r,mid+1,y,rson));
update(now);
}
}
int QueryMin(int l,int r,int x,int y,int now)
{
push_down(now);
if(l==x&&r==y)
{
return tree[now].min;
}
else
{
if(y<=mid)return QueryMin(l,mid,x,y,lson);
else if(x>mid)return QueryMin(mid+1,r,x,y,rson);
else return min(QueryMin(l,mid,x,mid,lson),QueryMin(mid+1,r,mid+1,y,rson));
update(now);
}
}
int QuerySum(int l,int r,int x,int y,int now)
{
push_down(now);
if(l==x&&r==y)
{
return tree[now].sum;
}
else
{
if(y<=mid)return QuerySum(l,mid,x,y,lson);
else if(x>mid)return QuerySum(mid+1,r,x,y,rson);
else return QuerySum(l,mid,x,mid,lson)+QuerySum(mid+1,r,mid+1,y,rson);
update(now);
}
}
void AddTag(int l,int r,int x,int y,int now,int delta)
{
push_down(now);
if(l==x&&r==y)calc(now,delta);
else
{
if(y<=mid)AddTag(l,mid,x,y,lson,delta);
else if(x>mid)AddTag(mid+1,r,x,y,rson,delta);
else AddTag(l,mid,x,mid,lson,delta),AddTag(mid+1,r,mid+1,y,rson,delta);
update(now);
}
}
//---------------------Segment End------------------
int SolveMax(int x,int y)
{
int ans=-INF;
while(top[x]!=top[y])
{
if(depth[top[x]]<depth[top[y]])swap(x,y);
ans=max(QueryMax(1,n,dfn[top[x]],dfn[x],1),ans);
x=fa[top[x]];
}
if(x==y)return ans;
if(dfn[x]>dfn[y])swap(x,y);
ans=max(ans,QueryMax(1,n,dfn[x]+1,dfn[y],1));
return ans;
}
int SolveMin(int x,int y)
{
int ans=INF;
while(top[x]!=top[y])
{
if(depth[top[x]]<depth[top[y]])swap(x,y);
ans=min(QueryMin(1,n,dfn[top[x]],dfn[x],1),ans);
x=fa[top[x]];
}
if(x==y)return ans;
if(dfn[x]>dfn[y])swap(x,y);
ans=min(ans,QueryMin(1,n,dfn[x]+1,dfn[y],1));
return ans;
}
int SolveSum(int x,int y)
{
int ans=0;
while(top[x]!=top[y])
{
if(depth[top[x]]<depth[top[y]])swap(x,y);
ans+=QuerySum(1,n,dfn[top[x]],dfn[x],1);
x=fa[top[x]];
}
if(x==y)return ans;
if(dfn[x]>dfn[y])swap(x,y);
ans+=QuerySum(1,n,dfn[x]+1,dfn[y],1);
return ans;
}
void ModifyPoint(int x,int y)
{
Modify(1,n,dfn[x],1,y);
return;
}
void ModifyLine(int x,int y)
{
while(top[x]!=top[y])
{
if(depth[top[x]]<depth[top[y]])swap(x,y);
AddTag(1,n,dfn[top[x]],dfn[x],1,1);
x=fa[top[x]];
}
if(x==y)return;
if(dfn[x]>dfn[y])swap(x,y);
AddTag(1,n,dfn[x]+1,dfn[y],1,1);
return;
}
int main()
{
int a,b,c;
n=init();
for(int i=1;i<n;i++)
{
a=init();b=init();c=init();
a++;b++;
addedge(a,b,c);
addedge(b,a,c);
}
predfs(1,0,0);
nextdfs(1,1);
//解决方案 放到较深的那个点里 这题有不需要lca
//下午cdq分治
int p;
for(int i=1;i<=cnt;i+=2)
{
if(depth[Edge[i].from]>depth[Edge[i].to])
{
p=Edge[i].from;
}
else p=Edge[i].to;
ModifyPoint(p,Edge[i].val);
}
char type[15];
q=init();
build(1,n,1);
Modify(1,n,1,1,0);
for(int i=1;i<=q;i++)
{
scanf("%s",type+1);
a=init();b=init();a++;b++;
if(strcmp(type+1,"SUM")==0)
{
printf("%d\n",SolveSum(a,b));
}
else if(strcmp(type+1,"MAX")==0)
{
printf("%d\n",SolveMax(a,b));
}
else if(strcmp(type+1,"MIN")==0)
{
printf("%d\n",SolveMin(a,b));
}
else if(strcmp(type+1,"C")==0)
{
a--;a<<=1;a--;b--;
if(depth[Edge[a].from]>depth[Edge[a].to])
{
p=Edge[a].from;
}
else p=Edge[a].to;
ModifyPoint(p,b);
}
else if(strcmp(type+1,"N")==0)
{
ModifyLine(a,b);
}
}
return 0;
}