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
#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define ST first
#define ND second
#define PB push_back
#define SIZE(a) ((int)a.size())

template<class T, class U>
ostream& operator<<(ostream &stream, pair<T, U> &p) {
    stream << "(" << p.ST << "," << p.ND << ")";
    return stream;
}

template<class T>
ostream& operator<<(ostream &stream, vector<T> &v) {
    stream << "[";
    for(auto elem : v) {
        stream << elem << ", ";
    }
    stream << "]";
    return stream;
}

const ll INF = 1e18;
ll res;
const int maxv = 1e9;

void rec(vector<int> &ids, vector<vector<int>> &v, vector<ll> &y, ll x) {
    int end = true;
    // cout << ids << "\n";
    for(int i=0; i < SIZE(ids); i++) {
        int id = ids[i];
        if(id < SIZE(v[i])) {
            ids[i]++;
            vector<ll> yn = y;
            ll xn = x;
            int o = v[i][id];
            if(o == maxv+1) {
                yn[i] = xn;
            } else if(o == maxv+2) {
                xn = yn[i];
            } else {
                yn[i]+=o;
            }
            rec(ids, v, yn, xn);
            ids[i]--;
            end = false;
        }
    }
    if(end) {
        // cout << x << "\n";
        // cout << y << "\n";
        res = min(res, x);
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    int t;
    cin >> t;
    while(t--) {
        res=INF;
        int n;
        cin >> n;
        vector<vector<int>> v(n);
        for(int i=0; i < n; i++) {
            int m;
            cin >> m;
            vector<int> p(m);
            for(int &o : p) {
                char c;
                cin >> c;
                if(c == 'W') {
                    o = maxv+1;
                } else if(c == 'Z') {
                    o = maxv+2;
                } else if(c == '+') {
                    cin >> o;
                } else {
                    cin >> o;
                    o*=-1;
                }
            }
            v[i] = p;
            // cout << p << "\n";
        }
        vector<int> init(n);
        vector<ll> y(n);
        rec(init, v, y, 0);
        cout << res << "\n";
    }
}