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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#include <bits/stdc++.h>

using namespace std;

typedef long long int LL;

#define st first
#define nd second
#define PLL pair <LL, LL>
#define PII pair <int, int>

const int N = 1e5 + 7;
const LL INF = 1e18 + 9LL;

struct segment {
	bool read;
	LL val, ext;
	
	segment(bool _read = false, LL _val = 0, LL _ext = 0) {
		read = _read, val = _val, ext = _ext;
	}
};

int n;
vector <segment> input[N];

namespace Debug {
	void write_line(const vector <segment> &in) {
		for(const auto &o: in) {
			if(o.read)
				printf("W ");
			
			if(o.val > 0) {
				printf("+ %lld Z ", o.val);
			} else if(o.val < 0) {
				printf("- %lld Z ", -o.val);
			} else {
				printf("Z ");
			}
			
			if(o.ext > 0)
				printf("+ %lld Z ", o.ext);
		}
		
		puts("");
	}
}

namespace Read {
	struct operation {
		LL v;
		bool save;
		bool read;
		
		operation(bool _save = false, bool _read = false, LL _v = 0) {
			save = _save;
			read = _read;
			v = _v;
		}
	};

	vector <operation> parse(const vector <operation> &in) {
		vector <operation> ret;
		for(const auto &o: in) {
			if(ret.size() == 0) {
				ret.push_back(o);
				continue;
			}
			
			if(o.read) {
				while(ret.size() > 0 && !ret.back().save)
					ret.pop_back();
				ret.push_back(o);
			} else if(o.save) {
				if(!ret.back().save)
					ret.push_back(o);
			} else {
				if(!ret.back().save && !ret.back().read) {
					ret[ret.size() - 1].v += o.v;
					if(ret.back().v == 0)
						ret.pop_back();
				}
				else
					ret.push_back(o);
			}
		}
		
		while(ret.size() && !ret.back().save)
			ret.pop_back();
		return ret;
	}
	
	vector <segment> get_segments(const vector <operation> &in) {
		vector <segment> ret;
		int cur = 0;
		while(cur < (int)in.size()) {
			int nxt = cur + 1;
			while(nxt < (int)in.size() && !in[nxt].read)
				++nxt;
			
			LL sum = 0, best = INF;
			for(int i = cur; i < nxt; ++i) {
				const auto &o = in[i];
				if(o.read)
					continue;
				
				if(o.save)
					best = min(best, sum);
				else
					sum += o.v;
			}
			
			ret.push_back(segment(in[cur].read, best, sum - best));
			cur = nxt;
		}
		
		return ret;
	}
	
	vector <segment> read_line() {
		int m;
		scanf("%d", &m);

		vector <operation> ret;
		while(m--) {
			char t;
			scanf(" %c", &t);

			if(t == 'Z') {
				ret.push_back(operation(true));
			} else if(t == 'W') {
				ret.push_back(operation(false, true));
			} else {
				int v;
				scanf("%d", &v);
				
				if(t == '+')
					ret.push_back(operation(false, false, v));
				else
					ret.push_back(operation(false, false, -v));
			}
		}

		ret = parse(ret);
		return get_segments(ret);
	}
	
	void read() {
		scanf("%d", &n);
		for(int i = 1; i <= n; ++i) {
			input[i] = read_line();
			if(input[i].size() == 0) {
				--i, --n;
			}
		}
		
//		for(int i = 1; i <= n; ++i)
//			Debug::write_line(input[i]);
	}
}

namespace Solution {
	
	struct result {
		vector <LL> dp[2][2];
		result(int s) {
			for(int fa = 0; fa < 2; ++fa)
				for(int fb = 0; fb < 2; ++fb)
					dp[fa][fb].resize(s, INF);
		}
	};
	
	result DC(vector <int> &seq, int from, int to) {
		auto ret = result(to - from + 1);
		ret.dp[0][0][0] = 0;

		if(from == to)
			return ret;

		if(from + 1 == to) {
			ret.dp[1][1][1] = seq[from];
			return ret;
		}
		
		int mid = (from + to) / 2;
		auto left = DC(seq, from, mid);
		auto right = DC(seq, mid, to);
		
		int la = mid - from, lb = to - mid;
		for(int fa = 0; fa < 2; ++fa)
			for(int fb = 0; fb < 2; ++fb)
				for(int fc = 0; fc < 2; ++fc)
					for(int fd = 0; fd < 2; ++fd) {
						int ta = 0, tb = 0;
						while(ta < la || tb < lb) {
							if(ta == la)
								++tb;
							else if(tb == lb)
								++ta;
							else if(left.dp[fa][fb][ta + 1] - left.dp[fa][fb][ta] < right.dp[fc][fd][tb + 1] - right.dp[fc][fd][tb])
								++ta;
							else
								++tb;
							
							ret.dp[fa][fd][ta + tb] = min(ret.dp[fa][fd][ta + tb], left.dp[fa][fb][ta] + right.dp[fc][fd][tb]);
							if(fb == 1 && fc == 1)
								ret.dp[fa][fd][ta + tb - 1] = min(ret.dp[fa][fd][ta + tb - 1], left.dp[fa][fb][ta] + right.dp[fc][fd][tb]);								
						}
					}

		return ret;
	}
	
