using System; using System.Collections.Generic; using System.Collections; using System.Collections.Specialized; using System.Linq; using System.Text; using System.IO; using System.Reflection; using static System.Math; using System.Numerics; using System.Threading; using System.Runtime.CompilerServices; using System.Diagnostics; static class Program{ const int mod=(int)1e9+7; static List[] li; static void Main(){ Sc sc=new Sc(); var s=sc.Ia; int n=s[0],m=n-1; n=(n<<1)-1; li=new List[n+1]; var h=new long[m][]; var b=new bool[n+1]; for(int i=1;i<=n;i++){li[i]=new List();} for(int i=0,j=m+2;iet.fa[h[i][1]]?h[i][0]:h[i][1]); for(int j = 0;jet.fa[li[f][j]]){r[f]=li[f][j];} } st.Add(et.ixl2[r[f]],et.ixl2[r[f]],h[i][2]); } var q=sc.I; StringBuilder sb=new StringBuilder(); for(int i = 0;i>1]++; } for(int i = cnt-n-1;i>1;i--) {ha[i>>1]+=ha[i];} } public Lst(int[] a){ n=a.Length; while(m0;i--) { if(i<<1=a&&b>=l){ Flz(k); Fad(a,b,k<<1,l,l+ha[k<<1]-1,d1); Fad(a,b,(k<<1)+1,l+ha[k<<1],r,d1); dat[k]=Dt.Cm(dat[k<<1],dat[(k<<1)+1]); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] private void Flz(int k){ if(bo[k]&&ha[k]>1){ if(ha[k<<1]>0){ dat[k<<1]=Dt.Lm(dat[k<<1],dat[k].lz1); dat[k<<1]=Dt.Um(dat[k<<1],dat[k].lz1,ha[k<<1]); bo[k<<1]=true; } if(ha[(k<<1)+1]>0){ dat[(k<<1)+1]=Dt.Lm(dat[(k<<1)+1],dat[k].lz1); dat[(k<<1)+1]=Dt.Um(dat[(k<<1)+1],dat[k].lz1,ha[(k<<1)+1]); bo[(k<<1)+1]=true; } dat[k]=Dt.Cl(dat[k]); bo[k]=false; } } } public class Et{ private List[] li; public int[] at2,ixl2,ixr2,fa; private int f=1,f2=1; public Et(List[] li,int o){ int n=li.Length; this.li=li; at2=new int[n]; ixl2=new int[n]; ixr2=new int[n]; fa=new int[n+1]; Fu1(o,1); } private void Fu1(int a,int g){ at2[f2]=a; ixl2[a]=f2; f++;f2++; fa[a]=g; for(int i=0;i(string s){return (T)Convert.ChangeType(s,typeof(T));} public virtual int I{get{return int.Parse(Rl());}} public virtual long L{get{return long.Parse(Rl());}} public virtual double D{get{return double.Parse(Rl());}} public virtual string S{get{return Rl();}} public int[] Ia{get{return Array.ConvertAll(Sp(Rl()),int.Parse);}} public long[] La{get{return Array.ConvertAll(Sp(Rl()),long.Parse);}} public double[] Da{get{return Array.ConvertAll(Sp(Rl()),double.Parse);}} public string[] Sa{get{return Sp(Rl());}} public object[] Oa{get{return Sp(Rl());}} public int[] Ia2{get{return Array.ConvertAll(Sp("0 "+Rl()+" 0"),int.Parse);}} public int[] Ia3(string a,string b){return Array.ConvertAll(Sp(a+Rl()+b),int.Parse);} public int[] Ia3(int a){return Array.ConvertAll(Sp(Rl()+" "+a.ToString()),int.Parse);} public long[] La2{get{return Array.ConvertAll(Sp("0 "+Rl()+" 0"),long.Parse);}} public long[] La3(string a,string b){return Array.ConvertAll(Sp(a+Rl()+b),long.Parse);} public long[] La3(int a){return Array.ConvertAll(Sp(Rl()+" "+a.ToString()),long.Parse);} public double[] Da2{get{return Array.ConvertAll(Sp("0 "+Rl()+" 0"),double.Parse);}} public double[] Da3(string a,string b){return Array.ConvertAll(Sp(a+Rl()+b),double.Parse);} public T[] Arr(int n,Func f){var a=new T[n];for(int i=0;i(int n,Func f){var a=new T[n];for(int i=0;i(int n,Func f){var a=new T[n];for(int i=0;i(int n,Func f){var a=new T[n];for(int i=0;i(){var s=Sp(Rl());return (Ct(s[0]),Ct(s[1]));} [MethodImpl(MethodImplOptions.AggressiveInlining)] public (T,T,T) Tp3(){var s=Sp(Rl());return (Ct(s[0]),Ct(s[1]),Ct(s[2]));} [MethodImpl(MethodImplOptions.AggressiveInlining)] public (T,T,T,T) Tp4(){var s=Sp(Rl());return (Ct(s[0]),Ct(s[1]),Ct(s[2]),Ct(s[3]));} [MethodImpl(MethodImplOptions.AggressiveInlining)] public (T,T,T,T,T) Tp5(){var s=Sp(Rl());return (Ct(s[0]),Ct(s[1]),Ct(s[2]),Ct(s[3]),Ct(s[4]));} [MethodImpl(MethodImplOptions.AggressiveInlining)] public (T,T,T,T,T,T) Tp6(){var s=Sp(Rl());return (Ct(s[0]),Ct(s[1]),Ct(s[2]),Ct(s[3]),Ct(s[4]),Ct(s[5]));} [MethodImpl(MethodImplOptions.AggressiveInlining)] public (T1,T2) Tp2(){var s=Sp(Rl());return (Ct(s[0]),Ct(s[1]));} [MethodImpl(MethodImplOptions.AggressiveInlining)] public (T1,T1,T2) Tp3(){var s=Sp(Rl());return (Ct(s[0]),Ct(s[1]),Ct(s[2]));} } public class Scs:Sc{ private StreamReader sr; public Scs(string t){sr=new StreamReader(t);} [MethodImpl(MethodImplOptions.AggressiveInlining)] protected override string Rl(){return sr.ReadLine();} } public class Sc2:Sc{ private string[] sps=new string[]{" "," ","\t"}; [MethodImpl(MethodImplOptions.AggressiveInlining)] protected override string[] Sp(string st){return st.Split(sps,StringSplitOptions.RemoveEmptyEntries);} public override int I{get{return int.Parse(Sp(Rl())[0]);}} public override long L{get{return long.Parse(Sp(Rl())[0]);}} public override double D{get{return double.Parse(Sp(Rl())[0]);}} public override string S{get{return Sp(Rl())[0];}} } public class Scs2:Sc2{ private StreamReader sr; public Scs2(string t){sr=new StreamReader(t);} [MethodImpl(MethodImplOptions.AggressiveInlining)] protected override string Rl(){return sr.ReadLine();} } public class Sct:Sc{ private List li=new List(); private int l=0; public void Add(int s){li.Add(s.ToString());} public void Add(long s){li.Add(s.ToString());} public void Add(double s){li.Add(s.ToString());} public void Add(string s){li.Add(s.ToString());} public void Add(object s){li.Add(s.ToString());} public void Add(int[] s){li.Add(string.Join(" ",s));} public void Add(long[] s){li.Add(string.Join(" ",s));} public void Add(double[] s){li.Add(string.Join(" ",s));} public void Add(string[] s){li.Add(string.Join(" ",s));} public void Add(object[] s){li.Add(string.Join(" ",s));} protected override string Rl(){return li[l++];} public void Clear(){li.Clear();l=0;} }