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
171
172
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <array>
#include <random>
#include <cmath>
#include <list>
#include <ctime>
#include <sstream>
#include <queue>
#include <climits>
#include <stack>
#include <random>
#include <bitset>
#include <numeric>
#include <cassert>
using namespace std;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;
#define rep(x, b, e) for(int x=(b); x<(e); ++x)
#define trav(a, x) for(auto& a : x)
#define ford(x, b, e) for(int x=((int)(b))-1; x>=(e); --x)
#define all(c) c.begin(),c.end()
#define sz(x) ((int)((x).size()))
#define pb push_back
#define st first
#define nd second
#define mp(x,y) make_pair(x,y)
typedef short int sint;

#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

int l;
int opCount;

struct SimpleNode {
	string op;
	int val;
};

struct GameState {
	vi nextOps;
	vector<ll> currentYs;
	ll sharedVal;

	GameState() {
		sharedVal = 0;
	}
	GameState(vi _nextOps, vector<ll> _currentYs, ll _sharedVal): nextOps(_nextOps), currentYs(_currentYs), sharedVal(_sharedVal) {

	}

	void print() {
		printf("STATE : %lld\n", sharedVal);
		printf("\tops: "); trav(x, nextOps) printf("%d ", x); printf("\n");
		printf("\tys: "); trav(x, currentYs) printf("%lld ", x); printf("\n");
	}
};

vector<SimpleNode> readBrut() {
	int n;
	cin >> n;
	vector<SimpleNode> res;
	rep(i, 0, n) {
		string pom;
		cin >> pom;
		SimpleNode node;
		if (pom[0] == 'W') {
			// save the previous values, if any
			node.op = pom;
			res.pb(node);
		} else if (pom[0] == 'Z') {
			node.op = pom;
		} else {
			int val;
			cin >> val;
			if (pom[0] == '-') {
				val = -val;
			}
			node.op = '+';
			node.val = val;
		}
		res.pb(node);
	}
	// printf("READ: "); trav(x, res) cout << x.op << " " << x.val << ", "; cout << endl;
	return res;
}

ll solveForce(vector<vector<SimpleNode>>& inp) {
	map<pair<vi, vector<ll>>, ll> dist;
	opCount = 0;
	GameState st;
	vi nextOps(l, 0);
	vector<ll> currentYs(l, 0);
	st.nextOps = nextOps;
	st.currentYs = currentYs;
	st.sharedVal = 0;
	queue<GameState> q;
	q.push(st);
	dist[mp(st.nextOps, st.currentYs)] = 0;
	ll smallest = LLONG_MAX;
	while (!q.empty()) {
		st = q.front(); q.pop();
		++opCount;
		if (opCount > 5e6) {
			printf("OOM\n");
			exit(1);
		}
		// if (opCount % 100000 == 0) {
		// 	printf("opCount: %d\n", opCount);
		// }
		// st.print();
		bool moveMade = false;
		rep(i, 0, l) {
			assert(st.nextOps[i] <= sz(inp[i]));
			if (st.nextOps[i] < sz(inp[i])) {
				moveMade = true;
				// I can make a move
				GameState newState = st;
				auto node = inp[i][newState.nextOps[i]];
				++newState.nextOps[i];
				if (node.op[0] == 'W')  {
					newState.currentYs[i] = st.sharedVal;
				} else if (node.op[0] == 'Z') {
					newState.sharedVal = newState.currentYs[i];
				} else {
					newState.currentYs[i] += node.val;
				}
				if (dist.find(mp(newState.nextOps, newState.currentYs)) == dist.end() || dist[mp(newState.nextOps, newState.currentYs)] > newState.sharedVal) {
					q.push(newState);
				}
			}
		}
		if (!moveMade) {
			// printf("Terminal State: val: %lld\n", st.sharedVal);
			smallest = min(smallest, st.sharedVal);
		}
	}
	return smallest;
}

void testc() {
	cin >> l;
	vector<vector<SimpleNode>> inp;
	rep(i, 0, l) {
		inp.pb(readBrut());
		// printf("sz(inp[%d]) = %d\n", i, sz(inp[i]));
	}

	// rep(i, 0, l) {
	// 	printf("prog: %d = ", i);
	// 	trav(x, inp[i]) {
	// 		printf(" (minVal: %lld, lastVal: %lld, hasR: %d, empty: %d) ", x.minVal, x.lastVal, x.hasR, x.empty);
	// 	}
	// 	printf("\n");
	// }
	printf("%lld\n", solveForce(inp));
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0);
	int t;
	cin >> t;
	while (t--) {
		testc();
	}
}