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.*;

public class E {
    static 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;
        }
    }

    static FastReader s = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);

    private static int[] rai(int n) {
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = s.nextInt();
        }
        return arr;
    }

    private static int[][] rai(int n, int m) {
        int[][] arr = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                arr[i][j] = s.nextInt();
            }
        }
        return arr;
    }

    private static long[] ral(int n) {
        long[] arr = new long[n];
        for (int i = 0; i < n; i++) {
            arr[i] = s.nextLong();
        }
        return arr;
    }

    private static long[][] ral(int n, int m) {
        long[][] arr = new long[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                arr[i][j] = s.nextLong();
            }
        }
        return arr;
    }

    private static int ri() {
        return s.nextInt();
    }

    private static long rl() {
        return s.nextLong();
    }

    private static String rs() {
        return s.next();
    }

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

    static int gcd(int a,int b)
    {
        if(b==0)
        {
            return a;
        }
        return gcd(b,a%b);
    }

    static boolean isPrime(int n) {
        //check if n is a multiple of 2
        if (n % 2 == 0) return false;
        //if not, then just check the odds
        for (int i = 3; i <= Math.sqrt(n); i += 2) {
            if (n % i == 0)
                return false;
        }
        return true;
    }
    static int MOD=1000000007;

    static long mpow(long base,long pow)
    {
        long res=1;
        while(pow>0)
        {
            if(pow%2==1)
            {
                res=(res*base)%MOD;
            }
            pow>>=1;
            base=(base*base)%MOD;
        }
        return res;
    }

    static enum Type
    {
        T1, T2,T3, T4
    }


    static class Node
    {
        String start,end;
        long count;
        long len ;

        public Node(String start, String end, long count, long len) {
            this.start = start;
            this.end = end;
            this.count = count;
            this.len = len;
        }

        @Override
        public String toString() {
            return "("+start+" "+end+" "+count+")";
        }
    }

    public static void main(String[] args) {
        StringBuilder ans = new StringBuilder();
        int t = ri();
//        int t = 1;
//        int tc = 0;
        while (t-- > 0)
        {
//            tc++;
            int n = ri();
//            if(tc == 66)
//            {
//                System.out.println(n);
//            }
            HashMap<String,Node> map = new HashMap<>();
            String last = "";
            for(int i=0;i<n;i++)
            {
                String vals = s.nextLine();
                String[] str = vals.split(" ");
//                if(tc==66)
//                {
//                    System.out.println(vals);
//                }
//                System.out.println(Arrays.toString(str));
                last = str[0];
                if(str.length==3)
                {
                    String in = str[2];
                    String st = "";
                    if(in.length()>=3)
                    {
//                         && in.charAt(0)=='a' && in.charAt(1)=='h' && in.charAt(2)=='a'
                        st = in.substring(0,3);
                    }
                    else if(in.length()>=2)
                    {
//                         && in.charAt(0)=='h' && in.charAt(1)=='a'
                        st = in.substring(0,2);
                    }
                    else if(in.length()>=1 )
                    {
//                        && in.charAt(0)=='a'
                        st = in.substring(0,1);
                    }
                    String end = "";
                    if(in.length()>=3 )
                    {
//                        && in.charAt(in.length()-3)=='h' && in.charAt(in.length()-2)=='a' && in.charAt(in.length()-1)=='h'
                        end = in.substring(in.length()-3);
                    }
                    else if(in.length()>=2 )
                    {
//                        && in.charAt(in.length()-2)=='h' && in.charAt(in.length()-1)=='a'
                        end = in.substring(in.length()-2);
                    }
                    else if(in.length()>=1 )
                    {
//                        && in.charAt(in.length()-1)=='h'
                        end = in.substring(in.length()-1);
                    }
                    int count = 0;
                    for(int j=0;j<=in.length()-4;j++)
                    {
                        String mid = in.substring(j,j+4);
                        if(mid.equals("haha"))
                        {
                            count++;
                        }
                    }

                    map.put(last, new Node(st,end,count,last.length()));
                }
                else
                {
                    Node first = map.get(str[2]);
                    Node sec = map.get(str[4]);
                    String st = first.start;
                    if(st.length()==2)
                    {
                        st += sec.start.charAt(0);
                    }
                    else if(st.length()==1)
                    {
                        st+=sec.start.charAt(0);
                        if(sec.start.length()>1)
                        {
                            st+=sec.start.charAt(1);
                        }
                    }

                    String end = sec.end;
                    if(end.length()==2)
                    {
                        end = first.end.charAt(first.end.length()-1)+end;
                    }
                    else if(end.length()==1)
                    {
                        end = first.end.charAt(first.end.length()-1)+end;
                        if(first.end.length()>1)
                        {
                            end = first.end.charAt(first.end.length()-2)+end;
                        }
                    }

                    Node node = new Node(st,end,first.count+sec.count, first.len+sec.len);

                    if(first.end.endsWith("h") && sec.start.startsWith("aha"))
                    {
                        node.count++;
                    }
                    if(first.end.endsWith("ha") && sec.start.startsWith("ha"))
                    {
                        node.count++;
                    }
                    if(first.end.endsWith("hah") && sec.start.startsWith("a"))
                    {
                        node.count++;
                    }

                    map.put(last, node);

                }
//                System.out.println(map);
            }

            ans.append(map.get(last).count).append("\n");
        }
        out.print(ans.toString());
        out.flush();
    }



}