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.util.*;
import java.io.*;

import static java.lang.Math.*;

public class Main
{
    static MyScanner scan;
    static PrintWriter pw;
    static long MOD = 1_000_000_007;
    static long INF = 2_000_000_000_000_000_000L;
    static long inf = 2_000_000_000;
    public static void main(String[] args) {
        new Thread(null, null, "_", 1 << 27) {
            public void run() {
                try {
                    solve();
                } catch (Exception e) {
                    e.printStackTrace();
                    System.exit(1);
                }
            }
        }.start();
    }

    static final String HAHA = "haha";
    static HashMap<String, Long> haha_count, length;
    static HashSet<String> contain_pref[][], contain_suff[][];
    static void solve() throws java.lang.Exception {
        //initIo(true, "");
        initIo(false, "");
        StringBuilder sb = new StringBuilder();

        int t = ni();
        while (t-->0) {
            haha_count = new HashMap<>();
            length = new HashMap<>();
            contain_pref = new HashSet[4][4];
            contain_suff = new HashSet[4][4];
            for(int i=0;i<4;i++) {
                for(int j=i;j<4;++j) {
                    contain_pref[i][j] = new HashSet<>();
                    contain_suff[i][j] = new HashSet<>();
                }
            }
            int q = ni();
            long ans = -1;
            for (int i=0;i<q;i++) {
                String s = nel();
                // pl("s: "+s);
                if(s.indexOf('+')==-1) {
                    String var_name = s.substring(0, s.indexOf(':') - 1);
                    String value = s.substring(s.indexOf('=') + 2);
                    populateInfo(var_name, value);
                    if(i==q-1) {
                        ans = haha_count.get(var_name);
                    }
                }
                else {
                    String var_name_lhs = s.substring(0, s.indexOf('=') - 1);
                    String var_name_rhs1 = s.substring(s.indexOf('=') + 2, s.indexOf('+') - 1);
                    String var_name_rhs2 = s.substring(s.indexOf('+') + 2);
                    add(var_name_lhs, var_name_rhs1, var_name_rhs2);
                    if(i==q-1) {
                        ans = haha_count.get(var_name_lhs);
                    }
                }
            }
            pl(ans);
        }

        pw.flush();
        pw.close();
    }

    static void add(String l, String r1, String r2) {
        boolean pref[][] = new boolean[4][4];
        boolean suff[][] = new boolean[4][4];
        long r1_len = length.get(r1), r2_len = length.get(r2);
        for(int i=0;i<4;i++) {
            for(int j=i;j<4;j++) {
                // pref
                if(r1_len>=(j-i+1)) {
                    pref[i][j] = contain_pref[i][j].contains(r1);
                }
                else {
                    pref[i][j] = contain_pref[i][i+(int)r1_len-1].contains(r1) && contain_pref[i+(int)r1_len][j].contains(r2);
                }

                // suff
                if(r2_len>=(j-i+1)) {
                    suff[i][j] = contain_suff[i][j].contains(r2);
                }
                else {
                    suff[i][j] = contain_suff[i][j-(int)r2_len].contains(r1) && contain_suff[j-(int)r2_len+1][j].contains(r2);
                }
            }
        }

        long H = haha_count.get(r1) + haha_count.get(r2);
        // pl("H: "+H);
        for(int i=0;i<3;i++) {
            if(contain_suff[0][i].contains(r1) && contain_pref[i+1][3].contains(r2)) {
                H++;
            }
        }
        // pl("H after merge: "+H);
        haha_count.put(l, H);
        length.put(l, r1_len+r2_len);
        // pl("length: "+length.get(l));
//        pa("pref", pref);
//        pa("suff", suff);
        for(int i=0;i<4;i++) {
            for(int j=i;j<4;j++) {
                if(pref[i][j]) {
                    contain_pref[i][j].add(l);
                    // pl("contains "+HAHA.substring(i,j+1)+" as prefix");
                }
                else {
                    contain_pref[i][j].remove(l);
                }
                if(suff[i][j]) {
                    contain_suff[i][j].add(l);
                    // pl("contains "+HAHA.substring(i,j+1)+" as suffix");
                }
                else {
                    contain_suff[i][j].remove(l);
                }
            }
        }
    }

    static void populateHahaCount(String varName, String varValue) {
        int n = varValue.length();
        long ans = 0;
        for(int i=0;i+3<n;i++) {
            if(varValue.charAt(i)=='h' && varValue.charAt(i+1)=='a' && varValue.charAt(i+2)=='h' && varValue.charAt(i+3)=='a') {
                ans++;
            }
        }
        haha_count.put(varName, ans);
    }

    static void populatePrefixes(String varName, String varValue) {
        // pl("populateing prefix for : "+varValue);
        for(int i=0;i<4;i++) {
            for(int j=i;j<4;j++) {
                if(varValue.length() >= (j-i+1) && varValue.charAt(j-i)==HAHA.charAt(j)) {
                    // pl("contains "+HAHA.substring(i, j+1)+" as prefix");
                    contain_pref[i][j].add(varName);
                }
                else {
                    break;
                }
            }
        }
    }

