#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int N=1e5+10;
typedef pair <int ,string> PII;
unordered_map<string, PII>m;
void pf(vector<string>t)
{
for (int i = 0; i < t.size(); i+= 2)
{
if (i == 0)cout << t[i] << " " << t[i + 1] << " ";
else cout << t[i] << endl << t[i] << " " << t[i + 1] << " ";
}cout << "-1" << endl;
}
void del(unordered_map<string, PII> m,string b)
{
set<int>s; vector<string>t1;vector<string>t2;
while (b!="-1")
{
if (s.count(fabs(m[b].first)))
{t2.push_back(b); t2.push_back(to_string(m[b].first));}
else
{
s.insert(fabs(m[b].first));
t1.push_back(b); t1.push_back(to_string(m[b].first));
}b = m[b].second;
}pf(t1);
if(t2.size())pf(t2);
}
int main()
{
IOS
string b, t; int n;
cin >> b >> n;
while (n--)
{cin >> t;PII g; cin >> g.first >> g.second;m[t] = g;}
del(m, b);
}
因为不太会链表 感觉大材小用了 但是思路很简单 直接模拟就行了