1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <bits/stdc++.h>

using namespace std;

int n;
char ss[110];
vector <pair <int, long long> > v[100010];

void prin(vector <pair <int, long long> > u)
{
	for(auto xd : u)
	{
		if(xd.first==0)
		{
			cout<<"W ";
		}
		else if(xd.first==1)
		{
			cout<<"Z "<<xd.second<<" ";
		}
		else
		{
			cout<<"+ "<<xd.second<<" ";
		}
	}
	cout<<endl;
}

vector <pair <int, long long> > compre(vector <pair <int, long long> > u)
{
	for(int i=u.size()-1; i>=0; --i)
	{
		if(u[i].first!=1)
		{
			u.pop_back();
		}
		else
		{
			break;
		}
	}
	vector <pair <int, long long> > ans;
	pair <int, long long> last={2, 0};
	for(int i=u.size()-1; i>=0; --i)
	{
		if(u[i].first==1)
		{
			if(last.first==1||(last.first==2&&last.second!=0))
			{
				ans.push_back(last);
			}
			//ans.push_back(u[i]);
			last=u[i];
		}
		else if(u[i].first==0)
		{
			if(last.first!=0)
			{
				if(last.second!=0||last.first==1)
				{
					ans.push_back(last);
				}
				ans.push_back(u[i]);
				last=u[i];
			}
		}
		else
		{
			if(last.first!=0)
			{
				last.second+=u[i].second;
			}
		}
	}
	if(last.first==1||(last.first==2&&last.second!=0))
	{
		ans.push_back(last);
	}
	reverse(ans.begin(), ans.end());
	return ans;
}

long long ba(vector <long long> u)
{
	vector <long long> cp;
	long long ans=(1ll<<60);
	bool kon=true;
	for(int i=0; i<n; ++i)
	{
		if(u[i]<(int)v[i].size())
		{
			kon=false;
			cp=u;
			if(v[i][u[i]].first==0)
			{
				cp[n+i]=cp[2*n];
			}
			else if(v[i][u[i]].first==1)
			{
				cp[n+i]+=v[i][u[i]].second;
				cp[2*n]=cp[n+i];
			}
			else
			{
				cp[n+i]+=v[i][u[i]].second;
			}
			++cp[i];
			ans=min(ans, ba(cp));
		}
	}
	if(kon)
	{
		return u[2*n];
	}
	else
	{
		return ans;
	}
}

void solve()
{
	scanf("%d", &n);
	vector <long long> p;
	p.push_back(0);
	for(int i=0; i<n; ++i)
	{
		int l;
		scanf("%d", &l);
		v[i].clear();
		for(int j=0; j<l; ++j)
		{
			scanf("%s", ss);
			if(ss[0]=='W')
			{
				v[i].push_back({0, 0});
			}
			else if(ss[0]=='Z')
			{
				v[i].push_back({1, 0});
			}
			else
			{
				long long x;
				scanf("%lld", &x);
				if(ss[0]=='+')
					v[i].push_back({2, x});
				else
					v[i].push_back({2, -x});
			}
		}
		//prin(v[i]);
		v[i]=compre(v[i]);
		//prin(v[i]);
		p.push_back(0);
		p.push_back(0);
	}
	printf("%lld\n", ba(p));
}

int main()
{
	int tt;
	scanf("%d", &tt);
	for(int i=0; i<tt; ++i)
	{
		solve();
	}
	return 0;
}