#include<map>
#include<iostream>
using namespace std;
map<string,string> father;
string getfather(string x)
{
    if(father[x]!=x)
    {
        return getfather(father[x]);
    }
    else
    {
        return father[x];
    }
}
int main()
{
    char c;
    string s,fat;
    cin>>c;
    while(c!='$')
    {
        cin>>s;
        if(c=='#')
        {
            fat=s;
            if(father[s]=="")    father[s]=s;
        }
        if(c=='+')
        {
            father[s]=fat;
        }
        if(c=='?')
        {
            cout<<s<<' '<<getfather(father[s])<<endl;
        }
        cin>>c;
    }
    return 0;
}
/**************************************************************
	Problem: 1508
	User: admin
	Language: C++
	Result: Accepted
	Time:10 ms
	Memory:2084 kb
****************************************************************/