#include<bits/stdc++.h>
using namespace std;
map<string,string>fa;
const int N=50005;
int h;
string b,tem,a[N];
string f(string x){
if(fa[x]==x) return x;
else return fa[x]=f(fa[x]);
}
int main(){
for(int i=1;cin>>tem;i++){
char ccf=tem[0];
tem.erase(0,1);
if(ccf=='$') return 0;
if(ccf=='#'&&fa.find(tem)==fa.end()){
fa[tem]=tem;
b=tem;
}
else if(ccf=='#'){
b=tem;
}
if(ccf=='+'){
string t1=f(b);
string t2=f(tem);
fa[t2]=t1;
}
if(ccf=='?'){
cout<<tem<<" "<<f(tem)<<endl;
}
}
return 0;
}
30分记录