#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vl;
typedef pair<ll,ll> pl;
typedef vector<pl> vpl;
typedef vector<vl> vvl;
typedef priority_queue<ll> prq;
#define vt vector
#define fr(i,a,b) for(ll (i)=(a) ; (i) <= (b) ; ++(i))
#define frr(i,a,b) for(ll (i)=(a) ; (i) >= (b) ; --(i))
#define trav(e,a) for(auto (e): (a))
#define tra(e,a) for(auto& (e): (a))
#define pb push_back
#define pob pop_back
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define allr(x) (x).rbegin(),(x).rend()
#define lbd(a,x) ll(lower_bound(all((a)),(x)) - (a).begin())
#define ubd(a,x) ll(upper_bound(all((a)),(x)) - (a).begin())
#define sz(x) (ll)(x).size()
#define IOS ios::sync_with_stdio(0);cin.tie(0) 
#define decimal(x) cout << fixed << setprecision(x)
#define dbg1(x9) cout << #x9 << " = " << x9 << endl
#define dbg2(x9, y9) cout << #x9 << " = " << x9 << "   " << #y9 << " = " << y9 << endl
#define dbg3(x9, y9, z9) cout << #x9 << " = " << x9 << "   " << #y9 << " = " << y9 << "  "<< #z9 <<" = "<< z9 << endl
#define dbg4(x9, y9, z9, w9) cout << #x9 << " = " << x9 << "   " << #y9 << " = " << y9 << "  "<< #z9 <<" = "<< z9 << "  " << #w9 <<" = "<< w9 << endl
#define get5(a9, b9, c9, d9, e9, ...) e9
#define DBG(...) get5(__VA_ARGS__, dbg4, dbg3, dbg2, dbg1)
#define dbg(...) DBG(__VA_ARGS__)(__VA_ARGS__)

template <typename T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
template <typename T> istream &operator>> (istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; }
template <typename T> ostream& operator<< (ostream& os, const vector<T>& v) 
{ os << "[ "; for (int i = 0; i < v.size(); ++i) { os << v[i]; if (i != v.size() - 1) os << ", "; } os << " ]\n"; return os; } 
template <typename T> ostream& operator<< (ostream& os, const set<T>& v) 
{ os << "[ "; for (auto it : v) { os << it; if (it != *v.rbegin()) os << ", "; } os << " ]\n"; return os; } 
template <typename T> ostream& operator<< (ostream& os, const multiset<T>& v) 
{ os << "[ "; for (auto it : v) os << it << ", "; os << " ]\n"; return os; }
template <typename T, typename S> ostream& operator<< (ostream& os, const map<T, S>& v) 
{ os << "{ "; for (auto it : v) os << it.first << " : " << it.second << ", "; os << " }\n"; return os; }
template <typename T, typename S> ostream& operator<< (ostream& os, const pair<T, S>& v) 
{ os << "("; os << v.first << ", " << v.second << ")"; return os; }
template <typename T> ostream& operator<< (ostream& os, priority_queue<T> p)
{ os << "[ "; while(!p.empty()){ os << p.top() << " ,"; p.pop(); } os << " ]\n"; return os; }

const long long INF=1e18;
const ll mod=1e9+7;
inline ll pmod(ll i, ll n=mod) { return (i % n + n) % n; }
inline ll cdiv(ll a,ll b){ return (a+b-1)/b; }

ll count(string& s){
    ll n=sz(s),ans=0;
    fr(i,0,n-4) if(s.substr(i,4)=="haha") ++ans; return ans;
}
struct funny{
    ll len,cnt; string first,last;
    funny(string s=""){
        len=sz(s); cnt=count(s);
        first=last=s; trim();
    }
    void trim(){
        if(sz(first)>3) first=first.substr(0,3);
        if(sz(last)>3) last=last.substr(sz(last)-3);
    }
};

funny merge(funny a,funny b){
    funny c;
    c.len=a.len+b.len;
    c.cnt=a.cnt+b.cnt;
    string s=a.last+b.first;
    c.cnt+=count(s);
    c.first=a.first+b.first;
    c.last=a.last+b.last;
    c.trim(); return c;
}

void solve(){
    ll n; cin >> n; map<string,funny> mp; funny last;
    fr(i,1,n){
        string name,type;
        cin >> name >> type;
        if(type==":="){
            string val; cin >> val;
            mp[name]=funny(val);
        }
        else{
            string a,op,b; cin >> a >> op >> b;
            mp[name]=merge(mp[a],mp[b]);
        }
        last=mp[name];
    }
    cout << last.cnt;
}

