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.
#include<bits/stdc++.h>

using namespace std;
#define MOD 1000000007
#define display(a) for(auto i:a)cout<<i<<" ";
#define nl '\n'
#define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL)
#define ll long long int
class mystr
{
public:
	string s;
	ll n;
	mystr()
	{
		s="";
		n=0;
	}
}temp;

map<string,mystr> val;
ll read()
{
	string s,eqn,a,b;

	cin>>s;
	cin>>eqn;
	if(eqn[0]==':')
	{
		int count=0;
		cin>>a;

		temp.s=a;
		

		for(int i=0;i<a.length();i++)
		{
			if(a.substr(i,4)=="haha")
			{
				count++;
			}
		}
		temp.n=count;
		val[s]=temp;
		return count;
	}
	else
	{
		cin>>a;
		cin>>b;
		cin>>b;
		string q;
		if(val[a].s.length()>3)
		{
			q=val[a].s.substr(val[a].s.length()-3,3)+val[b].s.substr(0,3);
		}
		else
		{
			q=val[a].s.substr(0,3)+val[b].s.substr(0,3);	
		}
		ll count=0;
		for(int i=0;i<q.length();i++)
		{
			if(q.substr(i,4)=="haha")
			{
				count++;
			}
		}

		if(val[b].s.length()>3)
		{
			val[s].s=val[a].s.substr(0,3)+val[b].s.substr(val[b].s.length()-3,3);
		}
		else
		{
			val[s].s=val[a].s.substr(0,3)+val[b].s.substr(0,3);	
		}
		val[s].n=count+val[a].n+val[b].n;
		return val[s].n;
	}


	// cout<<s<<nl;
	// cout<<eqn<<nl;
	// cout<<a<<nl;
	// cout<<b<<nl;
	// cout<<"---------"<<nl;
	
	return 0;
}
void solve()
{	
	val.clear();
	ll w,ans;
	cin>>w;
	for(int i=0;i<w;i++)
		ans=read();
	 map<string, mystr>::iterator itr;
	// for (itr = val.begin(); itr != val.end(); ++itr) {
 //        cout << '\t' << itr->first
 //             << '\t' << itr->second.s 
 //             << '\t' << itr->second.n << '\n';
 //    }
    cout<<ans<<nl;
}

int main()
{
	fast_io;
   
    int t;
	cin>>t;
	while(t--)
		solve();
	
}