第一次碰到这种题目,模板啊orz
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#define ls rt << 1
#define rs rt << 1 | 1
#define pi acos(-1.0)
#define eps 1e-8
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int N = 1002;
int pos, len;
char a[N];
int dfs()
{
if( pos >= len )
return 0;
if( a[pos] == 'a' )
{
pos += 3;
return dfs() + dfs();
}
else if( a[pos] == 'm' )
{
pos += 3;
if( a[pos-2] == 'i' )
return min( dfs(), dfs() );
else
return max( dfs(), dfs() );
}
else if( a[pos] == '(' || a[pos] == ')' )
{
pos++;
return dfs();
}
else if( a[pos] == ',' )
{
pos++;
return dfs();
}
else if( a[pos] >= '0' && a[pos] <= '9' )
{
int val = 0;
while( a[pos] >= '0' && a[pos] <= '9' )
{
val = val * 10 + a[pos] - '0';
pos++;
}
return val;
}
}
int main()
{
//freopen("test.in", "r", stdin);
//freopen("qqq.txt", "w", stdout);
int tot;
scanf("%d", &tot);
while( tot-- )
{
scanf("%s", a);
pos = 0;
len = strlen(a);
int ans = dfs();
printf("%d\n", ans);
}
return 0;
}