#include <bits/stdc++.h>
using namespace std;
string s="i+i*i#";
string state="#E";
vector<string>v;
vector<pair<string,string>>path;
vector<string>res_v;
vector<pair<string,string>>res_path;
void E(string state,string s);
bool T(string state,string s);
bool F(string state,string s);
bool K(string state,string s);
bool M(string state,string s);
void erase_ok(string &state,string &s)
{
state.pop_back();
s.erase(0, 1);
path.push_back({state,s});
v.push_back("");
}
void E(string state,string s)
{
state.pop_back();
state+="MT";
path.push_back({state,s});
v.push_back("E->TM");
T(state,s);
}
bool T(string state,string s)
{
if(state=="#"&&s=="#")
{
res_path=path;
res_v=v;
return true;
}
state.pop_back();
state+="KF";
path.push_back({state,s});
v.push_back("T->FK");
if(state=="#"&&s=="#")
{
res_path=path;
res_v=v;
return true;
}
if(F(state,s))return true;
}
bool F(string state,string s)
{
if(state=="#"&&s=="#")
{
res_path=path;
res_v=v;
return true;
}
state.pop_back();
state+="i";
path.push_back({state,s});
v.push_back("F->i");
erase_ok(state,s);
if(state=="#"&&s=="#")
{
res_path=path;
res_v=v;
return true;
}
if(K(state,s))return true;
}
bool K(string state,string s)
{
if(state=="#"&&s=="#")
{
res_path=path;
res_v=v;
return true;
}
string c="";
c+="K";
state.pop_back();
state+="KF*";
path.push_back({state,s});
v.push_back("K->*FK");
if(state.back()==s[0])
{
erase_ok(state,s);
if(state=="#"&&s=="#")
{
res_path=path;
res_v=v;
return true;
}
if(F(state,s))return true;
}
for(int i=0;i<3;i++)state.pop_back();
state+=c;
path.pop_back();
v.pop_back();
state.pop_back();
path.push_back({state,s});
v.push_back("K->3");
char head=state.back();
if(state=="#"&&s=="#")
{
res_path=path;
res_v=v;
return true;
}
if(head=='T'&&T(state,s))return true;
if(head=='F'&&F(state,s))return true;
if(head=='K'&&K(state,s))return true;
if(head=='M'&&M(state,s))return true;
return false;
}
bool M(string state,string s)
{
if(state=="#"&&s=="#")
{
res_path=path;
res_v=v;
return true;
}
string c="";
c+="M";
state.pop_back();
state+="MT+";
path.push_back({state,s});
v.push_back("M->+TM");
if(state.back()==s[0])
{
erase_ok(state,s);
if(state=="#"&&s=="#")
{
res_path=path;
res_v=v;
return true;
}
if(T(state,s))return true;
}
for(int i=0;i<3;i++)state.pop_back();
state+=c;
path.pop_back();
v.pop_back();
state.pop_back();
path.push_back({state,s});
v.push_back("M->3");
char head=state.back();
if(state=="#"&&s=="#")
{
res_path=path;
res_v=v;
return true;
}
if(head=='T'&&T(state,s))return true;
if(head=='F'&&F(state,s))return true;
if(head=='K'&&K(state,s))return true;
if(head=='M'&&M(state,s))return true;
return false;
}
int main()
{
path.push_back({state,s});
v.push_back("");
E(state,s);
for(int i=0;i<res_path.size();i++)
{
cout<<i<<' '<<res_path[i].first<<' '<<res_path[i].second<<' '<<res_v[i]<<endl;
}
}