#include <cstdio>
#include <cstring>
#include <algorithm>
#pragma GCC diagnostic ignored "-Wlong-long"
using namespace std;
int n;
char s[500001];
int lval[500000];
int rval[500000];
bool is_possible() {
int l = 0, r = 0; // [l, r]
for(int i = 0; i < n; i++) {
switch(s[i]) {
case '(': l++; r++; break;
case ')': l--; r--; break;
case '?': l--; r++; break;
}
if(l < 0) l += 2;
if(l > r) return false;
}
return l == 0;
}
struct Cmp {
bool operator() (int lhs, int rhs) {
return (long long) rval[lhs] - lval[lhs] < (long long) rval[rhs] - lval[rhs];
}
};
int vec[500000];
long long solve() {
int m = 0;
for(int i = 0; i < n; i++)
if(s[i] == '?')
vec[m++] = i;
sort(vec, vec + m, Cmp());
long long val = 0;
for(int index = 0; index < m; index++) {
int i = vec[index];
s[i] = ')';
if(is_possible()) {
val += rval[i];
} else {
s[i] = '(';
val += lval[i];
}
}
return val;
}
int main(){
int t;
scanf("%d", &t);
while(t--) {
scanf("%s", s);
n = strlen(s);
for(int i = 0; i < n; i++)
if(s[i] == '?')
scanf("%d %d", lval + i, rval + i);
if(is_possible()) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat"
printf("%lld\n", solve());
#pragma GCC diagnostic pop
} else {
puts("IMBA");
}
}
}
I2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPGNzdHJpbmc+CiNpbmNsdWRlIDxhbGdvcml0aG0+CiNwcmFnbWEgR0NDIGRpYWdub3N0aWMgaWdub3JlZCAiLVdsb25nLWxvbmciCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CmludCBuOwpjaGFyIHNbNTAwMDAxXTsKaW50IGx2YWxbNTAwMDAwXTsKaW50IHJ2YWxbNTAwMDAwXTsKCmJvb2wgaXNfcG9zc2libGUoKSB7CiAgICBpbnQgbCA9IDAsIHIgPSAwOyAvLyBbbCwgcl0KICAgIGZvcihpbnQgaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgICAgICBzd2l0Y2goc1tpXSkgewogICAgICAgIGNhc2UgJygnOiBsKys7IHIrKzsgYnJlYWs7CiAgICAgICAgY2FzZSAnKSc6IGwtLTsgci0tOyBicmVhazsKICAgICAgICBjYXNlICc/JzogbC0tOyByKys7IGJyZWFrOwogICAgICAgIH0KICAgICAgICBpZihsIDwgMCkgbCArPSAyOwogICAgICAgIGlmKGwgPiByKSByZXR1cm4gZmFsc2U7CiAgICB9CiAgICByZXR1cm4gbCA9PSAwOwp9CgpzdHJ1Y3QgQ21wIHsKICAgIGJvb2wgb3BlcmF0b3IoKSAoaW50IGxocywgaW50IHJocykgewogICAgICAgICByZXR1cm4gKGxvbmcgbG9uZykgcnZhbFtsaHNdIC0gbHZhbFtsaHNdIDwgKGxvbmcgbG9uZykgcnZhbFtyaHNdIC0gbHZhbFtyaHNdOwogICAgfQp9OwoKaW50IHZlY1s1MDAwMDBdOwoKbG9uZyBsb25nIHNvbHZlKCkgewogICAgaW50IG0gPSAwOwogICAgZm9yKGludCBpID0gMDsgaSA8IG47IGkrKykKICAgICAgICBpZihzW2ldID09ICc/JykKICAgICAgICAgICAgdmVjW20rK10gPSBpOwogICAgc29ydCh2ZWMsIHZlYyArIG0sIENtcCgpKTsKICAgIGxvbmcgbG9uZyB2YWwgPSAwOwogICAgZm9yKGludCBpbmRleCA9IDA7IGluZGV4IDwgbTsgaW5kZXgrKykgewogICAgICAgIGludCBpID0gdmVjW2luZGV4XTsKICAgICAgICBzW2ldID0gJyknOwogICAgICAgIGlmKGlzX3Bvc3NpYmxlKCkpIHsKICAgICAgICAgICAgdmFsICs9IHJ2YWxbaV07CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgc1tpXSA9ICcoJzsKICAgICAgICAgICAgdmFsICs9IGx2YWxbaV07CiAgICAgICAgfQogICAgfQogICAgcmV0dXJuIHZhbDsKfQoKaW50IG1haW4oKXsKICAgIGludCB0OwogICAgc2NhbmYoIiVkIiwgJnQpOwogICAgd2hpbGUodC0tKSB7CiAgICAgICAgc2NhbmYoIiVzIiwgcyk7CiAgICAgICAgbiA9IHN0cmxlbihzKTsKICAgICAgICBmb3IoaW50IGkgPSAwOyBpIDwgbjsgaSsrKQogICAgICAgICAgICBpZihzW2ldID09ICc/JykKICAgICAgICAgICAgICAgIHNjYW5mKCIlZCAlZCIsIGx2YWwgKyBpLCBydmFsICsgaSk7CiAgICAgICAgaWYoaXNfcG9zc2libGUoKSkgewogICAgICAgICAgICAjcHJhZ21hIEdDQyBkaWFnbm9zdGljIHB1c2gKICAgICAgICAgICAgI3ByYWdtYSBHQ0MgZGlhZ25vc3RpYyBpZ25vcmVkICItV2Zvcm1hdCIKICAgICAgICAgICAgcHJpbnRmKCIlbGxkXG4iLCBzb2x2ZSgpKTsKICAgICAgICAgICAgI3ByYWdtYSBHQ0MgZGlhZ25vc3RpYyBwb3AKICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBwdXRzKCJJTUJBIik7CiAgICAgICAgfQogICAgfQp9Cg==