#!/usr/bin/perl -w

use v5.36;
use strict;
use experimental 'multidimensional';

my @stacks;

while (<>) {
	last if /^ \d/;
	my $i = 1;
	my $st = 0;
	$stacks[$st] //= [];
	while ($i < length) {
		my $c = substr($_, $i, 1);
		unshift @{ $stacks[$st] }, $c if $c =~ /[A-Z]/;
		$i += 4;
		$st++;
	}
}

scalar <>;

while (<>) {
	my ($amount, $src, $dst) = /(\d+)/g;
	push @{ $stacks[$dst-1] }, pop @{ $stacks[$src-1] } for 1 .. $amount;
}

for my $st (@stacks) {
	print $st->[-1];
}
print "\n";
	
