/**
* 这题用的是STL,第一次用STL做题,感觉挺方便的!
* 不过这题还是贪心的思想吧,猜想:求最大值的时候先相加后相乘,求最小值的时候先相乘后相加。
* 证明: 依题: a,b,c,d...>= 1. 所以有 a + b * c > (a + b)*c 相减下就行了
* 所以以上思路是正确的。
* 这里说下实现方法,毕竟是第一次用stack.
* 用两个栈numPlus<int>, numMul<int>存需要相加的数和需要相乘的数。
* 求最大值:
* 在存储完字符串后,遍历一下,遇到数字就求sum,也就是当前数值。 用sum = sum * 10 + ch - '0' 这里ch即为当前字符。
* 遇到 * 号,把之前能加的数,也就是把numPlus<int> 里的所有数取出来相加,后把结果存入numMul<int>
* 遇到 + 号,把当前的sum加入到numPlus<int> 记住每次操作到要把sum 设回0.
* 求最小值可以按照最大值的思路。 类比下就行。。。。其实也就是个优先级的问题,先算哪些而已。
*/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <stack>
#include <algorithm>
#define INF 0x7fffffff
using namespace std;
int len;
char ch, str[100];
long long maxAns, minAns, sum, ope1, ope2;
stack<long long> numMul, numPlus;
void calMin() {
sum = 0;
while(!numMul.empty()) numMul.pop();
while(!numPlus.empty()) numPlus.pop();
for(int i = 0; i < len; i ++) {
ch = str[i];
if(ch >= '0' && ch <= '9') {
sum = sum * 10 + ch - '0';
} else if(ch == '*') {
numMul.push(sum);
sum = 0;
} else if(ch == '+') {
ope1 = sum;
while(!numMul.empty()) {
ope2 = numMul.top();
numMul.pop();
ope1 *= ope2;
}
numPlus.push(ope1);
sum = 0;
}
}
numMul.push(sum);
ope1 = 1;
while(numMul.empty() == false) {
ope2 = numMul.top();
numMul.pop();
ope1 *= ope2;
}
numPlus.push(ope1);
minAns = 0;
while(!numPlus.empty()) {
minAns += numPlus.top();
numPlus.pop();
}
}
void calMax() {
sum = 0;
while(!numMul.empty()) numMul.pop();
while(!numPlus.empty()) numPlus.pop();
for(int i = 0; i < len; i ++) {
ch = str[i];
if(ch >= '0' && ch <= '9') {
sum = sum * 10 + ch - '0';
} else if(ch == '+') {
numPlus.push(sum);
sum = 0;
} else if(ch == '*') {
ope1 = sum;
while(!numPlus.empty()) {
ope2 = numPlus.top();
numPlus.pop();
ope1 += ope2;
}
numMul.push(ope1);
sum = 0;
}
}
numPlus.push(sum);
ope1 = 0;
while(numPlus.empty() == false) {
ope2 = numPlus.top();
numPlus.pop();
ope1 += ope2;
}
numMul.push(ope1);
maxAns = 1;
while(!numMul.empty()) {
maxAns *= numMul.top();
numMul.pop();
}
}
int main()
{
int n;
scanf("%d", &n);
getchar();
while(n --) {
scanf("%s", str);
len = strlen(str);
calMax();
calMin();
printf("The maximum and minimum are %lld and %lld.\n", maxAns, minAns);
}
return 0;
}