#include<bits/stdc++.h>

using namespace std;

mt19937 rng((unsigned int) chrono::steady_clock::now().time_since_epoch().count());
#define rep(i,n) for(int i = 0 ; i < n; i++)
#define dep(i,n) for(int i = n-1 ; i >= 0; i--)
#define re1(i,n) for(int i = 1 ; i <= n; i++)
#define de1(i,n) for(int i = n ; i > 0; i--)
#define rab(i,a,b) for(int i = a ; i < b; i++)
#define dab(i,a,b) for(int i = b ; i >= a; i--)
#define trav(a,x) for (auto& a : x)
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define mod 1000000007
#define debug1(x) cout<<#x<<" = "<<x<<endl
#define debug2(x,y) cout<<#x<<" = "<<x<<", "<<#y<<" = "<<y<<endl
#define debug3(x,y,z) cout<<#x<<" = "<<x<<", "<<#y<<" = "<<y<<", "<<#z<<" = "<<z<<endl
#define debug4(x,y,z,q) cout<<#x<<" = "<<x<<", "<<#y<<" = "<<y<<", "<<#z<<" = "<<z<<", "<<#q<<" = "<<q<<endl
#define prq(x) priority_queue<x,vector<x> > 

typedef long long ll;
typedef double db;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
typedef pair<int,ll> pil;
typedef pair<ll,ll> pll;
typedef vector<vector<int> > vvi;
typedef pair<pii,int> ppi;

pll inv_help(ll a,ll b){
    pll res;
    res.F=1;
    res.S=0;
    if(b==0) return res;
    else{
        pll res1 = inv_help(b,a%b);
        res.F = res1.S;
        res.S = res1.F - (a/b)*res1.S;

        return res;
    }
}

ll inv(ll a,ll b){
    pll res = inv_help(a,b);
    ll x = res.F;
    // return x;
    if(x>0) x=x%b;
    if(x<0) x=(b - (-x)%b)%b;
    return x;
}

vector<int> l;
int N;

int binsearch(int p,int l_bnd){
    int i=l_bnd,j=N-1,k;
    if(l_bnd>N-1) return -1;
    if(l[N-1]<p) return -1;
    else if(l[i]>p) return i;
    else{
        
        while(i!=j){
            k=(i+j)/2;
            if(i==j){
                return i;
            }
            else if(i==j-1){
                if(l[i]==p) return i;
                else return i+1;
            }
            else if(l[k]<p) {
                i=k+1;
            }
            else if(l[k]>=p){
                j=k;
            }
        }
        return i;
    }
    
}

vector<ll> fact(2000001,1);

ll nCr(int n,int r){
    ll a1 = fact[n];
    ll a2 = fact[n-r];
    ll a3 = fact[r];

    ll ans = (((a1*inv(a2,mod))%mod)*inv(a3,mod))%mod;

    return ans;
}

bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;
  
    // This is checked so that we can skip
    // middle five numbers in below loop
    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;
}

vector<int> prime;

int check(int l,int r,int n){
    ll k = 1;
    rep(i,n) k = k*10;

    int p1 = (l/k)*k + k;
    int p2 = (r/k)*k;

    if(p2>=p1) return (p2 - p1)/k + 1;
    else return 0;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int T;
    cin>>T;
    int tt=T;

    // re1(i,2000000) fact[i] = (fact[i-1]*i)%mod;

    re1(i,100000) if(isPrime(i)) prime.pb(i);

    while(T--){
        int n;
        cin>>n;

        map<string,string> m;
        map<string,ll> mm;

        string sss;

        rep(i,n){
            string s1,s2,s3;
            cin>>s1>>s2>>s3;
            
            if(s2.size()==2){
                m[s1] = s3;
                mm[s1] = 0;
            }
            else {
                string s4,s5;
                cin>>s4>>s5;

                string r1 = m[s3];
                string r2 = m[s5];

                mm[s1] = mm[s3] + mm[s5];

                rep(j,r2.size()) r1 += r2[j];
                
                if(r1.size()>100){
                    ll cou = 0;
                    rep(j,r1.size()-3){
                        if(r1[j]=='h' && r1[j+1]=='a' && r1[j+2]=='h' && r1[j+3]=='a') cou++;
                    }
                    mm[s1] += cou;
                    string rrr;
                    rrr += r1[0];
                    rrr += r1[1];
                    rrr += r1[2];
                    rrr += 'q';
                    rrr += r1[r1.size()-3];
                    rrr += r1[r1.size()-2];
                    rrr += r1[r1.size()-1];
                    r1 = rrr;
                }

                m[s1] = r1;
            }
            if(i==n-1) sss = s1;
        }

        string ans = m[sss];
        // debug(ans);
        ll count = 0;
        ll couuu = mm[sss];
        if(ans.size()<4){
            cout<<couuu<<"\n";
            continue;
        }

        rep(i,ans.size()-3){
            if(ans[i]=='h' && ans[i+1]=='a' && ans[i+2]=='h' && ans[i+3]=='a') count++;
        }

        cout<<couuu + count<<"\n";
    }
}
### Prompt

In cpp, your task is to solve the following problem:

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.

