fork download
  1. #include <iostream>
  2. #include <unordered_map>
  3.  
  4. using namespace std;
  5.  
  6. int main(void)
  7. {
  8. int t;
  9. cin>>t;
  10. while(t--) {
  11. unordered_map<string,string> emap;
  12. unordered_map<string,string> rev_emap;
  13. int n;
  14. cin>>n;
  15. while(n--) {
  16. string from,to;
  17. cin>>from>>to;
  18. emap[from]=to;
  19. rev_emap[to]=from;
  20. }
  21. pair<string,string> ref;
  22. for(auto const &iter:emap) {
  23. if(rev_emap.find(iter.first)==rev_emap.end()) {
  24. ref=iter;
  25. break;
  26. }
  27. }
  28. cout<<ref.first<<' ';
  29. while(emap.find(ref.second)!=emap.end()) {
  30. ref=*emap.find(ref.second);
  31. cout<<ref.first<<' ';
  32. }
  33. cout<<ref.second<<' ';
  34. cout<<'\n';
  35. }
  36. return 0;
  37. }
Success #stdin #stdout 0s 15232KB
stdin
1
4
string ladle
cracker parrot
spoon string
ladle cracker
stdout
spoon string ladle cracker parrot