	LL solve(int a, result ans, LL sum, bool hard_st) {
//		printf("solves %d with sum = %lld and type %d\n", a, sum, (int)hard_st);
		int has_start = hard_st;
		int has_end = false;
		int has_both = 0;
		int took = hard_st;
		int m = ans.dp[0][0].size();
		
		/* Can take start from fa? */
		bool can_start = !hard_st || input[a][0].read;
		
		LL single = INF, en_min = INF;
		set <PLL> single_st, single_en;

		multiset <LL> st_cost, en_cost;
		vector <tuple <LL, int, LL> > all;
		
		single_st.insert({INF, -1});
		single_en.insert({INF, -2});

		st_cost.insert(INF);
		en_cost.insert(INF);

		for(int i = 1; i <= n; ++i) {
			if(i == a)
				continue;

			for(int j = 0; j < (int)input[i].size(); ++j) {
				const auto &o = input[i][j];
				if(!o.read)
					continue;

				int type = 0;
				if(j == 0 && j + 1 == (int)input[i].size()) {
					type = -i;
					single_st.insert({o.val, i});
					single_en.insert({o.val + o.ext, i});
				}
				else if(j == 0) {
					type = 1;
					st_cost.insert(o.val);
				}
				else if(j + 1 == (int)input[i].size()) {
					type = 2;
					en_cost.insert(o.val + o.ext);
				}
				
				all.push_back(tuple(o.val, type, o.ext));
			}
		}
				
		LL ret = INF;
		auto update_ans = [&]() {
			for(int fa = 0; fa < 1 + can_start; ++fa) {
				for(int fb = 0; fb < 2; ++fb) {
					LL cand = sum;
					int can_take = took + (hard_st ? 0 : fa) + fb - 1;
					
					if(fa + has_start == 0 && fb == 0) {
						auto mem_st = *single_st.begin();
						auto mem_en = *single_en.begin();

						LL tmp = *st_cost.begin() + *en_cost.begin();
						tmp = min(tmp, *st_cost.begin() + mem_en.st);
						tmp = min(tmp, *en_cost.begin() + mem_st.st);
						
						if(mem_st.nd != mem_en.nd)
							tmp = min(tmp, mem_st.st + mem_en.st);
						else {
							auto it_st = ++single_st.begin();
							auto it_en = ++single_en.begin();
							tmp = min(tmp, mem_st.st + mem_en.st + min((*it_st).st - mem_st.st, (*it_en).st - mem_en.st));
						}
						
						can_take++;
//						printf("for %d %d with %d %d %d we obtain candidate %lld with can_take = %d\n", fa, fb, has_start, has_end, has_both, cand + min(mem_st.st, *st_cost.begin()) + en_min, can_take);
						ret = min(ret, cand + en_min + min(mem_st.st, *st_cost.begin()) + ans.dp[fa][fb][min(can_take, m - 1)]);
						
						cand += tmp;
						can_take++;
					} else {
						if(fa + has_start == 0) {
							cand += min(*st_cost.begin(), (*single_st.begin()).st);
							can_take++;
						}
						
						if(fa == 0 && fb == 0 && has_start == 1 && has_both == 1) {
							cand += min(min(single + *st_cost.begin(), min(*en_cost.begin(), en_min)), min(single + (*single_st.begin()).st, (*single_en.begin()).st));
							can_take++;
						} else if (fb == 0) {
							if(has_end == 0)
								cand += min(*en_cost.begin(), (*single_en.begin()).st);
							else {
//								printf("for %d %d with %d %d %d we obtain candidate %lld with can_take = %d\n", fa, fb, has_start, has_end, has_both, cand + min(single, en_min), can_take);
								ret = min(ret, cand + min(single, en_min) + ans.dp[fa][fb][min(can_take, m - 1)]);
								cand += min(*en_cost.begin(), (*single_en.begin()).st);
							}
							
							can_take++;
						}
					}
					
//					printf("for %d %d with %d %d %d we obtain candidate %lld with can_take = %d\n", fa, fb, has_start, has_end, has_both, cand, can_take);
					ret = min(ret, cand + ans.dp[fa][fb][min(can_take, m - 1)]);
				}
			}
		};
		
		update_ans();
		sort(all.begin(), all.end());

		for(auto [v, t, add]: all) {
//			printf("adds %lld %d %lld\n", v, t, add);
			sum += v;
			took++;

			if(t == 1) {
				has_start++;
				st_cost.erase(st_cost.find(v));
			}
			
			if(t == 2) {
				has_end++;
				en_cost.erase(en_cost.find(v + add));
				en_min = min(en_min, add);
			}
			
			if(t < 0) {
				has_start++, has_end++;
				has_both++;
				
				single_st.erase({v, -t});
				single_en.erase({v + add, -t});
				single = min(single, add);
			}

			update_ans();
		}
		
//		printf("for this solve we get %lld\n", ret);
		return ret;
	}
	