### Response

```cpp
#include<bits/stdc++.h>

using namespace std;

mt19937 rng((unsigned int) chrono::steady_clock::now().time_since_epoch().count());
#define rep(i,n) for(int i = 0 ; i < n; i++)
#define dep(i,n) for(int i = n-1 ; i >= 0; i--)
#define re1(i,n) for(int i = 1 ; i <= n; i++)
#define de1(i,n) for(int i = n ; i > 0; i--)
#define rab(i,a,b) for(int i = a ; i < b; i++)
#define dab(i,a,b) for(int i = b ; i >= a; i--)
#define trav(a,x) for (auto& a : x)
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define mod 1000000007
#define debug1(x) cout<<#x<<" = "<<x<<endl
#define debug2(x,y) cout<<#x<<" = "<<x<<", "<<#y<<" = "<<y<<endl
#define debug3(x,y,z) cout<<#x<<" = "<<x<<", "<<#y<<" = "<<y<<", "<<#z<<" = "<<z<<endl
#define debug4(x,y,z,q) cout<<#x<<" = "<<x<<", "<<#y<<" = "<<y<<", "<<#z<<" = "<<z<<", "<<#q<<" = "<<q<<endl
#define prq(x) priority_queue<x,vector<x> > 

typedef long long ll;
typedef double db;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
typedef pair<int,ll> pil;
typedef pair<ll,ll> pll;
typedef vector<vector<int> > vvi;
typedef pair<pii,int> ppi;

pll inv_help(ll a,ll b){
    pll res;
    res.F=1;
    res.S=0;
    if(b==0) return res;
    else{
        pll res1 = inv_help(b,a%b);
        res.F = res1.S;
        res.S = res1.F - (a/b)*res1.S;

        return res;
    }
}

ll inv(ll a,ll b){
    pll res = inv_help(a,b);
    ll x = res.F;
    // return x;
    if(x>0) x=x%b;
    if(x<0) x=(b - (-x)%b)%b;
    return x;
}

vector<int> l;
int N;

int binsearch(int p,int l_bnd){
    int i=l_bnd,j=N-1,k;
    if(l_bnd>N-1) return -1;
    if(l[N-1]<p) return -1;
    else if(l[i]>p) return i;
    else{
        
        while(i!=j){
            k=(i+j)/2;
            if(i==j){
                return i;
            }
            else if(i==j-1){
                if(l[i]==p) return i;
                else return i+1;
            }
            else if(l[k]<p) {
                i=k+1;
            }
            else if(l[k]>=p){
                j=k;
            }
        }
        return i;
    }
    
}

vector<ll> fact(2000001,1);

ll nCr(int n,int r){
    ll a1 = fact[n];
    ll a2 = fact[n-r];
    ll a3 = fact[r];

    ll ans = (((a1*inv(a2,mod))%mod)*inv(a3,mod))%mod;

    return ans;
}

bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;
  
    // This is checked so that we can skip
    // middle five numbers in below loop
    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;
}

vector<int> prime;

int check(int l,int r,int n){
    ll k = 1;
    rep(i,n) k = k*10;

    int p1 = (l/k)*k + k;
    int p2 = (r/k)*k;

    if(p2>=p1) return (p2 - p1)/k + 1;
    else return 0;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int T;
    cin>>T;
    int tt=T;

    // re1(i,2000000) fact[i] = (fact[i-1]*i)%mod;

    re1(i,100000) if(isPrime(i)) prime.pb(i);

    while(T--){
        int n;
        cin>>n;

        map<string,string> m;
        map<string,ll> mm;

        string sss;

        rep(i,n){
            string s1,s2,s3;
            cin>>s1>>s2>>s3;
            
            if(s2.size()==2){
                m[s1] = s3;
                mm[s1] = 0;
            }
            else {
                string s4,s5;
                cin>>s4>>s5;

                string r1 = m[s3];
                string r2 = m[s5];

                mm[s1] = mm[s3] + mm[s5];

                rep(j,r2.size()) r1 += r2[j];
                
                if(r1.size()>100){
                    ll cou = 0;
                    rep(j,r1.size()-3){
                        if(r1[j]=='h' && r1[j+1]=='a' && r1[j+2]=='h' && r1[j+3]=='a') cou++;
                    }
                    mm[s1] += cou;
                    string rrr;
                    rrr += r1[0];
                    rrr += r1[1];
                    rrr += r1[2];
                    rrr += 'q';
                    rrr += r1[r1.size()-3];
                    rrr += r1[r1.size()-2];
                    rrr += r1[r1.size()-1];
                    r1 = rrr;
                }

                m[s1] = r1;
            }
            if(i==n-1) sss = s1;
        }

        string ans = m[sss];
        // debug(ans);
        ll count = 0;
        ll couuu = mm[sss];
        if(ans.size()<4){
            cout<<couuu<<"\n";
            continue;
        }

        rep(i,ans.size()-3){
            if(ans[i]=='h' && ans[i+1]=='a' && ans[i+2]=='h' && ans[i+3]=='a') count++;
        }

        cout<<couuu + count<<"\n";
    }
}
```