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;
const int mod1 = 1000000007;
#define el "\n"
#define sp <<" "
#define nw cout<<"\n";
#define ff first
#define ss second
#define float double
#define int long long
#define inf INT_MAX
#define ninf INT_MIN
#define pb push_back
#define mp make_pair
#define sz(x) x.size()
#define vec vector<int>
#define SORT(x) sort(x.begin(), x.end())
#define rep(i,n) for(int i=0;i<n;i++)
#define repp(i,j,n) for(int i= j;i<n;i++)
#define reppp(i,j,n,k) for(int i= j;i<n;i+=k)
#define min3(a, b, c) min(c, min(a, b))
#define min4(a, b, c, d) min(d, min(c, min(a, b)))
#define SORTD(arr,n) sort(arr, arr + n, greater<int>())
#define FIND(v,k) find(v.begin(), v.end(), k)-v.begin()
#define COUNT(vect, n) count(vect.begin(), vect.end(), n)
#define input(n) int arr[n];for(int i=0;i<n;i++)cin>>arr[i]
#define boost ios_base::sync_with_stdio(false), cin.tie(NULL);
#define sort_str(s) sort(s.begin(), s.end(), [](const std::string& first, const std::string& second){return first.size() < second.size();});
int gcd(int a, int b){int c=a%b; while(c!=0){a = b; b = c;c = a % b;}return b;}
int issub(string a,string b){if (a.find(b) != string::npos) return 1;else return 0;}
int expo(int a,int b){int res=1;while(b>0){if (b&1)res = res*a%mod1;a=a*a%mod1;b>>=1;}return res;}
int32_t lcm(int n1,int n2){int max=(n1>n2)?n1:n2;do{if (max%n1==0 && max%n2==0){return max;}else++max;} while (true);}
string operator * (string a, unsigned int b) {string output = "";while (b--) output += a;return output;}
bool isPerfectSquare(int n){for (int i = 1; i * i <= n; i++) {if ((n % i == 0) && (n / i == i)){return true;}}return false;}
#define vss pair<string,string>
int iseq(string s){
    rep(i,8) if (s[i]==':') return i;
    return false;
}
int32_t f(string s){
    rep(i,8) if (s[i]=='=') return i;
    return 1;
}
int32_t sec(string a,int z){
    repp(i,z+1,sz(a)) if (a[i]=='+') return i;
    return 1;
}
int32_t counts(string s){
    int c=0;
    rep(i,sz(s)){
        if (s.substr(i,4)=="haha") c++;
    }
    return c;
}

int32_t main(){
    boost;
    int t;
    cin >> t;
    while(t--){
        int n,count=0;
        map<string,pair<int,pair<string,string>>> m;
        string s,last;
        cin>>n;
        cin.ignore();
        rep(i,n){
            getline(cin,s);
            int z=iseq(s);
            if (z){
                string a =s.substr(0,z-1); 
                string b =s.substr(z+3,sz(s));
                string pre=b,suf=b;
                if (sz(b)>=3) pre = b.substr(0,3) , suf=b.substr(sz(b)-3,3);
                pair<int,vss> w;
                w=mp(counts(b),mp(pre,suf));
                auto itr = m.find(a); 
                if (itr != m.end()){ (*itr).second = w;}
                else m.insert(pair<string,pair<int,pair<string,string>>>(a,w));
                last=a;
            }
            else{
                pair<int,vss> w;
                int x = f(s),y=sec(s,z);
                string a =s.substr(0,x-1),b =s.substr(x+2,y-x-3), c =s.substr(y+2,sz(s));
                string d =(m[b].ss).ss + (m[c].ss).ff, e= (m[b].ss).ff + (m[c].ss).ss;
                int fu = m[b].ff + m[c].ff;
                string pre=e,suf=e;
                if (sz(e)>=3) pre = e.substr(0,3) , suf=e.substr(sz(e)-3,3);
                w=mp(counts(d)+fu,mp(pre,suf));
                auto itr = m.find(a); 
                if (itr != m.end()) (*itr).second = w;
                else m.insert(pair<string,pair<int,pair<string,string>>>(a,w)); 
                last=a;
            }
        }
        cout << m[last].ff << el;
    }
    return 0;
}
