#include<iostream>
#include<string>
#include<algorithm>
#include<iomanip>
#include<vector>
#define sz size()
using namespace std;
string str1Copy(""),strAns;
vector<string> store,storeAdd;
void getSum(string str1,string str2) {
strAns.erase();
if(str2=="0") {
strAns=str1;
return;
}
int carry=0,i=0,j=0,sum=0;
for(i=str1.sz-1,j=str2.sz-1;i>=0&&j>=0;i--,j--) {
sum=(str1[i]-'0')+(str2[j]-'0')+carry;
carry=sum==0?0:sum/10;
sum%=10;
strAns=char(sum+'0')+strAns;
}
while(i>=0) {
sum=carry+(str1[i]-'0');
carry=sum==0?0:sum/10;
sum%=10;
strAns=char(sum+'0')+strAns;
i--;
}
while(j>=0) {
sum=carry+(str2[j]-'0');
carry=sum==0?0:sum/10;
sum%=10;
strAns=char(sum+'0')+strAns;
j--;
}
strAns=carry>0?char(carry+'0')+strAns:strAns;
}
void getDiff(string str1, string str2) {
strAns.erase();
int carry=0,i=0,j,diff=0,temp=0;
for(i=str1.sz-1,j=str2.sz-1;i>=0&&j>=0;i--,j--) {
diff=(str1[i]-'0')-(str2[j]-'0');
if(diff>=0) {
strAns=char(diff+'0')+strAns;
continue;
}
diff+=10;
strAns=char(diff+'0')+strAns;
temp=i-1;
while(str1[temp]=='0') {
str1[temp]='9';
temp--;
}
str1[temp]--;
}
while(i>=0) {
strAns=str1[i]+strAns;
i--;
}
for(i=0;strAns[i]=='0';)
strAns.erase(i,1);
}
int main() {
int setwLen=0,cases=0,i=0,pos=0,j=0,caseVar=0;
string str(""),str1(""),str2(""),strDash(""),tempStr1("");
cin>>cases;
for(caseVar=0;caseVar<cases;caseVar++) {
strDash.erase();
str.erase();
str1.erase();
str2.erase();
cin>>str;
pos=str.find_first_of("+-*");
if(str[pos]=='+') {
str1=str.substr(0,pos);
str2=str.substr(pos+1);
getSum(str1,str2);
setwLen=max(strAns.sz,max(str2.sz+1,str1.sz));
cout<<setw(setwLen)<<str1<<"\n";
cout<<setw(setwLen-str2.sz)<<'+'<<str2<<"\n";
//setwLen=max(strAns.sz,str2.sz+1);
//cout<<setfill('-')<<setw(setwLen)<<"-"<<"\n";
for(i=0;i<max(strAns.sz,str2.sz+1);i++)
strDash+='-';
cout<<setw(setwLen)<<strDash<<"\n";
setwLen=max(strAns.sz,max(str2.sz+1,str1.sz));
cout<<setfill(' ')<<setw(setwLen)<<strAns<<"\n\n";
}
else if(str[pos]=='-') {
strDash.erase();
str1Copy.erase();
str1Copy=str1=str.substr(0,pos);
str2=str.substr(pos+1);
getDiff(str1Copy,str2);
setwLen=max(strAns.sz,max(str2.sz+1,str1.sz));
cout<<setw(setwLen)<<str1<<"\n";
cout<<setw(setwLen-str2.sz)<<'-'<<str2<<"\n";
for(i=0;i<max(strAns.sz,str2.sz+1);i++)
strDash+='-';
cout<<setw(setwLen)<<strDash<<"\n";
setwLen=max(strAns.sz,max(str2.sz+1,str1.sz));
cout<<setfill(' ')<<setw(setwLen)<<strAns<<"\n\n";
}
else if(str[pos]=='*') {
tempStr1.erase();
strDash.erase();
store.erase(store.begin(),store.end());
storeAdd.erase(store.begin(),store.end());
str1=str.substr(0,pos);
str2=str.substr(pos+1);
tempStr1=str1;
for(i=str2.sz-1;i>=0;i--) {
if(str2[i]=='0') {
tempStr1="0";
goto skip;
}
else if(str2[i]=='1') {
tempStr1=str1;
goto skip;
}
for(j=0,tempStr1=str1;j<(str2[i]-'0')-1;j++) {
getSum(tempStr1,str1);
tempStr1=strAns;
}
skip:
store.push_back(tempStr1);
storeAdd.push_back(tempStr1);
}
for(i=1;i<store.sz;i++)
for(j=0;j<i;j++)
storeAdd[i]+='0';
tempStr1=storeAdd[0];
for(i=1;i<store.size();i++) {
getSum(tempStr1,storeAdd[i]);
tempStr1=strAns;
}
setwLen=max(tempStr1.sz,max(str1.sz,str2.sz+1));
cout<<setw(setwLen)<<str1<<"\n";
cout<<setw(setwLen-str2.sz)<<'*'<<str2<<"\n";
for(i=0;i<max(str2.sz+1,store[0].sz);i++)
strDash+='-';
cout<<setw(setwLen)<<strDash<<"\n";
for(i=0;i<store.size();i++)
cout<<setw(setwLen-i)<<store[i]<<"\n";
if(store.sz==1)
continue;
strDash.erase();
for(i=0;i<max(tempStr1.sz,store[store.sz-1].sz);i++)
strDash+='-';
cout<<setw(setwLen)<<strDash<<"\n";
cout<<setw(setwLen)<<tempStr1<<"\n\n";
}
}
return 0;
}
I2luY2x1ZGU8aW9zdHJlYW0+CiNpbmNsdWRlPHN0cmluZz4KI2luY2x1ZGU8YWxnb3JpdGhtPgojaW5jbHVkZTxpb21hbmlwPgojaW5jbHVkZTx2ZWN0b3I+CiNkZWZpbmUgc3ogc2l6ZSgpCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnN0cmluZyBzdHIxQ29weSgiIiksc3RyQW5zOwp2ZWN0b3I8c3RyaW5nPiBzdG9yZSxzdG9yZUFkZDsKdm9pZCBnZXRTdW0oc3RyaW5nIHN0cjEsc3RyaW5nIHN0cjIpIHsKICAgIHN0ckFucy5lcmFzZSgpOwogICAgaWYoc3RyMj09IjAiKSB7CiAgICAgICAgc3RyQW5zPXN0cjE7CiAgICAgICAgcmV0dXJuOwogICAgfQogICAgaW50IGNhcnJ5PTAsaT0wLGo9MCxzdW09MDsKICAgIGZvcihpPXN0cjEuc3otMSxqPXN0cjIuc3otMTtpPj0wJiZqPj0wO2ktLSxqLS0pIHsKICAgICAgICBzdW09KHN0cjFbaV0tJzAnKSsoc3RyMltqXS0nMCcpK2NhcnJ5OwogICAgICAgIGNhcnJ5PXN1bT09MD8wOnN1bS8xMDsKICAgICAgICBzdW0lPTEwOwogICAgICAgIHN0ckFucz1jaGFyKHN1bSsnMCcpK3N0ckFuczsKICAgIH0KICAgIHdoaWxlKGk+PTApIHsKICAgICAgICBzdW09Y2FycnkrKHN0cjFbaV0tJzAnKTsKICAgICAgICBjYXJyeT1zdW09PTA/MDpzdW0vMTA7CiAgICAgICAgc3VtJT0xMDsKICAgICAgICBzdHJBbnM9Y2hhcihzdW0rJzAnKStzdHJBbnM7CiAgICAgICAgaS0tOwogICAgfQogICAgd2hpbGUoaj49MCkgewogICAgICAgIHN1bT1jYXJyeSsoc3RyMltqXS0nMCcpOwogICAgICAgIGNhcnJ5PXN1bT09MD8wOnN1bS8xMDsKICAgICAgICBzdW0lPTEwOwogICAgICAgIHN0ckFucz1jaGFyKHN1bSsnMCcpK3N0ckFuczsKICAgICAgICBqLS07CiAgICB9CiAgICBzdHJBbnM9Y2Fycnk+MD9jaGFyKGNhcnJ5KycwJykrc3RyQW5zOnN0ckFuczsKfQoKdm9pZCBnZXREaWZmKHN0cmluZyBzdHIxLCBzdHJpbmcgc3RyMikgewogICAgc3RyQW5zLmVyYXNlKCk7CiAgICBpbnQgY2Fycnk9MCxpPTAsaixkaWZmPTAsdGVtcD0wOwogICAgZm9yKGk9c3RyMS5zei0xLGo9c3RyMi5zei0xO2k+PTAmJmo+PTA7aS0tLGotLSkgewogICAgICAgIGRpZmY9KHN0cjFbaV0tJzAnKS0oc3RyMltqXS0nMCcpOwogICAgICAgIGlmKGRpZmY+PTApIHsKICAgICAgICBzdHJBbnM9Y2hhcihkaWZmKycwJykrc3RyQW5zOwogICAgICAgIGNvbnRpbnVlOwogICAgICAgIH0KICAgICAgICBkaWZmKz0xMDsKICAgICAgICBzdHJBbnM9Y2hhcihkaWZmKycwJykrc3RyQW5zOwogICAgICAgIHRlbXA9aS0xOwogICAgICAgIHdoaWxlKHN0cjFbdGVtcF09PScwJykgewogICAgICAgICAgICBzdHIxW3RlbXBdPSc5JzsKICAgICAgICAgICAgdGVtcC0tOwogICAgICAgIH0KICAgICAgICBzdHIxW3RlbXBdLS07CiAgICB9CiAgICB3aGlsZShpPj0wKSB7CiAgICAgICAgc3RyQW5zPXN0cjFbaV0rc3RyQW5zOwogICAgICAgIGktLTsKICAgIH0KICAgIGZvcihpPTA7c3RyQW5zW2ldPT0nMCc7KQogICAgICAgICAgICBzdHJBbnMuZXJhc2UoaSwxKTsKfQoKaW50IG1haW4oKSB7CiAgICBpbnQgc2V0d0xlbj0wLGNhc2VzPTAsaT0wLHBvcz0wLGo9MCxjYXNlVmFyPTA7CiAgICBzdHJpbmcgc3RyKCIiKSxzdHIxKCIiKSxzdHIyKCIiKSxzdHJEYXNoKCIiKSx0ZW1wU3RyMSgiIik7CiAgICBjaW4+PmNhc2VzOwogICAgZm9yKGNhc2VWYXI9MDtjYXNlVmFyPGNhc2VzO2Nhc2VWYXIrKykgewogICAgICAgIHN0ckRhc2guZXJhc2UoKTsKICAgICAgICBzdHIuZXJhc2UoKTsKICAgICAgICBzdHIxLmVyYXNlKCk7CiAgICAgICAgc3RyMi5lcmFzZSgpOwogICAgICAgIGNpbj4+c3RyOwogICAgICAgIHBvcz1zdHIuZmluZF9maXJzdF9vZigiKy0qIik7CiAgICAgICAgaWYoc3RyW3Bvc109PScrJykgewogICAgICAgICAgICBzdHIxPXN0ci5zdWJzdHIoMCxwb3MpOwogICAgICAgICAgICBzdHIyPXN0ci5zdWJzdHIocG9zKzEpOwogICAgICAgICAgICBnZXRTdW0oc3RyMSxzdHIyKTsKICAgICAgICAgICAgc2V0d0xlbj1tYXgoc3RyQW5zLnN6LG1heChzdHIyLnN6KzEsc3RyMS5zeikpOwogICAgICAgICAgICBjb3V0PDxzZXR3KHNldHdMZW4pPDxzdHIxPDwiXG4iOwogICAgICAgICAgICBjb3V0PDxzZXR3KHNldHdMZW4tc3RyMi5zeik8PCcrJzw8c3RyMjw8IlxuIjsKICAgICAgICAgICAgLy9zZXR3TGVuPW1heChzdHJBbnMuc3osc3RyMi5zeisxKTsKICAgICAgICAgICAgLy9jb3V0PDxzZXRmaWxsKCctJyk8PHNldHcoc2V0d0xlbik8PCItIjw8IlxuIjsKICAgICAgICAgICAgZm9yKGk9MDtpPG1heChzdHJBbnMuc3osc3RyMi5zeisxKTtpKyspCiAgICAgICAgICAgICAgICBzdHJEYXNoKz0nLSc7CiAgICAgICAgICAgIGNvdXQ8PHNldHcoc2V0d0xlbik8PHN0ckRhc2g8PCJcbiI7CiAgICAgICAgICAgIHNldHdMZW49bWF4KHN0ckFucy5zeixtYXgoc3RyMi5zeisxLHN0cjEuc3opKTsKICAgICAgICAgICAgY291dDw8c2V0ZmlsbCgnICcpPDxzZXR3KHNldHdMZW4pPDxzdHJBbnM8PCJcblxuIjsKICAgICAgICB9CiAgICAgICAgZWxzZSBpZihzdHJbcG9zXT09Jy0nKSAgewogICAgICAgICAgICBzdHJEYXNoLmVyYXNlKCk7CiAgICAgICAgICAgIHN0cjFDb3B5LmVyYXNlKCk7CiAgICAgICAgICAgIHN0cjFDb3B5PXN0cjE9c3RyLnN1YnN0cigwLHBvcyk7CiAgICAgICAgICAgIHN0cjI9c3RyLnN1YnN0cihwb3MrMSk7CiAgICAgICAgICAgIGdldERpZmYoc3RyMUNvcHksc3RyMik7CiAgICAgICAgICAgIHNldHdMZW49bWF4KHN0ckFucy5zeixtYXgoc3RyMi5zeisxLHN0cjEuc3opKTsKICAgICAgICAgICAgY291dDw8c2V0dyhzZXR3TGVuKTw8c3RyMTw8IlxuIjsKICAgICAgICAgICAgY291dDw8c2V0dyhzZXR3TGVuLXN0cjIuc3opPDwnLSc8PHN0cjI8PCJcbiI7CiAgICAgICAgICAgIGZvcihpPTA7aTxtYXgoc3RyQW5zLnN6LHN0cjIuc3orMSk7aSsrKQogICAgICAgICAgICAgICAgc3RyRGFzaCs9Jy0nOwogICAgICAgICAgICBjb3V0PDxzZXR3KHNldHdMZW4pPDxzdHJEYXNoPDwiXG4iOwogICAgICAgICAgICBzZXR3TGVuPW1heChzdHJBbnMuc3osbWF4KHN0cjIuc3orMSxzdHIxLnN6KSk7CiAgICAgICAgICAgIGNvdXQ8PHNldGZpbGwoJyAnKTw8c2V0dyhzZXR3TGVuKTw8c3RyQW5zPDwiXG5cbiI7CiAgICAgICAgfQogICAgICAgIGVsc2UgaWYoc3RyW3Bvc109PScqJykgewogICAgICAgICAgICB0ZW1wU3RyMS5lcmFzZSgpOwogICAgICAgICAgICBzdHJEYXNoLmVyYXNlKCk7CiAgICAgICAgICAgIHN0b3JlLmVyYXNlKHN0b3JlLmJlZ2luKCksc3RvcmUuZW5kKCkpOwogICAgICAgICAgICBzdG9yZUFkZC5lcmFzZShzdG9yZS5iZWdpbigpLHN0b3JlLmVuZCgpKTsKICAgICAgICAgICAgc3RyMT1zdHIuc3Vic3RyKDAscG9zKTsKICAgICAgICAgICAgc3RyMj1zdHIuc3Vic3RyKHBvcysxKTsKICAgICAgICAgICAgdGVtcFN0cjE9c3RyMTsKICAgICAgICAgICAgZm9yKGk9c3RyMi5zei0xO2k+PTA7aS0tKSB7CiAgICAgICAgICAgICAgICBpZihzdHIyW2ldPT0nMCcpIHsKICAgICAgICAgICAgICAgICAgICB0ZW1wU3RyMT0iMCI7CiAgICAgICAgICAgICAgICAgICAgZ290byBza2lwOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgZWxzZSBpZihzdHIyW2ldPT0nMScpIHsKICAgICAgICAgICAgICAgICAgICB0ZW1wU3RyMT1zdHIxOwogICAgICAgICAgICAgICAgICAgIGdvdG8gc2tpcDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGZvcihqPTAsdGVtcFN0cjE9c3RyMTtqPChzdHIyW2ldLScwJyktMTtqKyspIHsKICAgICAgICAgICAgICAgIGdldFN1bSh0ZW1wU3RyMSxzdHIxKTsKICAgICAgICAgICAgICAgIHRlbXBTdHIxPXN0ckFuczsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHNraXA6CiAgICAgICAgICAgICAgICBzdG9yZS5wdXNoX2JhY2sodGVtcFN0cjEpOwogICAgICAgICAgICAgICAgc3RvcmVBZGQucHVzaF9iYWNrKHRlbXBTdHIxKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBmb3IoaT0xO2k8c3RvcmUuc3o7aSsrKQogICAgICAgICAgICAgICAgZm9yKGo9MDtqPGk7aisrKQogICAgICAgICAgICAgICAgICAgIHN0b3JlQWRkW2ldKz0nMCc7CiAgICAgICAgICAgIHRlbXBTdHIxPXN0b3JlQWRkWzBdOwogICAgICAgICAgICBmb3IoaT0xO2k8c3RvcmUuc2l6ZSgpO2krKykgewogICAgICAgICAgICAgICAgZ2V0U3VtKHRlbXBTdHIxLHN0b3JlQWRkW2ldKTsKICAgICAgICAgICAgICAgIHRlbXBTdHIxPXN0ckFuczsKICAgICAgICAgICAgfQogICAgICAgICAgICBzZXR3TGVuPW1heCh0ZW1wU3RyMS5zeixtYXgoc3RyMS5zeixzdHIyLnN6KzEpKTsKICAgICAgICAgICAgY291dDw8c2V0dyhzZXR3TGVuKTw8c3RyMTw8IlxuIjsKICAgICAgICAgICAgY291dDw8c2V0dyhzZXR3TGVuLXN0cjIuc3opPDwnKic8PHN0cjI8PCJcbiI7CiAgICAgICAgICAgIGZvcihpPTA7aTxtYXgoc3RyMi5zeisxLHN0b3JlWzBdLnN6KTtpKyspCiAgICAgICAgICAgICAgICBzdHJEYXNoKz0nLSc7CiAgICAgICAgICAgIGNvdXQ8PHNldHcoc2V0d0xlbik8PHN0ckRhc2g8PCJcbiI7CiAgICAgICAgICAgIGZvcihpPTA7aTxzdG9yZS5zaXplKCk7aSsrKQogICAgICAgICAgICAgICAgY291dDw8c2V0dyhzZXR3TGVuLWkpPDxzdG9yZVtpXTw8IlxuIjsKICAgICAgICAgICAgaWYoc3RvcmUuc3o9PTEpCiAgICAgICAgICAgICAgICBjb250aW51ZTsKICAgICAgICAgICAgc3RyRGFzaC5lcmFzZSgpOwogICAgICAgICAgICBmb3IoaT0wO2k8bWF4KHRlbXBTdHIxLnN6LHN0b3JlW3N0b3JlLnN6LTFdLnN6KTtpKyspCiAgICAgICAgICAgICAgICBzdHJEYXNoKz0nLSc7CiAgICAgICAgICAgIGNvdXQ8PHNldHcoc2V0d0xlbik8PHN0ckRhc2g8PCJcbiI7CiAgICAgICAgICAgIGNvdXQ8PHNldHcoc2V0d0xlbik8PHRlbXBTdHIxPDwiXG5cbiI7CiAgICAgICAgfQogICAgfQogICAgcmV0dXJuIDA7Cn0KCgo=