	LL solve(int a) {
		vector <int> seq;
		for(int i = 0; i < (int)input[a].size(); ++i) {
			const auto &o = input[a][i];
			if(o.val > 0 && i > 0)
				seq.push_back(o.val + o.ext);
			else {
				seq.push_back(o.val);
				if(o.ext)
					seq.push_back(o.ext);
			}
		}
		
		auto ans = DC(seq, 0, seq.size());
		for(int fa = 0; fa < 2; ++fa)
			for(int fb = 0; fb < 2; ++fb)
				for(int i = 1; i < (int)ans.dp[fa][fb].size(); ++i)
					ans.dp[fa][fb][i] = min(ans.dp[fa][fb][i], ans.dp[fa][fb][i - 1]);

/*		printf("ans for %d\n", a);
		for(int fa = 0; fa < 2; ++fa)
			for(auto fb = 0; fb < 2; ++fb) {
				printf("ans[%d][%d] = ", fa, fb);
				for(auto v: ans.dp[fa][fb])
					printf("%lld ", v);
				puts("");
			}
	*/	
		LL hard_start = INF;
		for(int i = 1; i <= n; ++i) {
			if(i == a)
				continue;
			
			if(!input[i][0].read)
				hard_start = min(hard_start, input[i][0].val);
		}
		
		LL ret = solve(a, ans, hard_start, 1);
		ret = min(ret, solve(a, ans, 0, 0));
		return ret;
	}
}

LL getPluses() {
	LL ret = INF;
	vector <LL> pref_start(n + 2, INF);
	vector <LL> suf_start(n + 2, INF);
	
	vector <LL> pref_opt(n + 2, INF);
	vector <LL> suf_opt(n + 2, INF);

	for(int i = 1; i <= n; ++i) {
		pref_start[i] = min(pref_start[i - 1], input[i][0].val);
		pref_opt[i] = pref_opt[i - 1];
		
		LL sum = 0;
		for(const auto &o: input[i]) {
			sum += o.val + o.ext;
			if(o.read)
				pref_opt[i] = min(pref_opt[i], o.val);
		}
		
		ret = min(ret, sum);
	}
	
	for(int i = n; i >= 1; --i) {
		suf_start[i] = min(suf_start[i + 1], input[i][0].val);
		suf_opt[i] = suf_opt[i + 1];
		
		for(const auto &o: input[i])
			if(o.read)
				suf_opt[i] = min(suf_opt[i], o.val);
		
		const auto &o = input[i].back();
		if(o.read)
			ret = min(ret, o.val + o.ext + min(pref_start[i - 1], suf_start[i + 1]));
		
		if((int)input[i].size() > 1) {
			ret = min(ret, o.val + o.ext + input[i][0].val + min(pref_opt[i - 1], suf_opt[i + 1]));
		}
	}

	return ret;
}

LL getMinus() {
	vector <PII> segs;
	for(int i = 1; i <= n; ++i) {
		int cnt_seg = 0;
		for(auto &o: input[i])
			if(o.val < 0 && o.read)
				cnt_seg++;
		
		if(cnt_seg > 0)
			segs.push_back({cnt_seg, i});
	}
	
	if(segs.size() == 0)
		return INF;
	sort(segs.begin(), segs.end());
	
	if(segs.size() == 2 && segs[0].st == segs[1].st)
		return max(Solution::solve(segs[0].nd), Solution::solve(segs[1].nd));
	return Solution::solve(segs.back().nd);
}

void solve() {
	Read::read();
	if(n == 0) {
		puts("0");
		return;
	}
	
	LL ans = getPluses();

	if(n > 1)
		ans = min(ans, getMinus());
	printf("%lld\n", ans);
}

int main() {
	int cases;
	scanf("%d", &cases);
	
	while(cases--)
		solve();
	return 0;
}