#include<bits/stdc++.h>
using namespace std;
#define ll              long long int
#define ull             unsigned long long int
#define mod             1000000007
#define mod2            998244353 
#define pb              push_back
#define mp              make_pair
#define count_1(n)      __builtin_popcountll(n) //set bits
#define fr              first
#define sc              second

int z(int x)
{
    int ans=0;
    while ((x % 2) == 0)
    {
        ans++;
        x = x/2;
    }
 
    
    for (int i = 3; i <= sqrt(x); i = i + 2)
    {
        while (x % i == 0)
        {
            ans++;
            x = x/i;
        }
    }
 
    
    if (x > 2)
        ans++;
        
        
    return ans;
}

int main()
{  
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
   
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        ll ans=0;
        map<string,string> st,ed;
        map<string,ll> m1;
        while(n--)
        {
            string s,op;
            cin>>s>>op;
            if(op[0]==':')
            {
                string val;
                cin>>val;
                ll curr=0;
                if(val.size()>=4)
                {
                    if(val.substr(0,4)=="haha")
                        curr++;
                }
                if(val.size()>4)
                {
                    if(val.substr(1,4)=="haha")
                        curr++;
                }
                string temp="";
                for(int i=0;i<min(3,(int)val.size());i++)
                {
                    temp+=val[i];
                }
                st[s]=temp;
                temp="";
                for(int i=max(0,(int)val.size()-3);(int)i<val.size();i++)
                {
                    temp+=val[i];
                }
                ed[s]=temp;
                ans=curr;
                m1[s]=ans;
            }
            else
            {
                string a,b,c;
                cin>>a>>b>>c;
                ans=m1[a]+m1[c];
                string temp=ed[a]+st[c];
                //cout<<temp<<" ";
                for(int i=0;(i+4)<=(int)temp.size();i++)
                {
                    if(temp.substr(i,4)=="haha")
                    {
                        ans++;
                    }
                }
                temp=st[a]+st[c];
                st[s]="";
                for(int i=0;i<temp.size();i++)
                {
                    st[s]+=temp[i];
                    if(i==2) break;
                }
                temp=ed[a]+ed[c];
                ed[s]="";
                for(int i=max(0,(int)temp.size()-3);i<temp.size();i++)
                {
                    ed[s]+=temp[i];
                }
                
                m1[s]=ans;
                //cout<<a<<" "<<c<<"\n";
                
            }
            //cout<<ans<<" ";
        }
        
        // for(auto i=m.begin();i!=m.end();i++)
        //     cout<<(*i).first<<" "<<(*i).second<<"\n";
        cout<<ans<<"\n";
    }
}