#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef long long ll;
const int maxn=5e3+45,inf=0x3f3f3f3f;
//int fa[maxn],tot;
map<string,string>fa;
map<string,int>vis;
string find(string a){
if(fa[a]!=a)return fa[a]=find(fa[a]);
return a;
}
void unionn(string a,string b){
a=find(a);b=find(b);
fa[a]=b;
}
void work(){
char ch=getchar();
string fath;
while(ch!='$'){
string s;
char st=getchar();
while(st!='\n'){
s+=st;st=getchar();
}
// cout<<s<<endl;
vis[s]++;
if(vis[s]==1)fa[s]=s;
if(ch=='#')fath=s;
else if(ch=='+')fa[s]=fath;
else cout<<s<<' '<<find(s)<<endl;
ch=getchar();
}
}
int main()
{
work();
return 0;;
}