#include<stdio.h>
#include<string.h>
int stack(char a,int b,int c){
if(a=='d')
return b+c;
else if(a=='a')
return b>c?b:c;
else if(a=='i')
return b<c?b:c;}
int main()
{
int n,len,btop,ctop,num,i;
char a[310],b[310];
int c[310];
scanf("%d",&n);
while(n--){
btop=1;
ctop=1;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
scanf("%s",a);
len=strlen(a);
for(i=0;i<len;){
if(a[i]>='a'&&a[i]<='z'){
i++;
if(a[i]=='d')
b[btop++]='d';
else if(a[i]=='a')
b[btop++]='a';
else if(a[i]=='i')
b[btop++]='i';
i+=2;}
else if(a[i]=='('||a[i]==',')
i++;
else if(a[i]>='0'&&a[i]<='9'){
num=0;
while(a[i]>='0'&&a[i]<='9'){
num=num*10+a[i]-'0';
i++;}
c[ctop++]=num;}
else if(a[i]==')'){
num=stack(b[--btop],c[--ctop],c[--ctop]);
c[ctop++]=num;
i++;}
}
printf("%d\n",num);
}
return 0;
}
#include<string.h>
int stack(char a,int b,int c){
if(a=='d')
return b+c;
else if(a=='a')
return b>c?b:c;
else if(a=='i')
return b<c?b:c;}
int main()
{
int n,len,btop,ctop,num,i;
char a[310],b[310];
int c[310];
scanf("%d",&n);
while(n--){
btop=1;
ctop=1;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
scanf("%s",a);
len=strlen(a);
for(i=0;i<len;){
if(a[i]>='a'&&a[i]<='z'){
i++;
if(a[i]=='d')
b[btop++]='d';
else if(a[i]=='a')
b[btop++]='a';
else if(a[i]=='i')
b[btop++]='i';
i+=2;}
else if(a[i]=='('||a[i]==',')
i++;
else if(a[i]>='0'&&a[i]<='9'){
num=0;
while(a[i]>='0'&&a[i]<='9'){
num=num*10+a[i]-'0';
i++;}
c[ctop++]=num;}
else if(a[i]==')'){
num=stack(b[--btop],c[--ctop],c[--ctop]);
c[ctop++]=num;
i++;}
}
printf("%d\n",num);
}
return 0;
}