#include<bits/stdc++.h>
using namespace std;
unordered_map<string, string> p;
string find(string x)
{
if(x != p[x]) p[x] = find(p[x]);
return p[x];
}
int main()
{
char c;
string s;
while(cin >> c, c != '$')
{
string str;
cin >> str;
if(c == '#')
{
s = str;
if(p[s] == "") p[s] = s;
}
else if(c == '+') p[str] = s;
else cout << str << " " << p[find(str)] << endl;
}
return 0;
}