3912 {
3913 string head;
3914 int n;
3915 int k;
3916 cin >> head >> n >> k;
3917 unordered_map<string, node> um;
3918 for(int i = 0; i < n; i++) {
3919 string address;
3920 cin >> address;
3921 um[address].address = address;
3922 cin >> um[address].data >> um[address].next;
3923 }
3924 deque<vector<node>> deq;
3925 vector<node> block;
3926 string current = head;
3927 while(current != "-1") {
3928 block.emplace_back(um[current]);
3929 if(block.size() == k) {
3930 deq.emplace_front(block);
3931 block.clear();
3932 }
3933 current = um[current].next;
3934 }
3935 if(!block.empty()) {
3936 deq.emplace_front(block);
3937 }
3938 block.clear();
3939 for(const auto &blk: deq) {
3940 for(const auto &nd: blk) {
3941 block.emplace_back(nd);
3942 }
3943 }
3944 for(int i = 0; i < block.size(); i++) {
3945 cout << block[i].address << " " << block[i].data << " " << (i + 1 < block.size() ? block[i + 1].address : "-1") << endl;
3946 }
3947 return 0;
3948 }