#include<iostream>
#include<cassert>
#include<stack>
#include<vector>
#include<algorithm>
using namespace std;
#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n)   REP(i,0,n)
#define pb push_back
#define ALL(C)  (C).begin(),(C).end()
typedef long long ll;

class Q{
public:
  ll u,v;// u/v
  Q():u(0),v(1){}
  Q(ll tu,ll tv):u(tu),v(tv){
    if (tu == 0){
      v=1;
      return;
    }
    if (v < 0){
      v*=-1;
      u*=-1;
    }
    ll g = __gcd(u>0?u:-u,v);
    u/=g;
    v/=g;
  };
  Q operator+(const Q a)const{
    ll tu,tv;
    tv=v*a.v;
    tu=u*a.v + a.u*v;
    return Q(tu,tv);
  }
  Q operator-(const Q a)const{
    ll tu,tv;
    tv=v*a.v;
    tu=u*a.v-a.u*v;
    return Q(tu,tv);
  }
  Q operator*(const Q a)const{
    ll tu,tv;
    tv=v*a.v;
    tu=u*a.u;
    return Q(tu,tv);
  }
  Q operator/(const Q a)const{
    ll tu,tv;
    tv=v*a.u;
    tu=u*a.v;
    return Q(tu,tv);
  }
  ll myabs(){
    return u>0?u:-u;
  }
};

const int ROOT=2;
class Root{
public:
  Q val;
  ll a;
  Root(){};
  Root(Q tval,ll ta):val(tval),a(ta){};
  bool sameroot(Root &in){
    return a==in.a;
  }
  bool operator<(const Root & b)const{
    return  a < b.a;
  }
  void output(){
    //cout << val.u<<"/"<<val.v <<" " << a << endl;
  }
};

typedef vector<Root> vr;
typedef vector<int> vi;

void outputvr(vr &a){
  //rep(i,a.size())a[i].output();
  sort(ALL(a));
  rep(i,(int)a.size()){
    if (i)cout <<" + ";
    if (a[i].val.myabs() == 1 && a[i].val.v == 1){
      if (a[i].a == 1)cout << a[i].val.u;
      else if (a[i].val.u == -1)cout << "-";
    }else{
      cout << a[i].val.u;
      if (a[i].val.v != 1)cout << "/"<<a[i].val.v;
    }
    if (a[i].a != 1){
      if (a[i].val.myabs()==1 && a[i].val.v == 1){
	cout <<"sqrt("<< a[i].a << ")";
      }else {
	cout <<"*sqrt("<< a[i].a << ")";
      }
    }
  }
  cout << endl;
}

ll mypow(int in,int p){
  if( p == 0)return 0;
  if (p == 1)return in;
  ll ret=mypow(in,p/2);
  ret=ret*ret;
  if (p %2 == 1)ret*=in;
  return ret;
}

void fact(ll n,vi &prime,vi &power){
  for(int i=2;i*i <= n;i+=2){
    if (n%i == 0){
      int cnt=0;
      while(n%i == 0 && n/i > 0){
	cnt++;
	n/=i;
      }
      prime.pb(i);power.pb(cnt);
    }
    if(i == 2)i--;
  }
  if (n != 1)prime.pb(n),power.pb(1);
  return;
}

Root cleanup(Root &in){
  Root ret=in;
  vi prime,power;
  fact(in.a,prime,power);
  rep(i,(int)prime.size()){
    int tmp=power[i]/ROOT;
    power[i]%=ROOT;
    if (tmp != 0){ret.val =ret.val* Q(mypow(prime[i],tmp),1);}
  }
  ret.a=1;
  rep(i,(int)prime.size()){
    if (power[i] != 0)ret.a*=prime[i];
  } 
  return ret;
}

void cleanupall(vr &in){
  rep(i,(int)in.size()){
    in[i]=cleanup(in[i]);
  }
}

// all value in i must be cleanup-ed
vr arrange(vr &in){
  vr ret;
  rep(i,(int)in.size()){
    for(int j=(int)in.size()-1;j>i;j--){
      if (in[i].sameroot(in[j])){
	in[i].val=in[i].val+in[j].val;
	in.erase(in.begin()+j);
      }
    }
    ret.pb(in[i]);
  }
  for(int i=(int)ret.size()-1;i>=0;i--){
    if (ret[i].val.u == 0)ret.erase(ret.begin()+i);
  }
  if (ret.size() == 0)ret.pb(Root(Q(0,1),1));//0
  return ret;
}

