#!/usr/bin/perl
use 5.14.2;
use warnings;

sub stringToHash {
	my ($string) = @_;
	my @chars = split '', $string;
	my %hash = ();
	for my $char (@chars){
		$hash{$char}++;
	}
	return \%hash
}


sub compareHashes{
	my($hash1, $hash2) = @_;
	my %hash1 = %$hash1;
	my %hash2 = %$hash2;
	for my $k (keys %hash1){
		return 0 if $hash1{$k} > $hash2{$k};
	}
	return 1;
}

my $input = <>;
my %hashIn = stringToHash $input;

my $n = <>;
my @cuvinte = ();
for(my $i = 0; $i < $n; $i++)
{
	$cuvinte[$i] = <>;
}
@cuvinte = sort @cuvinte;


for my $cuvant (@cuvinte){
	print $cuvant if compareHashes (stringToHash ($cuvant), %hashIn);
}