#include <cstdio>
#include <cstring>
#include <utility>
#include <algorithm>
#pragma GCC diagnostic ignored "-Wlong-long"
using namespace std;
int n;
char s[500001];
int lval[500000];
int rval[500000];
long long dp[2][500001];
const long long impossible = 500000LL * 2147483647LL + 1;
long long solve() {
dp[1][0] = 0;
fill_n(dp[1] + 1, n, impossible * 2);
for(int i = 0; i < n; i++) {
int t = i % 2, f = 1 - t;
switch(s[i]) {
case '(':
dp[t][0] = impossible * 2;
copy(dp[f], dp[f] + n, dp[t] + 1);
break;
case ')':
copy(dp[f] + 1, dp[f] + n + 1, dp[t]);
dp[t][n] = impossible * 2;
break;
case '?':
dp[t][0] = dp[f][1] + rval[i];
for(int j = 1; j < n; j++)
dp[t][j] = min(dp[f][j - 1] + lval[i], dp[f][j + 1] + rval[i]);
dp[t][n] = dp[f][n - 1] + lval[i];
break;
}
}
return dp[(n - 1) % 2][0];
}
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);
long long x = solve();
if(x >= impossible) {
puts("IMBA");
} else {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat"
printf("%lld\n", x);
#pragma GCC diagnostic pop
}
}
}
I2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPGNzdHJpbmc+CiNpbmNsdWRlIDx1dGlsaXR5PgojaW5jbHVkZSA8YWxnb3JpdGhtPgojcHJhZ21hIEdDQyBkaWFnbm9zdGljIGlnbm9yZWQgIi1XbG9uZy1sb25nIgp1c2luZyBuYW1lc3BhY2Ugc3RkOwppbnQgbjsKY2hhciBzWzUwMDAwMV07CmludCBsdmFsWzUwMDAwMF07CmludCBydmFsWzUwMDAwMF07Cgpsb25nIGxvbmcgZHBbMl1bNTAwMDAxXTsKCmNvbnN0IGxvbmcgbG9uZyBpbXBvc3NpYmxlID0gNTAwMDAwTEwgKiAyMTQ3NDgzNjQ3TEwgKyAxOwoKbG9uZyBsb25nIHNvbHZlKCkgewogICAgZHBbMV1bMF0gPSAwOwogICAgZmlsbF9uKGRwWzFdICsgMSwgbiwgaW1wb3NzaWJsZSAqIDIpOwogICAgZm9yKGludCBpID0gMDsgaSA8IG47IGkrKykgewogICAgICAgIGludCB0ID0gaSAlIDIsIGYgPSAxIC0gdDsKICAgICAgICBzd2l0Y2goc1tpXSkgewogICAgICAgIGNhc2UgJygnOgogICAgICAgICAgICBkcFt0XVswXSA9IGltcG9zc2libGUgKiAyOwogICAgICAgICAgICBjb3B5KGRwW2ZdLCBkcFtmXSArIG4sIGRwW3RdICsgMSk7CiAgICAgICAgICAgIGJyZWFrOwogICAgICAgIGNhc2UgJyknOgogICAgICAgICAgICBjb3B5KGRwW2ZdICsgMSwgZHBbZl0gKyBuICsgMSwgZHBbdF0pOwogICAgICAgICAgICBkcFt0XVtuXSA9IGltcG9zc2libGUgKiAyOwogICAgICAgICAgICBicmVhazsKICAgICAgICBjYXNlICc/JzoKICAgICAgICAgICAgZHBbdF1bMF0gPSBkcFtmXVsxXSArIHJ2YWxbaV07CiAgICAgICAgICAgIGZvcihpbnQgaiA9IDE7IGogPCBuOyBqKyspCiAgICAgICAgICAgICAgICBkcFt0XVtqXSA9IG1pbihkcFtmXVtqIC0gMV0gKyBsdmFsW2ldLCBkcFtmXVtqICsgMV0gKyBydmFsW2ldKTsKICAgICAgICAgICAgZHBbdF1bbl0gPSBkcFtmXVtuIC0gMV0gKyBsdmFsW2ldOwogICAgICAgICAgICBicmVhazsKICAgICAgICB9CiAgICB9CiAgICByZXR1cm4gZHBbKG4gLSAxKSAlIDJdWzBdOwp9CgoKaW50IG1haW4oKXsKICAgIGludCB0OwogICAgc2NhbmYoIiVkIiwgJnQpOwogICAgd2hpbGUodC0tKSB7CiAgICAgICAgc2NhbmYoIiVzIiwgcyk7CiAgICAgICAgbiA9IHN0cmxlbihzKTsKICAgICAgICBmb3IoaW50IGkgPSAwOyBpIDwgbjsgaSsrKQogICAgICAgICAgICBpZihzW2ldID09ICc/JykKICAgICAgICAgICAgICAgIHNjYW5mKCIlZCAlZCIsIGx2YWwgKyBpLCBydmFsICsgaSk7CiAgICAgICAgbG9uZyBsb25nIHggPSBzb2x2ZSgpOwogICAgICAgIGlmKHggPj0gaW1wb3NzaWJsZSkgewogICAgICAgICAgICBwdXRzKCJJTUJBIik7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgI3ByYWdtYSBHQ0MgZGlhZ25vc3RpYyBwdXNoCiAgICAgICAgICAgICNwcmFnbWEgR0NDIGRpYWdub3N0aWMgaWdub3JlZCAiLVdmb3JtYXQiCiAgICAgICAgICAgIHByaW50ZigiJWxsZFxuIiwgeCk7CiAgICAgICAgICAgICNwcmFnbWEgR0NDIGRpYWdub3N0aWMgcG9wCiAgICAgICAgfQogICAgfQp9Cg==