#!/usr/bin/perl -p
# initialisation added to pass all inputs at once
@l=(); @h=(); $p=undef;
	
# golfed code
$p.="([#_]{$_})[x_]+"for@l=split;chop$p,$_=<>;/^[x_]*$p*$(?{$h[$_-1].=$$_ for 1..@l})(?!)/;$_=@h?join$",grep{$h[$_]!~/_/}0..$#l:ERROR;
	
# append "\n" for clearer output
$_.="\n";
	

