1 #include<cstdio>
2 #include<iostream>
3 #include<cmath>
4 #include<cstring>
5 #include<algorithm>
6 using namespace std;
7 char ch; bool ok;
8 void read(int &x){
9 for (ok=0,ch=getchar();!isdigit(ch);ch=getchar()) if (ch==‘-‘) ok=1;
10 for (x=0;isdigit(ch);x=x*10+ch-‘0‘,ch=getchar());
11 if (ok) x=-x;
12 }
13 const int maxn=200005;
14 const int inf=0x3f3f3f3f;
15 int n,a,b,c,q;
16 char op[10];
17 struct LCT{
18 #define ls son[x][0]
19 #define rs son[x][1]
20 int fa[maxn],son[maxn][2],neg[maxn],rev[maxn],val[maxn],sum[maxn],res[maxn][2];//0:min 1:max
21 void init(){res[0][0]=inf,res[0][1]=-inf;}
22 int which(int x){return son[fa[x]][1]==x;}
23 bool isroot(int x){return son[fa[x]][0]!=x&&son[fa[x]][1]!=x;}
24 void addtag_rev(int x){if (x) rev[x]^=1,swap(ls,rs);}
25 void addtag_neg(int x){
26 if (x){
27 neg[x]^=1,val[x]=-val[x],sum[x]=-sum[x];
28 swap(res[x][0],res[x][1]),res[x][0]=-res[x][0],res[x][1]=-res[x][1];
29 }
30 }
31 void pushdown(int x){
32 if (rev[x]) addtag_rev(ls),addtag_rev(rs),rev[x]=0;
33 if (neg[x]) addtag_neg(ls),addtag_neg(rs),neg[x]=0;
34 }
35 void relax(int x){
36 if (!isroot(x)) relax(fa[x]);
37 pushdown(x);
38 }
39 void updata(int x){
40 sum[x]=sum[ls]+val[x]+sum[rs];
41 res[x][0]=min(res[ls][0],res[rs][0]);
42 res[x][1]=max(res[ls][1],res[rs][1]);
43 if (x>n) res[x][0]=min(res[x][0],val[x]),res[x][1]=max(res[x][1],val[x]);
44 }
45 void rotate(int x){
46 int y=fa[x],z=fa[y],d=which(x),dd=which(y);
47 fa[son[x][d^1]]=y,son[y][d]=son[x][d^1],fa[x]=fa[y];
48 if (!isroot(y)) son[z][dd]=x;
49 son[x][d^1]=y,fa[y]=x,updata(y);
50 }
51 void splay(int x){
52 relax(x);
53 while (!isroot(x)){
54 if (isroot(fa[x])) rotate(x);
55 else if (which(fa[x])==which(x)) rotate(fa[x]),rotate(x);
56 else rotate(x),rotate(x);
57 }
58 updata(x);
59 }
60 void access(int x){for (int p=0;x;x=fa[x]) splay(x),son[x][1]=p,p=x;}
61 void make_root(int x){access(x),splay(x),addtag_rev(x);}
62 void init(int id,int v){int x=n+id; val[x]=sum[x]=res[x][0]=res[x][1]=v;}
63 void connect(int u,int v,int id,int w){make_root(u),fa[u]=n+id,fa[n+id]=v,init(id,w);}
64 int query_sum(int u,int v){make_root(u),access(v),splay(v);return sum[v];}
65 int query_min(int u,int v){make_root(u),access(v),splay(v);return res[v][0];}
66 int query_max(int u,int v){make_root(u),access(v),splay(v);return res[v][1];}
67 void change(int x,int v){splay(x),val[x]=v,updata(x);}
68 void _neg(int u,int v){make_root(u),access(v),splay(v),addtag_neg(v);}
69 }T;
70 int main(){
71 read(n),T.init();
72 for (int i=1;i<n;i++) read(a),read(b),read(c),T.connect(++a,++b,i,c);
73 for (read(q);q;q--){
74 scanf("%s",op+1),read(a),read(b);
75 if (op[1]==‘C‘) T.change(n+a,b);
76 else if (op[1]==‘N‘) T._neg(++a,++b);
77 else if (op[1]==‘S‘) printf("%d\n",T.query_sum(++a,++b));
78 else if (op[2]==‘I‘) printf("%d\n",T.query_min(++a,++b));
79 else if (op[2]==‘A‘) printf("%d\n",T.query_max(++a,++b));
80 }
81 return 0;
82 }