#include<bits/stdc++.h>
using namespace std;
map<string,string>p;
string find(string f)
{
    if(f!=p[f]){
    	p[f]=find(p[f]);
	}
    return  p[f];
}
string s,st;
int main()
{
    char ch;
    cin>>ch;
    while(ch!='$')
    {
        cin>>s;
        if(ch=='#')
        {
            st=s;
            if(p[s]=="") p[s]=s;
        }
        else if(ch=='+')
            p[s]=st;
        else
            cout<<s<<' '<<find(s)<<endl;
        cin>>ch;
    }
    return 0;
}
/**************************************************************
	Problem: 2236
	User: caizhihao
	Language: C++
	Result: Accepted
	Time:102 ms
	Memory:5048 kb
****************************************************************/