    static void populateSuffixes(String varName, String varValue) {
        // pl("populating suffix for: "+varValue);
        for(int j=3;j>=0;--j) {
            for(int i=j;i>=0;--i) {
                if(varValue.length() >= (j-i+1) && varValue.charAt(varValue.length() - 1 - (j-i)) == HAHA.charAt(i)) {
                    // pl("Contains "+HAHA.substring(i, j+1)+" as suffix");
                    contain_suff[i][j].add(varName);
                }
                else {
                    break;
                }
            }
        }
    }

    static void populateInfo(String varName, String varValue) {
        for(int i=0;i<4;i++) {
            for(int j=i;j<4;++j) {
                contain_pref[i][j].remove(varName);
                contain_suff[i][j].remove(varName);
            }
        }

        populatePrefixes(varName, varValue);
        populateSuffixes(varName, varValue);
        populateHahaCount(varName, varValue);
        length.put(varName, (long)varValue.length());
    }

    static void assert_in_range(String varName, int n, int l, int r) {
        if (n >=l && n<=r) return;
        System.out.println(varName + " is not in range. Actual: "+n+" l : "+l+" r: "+r);
        System.exit(1);
    }
    static void assert_in_range(String varName, long n, long l, long r) {
        if (n>=l && n<=r) return;
        System.out.println(varName + " is not in range. Actual: "+n+" l : "+l+" r: "+r);
        System.exit(1);
    }

    static void initIo(boolean isFileIO, String suffix) throws IOException {
        scan = new MyScanner(isFileIO, suffix);
        if(isFileIO) {
            pw = new PrintWriter("/Users/dsds/Desktop/output"+suffix+".txt");
        }
        else {
            pw = new PrintWriter(System.out, true);
        }
    }
    static int ni() throws IOException
    {
        return scan.nextInt();
    }
    static long nl() throws IOException
    {
        return scan.nextLong();
    }
    static double nd() throws IOException
    {
        return scan.nextDouble();
    }
    static String ne() throws IOException
    {
        return scan.next();
    }
    static String nel() throws IOException
    {
        return scan.nextLine();
    }
    static void pl()
    {
        pw.println();
    }
    static void p(Object o)
    {
        pw.print(o+" ");
    }
    static void pl(Object o)
    {
        pw.println(o);
    }
    static void psb(StringBuilder sb)
    {
        pw.print(sb);
    }
    static void pa(String arrayName, Object arr[])
    {
        pl(arrayName+" : ");
        for(Object o : arr)
            p(o);
        pl();
    }
    static void pa(String arrayName, int arr[])
    {
        pl(arrayName+" : ");
        for(int o : arr)
            p(o);
        pl();
    }
    static void pa(String arrayName, long arr[])
    {
        pl(arrayName+" : ");
        for(long o : arr)
            p(o);
        pl();
    }
    static void pa(String arrayName, double arr[])
    {
        pl(arrayName+" : ");
        for(double o : arr)
            p(o);
        pl();
    }
    static void pa(String arrayName, char arr[])
    {
        pl(arrayName+" : ");
        for(char o : arr)
            p(o);
        pl();
    }

    static void pa(String arrayName, TreeSet<Integer> set)
    {
        pl(arrayName+" : ");
        for(Object o : set)
            p(o);
        pl();
    }

    static void pa(String arrayName, boolean arr[])
    {
        pl(arrayName+" : ");
        for(boolean o : arr)
            p(o);
        pl();
    }
    static void pa(String listName, List list)
    {
        pl(listName+" : ");
        for(Object o : list)
            p(o);
        pl();
    }
    static void pa(String arrayName, Object[][] arr) {
        pl(arrayName+" : ");
        for(int i=0;i<arr.length;++i) {
            for(Object o : arr[i])
                p(o);
            pl();
        }
    }
    static void pa(String arrayName, int[][] arr) {
        pl(arrayName+" : ");
        for(int i=0;i<arr.length;++i) {
            for(int o : arr[i])
                p(o);
            pl();
        }
    }
    static void pa(String arrayName, long[][] arr) {
        pl(arrayName+" : ");
        for(int i=0;i<arr.length;++i) {
            for(long o : arr[i])
                p(o);
            pl();
        }
    }
    static void pa(String arrayName, char[][] arr) {
        pl(arrayName+" : ");
        for(int i=0;i<arr.length;++i) {
            for(char o : arr[i])
                p(o);
            pl();
        }
    }
    static void pa(String arrayName, double[][] arr) {
        pl(arrayName+" : ");
        for(int i=0;i<arr.length;++i) {
            for(double o : arr[i])
                p(o);
            pl();
        }
    }
    static void pa(String arrayName, boolean[][] arr) {
        pl(arrayName+" : ");
        for(int i=0;i<arr.length;++i) {
            for(boolean o : arr[i])
                p(o);
            pl();
        }
    }
    static class MyScanner
    {
        BufferedReader br;
        StringTokenizer st;
        MyScanner(boolean readingFromFile, String suffix) throws IOException
        {
            if(readingFromFile) {
                br = new BufferedReader(new FileReader("/Users/ddfds/Desktop/input"+suffix+".txt"));
            }
            else {
                br = new BufferedReader(new InputStreamReader(System.in));
            }
        }
        String nextLine()throws IOException
        {
            return br.readLine();
        }
        String next() throws IOException
        {
            if(st==null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }
        int nextInt() throws IOException
        {
            return Integer.parseInt(next());
        }
        long nextLong() throws IOException
        {
            return Long.parseLong(next());
        }
        double nextDouble() throws IOException
        {
            return Double.parseDouble(next());
        }
    }
}