#include <iostream> #include<string> #include<vector> #include<unordered_map> using namespace std; struct node{ string addr; char data; string next; int flag; }; int main() { int n; string s1,s2; while (cin >> s1 >> s2>>n) { // 注意 while 处理多个 case vector<node>svec(n); unordered_map<string, node>nodemap; for(int i=0;i<n;i++){ cin>>svec[i].addr>>svec[i].data>>svec[i].next;//这个vector实际上没用,只用map就行 svec[i].flag=0; nodemap[svec[i].addr]=svec[i]; } string cur=s1; while(nodemap[cur].next!="-1"){ nodemap[cur].flag=1; cur=nodemap[cur].next; } nodemap[cur].flag=1; string cur1=s2; while(nodemap[cur1].next!="-1"){ if(nodemap[cur1].flag==1){ break; } nodemap[cur1].flag=1; cur1=nodemap[cur1].next; } if(nodemap[cur1].next!="-1"){ cout<<cur1<<endl; } else{ if(cur1==cur){ cout<<cur1<<endl; } else{ cout<<-1<<endl; } } } } // 64 位输出请用 printf("%lld")