#include<iostream>
using namespace std;
string s[50001];
int f[50001];
int i,j,s1=0;
int find(int x)
{
if(f[x]!=x)
f[x]=find(f[x]);
return f[x];
}
main()
{
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
while(cin>>s[++s1] && s[s1][0]!='$');
s1--;
for(i=1;i<=s1;i++)
{
if(s[i][0]=='#')
f[i]=i;
else if(s[i][0]=='+')
{
j=i;
while(s[j][0]!='+')
j--;
f[j]=f[i];
}
else
{
for(j=1;j<=s[i].size();j++)
cout<<s[i][j];
cout<<" "<<s[find(f[i])]<<'\n';
}
}
}