#!/usr/bin/perl -w

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

my @stacks;

while (<>) {
	last if /^ \d/;
	my $i = 0;
        for (/.(.). ?/g) {
		unshift @{ $stacks[$i] }, $_ if /[A-Z]/;
		$i++;
	}
}

scalar <>;

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

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