Polycarp came up with a new programming language. There are only two types of statements in it: 

  * "x := s": assign the variable named x the value s (where s is a string). For example, the statement var := hello assigns the variable named var the value hello. Note that s is the value of a string, not the name of a variable. Between the variable name, the := operator and the string contains exactly one space each.
  * "x = a + b": assign the variable named x the concatenation of values of two variables a and b. For example, if the program consists of three statements a := hello, b := world, c = a + b, then the variable c will contain the string helloworld. It is guaranteed that the program is correct and the variables a and b were previously defined. There is exactly one space between the variable names and the = and + operators. 



All variable names and strings only consist of lowercase letters of the English alphabet and do not exceed 5 characters.

The result of the program is the number of occurrences of string haha in the string that was written to the variable in the last statement.

Polycarp was very tired while inventing that language. He asks you to implement it. Your task is — for given program statements calculate the number of occurrences of string haha in the last assigned variable.

Input

The first line contains an integer t (1 ≤ t ≤ 10^3). Then t test cases follow.

The first line of each test case contains a single integer n (1 ≤ n ≤ 50) — the number of statements in the program. All variable names and strings are guaranteed to consist only of lowercase letters of the English alphabet and do not exceed 5 characters.

This is followed by n lines describing the statements in the format described above. It is guaranteed that the program is correct.

Output

For each set of input data, output the number of occurrences of the haha substring in the string that was written to the variable in the last statement.

Example

Input


4
6
a := h
b := aha
c = a + b
c = c + c
e = c + c
d = a + c
15
x := haha
x = x + x
x = x + x
x = x + x
x = x + x
x = x + x
x = x + x
x = x + x
x = x + x
x = x + x
x = x + x
x = x + x
x = x + x
x = x + x
x = x + x
1
haha := hah
5
haahh := aaaha
ahhhh = haahh + haahh
haahh = haahh + haahh
ahhhh = ahhhh + haahh
ahhaa = haahh + ahhhh


Output


3
32767
0
0

Note

In the first test case the resulting value of d is hhahahaha.
import java.io.*;
import java.util.*;
import java.math.*;
import java.math.BigInteger;


public final class A
{    
	static PrintWriter out = new PrintWriter(System.out); 
	static StringBuilder ans=new StringBuilder();
	static FastReader in=new FastReader();
	static ArrayList<ArrayList<Integer>> g;
	static long mod=(long)1e9+7,INF=Long.MAX_VALUE;
	static boolean set[],col[]; 
	static int par[],tot[],partial[];
	static int D[],P[][];
	static long dp[][],max=0;
	static int s=0;
	static int maxN=0,edge[];
	static int cnt[]=new int[(int)(1e7)+5];
	static ArrayList<Integer> A=new ArrayList<>();
	static HashMap<String ,edge> mp;
	//static pair moves[]= {new pair(-1,0),new pair(1,0), new pair(0,-1), new pair(0,1)};
	public static void main(String args[])throws IOException
	{				
		int T=i();
		outer:while(T-->0)
		{
			int N=i();
			mp=new HashMap<>();
			String last="";
			StringTokenizer str;
			for(int i=0; i<N; i++)
			{
				str=new StringTokenizer(in.nextLine());
				int j=0;
				boolean type1=false;
				String var="",a="",b="";
                 while(str.hasMoreElements())
                 {
                	 String X=str.nextToken();
                	 if(j==0)var=X;
                	 if(j==1)
                	 {
                		 if(X.charAt(0)==':')type1=true;
                	 }
                	 if(j==2)
                	 {
                		a=X; 
                	 }
                	 if(j==4)b=X;               	
                	 j++;
                 }
                 if(type1)
                 {
                	 int count=countHaha(a);                	 
                	 mp.put(var, new edge(count,left(a),right(a)));
                 }
                 else
                 {
                	 int x=combine(a,b);
                	 edge y=new edge(mp.get(a).a+mp.get(b).a+x,left(a,b),right(a,b));       	 
                	 mp.put(var,y);
                 }
                 last=var;
                // System.out.println(var+" "+type1+" "+a+" "+b);
			}
			out.println(mp.get(last).a);
		}
		out.close();
	}
	static String left(String X,String Y)
	{
		String A=mp.get(X).l+mp.get(Y).l;
		return left(A);
		
	}
	static String right(String X,String Y)
	{
		String A=mp.get(X).r+mp.get(Y).r;
		return right(A);
	}
	static String left(String X)
	{
		return X.substring(0,Math.min(3,X.length()));
	}
	static String right(String X)
	{
		int N=X.length();
		return X.substring(Math.max(0, N-3),N);
	}
	static int combine(String X,String Y)
	{
		//X is left and Y is right
		//3 1, 2 2, 1 3
		int c=0;		
		String r=mp.get(X).r,l=mp.get(Y).l;
		if(r.length()>=3 && l.length()>=1)
		{
			String str=r+l.charAt(0);
			if(str.equals("haha"))c++;
		}
		if(r.length()>=2 && l.length()>=2)
		{
			int sz=r.length();
			String str=r.charAt(sz-2)+""+r.charAt(sz-1)+""+l.charAt(0)+""+l.charAt(1);
			if(str.equals("haha"))c++;
		}
		if(r.length()>=1 && l.length()>=3)
		{
			int sz=r.length();
			String str=r.charAt(sz-1)+""+l;
			if(str.equals("haha"))c++;
		}
		return c;
	}
	static int countHaha(String X)
	{
		int c=0;
		for(int i=0; i<X.length()-3; i++)
		{
			String y=X.charAt(i)+""+X.charAt(i+1)+""+X.charAt(i+2)+""+X.charAt(i+3);
			if(y.equals("haha"))c++;
		}
		return c;
	}