vr add(vr &a,vr &b){
  vr ret;
  rep(i,(int)a.size())ret.pb(a[i]);
  rep(i,(int)b.size())ret.pb(b[i]);
  return arrange(ret);
}

vr sub(vr &a,vr &b){
  rep(i,(int)b.size())b[i].val=b[i].val*Q(-1,1);
  return add(a,b);
}

Root multerm(Root &a,Root &b){
  Root ret;
  ret.val=a.val*b.val;
  ret.a=a.a*b.a;
  return cleanup(ret);
}

vr mulall(vr &a,vr &b){
  vr ret;
  rep(i,(int)a.size()){
    rep(j,(int)b.size()){
      ret.pb(multerm(a[i],b[j]));
    }
  }
  return arrange(ret);
}

vr div(vr &a,vr &b){
  vr ret;
  vr U=mulall(a,b);
  vr V=mulall(b,b);
  assert(V.size() == 1 && V[0].a == 1);
  rep(i,(int)U.size()){
    U[i].val=U[i].val/V[0].val;
    ret.pb(U[i]);
  }
  return arrange(ret);
}

vr sqrt(Q a){
  if (a.u == 0){
    vr ret;
    ret.pb(Root(Q(0,1),1));
    return ret;
  }
  vr ta,tb;
  Root tmp(Q(1,1),a.u),tmp2(Q(1,1),a.v);
  ta.pb(tmp);tb.pb(tmp2);
  vr ret=div(ta,tb);
  cleanupall(ret);
  return arrange(ret);
}

vr push(int a){
  vr ret;
  Root tmp(Q(a,1),1);
  ret.pb(cleanup(tmp));
  return arrange(ret);
}

void test(){
  /*
  vr a=sqrt(Q(6,5));
  outputvr(a);
  */

  /*
  int a=8;
  vr tmp = sqrt(a);
  outputvr(tmp);
  */
  /*
  vr a,b;
  //Root tmpa(Q(1,1),15),tmpb(Q(1,1),2);
  Root tmpa(Q(1,1),22),tmpb(Q(1,1),2);
  a.pb(tmpa);
  b.pb(tmpb);
  vr res = div(a,b);
  outputvr(res);
  */

  /*
  Root a(Q(1,1),2),b(Q(1,1),6);
  Root hoge=multerm(a,b);
  cout << hoge.val.u <<"/"<< hoge.val.v
       <<" " << hoge.a << endl;
  */

  /*
  Root a(Q(1,1),6561000000ll);
  Root b = cleanup(a);
  cout << b.val.u<<"/"<<b.val.v <<
    " "<< b.a <<" " << endl;
  */
}
/*
int main(){
  test();
}
*/

int main(){
  stack<vr> S;
  string in;
  int n;
  while(cin>>in && in != "stop"){
    if (in =="push"){
      cin>>n;
      S.push(push(n));
    }else if (in == "add"){
      vr a=S.top();S.pop();
      vr b=S.top();S.pop();
      S.push(add(a,b));
    }else if (in == "sub"){
      vr x1=S.top();S.pop();
      vr x2=S.top();S.pop();
      S.push(sub(x2,x1));
    }else if (in == "mul"){
      vr a=S.top();S.pop();
      vr b=S.top();S.pop();
      S.push(mulall(a,b));
    }else if (in == "div"){
      vr x1=S.top();S.pop();
      vr x2=S.top();S.pop();
      S.push(div(x2,x1));
    }else if (in == "sqrt"){
      Q rt = S.top()[0].val;
      S.pop();
      S.push(sqrt(rt));
    }else if (in =="disp"){
      vr out=S.top();S.pop();
      outputvr(out);
    }
    //if (S.size() >= 257)assert(false);//no such case

    if (S.size() != 0){
      vr tmp=S.top();
      rep(i,tmp.size()){
	assert(-32768 <= tmp[i].val.u && tmp[i].val.u <= 32768 &&
	       -32768 <= tmp[i].val.v && tmp[i].val.v <= 32768 );
      }


    }

    /*
    cout <<"the operatrion " << in << endl;
    cout <<"the most top is"<<endl;
    if (S.size()){
      vr hoge=S.top();
      outputvr(hoge);
    }
    cout <<"the most top is end"<<endl;
    */
  }
  assert(S.size() == 0);
  return false;
}

Measured memory_bytes: 0