1 #include<cstdio>
2 #include<iostream>
3 #include<math.h>
4 using namespace std;
5 const int maxn=20233;
6 const int inf=1033333333;
7 struct zs{
8 int c[2],fa,max,min,val,sum;
9 bool rev,revval;
10 }tree[maxn<<1];
11 int stack[maxn<<1];
12 int i,j,n,m,x,y;
13 char id[233];
14 inline void deal(int x){
15 tree[x].sum=-tree[x].sum;tree[x].val=-tree[x].val;
16 tree[x].max=-tree[x].max;tree[x].min=-tree[x].min;
17 swap(tree[x].max,tree[x].min);
18 tree[x].revval^=1;
19 }
20 inline bool isroot(int x){return tree[tree[x].fa].c[0]!=x&&tree[tree[x].fa].c[1]!=x;
21 }
22 inline void pushdown(int x){
23 if(!tree[x].rev&&!tree[x].revval)return;
24 int l=tree[x].c[0],r=tree[x].c[1];
25 if(tree[x].revval){
26 if(l)deal(l);if(r)deal(r);
27 tree[x].revval^=1;
28 }
29 if(tree[x].rev){
30 if(l)tree[l].rev^=1;if(r)tree[r].rev^=1;tree[x].rev^=1;
31 swap(tree[x].c[0],tree[x].c[1]);
32 }
33 }
34 inline void update(int x){
35 int l=tree[x].c[0],r=tree[x].c[1];
36 tree[x].max=max(tree[l].max,tree[r].max);tree[x].min=min(tree[l].min,tree[r].min);
37 if(x>n)tree[x].max=max(tree[x].max,tree[x].val),tree[x].min=min(tree[x].min,tree[x].val);
38 tree[x].sum=tree[l].sum+tree[r].sum+tree[x].val;
39 }
40 void rotate(int x){
41 int fa=tree[x].fa,gfa=tree[fa].fa;
42 if(!isroot(fa))tree[gfa].c[tree[gfa].c[1]==fa]=x;
43 int l=tree[fa].c[1]==x,r=l^1;
44 tree[fa].c[l]=tree[x].c[r];tree[x].c[r]=fa;
45 tree[fa].fa=x;tree[x].fa=gfa;tree[tree[fa].c[l]].fa=fa;
46 update(fa);update(x);
47 }
48 void splay(int x){
49 int top=0,tmp=x;stack[++top]=x;
50 while(!isroot(tmp))stack[++top]=tree[tmp].fa,tmp=tree[tmp].fa;
51 while(top)pushdown(stack[top]),top--;
52 int fa,gfa;
53 while(!isroot(x)){
54 fa=tree[x].fa;gfa=tree[fa].fa;
55 if(!isroot(fa))
56 if((tree[fa].c[0]==x)^(tree[gfa].c[0]==fa))rotate(x);
57 else rotate(fa);
58 rotate(x);
59 }
60 }
61 inline void access(int x){
62 int son=0;
63 while(x){
64 splay(x);tree[x].c[1]=son;
65 update(x);son=x;x=tree[x].fa;
66 }
67 }
68 inline void makeroot(int x){
69 access(x);splay(x);tree[x].rev^=1;
70 }
71 inline void link(int x,int y){
72 makeroot(x);tree[x].fa=y;
73 }
74 inline void reverval(int x,int y){makeroot(x);access(y);splay(y);deal(y);
75 }
76 inline void change(int x,int val){splay(x);tree[x].val=val;update(x);
77 }
78 inline int querysum(int x,int y){makeroot(x);access(y);splay(y);return tree[y].sum;
79 }
80 inline int querymax(int x,int y){makeroot(x);access(y);splay(y);return tree[y].max;
81 }
82 inline int querymin(int x,int y){makeroot(x);access(y);splay(y);return tree[y].min;
83 }
84 int main(){
85 scanf("%d",&n);
86 for(i=1;i<=n;i++)tree[i].max=-inf,tree[i].min=inf;
87 tree[0].min=inf,tree[0].max=-inf;
88 for(i=n+1;i<n+n;i++){
89 scanf("%d%d%d",&x,&y,&tree[i].val);tree[i].sum=tree[i].min=tree[i].max=tree[i].val;
90 x++;y++;
91 link(x,i);link(y,i);
92 }
93 scanf("%d",&m);
94 while(m--){
95 scanf("%s%d%d",id,&x,&y);
96 if(id[0]==‘C‘)change(x+n,y);
97 if(id[0]==‘N‘)reverval(x+1,y+1);
98 if(id[0]==‘S‘)printf("%d\n",querysum(x+1,y+1));
99 if(id[0]==‘M‘&&id[1]==‘A‘)printf("%d\n",querymax(x+1,y+1));
100 if(id[0]==‘M‘&&id[1]==‘I‘)printf("%d\n",querymin(x+1,y+1));
101 }
102 return 0;
103 }