	static int bin(int x,ArrayList<Integer> A)
	{
		int l=0,r=A.size()-1;
		while(l<=r)
		{
			int m=(l+r)/2;
			int a=A.get(m);
			if(a==x)return m;
			if(a<x)l=m+1;
			else r=m-1;
		}
		return 0;
	}
	static int left(int x,ArrayList<Integer> A)
	{
		int l=-1,r=A.size();
		while(r-l>1)
		{
			int m=(l+r)/2;
			int a=A.get(m);
			if(a<=x)l=m;
			else r=m;
		}
		return l;
	}
	static int right(int x,ArrayList<Integer> A)
	{
		int l=-1,r=A.size();
		while(r-l>1)
		{
			int m=(l+r)/2;
			int a=A.get(m);
			if(a<x)l=m;
			else r=m;
		}
		return r;
	}



	static boolean equal(long A[],long B[])
	{
		for(int i=0; i<A.length; i++)if(A[i]!=B[i])return false;
		return true;	
	}
	static int lower(long A[],long x)
	{
		int l=-1,r=A.length;
		while(r-l>1)
		{
			int m=(l+r)/2;
			if(A[m]>=x)r=m;
			else l=m;				
		}
		return r;
	}
	static int upper(long A[],long x)
	{
		int l=-1,r=A.length;
		while(r-l>1)
		{
			int m=(l+r)/2;
			if(A[m]<=x)l=m;
			else r=m;
		}
		return l;
	}

	static int max(int a ,int b,int c,int d)
	{
		a=Math.max(a, b);
		c=Math.max(c,d);
		return Math.max(a, c);
	}
	static int min(int a ,int b,int c,int d)
	{
		a=Math.min(a, b);
		c=Math.min(c,d);
		return Math.min(a, c);
	}

	static int find(int l,int r,long A[] ,long val)
	{
		int m=0;
		while(l<=r)
		{
			m=(l+r)/2;
			if(A[m]>=val)r=m-1;
			else l=m+1;
		}
		return m;
	}

	static HashMap<Integer,Integer> Hash(int A[])
	{
		HashMap<Integer,Integer> mp=new HashMap<>();
		for(int a:A)
		{
			int f=mp.getOrDefault(a,0)+1;
			mp.put(a, f);
		}
		return mp;
	}
	static long mul(long a, long b)
	{
		return ( a %mod * 1L * b%mod )%mod;
	}
	static void swap(int A[],int a,int b)
	{
		int t=A[a];
		A[a]=A[b];
		A[b]=t;
	}


	static int find(int a)
	{
		if(par[a]<0)return a;
		return par[a]=find(par[a]);
	}
	static void union(int a,int b)
	{
		a=find(a);
		b=find(b);
		if(a!=b)
		{
			par[a]+=par[b];
			par[b]=a;
		}
	}
	static boolean isSorted(int A[])
	{
		for(int i=1; i<A.length; i++)
		{
			if(A[i]<A[i-1])return false;
		}
		return true;
	}

	static boolean isDivisible(StringBuilder X,int i,long num)
	{
		long r=0;
		for(; i<X.length(); i++)
		{
			r=r*10+(X.charAt(i)-'0');
			r=r%num;
		}
		return r==0;
	}    



	static int lower_Bound(int A[],int low,int high, int x) 
	{ 
		if (low > high) 
			if (x >= A[high]) 
				return A[high]; 

		int mid = (low + high) / 2; 

		if (A[mid] == x) 
			return A[mid]; 

		if (mid > 0 && A[mid - 1] <= x && x < A[mid]) 
			return A[mid - 1]; 

		if (x < A[mid]) 
			return lower_Bound( A, low, mid - 1, x); 

		return lower_Bound(A, mid + 1, high, x); 
	} 

	static String f(String A)
	{
		String X="";
		for(int i=A.length()-1; i>=0; i--)
		{
			int c=A.charAt(i)-'0';
			X+=(c+1)%2;
		}
		return X;
	}

