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
#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int W=1e9+20,Z=1e9+30;
const long long INF=1e18+7;
vector<int> tab[100010];
long long w[100010];
long long ww;
vector<int> it;
int n;
long long solve()
{
	long long ans=INF;
	for(int i=0;i<n;i++)
	{
		if(it[i]<tab[i].size())
		{
			//cerr<<i<<" "<<it[i]<<" "<<tab[i][it[i]]<<"\n";
			if(tab[i][it[i]]==W)
			{
				long long tmp=w[i];
				w[i]=ww;
				it[i]++;
				ans=min(ans,solve());
				it[i]--;
				w[i]=tmp;
			}
			else if(tab[i][it[i]]==Z)
			{
				long long tmp=ww;
				ww=w[i];
				it[i]++;
				ans=min(ans,solve());
				it[i]--;
				ww=tmp;
			}
			else
			{
				w[i]+=tab[i][it[i]];
				it[i]++;
				ans=min(ans,solve());
				it[i]--;
				w[i]-=tab[i][it[i]];
			}
		}
	}
	return (ans==INF ? ww:ans);
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int q,l;
	cin>>q;
	while(q--)
	{
		cin>>n;
		it.clear();
		it.resize(n);
		for(int i=0;i<n;i++)
		{
			it[i]=0;
			w[i]=0;
			tab[i].clear();
			cin>>l;
			tab[i].resize(l);
			for(int j=0;j<l;j++)
			{
				char c;
				cin>>c;
				if(c=='W')
					tab[i][j]=W;
				else if(c=='Z')
					tab[i][j]=Z;
				else
				{
					cin>>tab[i][j];
					if(c=='-')
						tab[i][j]=-tab[i][j];
				}
			}
		}
		ww=0;
		cout<<solve()<<"\n";
	}
	return 0;
}