#!/usr/bin/perl -w

use v5.16;

my @drawn = split/[,\s]/, scalar <>;
scalar <>;

$/ = "\n\n";

my @boards = <>;

say $boards[0];

my $remaining = @boards;

for my $d (@drawn) {
	for my $board (@boards) {
		$board =~ s/\b$d\b/' ' x length $d/e;
		if ($board =~ /              /
			|| $board =~ /\s{3}.{12}\s{3}.{12}\s{3}.{12}\s{3}.{12}\s{3}/xms) {
			if (--$remaining) {
				$board = 'x';
				next;
			}
			say $board;
			$board =~ s/(?<=\d)\s+(?=\d)/+/gxms;
			say "$board";
			say eval "$d*($board)";
			exit;
		}
	}
}