	static void sort(long[] a) //check for long
	{
		ArrayList<Long> l=new ArrayList<>();
		for (long i:a) l.add(i);
		Collections.sort(l);
		for (int i=0; i<a.length; i++) a[i]=l.get(i);
	}

	static String swap(String X,int i,int j)
	{
		char ch[]=X.toCharArray();
		char a=ch[i];
		ch[i]=ch[j];
		ch[j]=a;
		return new String(ch);
	}

	static int sD(long n)  
	{  

		if (n % 2 == 0 )  
			return 2;  

		for (int i = 3; i * i <= n; i += 2) {  
			if (n % i == 0 )  
				return i;  
		}  

		return (int)n;  
	}  

	static void setGraph(int N)
	{
		tot=new int[N+1];
		partial=new int[N+1];
		D=new int[N+1];
		P=new int[N+1][(int)(Math.log(N)+10)];
		set=new boolean[N+1];
		g=new ArrayList<ArrayList<Integer>>();
		for(int i=0; i<=N; i++)
		{

			g.add(new ArrayList<>());		
			D[i]=0;
			//D2[i]=INF;
		}
	}



	static  long pow(long a,long b)
	{
		//long mod=1000000007;
		long pow=1;
		long x=a;
		while(b!=0)
		{
			if((b&1)!=0)pow=(pow*x)%mod;
			x=(x*x)%mod;
			b/=2;
		}
		return pow;
	}

	static long toggleBits(long x)//one's complement || Toggle bits
	{
		int n=(int)(Math.floor(Math.log(x)/Math.log(2)))+1;

		return ((1<<n)-1)^x;
	}

	static int countBits(long a)
	{
		return (int)(Math.log(a)/Math.log(2)+1);
	}

	static long fact(long N)
	{ 
		long n=2; 
		if(N<=1)return 1;
		else
		{
			for(int i=3; i<=N; i++)n=(n*i)%mod;
		}
		return n;
	}

	static int kadane(int A[])
	{
		int lsum=A[0],gsum=A[0];
		for(int i=1; i<A.length; i++)
		{
			lsum=Math.max(lsum+A[i],A[i]);
			gsum=Math.max(gsum,lsum);
		}
		return gsum;
	}

	static void sort(int[] a) {
		ArrayList<Integer> l=new ArrayList<>();
		for (int i:a) l.add(i);
		Collections.sort(l);
		for (int i=0; i<a.length; i++) a[i]=l.get(i);
	}

	static boolean isPrime(long N)
	{
		if (N<=1)  return false; 
		if (N<=3)  return true; 
		if (N%2 == 0 || N%3 == 0) return false; 
		for (int i=5; i*i<=N; i=i+6) 
			if (N%i == 0 || N%(i+2) == 0) 
				return false; 
		return true; 
	}
	static void print(char A[])
	{
		for(char c:A)System.out.print(c+" ");
		System.out.println();
	}
	static void print(boolean A[])
	{
		for(boolean c:A)System.out.print(c+" ");
		System.out.println();
	}
	static void print(int A[])
	{
		for(int a:A)System.out.print(a+" ");
		System.out.println();	
	}
	static void print(long A[])
	{
		for(long i:A)System.out.print(i+ " ");
		System.out.println();

	}
	static void print(ArrayList<Integer> A)
	{
		for(int a:A)System.out.print(a+" ");
		System.out.println();
	}

	static int i()
	{
		return in.nextInt();
	}

	static long l()
	{
		return in.nextLong();
	}

	static int[] input(int N){
		int A[]=new int[N];
		for(int i=0; i<N; i++)
		{
			A[i]=in.nextInt();
		}
		return A;
	}

	static long[] inputLong(int N)     {
		long A[]=new long[N];
		for(int i=0; i<A.length; i++)A[i]=in.nextLong();
		return A;
	}

	static long GCD(long a,long b) 
	{
		if(b==0)
		{
			return a;
		}
		else return GCD(b,a%b );
	}

}
class edge 
{
	long a;
	String l,r;
	edge(long a,String l,String r)
	{
		this.a=a;
		this.l=l;
		this.r=r;
	}
}


//Code For FastReader
//Code For FastReader
//Code For FastReader
//Code For FastReader
class FastReader
{
	BufferedReader br;
	StringTokenizer st;
	public FastReader()
	{
		br=new BufferedReader(new InputStreamReader(System.in));
	}

	String next()
	{
		while(st==null || !st.hasMoreElements())
		{
			try
			{
				st=new StringTokenizer(br.readLine());
			}
			catch(IOException e)
			{
				e.printStackTrace();
			}
		}
		return st.nextToken();
	}

	int nextInt()
	{
		return Integer.parseInt(next());
	}

	long nextLong()
	{
		return Long.parseLong(next());
	}

	double nextDouble()
	{
		return Double.parseDouble(next());
	}

	String nextLine()
	{
		String str="";
		try
		{
			str=br.readLine();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		return str;
	}

}