/* I will wait for you */ #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<ctime> #include<algorithm> #include<iostream> #include<fstream> #include<vector> #include<queue> #include<deque> #include<set> #include<map> #include<string> typedef long long LL; typedef unsigned long long ULL; using namespace std; const int maxn=50010; const int maxm=1010; const int maxs=26; const int INF=1<<29; const int P=1000000007; const double error=1e-9; struct node { int key,sum,min,max,rev,set; node *fa,*son[2]; int dir() { return this==fa->son[1]; } int isroot() { return !fa||this!=fa->son[0]&&this!=fa->son[1]; } }no[maxn]; inline void pushdown(node* o) { if(o->rev) { o->rev=0;swap(o->son[0],o->son[1]); for(int i=0;i<2;i++) if(o->son[i]) o->son[i]->rev^=1; } if(o->set) { o->set=0;o->key=-o->key;o->sum=-o->sum; swap(o->min,o->max);o->min=-o->min;o->max=-o->max; for(int i=0;i<2;i++) if(o->son[i]) o->son[i]->set^=1; } } inline void maintain(node* o) { if(o->key!=INF&&o->key!=-INF) o->sum=o->min=o->max=o->key; else o->sum=0,o->min=INF,o->max=-INF; for(int i=0;i<2;i++) if(o->son[i]) { pushdown(o->son[i]); o->son[i]->fa=o; o->sum+=o->son[i]->sum; o->max=max(o->max,o->son[i]->max); o->min=min(o->min,o->son[i]->min); } } inline void rotate(node* o) { node* p=o->fa; pushdown(p);pushdown(o); int d=o->dir(); p->son[d]=o->son[d^1]; o->son[d^1]=p;o->fa=p->fa; if(!p->isroot()) p->fa->son[p->dir()]=o; maintain(p);maintain(o); } inline void splay(node* o) { pushdown(o); while(!o->isroot()) { node* p=o->fa; if(p->isroot()) rotate(o); else if(o->dir()==p->dir()) rotate(p),rotate(o); else rotate(o),rotate(o); } } inline void access(node* o) { for(node* t=0;o;t=o,o=o->fa) splay(o),o->son[1]=t,maintain(o); } inline void evert(node* o) { access(o);splay(o);o->rev^=1; } inline void change(node* o,int k) { access(o);splay(o);o->key=k;maintain(o); } inline void qset(node* u,node* v) { evert(u);access(v);splay(v);v->set^=1; } inline void link(node* u,node* v) { evert(u);u->fa=v; } inline void qmax(node* u,node* v) { evert(u);access(v);splay(v);printf("%d\n",v->max); } inline void qmin(node* u,node* v) { evert(u);access(v);splay(v);printf("%d\n",v->min); } inline void qsum(node* u,node* v) { evert(u);access(v);splay(v);printf("%d\n",v->sum); } inline void make(node &o,int k) { o.key=o.sum=o.min=o.max=k; o.rev=o.set=0;o.fa=o.son[0]=o.son[1]=0; } int main() { int n,cnt=0;scanf("%d",&n); for(int i=0;i<n;i++) make(no[i],INF); for(int i=1,u,v,w;i<n;i++) { scanf("%d%d%d",&u,&v,&w);make(no[n+i],w); link(&no[n+i],&no[u]);link(&no[n+i],&no[v]); } int m,u,v;scanf("%d",&m); while(m--) { char s[10];scanf("%s%d%d",s,&u,&v); if(s[0]=='C') change(&no[n+u],v); else if(s[0]=='N') qset(&no[u],&no[v]); else if(s[0]=='S') qsum(&no[u],&no[v]); else if(s[0]=='M'&&s[1]=='A') qmax(&no[u],&no[v]); else if(s[0]=='M'&&s[1]=='I') qmin(&no[u],&no[v]); } return 0; }