#!/usr/bin/perl -w

use v5.30;
use strict;

my %prereqs;
my %steps;
while (<>) {
	my ($src, $dst) = /(\w) must be.* (\w) can/;
	$prereqs{$dst}->{$src} = 1;
	$steps{$src} = 1;
	$steps{$dst} = 1;
}

my $res = '';
while (%steps) {
	my @avail = sort grep { keys %{ $prereqs{$_} } == 0 } keys %steps;
	my $finished = $avail[0];
	$res .= $finished;
	delete $steps{$finished};
	for my $p (keys %prereqs) {
		delete $prereqs{$p}->{$finished};
	}
}

say $res;