int main(){
    IOS;
    int t = 1;
    cin >> t;
    
    for(int t1=1;t1<=t;++t1){
        //cout << "Case #" << t1 << ": ";
        solve();
        cout << '\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;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vl;
typedef pair<ll,ll> pl;
typedef vector<pl> vpl;
typedef vector<vl> vvl;
typedef priority_queue<ll> prq;
#define vt vector
#define fr(i,a,b) for(ll (i)=(a) ; (i) <= (b) ; ++(i))
#define frr(i,a,b) for(ll (i)=(a) ; (i) >= (b) ; --(i))
#define trav(e,a) for(auto (e): (a))
#define tra(e,a) for(auto& (e): (a))
#define pb push_back
#define pob pop_back
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define allr(x) (x).rbegin(),(x).rend()
#define lbd(a,x) ll(lower_bound(all((a)),(x)) - (a).begin())
#define ubd(a,x) ll(upper_bound(all((a)),(x)) - (a).begin())
#define sz(x) (ll)(x).size()
#define IOS ios::sync_with_stdio(0);cin.tie(0) 
#define decimal(x) cout << fixed << setprecision(x)
#define dbg1(x9) cout << #x9 << " = " << x9 << endl
#define dbg2(x9, y9) cout << #x9 << " = " << x9 << "   " << #y9 << " = " << y9 << endl
#define dbg3(x9, y9, z9) cout << #x9 << " = " << x9 << "   " << #y9 << " = " << y9 << "  "<< #z9 <<" = "<< z9 << endl
#define dbg4(x9, y9, z9, w9) cout << #x9 << " = " << x9 << "   " << #y9 << " = " << y9 << "  "<< #z9 <<" = "<< z9 << "  " << #w9 <<" = "<< w9 << endl
#define get5(a9, b9, c9, d9, e9, ...) e9
#define DBG(...) get5(__VA_ARGS__, dbg4, dbg3, dbg2, dbg1)
#define dbg(...) DBG(__VA_ARGS__)(__VA_ARGS__)

template <typename T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
template <typename T> istream &operator>> (istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; }
template <typename T> ostream& operator<< (ostream& os, const vector<T>& v) 
{ os << "[ "; for (int i = 0; i < v.size(); ++i) { os << v[i]; if (i != v.size() - 1) os << ", "; } os << " ]\n"; return os; } 
template <typename T> ostream& operator<< (ostream& os, const set<T>& v) 
{ os << "[ "; for (auto it : v) { os << it; if (it != *v.rbegin()) os << ", "; } os << " ]\n"; return os; } 
template <typename T> ostream& operator<< (ostream& os, const multiset<T>& v) 
{ os << "[ "; for (auto it : v) os << it << ", "; os << " ]\n"; return os; }
template <typename T, typename S> ostream& operator<< (ostream& os, const map<T, S>& v) 
{ os << "{ "; for (auto it : v) os << it.first << " : " << it.second << ", "; os << " }\n"; return os; }
template <typename T, typename S> ostream& operator<< (ostream& os, const pair<T, S>& v) 
{ os << "("; os << v.first << ", " << v.second << ")"; return os; }
template <typename T> ostream& operator<< (ostream& os, priority_queue<T> p)
{ os << "[ "; while(!p.empty()){ os << p.top() << " ,"; p.pop(); } os << " ]\n"; return os; }

const long long INF=1e18;
const ll mod=1e9+7;
inline ll pmod(ll i, ll n=mod) { return (i % n + n) % n; }
inline ll cdiv(ll a,ll b){ return (a+b-1)/b; }

ll count(string& s){
    ll n=sz(s),ans=0;
    fr(i,0,n-4) if(s.substr(i,4)=="haha") ++ans; return ans;
}
struct funny{
    ll len,cnt; string first,last;
    funny(string s=""){
        len=sz(s); cnt=count(s);
        first=last=s; trim();
    }
    void trim(){
        if(sz(first)>3) first=first.substr(0,3);
        if(sz(last)>3) last=last.substr(sz(last)-3);
    }
};

funny merge(funny a,funny b){
    funny c;
    c.len=a.len+b.len;
    c.cnt=a.cnt+b.cnt;
    string s=a.last+b.first;
    c.cnt+=count(s);
    c.first=a.first+b.first;
    c.last=a.last+b.last;
    c.trim(); return c;
}

void solve(){
    ll n; cin >> n; map<string,funny> mp; funny last;
    fr(i,1,n){
        string name,type;
        cin >> name >> type;
        if(type==":="){
            string val; cin >> val;
            mp[name]=funny(val);
        }
        else{
            string a,op,b; cin >> a >> op >> b;
            mp[name]=merge(mp[a],mp[b]);
        }
        last=mp[name];
    }
    cout << last.cnt;
}

int main(){
    IOS;
    int t = 1;
    cin >> t;
    
    for(int t1=1;t1<=t;++t1){
        //cout << "Case #" << t1 << ": ";
        solve();
        cout << '\n';
    }
}
```