#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
string start;int N;
struct node{
string s;int v;string e;
}ans1[maxn],ans2[maxn];
int tail1,tail2;
map<string,node> mp;
bool vis[maxn];
void solve(){
string cur = start;
while(cur != "-1"){
string s,e;int v;
s = mp[cur].s,v = mp[cur].v,e = mp[cur].e;
if(vis[abs(v)]){
if(tail2 == 0){
ans2[++tail2] = {s,v,"-1"};
}else{
ans2[tail2].e = s;
ans2[++tail2] = {s,v,"-1"};
}
}else{
if(tail1 == 0){
ans1[++tail1] = {s,v,"-1"};
}else{
ans1[tail1].e = s;
ans1[++tail1] = {s,v,"-1"};
}
vis[abs(v)] = 1;
}
cur = e;
}
}
int main(){
// debug_in;
cin>>start>>N;
for(int i = 1;i<=N;i++){
string s,e;int v;
cin>>s>>v>>e;
mp[s] = {s,v,e};
}
solve();
for(int i = 1;i<=tail1;i++){
cout<<ans1[i].s<<" "<<ans1[i].v<<" "<<ans1[i].e<<'\n';
}
for(int i = 1;i<=tail2;i++){
cout<<ans2[i].s<<" "<<ans2[i].v<<" "<<ans2[i].e<<'\n';
}
return 0;
}