#include <iostream>
#include <map>
#include <string>
using namespace std;
map<string,string> m;
string s1,s2;
string fd(string s){
if(s!=m[s]){
m[s]=fd(m[s]);
}
return m[s];
}
int main(){
char c,now;
cin >> c;
while(c!='$'){
if(c=='#'){
cin >> s1;
if(m[s1]==""){
m[s1]=s1;
}
cin >> now;
while(now=='+'){
cin >> s2;
m[s2]=s1;
cin >> now;
}
c=now;
}else if(c=='?'){
cin >> s1;
cout << s1 << " " << fd(s1) << endl;
cin >> c;
}
}
return 0;
}