while(<>){

	$max = (sort {$b <=> $a} split)[0];
	$cmax = () = /\b$max\b/g;

	$area = eval join '+', map { eval join '*', split } @groups = /\d+ \d+/g;

	$area == $max ** 2 and $cmax =~ /[13]/ or do { print -1; next };

	$letter = 'A';

	print join "\n", $max,
		$cmax == 3 ?
		do {
			map { ( $letter ++ x $max ) x $_ } grep !/\b$max\b/, split
			}
		:
		do {
			($wide2) = grep $max != $_, split ' ', join '', grep /\b$max\b/, @groups;
			$wide1 = $max - $wide2;

			$pre = join "\n", map { s/ ?\b$wide1\b ?//; ( $letter ++ x $wide1 . 'C' x $wide2 ) x $_ }
				grep !/\b$max\b/, @groups;

			$groups[0] =~ /\b$max\b/ and $pre =~ y/ABC/BCA/;
			$groups[1] =~ /\b$max\b/ and $pre =~ y/ABC/ACB/;

			$pre
		}
}

use 5.010;
use strict;
use warnings FATAL => 'all';

my @input  = split / /, <>;
my $logos  = {};
my $letter = 'A';

while ( my ( $x, $y ) = splice @input, 0, 2 ) {
    @{ $logos->{ $letter++ } }{qw|x y|}
        = ( sort { $b <=> $a } $x, $y );
}

my $sqrt = sqrt eval join q{+}, map { $_->{x} * $_->{y} } values %{$logos};

if ( $sqrt != int($sqrt) ) {
    say -1;
    exit;
}

my $banner = [];
my $n      = -( scalar keys %{$logos} );

while (1) {
    for ( my $y = 0; $y < $sqrt; $y++ ) {
        for ( my $x = 0; $x < $sqrt; $x++ ) {
            $banner->[$y][$x] = '0';
        }
    }
    for my $logo ( sort {$n} keys %{$logos} ) {
        my ( $width, $height ) = @{ $logos->{$logo} }{qw|x y|};
        my ( $os_x, $os_y, $rotate ) = find_area( $banner, $width, $height )
            or last;

        ( $width, $height ) = ( $height, $width ) if $rotate;

        for ( my $cur_y = 0; $cur_y < $height; $cur_y++ ) {
            for ( my $cur_x = 0; $cur_x < $width; $cur_x++ ) {
                $banner->[ $os_y + $cur_y ][ $os_x + $cur_x ] = $logo;
            }
        }
    }
    if ( grep {/0/} map { @$_ ? @$_ : () } @{$banner} ) {
        if ( $n++ >= scalar keys %{$logos} ) {
            say -1;
            exit;
        }
        next;
    }
    last;
}

say $sqrt;
say join q{}, @$_ for @{$banner};

sub find_area {
    my ( $matrix, $width, $height ) = @_;
    my $_sqrt = scalar @{$matrix};

    for ( my $y = 0; $y < $_sqrt; $y++ ) {
        for ( my $x = 0; $x < $_sqrt; $x++ ) {
            my $paste = sub {
                my ( $_width, $_height ) = @_;
                for ( 0 .. $_width - 1 ) {
                    return if $x + $_ >= $_sqrt;
                    next   if $matrix->[$y][ $x + $_ ] !~ /0/;
                }

                for ( 0 .. $_height - 1 ) {
                    return if $y + $_ >= $_sqrt;
                    next   if $matrix->[ $y + $_ ][$x] !~ /0/;
                }
                return 1;
            };

            if ( $matrix->[$y][$x] =~ /0/ ) {
                if ( $paste->( $width, $height ) ) {
                    return $x, $y;
                }

                if ( $paste->( $height, $width ) ) {
                    return $x, $y, 1;
                }
            }
        }
    }
    return;
}

use 5.010;
use strict;
use warnings FATAL => 'all';

my @input  = split / /, <>;
my $logos  = {};
my $letter = 'A';

while ( my ( $x, $y ) = splice @input, 0, 2 ) {
    @{ $logos->{ $letter++ } }{qw|x y|}
        = ( sort { $b <=> $a } $x, $y );
}

my $sqrt = sqrt eval join q{+}, map { $_->{x} * $_->{y} } values %{$logos};

if ( $sqrt != int($sqrt) ) {
    say -1;
    exit;
}

my $banner = [];
my $n      = -1;

while (1) {
    push @{$banner}, [ (undef) x $sqrt ] for 1 .. $sqrt;
    for my $logo ( sort {$n} keys %{$logos} ) {
        my ( $width, $height ) = @{ $logos->{$logo} }{qw|x y|};
        my ( $os_x, $os_y, $rotate ) = find_area( $banner, $width, $height )
            or last;

        ( $width, $height ) = ( $height, $width ) if $rotate;

        for ( my $cur_y = 0; $cur_y < $height; $cur_y++ ) {
            for ( my $cur_x = 0; $cur_x < $width; $cur_x++ ) {
                $banner->[ $os_y + $cur_y ][ $os_x + $cur_x ] = $logo;
            }
        }
    }
    if ( grep { !defined } map { @$_ ? @$_ : () } @{$banner} ) {
        if ( $n++ >= 1 ) {
            say -1;
            exit;
        }
        undef $banner;
        next;
    }
    last;
}

say $sqrt;
say q{}, @$_ for @{$banner};

sub find_area {
    my ( $matrix, $width, $height ) = @_;
    my $_sqrt = scalar @{$matrix};

    for ( my $y = 0; $y < $_sqrt; $y++ ) {
        for ( my $x = 0; $x < $_sqrt; $x++ ) {
            my $paste = sub {
                my ( $_width, $_height ) = @_;
                for ( 0 .. $_width - 1 ) {
                    return if $x + $_ >= $_sqrt;
                    next   if defined $matrix->[$y][ $x + $_ ];
                }

                for ( 0 .. $_height - 1 ) {
                    return if $y + $_ >= $_sqrt;
                    next   if defined $matrix->[ $y + $_ ][$x];
                }
                return 1;
            };

            if ( !defined $matrix->[$y][$x] ) {
                if ( $paste->( $width, $height ) ) {
                    return $x, $y;
                }

                if ( $paste->( $height, $width ) ) {
                    return $x, $y, 1;
                }
            }
        }
    }
    return;
}

$\ = $/;

while(<>){

	@s = sort {$b <=> $a} split;
	($max, $min) = @s[0,-1];
	$cmax = () = /\b$max\b/g;

	s/(\d+) (\d+)/ join ' ', sort {$b <=> $a} @_ = ($1, $2) /ge;

	$area = eval join '+', map { eval join '*', split } /\d+ \d+/g;

	$area == $max ** 2 or do { print -1; next };

	%hash = ();
	map { ++ $hash{ $_ } } split;
	(join ' ', values %hash) =~ /[46]/ and do { print -1; next };

	$let = 'A';

	$cmax == 3 ?
	do {
		s/\b$max\b//g;
		(eval join '+', split) == $max or do { print -1; next };

		print join "\n", $max , map { ( $let ++ x $max ) x $_ } split;
		}
	:
	do {
		$cmax == 1 or do { print -1; next };

		($bemax = $_) =~ s/\b$max\b (\d+) ?//;
		$wide2 = $1;
		$wide1 = $max - $wide2;

		$pre = join "\n", map { s/ ?\b$wide1\b ?//; ( $let ++ x $wide1 ) x $_ } $bemax =~ /\d+ \d+/g;

		$pre =~ s/$/ 'C' x $wide2 /gem;

		/^$max\b/         and $pre =~ y/ABC/BCA/;
		/^\d+ \d+ $max\b/ and $pre =~ y/ABC/ACB/;

		print join "\n", $max, $pre;

	}
}

use 5.010;
use strict;
use warnings FATAL => 'all';

my @input  = split / /, <>;
my $logos  = {};
my $letter = 'A';

while ( my ( $x, $y ) = splice @input, 0, 2 ) {
    @{ $logos->{ $letter++ } }{qw|x y|}
        = ( sort { $b <=> $a } $x, $y );
}

my $sqrt = sqrt eval join q{+}, map { $_->{x} * $_->{y} } values %{$logos};

if ( $sqrt != int($sqrt) ) {
    say -1;
    exit;
}

my $banner = [];
my $n      = -( scalar keys %{$logos} );

while (1) {
    for ( my $y = 0; $y < $sqrt; $y++ ) {
        for ( my $x = 0; $x < $sqrt; $x++ ) {
            $banner->[$y][$x] = '0';
        }
    }
    for my $logo ( sort {$n} keys %{$logos} ) {
        my ( $width, $height ) = @{ $logos->{$logo} }{qw|x y|};
        my ( $os_x, $os_y, $rotate ) = find_area( $banner, $width, $height )
            or last;

        ( $width, $height ) = ( $height, $width ) if $rotate;

        for ( my $cur_y = 0; $cur_y < $height; $cur_y++ ) {
            for ( my $cur_x = 0; $cur_x < $width; $cur_x++ ) {
                $banner->[ $os_y + $cur_y ][ $os_x + $cur_x ] = $logo;
            }
        }
    }
    if ( grep {/0/} map { @$_ ? @$_ : () } @{$banner} ) {
        if ( $n++ >= scalar keys %{$logos} ) {
            say -1;
            exit;
        }
        undef $banner;
        next;
    }
    last;
}

say $sqrt;
say join q{}, @$_ for @{$banner};

sub find_area {
    my ( $matrix, $width, $height ) = @_;
    my $_sqrt = scalar @{$matrix};

    for ( my $y = 0; $y < $_sqrt; $y++ ) {
        for ( my $x = 0; $x < $_sqrt; $x++ ) {
            my $paste = sub {
                my ( $_width, $_height ) = @_;
                for ( 0 .. $_width - 1 ) {
                    return if $x + $_ >= $_sqrt;
                    next   if $matrix->[$y][ $x + $_ ] !~ /0/;
                }

                for ( 0 .. $_height - 1 ) {
                    return if $y + $_ >= $_sqrt;
                    next   if $matrix->[ $y + $_ ][$x] !~ /0/;
                }
                return 1;
            };

            if ( $matrix->[$y][$x] =~ /0/ ) {
                if ( $paste->( $width, $height ) ) {
                    return $x, $y;
                }

                if ( $paste->( $height, $width ) ) {
                    return $x, $y, 1;
                }
            }
        }
    }
    return;
}

use 5.010;
use strict;
use warnings FATAL => 'all';

my @input  = split / /, <>;
my $logos  = {};
my $letter = 'A';

while ( my ( $x, $y ) = splice @input, 0, 2 ) {
    @{ $logos->{ $letter++ } }{qw|x y|}
        = ( sort { $b <=> $a } $x, $y );
}

my $sqrt = sqrt eval join q{+}, map { $_->{x} * $_->{y} } values %{$logos};

if ( $sqrt != int($sqrt) ) {
    say -1;
    exit;
}

my $banner = [];
my $n      = -( scalar keys %{$logos} );

while (1) {
    for ( my $y = 0; $y < $sqrt; $y++ ) {
        for ( my $x = 0; $x < $sqrt; $x++ ) {
            $banner->[$y][$x] = '0';
        }
    }
    for my $logo ( sort { $n++ } sort keys %{$logos} ) {
        my ( $width, $height ) = @{ $logos->{$logo} }{qw|x y|};
        my ( $os_x, $os_y, $rotate ) = find_area( $banner, $width, $height )
            or last;

        ( $width, $height ) = ( $height, $width ) if $rotate;

        for ( my $cur_y = 0; $cur_y < $height; $cur_y++ ) {
            for ( my $cur_x = 0; $cur_x < $width; $cur_x++ ) {
                $banner->[ $os_y + $cur_y ][ $os_x + $cur_x ] = $logo;
            }
        }
    }
    if ( grep {/0/} map { @$_ ? @$_ : () } @{$banner} ) {
        if ( $n >= scalar keys %{$logos} ) {
            say -1;
            exit;
        }
        next;
    }
    last;
}

say $sqrt;
say join q{}, @$_ for @{$banner};

sub find_area {
    my ( $matrix, $width, $height ) = @_;
    my $_sqrt = scalar @{$matrix};

    for ( my $y = 0; $y < $_sqrt; $y++ ) {
        for ( my $x = 0; $x < $_sqrt; $x++ ) {
            my $paste = sub {
                my ( $_width, $_height ) = @_;
                for ( 0 .. $_width - 1 ) {
                    return if $x + $_ >= $_sqrt;
                    next   if $matrix->[$y][ $x + $_ ] !~ /0/;
                }

                for ( 0 .. $_height - 1 ) {
                    return if $y + $_ >= $_sqrt;
                    next   if $matrix->[ $y + $_ ][$x] !~ /0/;
                }
                return 1;
            };

            if ( $matrix->[$y][$x] =~ /0/ ) {
                if ( $paste->( $width, $height ) ) {
                    return $x, $y;
                }

                if ( $paste->( $height, $width ) ) {
                    return $x, $y, 1;
                }
            }
        }
    }
    return;
}

use 5.010;
use strict;
use warnings FATAL => 'all';

# my @input  = split / /, <>;
my @input = qw(1 2 2 4 3 2);
my $logos  = {};
my $letter = 'A';

while ( my ( $x, $y ) = splice @input, 0, 2 ) {
    @{ $logos->{ $letter++ } }{qw|x y|} = ( $x, $y );
}

my $sqrt = sqrt eval join q{+}, ( map { $_->{x} * $_->{y} } values %{$logos} );
if ( $sqrt - int($sqrt) ) {
    say -1;
    exit 1;
}

my $banner = [];

for ( my $y = 0; $y < $sqrt; $y++ ) {
    for ( my $x = 0; $x < $sqrt; $x++ ) {
        $banner->[$y][$x] = 0;
    }
}

for my $logo ( keys %{$logos} ) {
    my ( $width, $height ) = @{ $logos->{$logo} }{qw|x y|};
    my ( $os_x, $os_y, $rotate ) = find_area( $banner, $width, $height )
        or next;

    ( $width, $height ) = ( $height, $width ) if $rotate;
    say $logo, $rotate//0;

    for ( my $cur_y = 0; $cur_y < $height; $cur_y++ ) {
        for ( my $cur_x = 0; $cur_x < $width; $cur_x++ ) {
            $banner->[ $os_y + $cur_y ][ $os_x + $cur_x ] = $logo;
        }
    }
}

say $sqrt;

say join q{}, @$_ for map { @$_ ? $_ : () } @{$banner};

sub find_area {
    my ( $matrix, $width, $height ) = @_;

    for ( my $y = 0; $y < $sqrt; $y++ ) {
        for ( my $x = 0; $x < $sqrt; $x++ ) {
            my $paste = sub {
                my ( $_width, $_height ) = @_;
                my ( $success, $width_success );
                for ( 0 .. $_width - 1 ) {
                    if ( !defined $matrix->[$y][ $x + $_ ] ) {
                        $width_success = 0;
                        last;
                    }
                    next if $matrix->[$y][ $x + $_ ] ne 0;
                    $width_success++;
                }

                if ($width_success) {
                    for ( 0 .. $_height - 1 ) {
                        if ( !defined $matrix->[ $y + $_ ][$x] ) {
                            $success = 0;
                            last;
                        }
                        next if $matrix->[ $y + $_ ][$x] ne 0;
                        $success++;
                    }
                }
                return $success;
            };

            if ( $matrix->[$y][$x] eq 0 ) {
                if ( $paste->( $width, $height ) ) {
                    return $x, $y;
                }
                elsif ( $paste->( $height, $width ) ) {
                    return $x, $y, 1;
                }
            }
        }
    }
    return;
}

use 5.010;
use strict;
use warnings FATAL => 'all';

my @input  = split / /, <>;
my $logos  = {};
my $letter = 'A';

while ( my ( $x, $y ) = splice @input, 0, 2 ) {
    @{ $logos->{ $letter++ } }{qw|x y|} = ( $x, $y );
}

my $sqrt = sqrt eval join q{+}, ( map { $_->{x} * $_->{y} } values %{$logos} );
if ( $sqrt - int($sqrt) ) {
    say -1;
    exit 1;
}

my $banner = [];

for ( my $y = 0; $y < $sqrt; $y++ ) {
    for ( my $x = 0; $x < $sqrt; $x++ ) {
        $banner->[$y][$x] = 0;
    }
}

for my $logo ( keys %{$logos} ) {
    my ( $width, $height ) = @{ $logos->{$logo} }{qw|x y|};
    my ( $os_x, $os_y, $rotate ) = find_area( $banner, $width, $height )
        or next;

    ( $width, $height ) = ( $height, $width ) if $rotate;

    for ( my $cur_y = 0; $cur_y < $height; $cur_y++ ) {
        for ( my $cur_x = 0; $cur_x < $width; $cur_x++ ) {
            $banner->[ $os_y + $cur_y ][ $os_x + $cur_x ] = $logo;
        }
    }
}

say $sqrt;

say join q{}, @$_ for map { @$_ ? $_ : () } @{$banner};

sub find_area {
    my ( $matrix, $width, $height ) = @_;

    for ( my $y = 0; $y < $sqrt; $y++ ) {
        for ( my $x = 0; $x < $sqrt; $x++ ) {
            my $paste = sub {
                my ( $_width, $_height ) = @_;
                my ( $success, $width_success );
                for ( 0 .. $_width - 1 ) {
                    if ( !defined $matrix->[$y][ $x + $_ ] ) {
                        $width_success = 0;
                        last;
                    }
                    next if $matrix->[$y][ $x + $_ ] ne 0;
                    $width_success++;
                }

                if ($width_success) {
                    for ( 0 .. $_height - 1 ) {
                        if ( !defined $matrix->[ $y + $_ ][$x] ) {
                            $success = 0;
                            last;
                        }
                        next if $matrix->[ $y + $_ ][$x] ne 0;
                        $success++;
                    }
                }
                return $success;
            };

            if ( $matrix->[$y][$x] eq 0 ) {
                if ( $paste->( $width, $height ) ) {
                    return $x, $y;
                }
                elsif ( $paste->( $height, $width ) ) {
                    return $x, $y, 1;
                }
            }
        }
    }
    return;
}

use 5.010;
use strict;
use warnings FATAL => 'all';

my @input  = split / /, <>;
my $logos  = {};
my $letter = 'A';

while ( my ( $x, $y ) = splice @input, 0, 2 ) {
    @{ $logos->{ $letter++ } }{qw|x y|} = ( $x, $y );
}
my $sqrt = sqrt eval join q{+},
    ( map { $_->{x} * $_->{y} } values %{$logos} );
if ( $sqrt - int($sqrt) ) {
    say -1;
    exit 1;
}

my $banner = [];

LOOP: while (1) {
    for ( my $y = 0; $y < $sqrt; $y++ ) {
        for ( my $x = 0; $x < $sqrt; $x++ ) {
            $banner->[$y][$x] = '0';
        }
    }

    for my $logo ( sort { int( rand(3) ) - 1 } keys %{$logos} ) {
        my ( $width, $height ) = @{ $logos->{$logo} }{qw|x y|};
        my ( $os_x, $os_y, $rotate ) = find_area( $banner, $width, $height )
            or next LOOP;

        ( $width, $height ) = ( $height, $width ) if $rotate;

        for ( my $cur_y = 0; $cur_y < $height; $cur_y++ ) {
            for ( my $cur_x = 0; $cur_x < $width; $cur_x++ ) {
                $banner->[ $os_y + $cur_y ][ $os_x + $cur_x ] = $logo;
            }
        }
    }

    last;
}

say $sqrt;

say join q{}, @$_ for map { @$_ ? $_ : () } @{$banner};

sub find_area {
    my ( $matrix, $width, $height ) = @_;

    for ( my $y = 0; $y < $sqrt; $y++ ) {
        for ( my $x = 0; $x < $sqrt; $x++ ) {
            my $paste = sub {
                my ( $_width, $_height ) = @_;
                my ( $success, $width_success );
                for ( 0 .. $_width - 1 ) {
                    if ( !defined $matrix->[$y][ $x + $_ ] ) {
                        $width_success = 0;
                        last;
                    }
                    if ( $matrix->[$y][ $x + $_ ] ne '0' ) {
                        $width_success = 0;
                        next;
                    }
                    $width_success++;
                }

                if ($width_success) {
                    for ( 0 .. $_height - 1 ) {
                        if ( !defined $matrix->[ $y + $_ ][$x] ) {
                            $success = 0;
                            last;
                        }
                        if ( $matrix->[ $y + $_ ][$x] ne '0' ) {
                            $success = 0;
                            next;
                        }
                        $success++;
                    }
                }
                return $success;
            };

            if ( $matrix->[$y][$x] eq '0' ) {
                if ( $paste->( $width, $height ) ) {
                    return $x, $y;
                }

                if ( $paste->( $height, $width ) ) {
                    return $x, $y, 1;
                }
            }
        }
    }
    return;
}

use 5.010;
use strict;
use warnings FATAL => 'all';

my @input  = split / /, <>;
my $logos  = {};
my $letter = 'A';

while ( my ( $x, $y ) = splice @input, 0, 2 ) {
    @{ $logos->{ $letter++ } }{qw|x y|}
        = ( sort { $b <=> $a } $x, $y );
}

my $sqrt = sqrt eval join q{+}, map { $_->{x} * $_->{y} } values %{$logos};

if ( $sqrt != int($sqrt) ) {
    say -1;
    exit;
}

my $banner = [];
my $n      = -( scalar keys %{$logos} );

while (1) {
    for ( my $y = 0; $y < $sqrt; $y++ ) {
        for ( my $x = 0; $x < $sqrt; $x++ ) {
            $banner->[$y][$x] = '0';
        }
    }

    for my $logo ( sort { $n++ } keys %{$logos} ) {
        my ( $width, $height ) = @{ $logos->{$logo} }{qw|x y|};
        my ( $os_x, $os_y, $rotate ) = find_area( $banner, $width, $height )
            or last;

        ( $width, $height ) = ( $height, $width ) if $rotate;

        for ( my $cur_y = 0; $cur_y < $height; $cur_y++ ) {
            for ( my $cur_x = 0; $cur_x < $width; $cur_x++ ) {
                $banner->[ $os_y + $cur_y ][ $os_x + $cur_x ] = $logo;
            }
        }
    }
    if ( grep {/0/} map { @$_ ? @$_ : () } @{$banner} ) {
        if ( $n = scalar keys %{$logos} ) {
            say -1;
            exit;
        }
        next;
    }
    last;
}

say $sqrt;
say join q{}, @$_ for @{$banner};

sub find_area {
    my ( $matrix, $width, $height ) = @_;
    my $_sqrt = scalar @{$matrix};

    for ( my $y = 0; $y < $_sqrt; $y++ ) {
        for ( my $x = 0; $x < $_sqrt; $x++ ) {
            my $paste = sub {
                my ( $_width, $_height ) = @_;
                for ( 0 .. $_width - 1 ) {
                    return if $x + $_ >= $_sqrt;
                    next   if $matrix->[$y][ $x + $_ ] !~ /0/;
                }

                for ( 0 .. $_height - 1 ) {
                    return if $y + $_ >= $_sqrt;
                    next   if $matrix->[ $y + $_ ][$x] !~ /0/;
                }
                return 1;
            };

            if ( $matrix->[$y][$x] =~ /0/ ) {
                if ( $paste->( $width, $height ) ) {
                    return $x, $y;
                }

                if ( $paste->( $height, $width ) ) {
                    return $x, $y, 1;
                }
            }
        }
    }
    return;
}

use 5.010;
use strict;
use warnings FATAL => 'all';

my @input  = split / /, <>;
my $logos  = {};
my $letter = 'A';

while ( my ( $x, $y ) = splice @input, 0, 2 ) {
    @{ $logos->{ $letter++ } }{qw|x y|} = sort { $b <=> $a } ( $x, $y );
}
my $sqrt = sqrt eval join q{+},
    ( map { $_->{x} * $_->{y} } values %{$logos} );
if ( $sqrt - int($sqrt) ) {
    say -1;
    exit 1;
}

my $banner = [];

for ( my $y = 0; $y < $sqrt; $y++ ) {
    for ( my $x = 0; $x < $sqrt; $x++ ) {
        $banner->[$y][$x] = '0';
    }
}

for my $logo (keys %{$logos}) {
    my ( $width, $height ) = @{ $logos->{$logo} }{qw|x y|};
    my ( $os_x, $os_y, $rotate ) = find_area( $banner, $width, $height )
        or next;

    ( $width, $height ) = ( $height, $width ) if $rotate;

    for ( my $cur_y = 0; $cur_y < $height; $cur_y++ ) {
        for ( my $cur_x = 0; $cur_x < $width; $cur_x++ ) {
            $banner->[ $os_y + $cur_y ][ $os_x + $cur_x ] = $logo;
        }
    }
}

say $sqrt;

say join q{}, @$_ for map { @$_ ? $_ : () } @{$banner};

sub find_area {
    my ( $matrix, $width, $height ) = @_;

    for ( my $y = 0; $y < $sqrt; $y++ ) {
        for ( my $x = 0; $x < $sqrt; $x++ ) {
            my $paste = sub {
                my ( $_width, $_height ) = @_;
                my ( $success, $width_success );
                for ( 0 .. $_width - 1 ) {
                    if ( !defined $matrix->[$y][ $x + $_ ] ) {
                        $width_success = 0;
                        last;
                    }
                    if ( $matrix->[$y][ $x + $_ ] ne '0' ) {
                        $width_success = 0;
                        next;
                    }
                    $width_success++;
                }

                if ($width_success) {
                    for ( 0 .. $_height - 1 ) {
                        if ( !defined $matrix->[ $y + $_ ][$x] ) {
                            $success = 0;
                            last;
                        }
                        if ( $matrix->[ $y + $_ ][$x] ne '0' ) {
                            $success = 0;
                            next;
                        }
                        $success++;
                    }
                }
                return $success;
            };

            if ( $matrix->[$y][$x] eq '0' ) {
                if ( $paste->( $width, $height ) ) {
                    return $x, $y;
                }

                if ( $paste->( $height, $width ) ) {
                    return $x, $y, 1;
                }
            }
        }
    }
    return;
}

use 5.010;
use strict;
use warnings FATAL => 'all';

my @input  = split / /, <>;
my $logos  = {};
my $letter = 'A';

while ( my ( $x, $y ) = splice @input, 0, 2 ) {
    @{ $logos->{ $letter++ } }{qw|x y|} = ( $x, $y );
}

my $sqrt = sqrt eval join q{+},
    ( map { $_->{x} * $_->{y} } values %{$logos} );
if ( $sqrt - int($sqrt) ) {
    say -1;
    exit 1;
}

my $banner = [];

for ( my $y = 0; $y < $sqrt; $y++ ) {
    for ( my $x = 0; $x < $sqrt; $x++ ) {
        $banner->[$y][$x] = '0';
    }
}

for my $logo ( keys %{$logos} ) {
    my ( $width, $height ) = @{ $logos->{$logo} }{qw|x y|};
    my ( $os_x, $os_y, $rotate ) = find_area( $banner, $width, $height )
        or next;

    ( $width, $height ) = ( $height, $width ) if $rotate;

    for ( my $cur_y = 0; $cur_y < $height; $cur_y++ ) {
        for ( my $cur_x = 0; $cur_x < $width; $cur_x++ ) {
            $banner->[ $os_y + $cur_y ][ $os_x + $cur_x ] = $logo;
        }
    }
}

say $sqrt;

say join q{}, @$_ for map { @$_ ? $_ : () } @{$banner};

sub find_area {
    my ( $matrix, $width, $height ) = @_;

    for ( my $y = 0; $y < $sqrt; $y++ ) {
        for ( my $x = 0; $x < $sqrt; $x++ ) {
            my $paste = sub {
                my ( $_width, $_height ) = @_;
                my ( $success, $width_success );
                for ( 0 .. $_width - 1 ) {
                    if ( !defined $matrix->[$y][ $x + $_ ] ) {
                        $width_success = 0;
                        last;
                    }
                    if ( $matrix->[$y][ $x + $_ ] ne '0' ) {
                        $width_success = 0;
                        next;
                    }
                    $width_success++;
                }

                if ($width_success) {
                    for ( 0 .. $_height - 1 ) {
                        if ( !defined $matrix->[ $y + $_ ][$x] ) {
                            $success = 0;
                            last;
                        }
                        if ( $matrix->[ $y + $_ ][$x] ne '0' ) {
                            $success = 0;
                            next;
                        }
                        $success++;
                    }
                }
                return $success;
            };

            if ( $matrix->[$y][$x] eq 0 ) {
                if ( $paste->( $width, $height ) ) {
                    return $x, $y;
                }

                if ( $paste->( $height, $width ) ) {
                    return $x, $y, 1;
                }
            }
        }
    }
    return;
}

use 5.010;
use strict;
use warnings FATAL => 'all';

my @input  = split / /, <>;
my $logos  = {};
my $letter = 'A';

while ( my ( $x, $y ) = splice @input, 0, 2 ) {
    @{ $logos->{ $letter++ } }{qw|x y|} = ( $x, $y );
}

my $sqrt = sqrt sum( map { $_->{x} * $_->{y} } values %{$logos} );
if ( $sqrt - int($sqrt) ) {
    say -1;
    exit 1;
}

my $banner = [];

for ( my $y = 0; $y < $sqrt; $y++ ) {
    for ( my $x = 0; $x < $sqrt; $x++ ) {
        $banner->[$y][$x] = 0;
    }
}

for my $logo ( sort keys %{$logos} ) {
    my ( $width, $height ) = @{ $logos->{$logo} }{qw|x y|};
    my ( $os_x, $os_y, $rotate ) = find_area( $banner, $width, $height )
        or next;

    ( $width, $height ) = ( $height, $width ) if $rotate;

    for ( my $cur_y = 0; $cur_y < $height; $cur_y++ ) {
        for ( my $cur_x = 0; $cur_x < $width; $cur_x++ ) {
            $banner->[ $os_y + $cur_y ][ $os_x + $cur_x ] = $logo;
        }
    }
}

say $sqrt;

say join q{}, @$_ for map { @$_ ? $_ : () } @{$banner};

sub sum {
    my $sum;
    $sum += $_ for @_;
    return $sum;
}

sub find_area {
    my ( $matrix, $width, $height ) = @_;

    for ( my $y = 0; $y < $sqrt; $y++ ) {
        for ( my $x = 0; $x < $sqrt; $x++ ) {
            my $paste = sub {
                my ( $_width, $_height ) = @_;
                my ( $success, $width_success );
                for ( 0 .. $_width - 1 ) {
                    if ( !defined $matrix->[$y][ $x + $_ ] ) {
                        $width_success = 0;
                        last;
                    }
                    next if $matrix->[$y][ $x + $_ ] ne 0;
                    $width_success++;
                }

                if ($width_success) {
                    for ( 0 .. $_height - 1 ) {
                        if ( !defined $matrix->[ $y + $_ ][$x] ) {
                            $success = 0;
                            last;
                        }
                        next if $matrix->[ $y + $_ ][$x] ne 0;
                        $success++;
                    }
                }
                return $success;
            };

            if ( $matrix->[$y][$x] eq 0 ) {
                if ( $paste->( $width, $height ) ) {
                    return $x, $y;
                }
                elsif ( $paste->( $height, $width ) ) {
                    return $x, $y, 1;
                }
            }
        }
    }
    return;
}

$\ = $/;

while(<>){

	print;
	%h = ();
	map { ++ $h{ $_ } } split;
	$f = 0;
	$_ > 3 and $f ++  for values %h;
	$f and do { print -1; next };
	s/(\d+) (\d+)/ join ' ', sort {$b <=> $a} @_ = ($1, $2) /ge;
#	print;

	/(\b\d+\b).{2,}\1.{2,}\1/ ?
	do {
		$del = $1;
		s/\b$del\b//g;
		$sum = eval join '+', split;
		$sum == $del or do { print -1; next };
		$let = 'A';
		print join "\n", $del , map { ( $let ++x $del ) x $_ } split;

		}
	:
	do {
		(eval join '==', (sort {$b <=> $a} split)[0 .. 1]) and do { print -1; next };
		$sum = eval join '+', map { eval join '*', split } /\d+ \d+/g;
		$max = (sort {$b <=> $a} split)[0];
		$sum == $max ** 2 or do { print -1; next };
		$bemax = $_;
		$bemax =~ s/\b$max\b (\d+) ?//;
		$wide = $1;
#		print $bemax;
		$let = 'A';
		$pre = join "\n", ( $let x ($max - $wide) ) x (split ' ', $bemax)[1] ,
			( ++ $let x ($max - $wide) ) x (reverse split ' ', $bemax)[0];
#		print $pre;
		$pre =~ s/$/ 'C' x $wide /gems;
#		print $pre;

		/^\b$max\b/         and $pre =~ y/ABC/BCA/;
		/^\d+ \d+ \b$max\b/ and $pre =~ y/ABC/ACB/;
		/\b$max\b \d+$/     and $pre =~ y/ABC/ABC/;

		print $max, "\n", $pre;
		}

	}

$\ = $/;

while(<>){

	%h = ();
	map { ++ $h{ $_ } } split;
	$_ > 3 and do { print -1; next } for values %h;
	s/(\d+) (\d+)/ join ' ', sort {$b <=> $a} @_ = ($1, $2) /ge;
#	print;

	/(\b\d+\b).{2,}\1.{2,}\1/ ?
	do {
		$del = $1;
		s/\b$del\b//g;
		$sum = eval join '+', split;
		$sum == $del or do { print -1; next };
		$let = 'A';
		print join "\n", $del , map { ( $let ++x $del ) x $_ } split;

		}
	:
	do {
		(eval join '==', (sort {$b <=> $a} split)[0 .. 1]) and do { print -1; next };
		$sum = eval join '+', map { eval join '*', split } /\d+ \d+/g;
		$max = (sort {$b <=> $a} split)[0];
		$sum == $max ** 2 or do { print -1; next };
		$bemax = $_;
		$bemax =~ s/\b$max\b (\d+) ?//;
		$wide = $1;
#		print $bemax;
		$let = 'A';
		$pre = join "\n", ( $let x ($max - $wide) ) x (split ' ', $bemax)[1] ,
			( ++ $let x ($max - $wide) ) x (reverse split ' ', $bemax)[0];
#		print $pre;
		$pre =~ s/$/ 'C' x $wide /gems;
#		print $pre;

		/^\b$max\b/         and $pre =~ y/ABC/BCA/;
		/^\d+ \d+ \b$max\b/ and $pre =~ y/ABC/ACB/;
		/\b$max\b \d+$/     and $pre =~ y/ABC/ABC/;

		print $max, "\n", $pre;
		}

	}

$\ = $/;

while(<>){

#	print;
	%h = ();
	map { ++ $h{ $_ } } split;
	$f = 0;
	$_ > 3 and $f ++  for values %h;
	$f and do { print -1; next };
	s/(\d+) (\d+)/ join ' ', sort {$b <=> $a} @_ = ($1, $2) /ge;
#	print;

	/(\b\d+\b).{2,}\1.{2,}\1/ ?
	do {
		$del = $1;
		s/\b$del\b//g;
		$sum = eval join '+', split;
		$sum == $del or do { print -1; next };
		$let = 'A';
		print join "\n", $del , map { ( $let ++x $del ) x $_ } split;

		}
	:
	do {
		(eval join '==', (sort {$b <=> $a} split)[0 .. 1]) and do { print -1; next };
		$sum = eval join '+', map { eval join '*', split } /\d+ \d+/g;
		$max = (sort {$b <=> $a} split)[0];
		$sum == $max ** 2 or do { print -1; next };
		$bemax = $_;
		$bemax =~ s/\b$max\b (\d+) ?//;
		$wide = $1;
#		print $bemax;
		$let = 'A';
		$pre = join "\n", ( $let x ($max - $wide) ) x (split ' ', $bemax)[1] ,
			( ++ $let x ($max - $wide) ) x (reverse split ' ', $bemax)[0];
#		print $pre;
		$pre =~ s/$/ 'C' x $wide /gems;
#		print $pre;

		/^\b$max\b/         and $pre =~ y/ABC/BCA/;
		/^\d+ \d+ \b$max\b/ and $pre =~ y/ABC/ACB/;
		/\b$max\b \d+$/     and $pre =~ y/ABC/ABC/;

		print $max, "\n", $pre;
		}

	}

$\ = $/;

while(<>){

#	print;
	%h = ();
	map { ++ $h{ $_ } } split;
	$f = 0;
	$_ > 3 and $f ++  for values %h;
	$f and do { print -1; next };
	s/(\d+) (\d+)/ join ' ', sort {$b <=> $a} @_ = ($1, $2) /ge;
#	print;

	/(\d+).{2,}\b\1\b.{2,}\b\1\b/ ?
	do {
		$del = $1;
		s/\b$del\b//g;
		$sum = eval join '+', split;
		$sum == $del or do { print -1; next };
		$let = 'A';
		print join "\n", $del , map { ( $let ++x $del ) x $_ } split;

		}
	:
	do {
		(eval join '==', (sort {$b <=> $a} split)[0 .. 1]) and do { print -1; next };
		$sum = eval join '+', map { eval join '*', split } /\d+ \d+/g;
		$max = (sort {$b <=> $a} split)[0];
		$sum == $max ** 2 or do { print -1; next };
		$bemax = $_;
		$bemax =~ s/\b$max\b (\d+) ?//;
		$wide = $1;
#		print $bemax;
		$let = 'A';
		$pre = join "\n", ( $let x ($max - $wide) ) x (split ' ', $bemax)[1] ,
			( ++ $let x ($max - $wide) ) x (reverse split ' ', $bemax)[0];
#		print $pre;
		$pre =~ s/$/ 'C' x $wide /gems;
#		print $pre;

		/^\b$max\b/         and $pre =~ y/ABC/BCA/;
		/^\d+ \d+ \b$max\b/ and $pre =~ y/ABC/ACB/;
		/\b$max\b \d+$/     and $pre =~ y/ABC/ABC/;

		print $max, "\n", $pre;
		}

	}

$\ = $/;

while(<>){

	$sum = eval join '+', map { eval join '*', split } /\d+ \d+/g;
	$max = (sort {$b <=> $a} split)[0];
	$sum == $max ** 2 or do { print -1; next };

#	print;
	%h = ();
	map { ++ $h{ $_ } } split;
	$f = 0;
	$_ =~ /[46]/ and $f ++  for values %h;
	$f and do { print -1; next };
	s/(\d+) (\d+)/ join ' ', sort {$b <=> $a} @_ = ($1, $2) /ge;
#	print;

	$g = 0;
	$_ == 5 and $g ++  for values %h;
	$g and do {
		$max = (sort {$b <=> $a} split)[0];
		/\b$max $max\b/ and do { print -1; next };
		$min = (sort {$b <=> $a} split)[1];
		$min * 2 == $max or do { print -1; next };

		$let = 'A';
		$pre = join "\n", ($let x $min) x $min , (++ $let x $min) x $min;
		$pre =~ s/$/ 'C' x $min /gems;

		/^\b$max\b/         and $pre =~ y/ABC/BCA/;
		/^\d+ \d+ \b$max\b/ and $pre =~ y/ABC/ACB/;
		/\b$max\b \d+$/     and $pre =~ y/ABC/ABC/;

		print $max, "\n", $pre;
		next;
		};

	/\b(\d+)\b.{2,}\b\1\b.{2,}\b\1\b/ ?
	do {
		$del = $1;
		s/\b$del\b//g;
		$sum = eval join '+', split;
		$sum == $del or do { print -1; next };
		$let = 'A';
		print join "\n", $del , map { ( $let ++x $del ) x $_ } split;

		}
	:
	do {
		(eval join '==', (sort {$b <=> $a} split)[0 .. 1]) and do { print -1; next };
		$sum = eval join '+', map { eval join '*', split } /\d+ \d+/g;
		$max = (sort {$b <=> $a} split)[0];
		$sum == $max ** 2 or do { print -1; next };
		$bemax = $_;
		$bemax =~ s/\b$max\b (\d+) ?//;
		$wide = $1;
#		print $bemax;
		$let = 'A';
		$pre = join "\n", ( $let x ($max - $wide) ) x (split ' ', $bemax)[1] ,
			( ++ $let x ($max - $wide) ) x (reverse split ' ', $bemax)[0];
#		print $pre;
		$pre =~ s/$/ 'C' x $wide /gems;
#		print $pre;

		/^\b$max\b/         and $pre =~ y/ABC/BCA/;
		/^\d+ \d+ \b$max\b/ and $pre =~ y/ABC/ACB/;
		/\b$max\b \d+$/     and $pre =~ y/ABC/ABC/;

		print $max, "\n", $pre;
		}

	}

$\ = $/;

while(<>){

#	print;
	%h = ();
	map { ++ $h{ $_ } } split;
	$f = 0;
	$_ =~ /[46]/ and $f ++  for values %h;
	$f and do { print -1; next };
	s/(\d+) (\d+)/ join ' ', sort {$b <=> $a} @_ = ($1, $2) /ge;
#	print;

	$g = 0;
	$_ == 5 and $g ++  for values %h;
	$g and do {
		$max = (sort {$b <=> $a} split)[0];
		/\b$max $max\b/ and do { print -1; next };
		$min = (sort {$b <=> $a} split)[1];
		$min * 2 == $max or do { print -1; next };

		$let = 'A';
		$pre = join "\n", ($let x $min) x $min , (++ $let x $min) x $min;
		$pre =~ s/$/ 'C' x $min /gems;

		/^\b$max\b/         and $pre =~ y/ABC/BCA/;
		/^\d+ \d+ \b$max\b/ and $pre =~ y/ABC/ACB/;
		/\b$max\b \d+$/     and $pre =~ y/ABC/ABC/;

		print $max, "\n", $pre;
		next;
		};

	/(\d+).{2,}\b\1\b.{2,}\b\1\b/ ?
	do {
		$del = $1;
		s/\b$del\b//g;
		$sum = eval join '+', split;
		$sum == $del or do { print -1; next };
		$let = 'A';
		print join "\n", $del , map { ( $let ++x $del ) x $_ } split;

		}
	:
	do {
		(eval join '==', (sort {$b <=> $a} split)[0 .. 1]) and do { print -1; next };
		$sum = eval join '+', map { eval join '*', split } /\d+ \d+/g;
		$max = (sort {$b <=> $a} split)[0];
		$sum == $max ** 2 or do { print -1; next };
		$bemax = $_;
		$bemax =~ s/\b$max\b (\d+) ?//;
		$wide = $1;
#		print $bemax;
		$let = 'A';
		$pre = join "\n", ( $let x ($max - $wide) ) x (split ' ', $bemax)[1] ,
			( ++ $let x ($max - $wide) ) x (reverse split ' ', $bemax)[0];
#		print $pre;
		$pre =~ s/$/ 'C' x $wide /gems;
#		print $pre;

		/^\b$max\b/         and $pre =~ y/ABC/BCA/;
		/^\d+ \d+ \b$max\b/ and $pre =~ y/ABC/ACB/;
		/\b$max\b \d+$/     and $pre =~ y/ABC/ABC/;

		print $max, "\n", $pre;
		}

	}

$\ = $/;

while(<>){

	s/(\d+) (\d+)/ join ' ', sort {$b <=> $a} @_ = ($1, $2) /ge;
#	print;

	/(\b\d+\b).{2,}\1.{2,}\1/ ?
	do {
		$del = $1;
		s/\b$del\b//g;
		$sum = eval join '+', split;
		$sum == $del or do { print -1; next };
		$let = 'A';
		print join "\n", $del , map { ( $let ++x $del ) x $_ } split;

		}
	:
	do {
		(eval join '==', (sort {$b <=> $a} split)[0 .. 1]) and do { print -1; next };
		$sum = eval join '+', map { eval join '*', split } /\d+ \d+/g;
		$max = (sort {$b <=> $a} split)[0];
		$sum == $max ** 2 or do { print -1; next };
		$bemax = $_;
		$bemax =~ s/\b$max\b (\d+) ?//;
		$wide = $1;
#		print $bemax;
		$let = 'A';
		$pre = join "\n", ( $let x ($max - $wide) ) x (split ' ', $bemax)[1] ,
			( ++ $let x ($max - $wide) ) x (reverse split ' ', $bemax)[0];
#		print $pre;
		$pre =~ s/$/ 'C' x $wide /gems;
#		print $pre;

		/^\b$max\b/         and $pre =~ y/ABC/BCA/;
		/^\d+ \d+ \b$max\b/ and $pre =~ y/ABC/ACB/;
		/\b$max\b \d+$/     and $pre =~ y/ABC/ABC/;

		print $max, "\n", $pre;
		}

	}

$\ = $/;

while(<>){

#	print;
	%h = ();
	map { ++ $h{ $_ } } split;
	$f = 0;
	$_ =~ /[46]/ and $f ++  for values %h;
	$f and do { print -1; next };
	s/(\d+) (\d+)/ join ' ', sort {$b <=> $a} @_ = ($1, $2) /ge;
#	print;

	$g = 0;
	$_ == 5 and $g ++  for values %h;
	$g and do {
		$max = (sort {$b <=> $a} split)[0];
		/\b$max $max\b/ and do { print -1; next };
		$min = (sort {$b <=> $a} split)[1];
		$min * 2 == $max or do { print -1; next };

		$let = 'A';
		$pre = join "\n", ($let x $min) x $min , (++ $let x $min) x $min;
		$pre =~ s/$/ 'C' x $min /gems;

		/^\b$max\b/         and $pre =~ y/ABC/BCA/;
		/^\d+ \d+ \b$max\b/ and $pre =~ y/ABC/ACB/;
		/\b$max\b \d+$/     and $pre =~ y/ABC/ABC/;

		print $max, "\n", $pre;
		next;
		};

	/\b(\d+)\b.{2,}\b\1\b.{2,}\b\1\b/ ?
	do {
		$del = $1;
		s/\b$del\b//g;
		$sum = eval join '+', split;
		$sum == $del or do { print -1; next };
		$let = 'A';
		print join "\n", $del , map { ( $let ++x $del ) x $_ } split;

		}
	:
	do {
		(eval join '==', (sort {$b <=> $a} split)[0 .. 1]) and do { print -1; next };
		$sum = eval join '+', map { eval join '*', split } /\d+ \d+/g;
		$max = (sort {$b <=> $a} split)[0];
		$sum == $max ** 2 or do { print -1; next };
		$bemax = $_;
		$bemax =~ s/\b$max\b (\d+) ?//;
		$wide = $1;
#		print $bemax;
		$let = 'A';
		$pre = join "\n", ( $let x ($max - $wide) ) x (split ' ', $bemax)[1] ,
			( ++ $let x ($max - $wide) ) x (reverse split ' ', $bemax)[0];
#		print $pre;
		$pre =~ s/$/ 'C' x $wide /gems;
#		print $pre;

		/^\b$max\b/         and $pre =~ y/ABC/BCA/;
		/^\d+ \d+ \b$max\b/ and $pre =~ y/ABC/ACB/;
		/\b$max\b \d+$/     and $pre =~ y/ABC/ABC/;

		print $max, "\n", $pre;
		}

	}

$\ = $/;

while(<>){

	@s = sort {$b <=> $a} split;
	($max, $min) = @s[0,-1];
	$cmax = () = /\b$max\b/g;

	s/(\d+) (\d+)/ join ' ', sort {$b <=> $a} @_ = ($1, $2) /ge;

	$area = eval join '+', map { eval join '*', split } /\d+ \d+/g;

	$area == $max ** 2 or do { print -1; next };

	%hash = ();
	map { ++ $hash{ $_ } } split;
	(join ' ', values %hash) =~ /[46]/ and do { print -1; next };

	$let = 'A';

	$cmax == 3 ?
	do {
		s/\b$max\b//g;
		(eval join '+', split) == $max or do { print -1; next };

		print join "\n", $max , map { ( $let ++ x $max ) x $_ } split;
		}
	:
	do {
		$cmax == 1 or do { print -1; next };

		($bemax = $_) =~ s/\b$max\b (\d+) ?//;
		$wide2 = $1;
		$wide1 = $max - $wide2;

		$bemax =~ s/\b$wide1\b// for 1 .. 2;

		$pre = join "\n", map { ( $let ++ x $wide1 ) x $_ } split ' ', $bemax;

		$pre =~ s/$/ 'C' x $wide2 /gem;

		/^$max\b/         and $pre =~ y/ABC/BCA/;
		/^\d+ \d+ $max\b/ and $pre =~ y/ABC/ACB/;

		print join "\n", $max, $pre;

	}
}


use 5.020;
use warnings;
use utf8;

sub readNext {
    my $line = <STDIN>;
    chomp($line);
    return $line;
}

my $count = int( &readNext() );

READ: while ($count) {

    my @a = split //, &readNext();
    my @b = split //, &readNext();
    my @c = split //, &readNext();

    $count--;

    foreach ( 0 .. $#a ) {
        if ( $a[$_] eq $c[$_] || $b[$_] eq $c[$_] ) {
            next;
        }
        else {
            say "NO";
            next READ;
        }
    }

    say "YES";
}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

while(<>){
	my $A = $_;
	my $B = <>;
	my $C = <>;

	chomp for $A, $B, $C;

	my $fail = 0;

	for my $i ( 0 .. -1 + length $A ){
		my $cA = substr $A, $i, 1;
		my $cB = substr $B, $i, 1;
		my $cC = substr $C, $i, 1;

		if( not( $cA eq $cC || $cB eq $cC ) ){
			$fail = 1;
			last;
			}
		}

	print $fail ? "NO" : "YES";
	}


$a=<>;
$b=<>;
@x=sort split '',$a;
@y=sort split '',$b;
$flag=0;
if((join '',@x)eq(join '',@y)){
  @x=split '',$a;
  @y=split '',$b;
  for($i=0;$i<@x;++$i){
    ++$cnt if $x[$i]ne$y[$i];
  }
  $flag=1 if $cnt>2;
} else {
  $flag=1;
}
print qw/YES NO/[$flag];

$a=<>;
$b=<>;
@x=sort split '',$a;
@y=sort split '',$b;
$flag=0;
if((join '',@x)eq(join '',@y)){
  @x=split '',$a;
  @y=split '',$b;
  for($i=0;$i<@x;++$i){
    ++$cnt if $x[$i]ne$y[$i];
  }
  if($cnt>2){
    $flag=1;
    exit;
  }
} else {
  $flag=1;
}
print qw/YES NO/[$flag];


#!perl
use strict;
use warnings;
while(<>){
    my(@b,@a,$i,$x0,$n,$m,$s0,$pos,$length);
    ($n,$m)=split;
    for $i ( 0..$n-1 ) {
        ($s0,$x0)=split ' ',<>;
        push @a,$s0;
    }
    $length = 1;
    $b[0]=$a[0];
    for $i ( 1..$#a ){
        if ($a[$i] < $b[0]) {
            $b[0] = $a[$i];
        }
        elsif ($a[$i] >= $b[$length-1]) {
            $b[$length++] = $a[$i];
        }
        else {
            $pos = lower_bound($a[$i], \@b);
            $b[$pos] = $a[$i];
        }
    }
    #print "length=$length\n";
    print $n-$length,"\n";
}
#find the first more than or equal to $num;
sub lower_bound {
    my ($num,$array)=@_;
    my ($i,$high,$low,$mid);
    $low = -1;
    $high = $#{$array};
    while($low+1 < $high) {
        $mid=$low + int(($high-$low)/2);
        if($array->[$mid] > $num) {
            $high=$mid;
        } else {
            $low=$mid;
        }
    }
    return $high;
}

#!perl
use v5.18;
use strict;
use warnings;
# time complexity: O(Nlog(N))
# got Accepted
while(<>){
    my(@b,@a,$i,$n,$m,$s0,$pos);
    ($n,$m)=split;
    for $i ( 0..$n-1 ) {
        ($s0,$_)=split ' ',<>; push @a,$s0;
    }
    $b[0]=$a[0];
    for $i ( 1..$#a ){
        $pos=upper_bound($a[$i], \@b);
        if ($pos == @b) {push @b, $a[$i];}
        else {$b[$pos] = $a[$i];}
    }
    say $n-@b;
}

#find the first more than $num;
sub upper_bound {
    my ($num,$array)=@_;
    my ($i,$high,$low,$mid);
    $low = -1; $high = $#{$array};
    while($low+1 < $high) {
        $mid=$low + int(($high-$low)/2);
        if($array->[$mid] > $num) {$high=$mid;}
        else {$low=$mid;}
    }
    return $array->[$high]<=$num ? scalar @$array : $high;
}

#!perl
use v5.18;
# time complexity: O(Nlog(N))
while(<>){
    my(@b,@a,$i,$x0,$n,$m,$s0,$pos,$length);
    ($n,$m)=split;
    for $i ( 0..$n-1 ) {
        ($s0,$x0)=split ' ',<>; push @a,$s0;
    }
    $length = 1; $b[0]=$a[0];
    for $i ( 1..$#a ){
        $pos=upper_bound($a[$i], \@b);
        if ($pos == @b) {push @b, $a[$i];}
        else {$b[$pos] = $a[$i];}
    }
    say $n-@b;
}

#find the first more than $num;
sub upper_bound {
    my ($num,$array)=@_;
    my ($i,$high,$low,$mid);
    $low = -1; $high = $#{$array};
    while($low+1 < $high) {
        $mid=$low + int(($high-$low)/2);
        if($array->[$mid] > $num) {$high=$mid;}
        else {$low=$mid;}
    }
    return $array->[$high]<=$num ? scalar @$array : $high;
}

#!perl
use v5.18;
# time complexity: O(Nlog(N))
# got Accepted
while(<>){
    my(@b,@a,$i,$n,$m,$s0,$pos);
    ($n,$m)=split;
    for $i ( 0..$n-1 ) {
        ($s0,$_)=split ' ',<>; push @a,$s0;
    }
    $b[0]=$a[0];
    for $i ( 1..$#a ){
        $pos=upper_bound($a[$i], \@b);
        if ($pos == @b) {push @b, $a[$i];}
        else {$b[$pos] = $a[$i];}
    }
    say $n-@b;
}

#find the first more than $num;
sub upper_bound {
    my ($num,$array)=@_;
    my ($i,$high,$low,$mid);
    $low = -1; $high = $#{$array};
    while($low+1 < $high) {
        $mid=$low + int(($high-$low)/2);
        if($array->[$mid] > $num) {$high=$mid;}
        else {$low=$mid;}
    }
    return $array->[$high]<=$num ? scalar @$array : $high;
}

#!perl
use v5.20;
# time complexity: O(Nlog(N))
while(<>){
    my(@b,@a,$i,$x0,$n,$m,$s0,$pos,$length);
    ($n,$m)=split;
    for $i ( 0..$n-1 ) {
        ($s0,$x0)=split ' ',<>; push @a,$s0;
    }
    $length = 1; $b[0]=$a[0];
    for $i ( 1..$#a ){
        if ($a[$i] < $b[0]) {$b[0] = $a[$i];}
        elsif ($a[$i] >= $b[$length-1]) {$b[$length++] = $a[$i];}
        else {$b[lower_bound($a[$i], \@b)] = $a[$i];}
    }
    say $n-$length;
}

#find the first more than $num;
sub lower_bound {
    my ($num,$array)=@_;
    my ($i,$high,$low,$mid);
    $low = -1; $high = $#{$array};
    while($low+1 < $high) {
        $mid=$low + int(($high-$low)/2);
        if($array->[$mid] > $num) {$high=$mid;}
        else {$low=$mid;}
    }
    return $high;
}

#!perl
use strict;
use warnings;
while(<>){
    my(@b,@a,@len,$i,$x0,$n,$m,$s0,$pos,$result);
    ($n,$m)=split;
    for $i ( 0..$n-1 ) {
        ($s0,$x0)=split ' ',<>;
        push @a,$s0;
    }
    $b[0]=$a[0];$len[0]=1;
    for $i ( 1..$#a ){
        $pos=upper_bound($a[$i],\@b);
        if($pos==@b){
            $len[$pos]=$len[$pos-1]+1;
            $b[$pos]=$a[$i];
        } elsif ($pos==0 && $a[$i] < $b[$pos]) {
            $b[$pos] = $a[$i];
        }
        else{
            if($a[$i]==$b[$pos]){
                $len[$pos]++;
            } else{
                $b[$pos+1]=$a[$i];
            }
        }
    }
    $result=1;
    for $i ( 0.. $#len ) { $result = $len[$i] if ($result < $len[$i]); }
    print $n-$result,"\n";
}
#find the last less or equal to $num; if all of the element
#in array are less than num. return the length of the array
sub upper_bound {
    my ($num,$array)=@_;
    my ($i,$high,$low,$mid);
    $low=0;$high=$#{$array};
    while($low<=$high){
        $mid=int(($low+$high)/2);
        if($array->[$mid] > $num){
            $high=$mid-1;
        } else{
            $low=$mid+1;
        }
    }
    if($high==$#{$array} &&
       $num > $array->[$high]){
        return $high+1;
    }
    return -1!=$high ? $high : 0;
}


#!/usr/bin/perl
# your code here
$/ = $\;
$s = <>;

print -1e3 + (sort map { 1e3 + length join '', $s =~ /^[$_]+$/gm } aa .. zz)[-1];


use strict ;
use warnings ;

my $noCounters = <STDIN>;
my $noPeople = <STDIN>;

my $min = 0;

for(my $i=0;$i<$noCounters;$i++){
    my $Items = <STDIN>;
    my @noItems = split(/ /,$Items);
    my $sum = 0;
    for(my $j=0;$j<=$#noItems;$j++){
        $sum += 5*$noItems[$j];
    }
    $sum += 15*($#noItems+1);
    #print "$sum";
    if($i==0){
        $min=$sum;
    }else{
        if($sum<=$min){
            $min=$sum;
        }
    }
}

print "$min" ;


#!/usr/bin/env perl

use strict;
use warnings;
use feature qw/ say /;

# essential
my @tokens = ();

my %poly = (
    Tetrahedron => 4,
    Cube        => 6,
    Octahedron  => 8,
    Dodecahedron=> 12,
    Icosahedron => 20,
);

my $answer = 0;

my $n = read_token();

for ( 1..$n ) {
    $answer += $poly{read_token()};
}

say $answer;

sub read_line {
    chomp (my $line = <STDIN>);
    return $line;
}

sub read_token {
    @tokens = split q{ }, read_line() if @tokens == 0;
    return shift @tokens;
}

sub min {
    my @numbers = @_;
    my $min = shift @numbers;
    for ( @numbers ) {
        $min = $min<$_ ? $min : $_;
    }
    return $min;
}

sub max {
    my @numbers = @_;
    my $max = shift @numbers;
    for ( @numbers ) {
        $max = $max > $_ ? $max : $_;
    }
    return $max;
}

sub sum {
    my @numbers = @_;
    my $sum = 0;
    for (@numbers) {
        $sum += $_;
    }
    return $sum;
}

use 5.016;

my %sides = (
  Tetrahedron  => 4,
  Cube         => 6,
  Octahedron   => 8,
  Dodecahedron => 12,
  Icosahedron  => 20,
);

my $n = <>;
my $s = 0;
for (1..$n) {
  chomp (my $shape = <>);
  $s += $sides{$shape};
}
say $s;

$n = <STDIN>;
$ans = 0;
for($i=0;$i<$n;$i++)
{
	$str = <STDIN>;
	chomp($str);
	if($str =~ /Tetrahedron/)
	{
		$ans += 4;
	}
	elsif($str =~ /Cube/)
	{
		$ans += 6;
	}
	elsif($str =~ /Octahedron/)
	{
		$ans += 8;
	}
	elsif($str =~ /Dodecahedron/)
	{
		$ans += 12;
	}
	else
	{
		$ans += 20;
	}
}
print("$ans\n");

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my %h;
map { /(\w+).*?(\d+)/; $h{ $1 } = $2 } <DATA>;

while(<>){

	my $sum = 0;

	for (1 .. $_){
		$_ = <>, chomp;
		$sum += $h{ $_ };
		}

	print $sum;
	}

__DATA__
    Tetrahedron. Tetrahedron has 4 triangular faces.
    Cube. Cube has 6 square faces.
    Octahedron. Octahedron has 8 triangular faces.
    Dodecahedron. Dodecahedron has 12 pentagonal faces.
    Icosahedron. Icosahedron has 20 triangular faces.


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	my( $n, $k, $m ) = split;

	my @dict = split ' ', <>;

	my @w = split ' ', <>;

	my @groups = map ~~<>, 1 .. $k;

	my @orig = split ' ', <>;

	for( @groups ){
		my( undef, @set ) = split;

		my $min = ( sort { $a <=> $b } map { $w[ $_ - 1 ] } @set )[ 0 ];

		map { $w[ $_ - 1 ] = $min } @set;
		}

	my $i = 0;

	my %h = map { $_ => $i ++ } @dict;

	print 0 + eval join ' + ', map { $w[ $h{ $_ } ] } @orig;
	}


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while( my $n = <>){

	my $blue_area = ( $n - $n % 2 ) ** 2;

	my %count;

	map { $count{ $_ } ++ } split ' ', <>;

	my @BLUE;
	my @YELLOW;
	my @GREEN;

	for my $key ( keys %count ){
		push @BLUE, ( $key ) x ( $count{ $key } / 4 );
		push @YELLOW, $key if $count{ $key } % 4 >= 2;
		push @GREEN, $key if $count{ $key } % 2 == 1;
		}

	push @YELLOW, ( pop @BLUE ) x 2 while @BLUE * 4 > $blue_area;

	if( $blue_area != @BLUE * 4 or
		$n % 2 and 1 != @GREEN ){
		print "NO";
		next;
		}

	my @MATRIX;

	while( @BLUE ){
		push @MATRIX, [ splice @BLUE, 0, $n / 2, () ];
		}

	for( @MATRIX ){
		push @{ $_ }, grep { defined } pop @YELLOW, reverse @{ $_ };
		}

	@GREEN and push @MATRIX, [ @YELLOW, @GREEN, reverse @YELLOW ];

	push @MATRIX, reverse @MATRIX[ 0 .. ( $n >> 1 ) - 1 ];

	print "YES";
	print "@{ $_ }" for @MATRIX;
	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while( my $n = <>){

	my $blue_area = ( $n - $n % 2 ) ** 2;

	$_ = join ' ', sort split ' ', <>;

	my @BLUE;
	my @YELLOW;

	s/\b(\d+)(\s+\1){3}\b/ '' x push @BLUE  , $1 /ge;
	s/\b(\d+)(\s+\1){1}\b/ '' x push @YELLOW, $1 /ge;

	my @GREEN = split;

	push @YELLOW, ( pop @BLUE ) x 2 while @BLUE * 4 > $blue_area;

	if( $blue_area != @BLUE * 4 or
		$n % 2 and 1 != @GREEN ){
		print "NO";
		next;
		}

	my @MATRIX;

	$n % 2 and @MATRIX = [ grep { defined } ( reverse @YELLOW[ 0 .. $n / 2 - 1 ] ), @GREEN, @YELLOW[ 0 .. $n / 2 - 1 ] ];

	while( @BLUE ){
		my @half = splice @BLUE, 0, $n / 2, ();

		my $row = [ grep { defined } @half, pop @YELLOW, reverse @half ];
		@MATRIX = ( $row, @MATRIX, $row );
		}

	print "YES";
	print "@{ $_ }" for @MATRIX;
	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my $debug = 0;

while(<>){
	$debug and print '-' x 20;

	chomp;
	my $n = $_;

	my %h;

	map { $h{ $_ } ++ } split ' ', <>;

	my @odd_cross;

	if( $n % 2 == 1 ){

		for my $key ( sort keys %h ){
			if( $h{ $key } % 2 == 1 ){
				$h{ $key } -= 1;
				push @odd_cross, $key;
				last;
				}
			}

		my $require = $n - 1;

		for my $key ( sort keys %h ){
			if( $require > 0 and $h{ $key } % 4 == 2 ){
				$h{ $key } -= 2;
				push @odd_cross, $key;
				$require --;
				}
			}

		for my $key ( sort keys %h ){
			while( $require > 0 and $h{ $key } % 4 == 0 and $h{ $key } > 0 ){
				$h{ $key } -= 4;
				push @odd_cross, ( $key ) x 2;
				$require -= 2;
				}
			}
		}

	$debug and print "odd_cross:[@odd_cross]";

	my $sum = eval join '+', map { $h{ $_ } } keys %h;
	my $not_tetra = grep { $h{ $_ } % 4 != 0 } keys %h;

	$debug and print "n:[$n], sum:[$sum], not_tetra:[$not_tetra]";

	if( $not_tetra or $sum != ( $n - ( $n % 2 ? 1 : 0 ) ) ** 2 ){
		print "NO";
		next;
		}

	my @fill = map { ( $_ ) x ( $h{ $_ } / 4 ) } sort keys %h;

	$debug and print "fill:[@fill]";

	my @palindrome;

	@odd_cross and @palindrome = [ ( reverse @odd_cross[ 0 .. $n / 2 ] ), @odd_cross[ 1 .. $n / 2 ] ];

	while( @fill ){
		my @line = splice @fill, 0, $n / 2, ();

		my $unshift_push = [ @line, @odd_cross ? pop @odd_cross : (), reverse @line ];
		unshift @palindrome, $unshift_push;
		push @palindrome, $unshift_push;
		}

	print "YES";
	print "@{ $_ }" for @palindrome;
	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while( my $n = <>){

	my $blue_area = ( $n - $n % 2 ) ** 2;

	my %count;

	map { $count{ $_ } ++ } split ' ', <>;

	my @BLUE;
	my @YELLOW;
	my @GREEN;

	for my $key ( keys %count ){
		push @BLUE, ( $key ) x ( $count{ $key } / 4 );
		push @YELLOW, $key if $count{ $key } % 4 == 2;
		push @GREEN, $key if $count{ $key } % 2 == 1;
		}

	push @YELLOW, ( pop @BLUE ) x 2 while @BLUE * 4 > $blue_area;

	if( $blue_area != @BLUE * 4 or
		$n % 2 and 1 != @GREEN ){
		print "NO";
		next;
		}

	my @MATRIX;

	while( @BLUE ){
		push @MATRIX, [ splice @BLUE, 0, $n / 2, () ];
		}

	for( @MATRIX ){
		push @{ $_ }, grep { defined } pop @YELLOW, reverse @{ $_ };
		}

	@GREEN and push @MATRIX, [ @YELLOW, @GREEN, reverse @YELLOW ];

	push @MATRIX, reverse @MATRIX[ 0 .. ( $n >> 1 ) - 1 ];

	print "YES";
	print "@{ $_ }" for @MATRIX;
	}


#!/usr/bin/env perl

use strict;
use warnings;
use feature qw/ say /;

use Carp;

# essential
my @tokens = ();

sub ceil {
    my $n = shift;
    my $in = int($n);
    return $n if $in == $n;
    return $in + 1;
}

sub say_all {
    my @args = @_;
    say join ' ', @args;
}

sub read_line {
    chomp (my $line = <STDIN>);
    return $line;
}

sub read_token {
    @tokens = split q{ }, read_line() if @tokens == 0;
    return shift @tokens;
}

sub min {
    my @numbers = @_;
    my $min = shift @numbers;
    for ( @numbers ) {
        $min = $min<$_ ? $min : $_;
    }
    return $min;
}

sub max {
    my @numbers = @_;
    my $max = shift @numbers;
    for ( @numbers ) {
        $max = $max > $_ ? $max : $_;
    }
    return $max;
}

sub sum {
    my @numbers = @_;
    my $sum = 0;
    for (@numbers) {
        $sum += $_;
    }
    return $sum;
}

sub ref_ref_scalar {
    my $ref = shift;
    return 1 if ref($ref) eq 'SCALAR';
}

sub toggle {
    my $ref = shift;
    croak "$ref does not reference to scalar" if !ref_ref_scalar($ref);

    $$ref = !$$ref;
}

sub odd {
    my $num = shift;
    return $num % 2 == 1;
}

sub even {
    my $num = shift;
    return $num % 2 == 0;
}

sub sum_of_digits {
    my $n = shift;
    my @numbers = split q{}, $n;

    my $sum = 0;

    for (@numbers) {
        $sum += $_;
    }

    return $sum;
}

# solution

my $n = read_line;

for (1..$n) {
    my ($a, $b) = split q{ }, read_line;
    if ($a == 1 || $b == 1) {
        say 'YES';
        next;
    }
    if ($a == 2 && $b == 2) {
        say 'YES';
        next;
    }
    say 'NO';
}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

while(<>){
	my( $n, $m ) = split;

	print $n == 2 && $m == 2 || $n == 1 || $m == 1 ? "YES" : "NO";
	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

while(<>){
	my( $n, $m ) = split;

	print $n == 2 && $n == 2 || $n == 1 || $m == 1 ? "YES" : "NO";
	}


my $n = <STDIN>;
chomp $n;

my $int_list = <STDIN>;
chomp $int_list;

my @array = split(" ",$int_list);

my $min = $array[0];

my $sec = "NO";

for my $elem (@array){
    if ($elem < $min){
        $sec = $min;
        $min = $elem;
    }
    elsif (($elem > $min) && (($sec eq "NO") ||($sec > $elem))){
        $sec = $elem;
    }
}

print $sec;

#!/usr/bin/perl

use strict;
use warnings;
<>;
my $str = <>;
chomp($str);
my @arr = split(/ /, $str);
my $min = $arr[0];
my $mmin;
for my $i (@arr) {
    if ($i < $min) {
        $mmin = $min;
        $min = $i;
    } elsif ($i > $min) {
        if ( (! defined $mmin) || ($mmin > $i) ) {
             $mmin = $i;
        }
    }
}
if (!defined $mmin) {
    print "NO";
} else {
    print $mmin;
}

$n = <>;
@a = split(' ', <>);
$m = 123;
foreach(@a){
	$m = $_ if $_ < $m;
}
foreach(@a){
	print $_ and exit
	if $_ > $m;
}
print "NO";

$n = <>;
@a = split(' ', <>);
unshift @a, $m = 123;
foreach(@a){
	$m = $_ if $_ < $m;
}
foreach(@a){
	print $_ and exit
	if $_ > $m;
}
print "NO";


#!/usr/bin/env perl
#===============================================================================
#         STDIN: dzy_string.pl
#       AUTHOR: Phoenix Ikki (liuxueyang.github.io), liuxueyang457@gmail.com
# ORGANIZATION: Hunan University
#      CREATED: 05/26/2015 09:44:35 AM
#===============================================================================

use strict;
use warnings;
use utf8;
use 5.012;

#open STDIN, "in.txt";
chomp(my $str = <STDIN>);
my @st = split //, $str;
chomp(my $cnt = <STDIN>);
chomp($_ = <STDIN>);
my @alph = split;
my %value;
while (my ($inde, $val) = each @alph){
    $value{chr($inde + ord('a'))} = $val;
}
my $sum = 0;
while (my ($inde, $val) = each @st){
    $sum += ($inde + 1) * $value{$val};
}
my @sor_alph = sort {$b <=> $a} @alph;
my $len = @st;
while ($cnt) {
    $sum += $sor_alph[0] * ($len + $cnt);
    $cnt--;
}
print $sum . "\n";


use strict;
use warnings;

my ( $n, $m ) = split ( " ", <> );
my %map;
while ( $m-- ){
	my ( $a, $b ) = split ( " ", <> );
	$map{$a} = $b;
}

print join ( " ", map ( ( length $_ <= length $map{$_} ? $_ : $map{$_} ), split ( " ", <> ) ) )."\n";

$\ = $/;
while (<>){
	($n, $m)=split;
	undef %h;
	for $i (1..$m){
		($first, $second) = split " ", <>;
		$value = length $second < length $first ? $second : $first;
		$h{$first} = $value;
		}
	$, = $";
#	print keys %h;
#	print values %h;

	$_ = <>;
	s/(\w+)/$h{$1}/eg;

	print
	}


#!/usr/bin/perl

use strict;
use warnings;
use integer;

<>;

while (<>) {
	chomp;
	if (is_homo($_)) {
		print "-1\n";
	} else {
		my @chars = split //;
		print sort @chars;
		print "\n";
	}
}

sub is_homo {
	my $string = shift;
	my @chars = split //, $string;
	my $is_homo = 1;

	if (@chars > 0) {
		my $prev = $chars[0];
		for (my $i = 1; $i < @chars; $i++) {
			$is_homo = $is_homo && $chars[$i] eq $prev;
			$prev = $chars[$i];
		}
	}
	return $is_homo;
}

sub is_palindrome {
	my $string = shift;
	my @chars = split //, $string;
	my $radius = @chars / 2;

	my $is_palindrome = 1;
	for (my $i = 0; $i < $radius; $i++) {
		$is_palindrome = $is_palindrome && $chars[$i] eq $chars[$#chars - $i];
	}
	return $is_palindrome;
}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

while(<>){
	chomp;

	$_ = join '', sort split //;

	print /^(.)\1*$/ ? -1 : $_;
	}


#!/usr/bin/perl

use warnings;
use strict;

my $debug = 0;

$\ = $/;

<>;

while(<>){
	$debug and print '-' x 15;

	my( $A, $B ) = split;

	$debug and print "$A $B";

	$_ = sprintf "%021b", $_ for $A, $B;

	my $len;
	my $c = 0;

	OUTER:
	while( 1 ){
		while( $A =~ m/^0/ and $B =~ m/^0/ ){
			$A =~ s/.//;
			$B =~ s/.//;
			next OUTER;
			}

		while( $A =~ m/^1/ and $B =~ m/^1/ ){
			$A =~ s/.//;
			$B =~ s/.//;
			next OUTER;
			}

		while( $A =~ m/^0/ and $B =~ m/^1/ ){

			$len = length $A;
			$c ++;

			$A =~ s/.//;
			$B =~ s/.//;

			next OUTER;
			}

		last;
		}

	$debug and print $A;
	$debug and print $B;

	my $copy_B = $B;

	$_ = eval "0b$_" for $A, $B;

	$debug and print "$A $B";

	print +( sort { $a <=> $b }
		1 + $A - $B,
		( $c > 1 || $copy_B =~ m/1/ ? 1 : 0 ) + 0 - $A + ( 1 << ( $len - 1 ) )
		)[ 0 ];
	}

#!/usr/bin/perl

use warnings;
use strict;

my $debug = 0;

$\ = $/;

<>;

while(<>){
	$debug and print '-' x 15;

	my( $A, $B ) = split;

	$debug and print "$A $B";

	$_ = sprintf "%021b", $_ for $A, $B;

	my $len;
	my $c = 0;

	OUTER:
	while( 1 ){
		while( $A =~ m/^0/ and $B =~ m/^0/ ){
			$A =~ s/.//;
			$B =~ s/.//;
			next OUTER;
			}

		while( $A =~ m/^1/ and $B =~ m/^1/ ){
			$A =~ s/.//;
			$B =~ s/.//;
			next OUTER;
			}

		while( $A =~ m/^0/ and $B =~ m/^1/ ){

			$len = length $A;
			$c ++;

			$A =~ s/.//;
			$B =~ s/.//;

			next OUTER;
			}

		last;
		}

	$debug and print $A;
	$debug and print $B;

	$_ = eval "0b$_" for $A, $B;

	$debug and print "$A $B";

	print +( sort { $a <=> $b }
		1 + $A - $B,
		( $c > 1 ? 1 : 0 ) + 0 - $A + ( 1 << ( $len - 1 ) )
		)[ 0 ];
	}

#!/usr/bin/perl

use warnings;
use strict;

my $debug = 0;

$\ = $/;

<>;

while(<>){
	$debug and print '-' x 15;

	my( $A, $B ) = split;

	$debug and print "$A $B";

	$_ = sprintf "%021b", $_ for $A, $B;

	my $len;
	my $c = 0;

	OUTER:
	while( 1 ){
		while( $A =~ m/^0/ and $B =~ m/^0/ ){
			$A =~ s/.//;
			$B =~ s/.//;
			next OUTER;
			}

		while( $A =~ m/^1/ and $B =~ m/^1/ ){
			$A =~ s/.//;
			$B =~ s/.//;
			next OUTER;
			}

		while( $A =~ m/^0/ and $B =~ m/^1/ ){

			$len = length $A;
			$c ++;

			$A =~ s/.//;
			$B =~ s/.//;

			next OUTER;
			}

		last;
		}

	$debug and print $A;
	$debug and print $B;

	$_ = eval "0b$_" for $A, $B;

	$debug and print "$A $B";

	print +( sort { $a <=> $b }
		1 + $A - $B,
		( $c > 1 ? 1 : 0 ) + $B - $A + ( 1 << ( $len - 1 ) )
		)[ 0 ];
	}


$t=readline STDIN;
chomp $t;
 while ($t) {
     $n= readline STDIN; chomp $n;
     @line=split / /, readline STDIN; chomp @line;

    my $min;
    while($l=shift @line) {
     foreach $k (@line) {
        $min=abs($k-$l) if abs($k-$l)<$min or !defined $min;
        last if !$min;
     }
    }

    print $min;
    print "\n";
    $t--;
 }

$t=readline STDIN;
chomp $t;
 while ($t) {
     $n= readline STDIN; chomp $n;
     @line=split / /, readline STDIN; chomp @line;

    my $min;
    while($l=shift @line) {
     foreach $k (@line) {
        $min=abs($k-$l) if abs($k-$l)<$min or !defined $min;
        last if !$min;
     }
     last if !min;
    }

    print $min;
    print "\n";
    $t--;
 }


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

while(<>){
	my( $n, $c ) = split;
	$_ = <>, chomp;
	$_ x= 2;

	if( $c eq 'g' ){
		print 0;
		next;
		}

	print -1 + ( sort { $b <=> $a } map length, m/$c.*?g/g )[ 0 ];
	}


#!/usr/bin/env perl
use strict;
use warnings;
use feature qw/ say /;
use constant TRUE => 1;
use constant FALSE => 0;

sub min {
    my @numbers = @_;
    my $min = shift @numbers;
    for ( @numbers ) {
        $min = $min<$_ ? $min : $_;
    }
    return $min;
}

# start

chomp (my $t = <STDIN>);

for (1..$t) {
    chomp (my $n = <STDIN>);
    chomp (my $line = <STDIN>);
    my @prices = split q{ }, $line;
    my $i = 1;
    my @prices_and_order
        = map { [$i++,$_] } @prices;

    @prices_and_order
        = sort { $a->[1] <=> $b->[1] } @prices_and_order;

    my $pao = shift @prices_and_order;
    my $max = $pao->[0];
    my $answer = 0;
    for ( @prices_and_order ) {
        if ( $pao->[1] == $_->[1] ) {
            if ( $_->[0] > $max ) {
                $max = $_->[0];
            }
            else {
                $answer++;
            }
        }
        else {
            if ( $max > $_->[0] ) {
                $answer++;
            }
            else {
                $max = $_->[0];
            }
        }
        $pao = $_;
    }
    say $answer;
}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my $debug = 0;

<>;

while(<>){
	$debug and print '-' x 15;

	@_ = split ' ', <>;

	$debug and print "\@_: @_";

	@_ = reverse @_;

	my @min;

	$min[ 0 ] = $_[ 0 ];

	my $min = $min[ 0 ];

	for my $i ( 1 .. @_ - 1 ){
		$min > $_[ $i ] and $min = $_[ $i ];
		$min[ $i ] = $min;
		}

	@min = reverse @min;
	@_ = reverse @_;

	$debug and print "\@min: @min";

	my $cnt = 0;

	for my $i ( 0 .. @_ - 1 ){
		$cnt += $_[ $i ] > $min[ $i ];
		}

	print $cnt;
	}

#!/usr/bin/env perl
use strict;
use warnings;
use feature qw/ say /;
use constant TRUE => 1;
use constant FALSE => 0;

sub min {
    my @numbers = @_;
    my $min = shift @numbers;
    for ( @numbers ) {
        $min = $min<$_ ? $min : $_;
    }
    return $min;
}

# start

chomp (my $t = <STDIN>);

for (1..$t) {
    chomp (my $n = <STDIN>);
    chomp (my $line = <STDIN>);
    my @prices = split q{ }, $line;
    my $i = 1;
    my @prices_and_order
        = map { [$i++,$_] } @prices;

    @prices_and_order
        = sort { $a->[1] <=> $b->[1] } @prices_and_order;

    my $pao = shift @prices_and_order;
    my $max = $pao->[0];
    my $answer = 0;
    for ( @prices_and_order ) {
        if ( $pao->[1] == $_->[1] ) {
        }
        else {
            if ( $max > $_->[0] ) {
                $answer++;
            }
            else {
                $max = $_->[0];
            }
        }
        $pao = $_;
    }
    say $answer;
}

#!/usr/bin/env perl
use strict;
use warnings;
use feature qw/ say /;
use constant TRUE => 1;
use constant FALSE => 0;

sub min {
    my @numbers = @_;
    my $min = shift @numbers;
    for ( @numbers ) {
        $min = $min<$_ ? $min : $_;
    }
    return $min;
}

# start

chomp (my $t = <STDIN>);

for (1..$t) {
    chomp (my $n = <STDIN>);
    chomp (my $line = <STDIN>);
    my @prices = split q{ }, $line;
    my $i = 1;
    my @prices_and_order
        = map { [$i++,$_] } @prices;

    @prices_and_order
        = sort { $a->[1] <=> $b->[1] } @prices_and_order;

    my $pao = shift @prices_and_order;
    my $max = $pao->[0];
    my $answer = 0;
    for ( @prices_and_order ) {
        if ( $pao->[1] == $_->[1] ) {
            $max = $_->[0];
        }
        else {
            if ( $max > $_->[0] ) {
                $answer++;
            }
            else {
                $max = $_->[0];
            }
        }
        $pao = $_;
    }
    say $answer;
}


#!/bin/perl -w
use strict;
use warnings;

my $n = <>;
if ($n =~ /0/) {
    $n =~ s/0//;
} else {
    $n =~ s/1//;
}
print $n;

#!/bin/bash/perl
$input=<>;
@inputProcessor=split(" ",$input);
$input=$inputProcessor[0];
@elements=split("",$input);
$f=0;
for($i=0;$i<@elements;++$i){
	if($f==0 && $i==(@elements-1)){
		#do nothing
	}
	else{
		if($elements[$i] eq 0 && $f==0){
			#then trim it and dont print
			#also signal the flag
			$f=1;
		}
		else{
			print $elements[$i];
		}
	}
}

#!/bin/bash/perl
$input=<>;
@inputProcessor=split(" ",$input);
$input=$inputProcessor[0];
$input=$input+0;
@elements=split("",$input);
$f=0;
for($i=0;$i<@elements;++$i){
	if($f==0 && $i==(@elements-1)){
		#do nothing
	}
	else{
		if($elements[$i]==0 && $f==0){
			#then trim it and dont print
			#also signal the flag
			$f=1;
		}
		else{
			print $elements[$i];
		}
	}
}


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	my( $n, $x ) = split;
	@_ = split ' ', <>;

	my $sum = unpack '%32i' . @_, pack 'i' . @_, @_;

	print $sum + @_ - 1 == $x ? "YES" : "NO";
	}


while(<>){
	chomp;
	$a=0;
	while (s/^(.*?)bear/ear/){
		$a+=(1+length $1)*(1+length $');
		}
	print $a
	}

#!/bin/perl

chomp(my $str = <>);
my $str_len = length $str;
my $start = 0;
my $ans = 0;
while ((my $idx = index($str, 'bear', $start)) != -1) {
    my ($f, $s) = ($idx - $start, $str_len - $idx - 4);
    $ans += $f + $s + 1 + $f * $s;
    $start = $idx + 1;
}
print $ans . "\n";


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

while(<>){
	chomp;

	/./;
	my $open = $&;

	s/$open/(/g;

	/[ABC]$/ or do {
		print 'NO';
		next;
		};

	my $close = $&;

	s/$close/)/g;

	my $A = s/[ABC]/(/gr;
	my $B = s/[ABC]/)/gr;

	1 while $A =~ s/[(][)]//g;
	1 while $B =~ s/[(][)]//g;

	if( ! $A or ! $B ){
		print "YES";
		}
	else{
		print "NO";
		}
	}


#!/bin/bash/perl
$input=<>;
@reference = split(" ",$input);
$input=<>;
@tofind=split(" ",$input);
#for($x=0;$x<@tofind;++$x){print length($tofind[$x])."\n";}
%hash;
for($var='a';$var ne 'aa';++$var){
    $hash{$var}=0;
}
for($var='A';$var ne 'AA';++$var){
    $hash{$var}=0;
}
#fill the hash
for($i=0;$i<@reference;++$i){
    @temp=split("",$reference[$i]);
    for($j=0;$j<@temp;++$j){
        ++$hash{$temp[$j]};
    }
}

#start checking the hash
$notfound=1;
for($i=0;$i<@tofind && $notfound==1;++$i){
    @temp=split("",$tofind[$i]);
    for($j=0;$j<@temp;++$j){
        if($hash{$temp[$j]}<=0){
            $notfound=0;
            $j=@temp;
        }
        else{
            $hash{$temp[$j]}--;
        }
    }
}

if($notfound==0){
    print "NO";
}
else{
    print "YES";
}

#!/bin/bash/perl
$input=<>;
@reference = split(" ",$input);
$input=<>;
@tofind=split(" ",$input);
#for($x=0;$x<@tofind;++$x){print length($tofind[$x])."\n";}
$tofindnums=@tofind;
$havenums=@reference;
$found=0;
$inflag;

for($i=0;$i<@tofind;++$i){
    $wordlen=length($tofind[$i]);
    $inflag=1;

    for($j=0;$j<@reference && $inflag;++$j){

        if(length($reference[$j])>= $wordlen){

            for($k=0;$k<=length($reference[$j])-$wordlen;++$k){
                $sub = substr($reference[$j],$k,$wordlen);
                #print "sub found is $sub \n";
                if($sub eq $tofind[$i]){
                    #increase found enrty
                    ++$found;
                    #now replenish this with new character
                    @temp=split("",$reference[$j]);
                    for($s=$k;$s<$k+$wordlen;++$s){
                        $temp[$s]='-';
                    }
                    $tempnew;
                    for($s=0;$s<@temp;++$s){
                        if($temp[$s] ne '-')
                        {$tempnew=$tempnew.$temp[$s];}
                    }
                    $reference[$j]=$tempnew;

                    #propogate out of array
                    $k=length($reference[$j]);
                    #mark outer array bbreaker
                    $inflag=0;
                }
            }
        }
    }
}

if($found==$tofindnums){
    print "YES";
}
else{
    #print "found value is $found\n expected value $tofindnums\n";
    print "NO";
}

#!/bin/bash/perl
$input=<>;
@reference = split(" ",$input);
$input=<>;
@tofind=split(" ",$input);
#for($x=0;$x<@tofind;++$x){print length($tofind[$x])."\n";}
%hash;
for($var='a';$var ne 'aa';++$var){
    $hash{$var}=0;
}
for($var='A';$var ne 'AA';++$var){
    $hash{$var}=0;
}
#fill the hash
for($i=0;$i<@reference;++$i){
    @temp=split("",$reference[$i]);
    for($j=0;$j<@temp;++$j){
        ++$hash{$temp[$j]};
    }
}
#start checking the hash
$notfound=1;
for($i=0;$i<@tofind && $notfound==1;++$i){
    @temp=split("",$tofind[$$i]);
    for($j=0;$j<@temp;++$j){
        if($hash{$temp[$j]}<=0){
            $notfound=0;
            $j=@temp;
        }
    }
}

if($notfound==0){
    print "NO";
}
else{
    print "YES";
}

#!/bin/bash/perl
$input=<>;
@reference = split(" ",$input);
$input=<>;
@tofind=split(" ",$input);
#for($x=0;$x<@tofind;++$x){print length($tofind[$x])."\n";}
%hash;
for($var='a';$var ne 'aa';++$var){
    $hash{$var}=0;
}
for($var='A';$var ne 'AA';++$var){
    $hash{$var}=0;
}
#fill the hash
for($i=0;$i<@reference;++$i){
    @temp=split("",$reference[$i]);
    for($j=0;$j<@temp;++$j){
        ++$hash{$temp[$j]};
    }
}
#start checking the hash
$notfound=1;
for($i=0;$i<@tofind && $notfound==1;++$i){
    @temp=split("",$tofind[$$i]);
    for($j=0;$j<@temp;++$j){
        if($hash{$temp[$j]}<=0){
            $notfound=0;
            $j=@temp;
        }
        else{
            $hash{$temp[$j]}--;
        }
    }
}

if($notfound==0){
    print "NO";
}
else{
    print "YES";
}


#
my $n = <STDIN>;
chomp ( $n );

my $f = <STDIN>;
chomp ( $f );

my @arr = split m/ /, $f;
my @arr_two = (sort { $a <=> $b } @arr);

my @ans;
my $i = 0;
my $temp;
while ( @arr_two > 0 ) {

    if ( $i % 2 == 0 ) {
	push @ans, (shift @arr_two);}
    else {
	push @ans, (pop @arr_two);}
    $i += 1;
}

print (join " ", @ans);
print "\n";

# never impossible

<>;
@_ = sort {$a <=> $b} split ' ', <>;
push @ans, shift @_, pop @_ while @_;
print "@ans"

my $n = <STDIN>;
chomp ( $n );
my @arr;
my $f = <STDIN>;
chomp ( $f );
@arr = split m/ /, $f;

my @arr_two = sort @arr;
my @one, @two;

my $i;
while ( @arr_two > 0 ) {

    if ( ($i+1) % 2 == 0 ) {
	push @one, (shift @arr_two);
    }
    else {
	push @two, (shift @arr_two);
    }

    $i += 1;
}

my @two_two = reverse @two;

my @ans = ();
$i = 0;
while ( @two_two > 0 or @one ) {

    if ( ($i + 1) % 2 == 0 ) {
	push @ans, (shift @one);}
    else {
	push @ans, (shift @two_two);}
    $i += 1;
}

print (join " ", @ans);
print "\n";

#
my $n = <STDIN>;
chomp ( $n );

my $f = <STDIN>;
chomp ( $f );

my @arr = split m/ /, $f;
my @arr_two = sort @arr;

my @ans;
my $i = 0;
while ( @arr_two > 0 ) {

    if ( $i % 2 == 0 ) {
	push @ans, (shift @arr_two);}
    else {
	push @ans, (pop @arr_two);}
    $i += 1;
}

print (join " ", @ans);
print "\n";

# never impossible

#
my $n = <STDIN>;
chomp ( $n );
my @arr;
my $f = <STDIN>;
chomp ( $f );
@arr = split m/ /, $f;

my @arr_two = sort @arr;
my @one, @two;

my $i;
while ( @arr_two > 0 ) {

    if ( ($i+1) % 2 == 0 ) {
	push @one, (shift @arr_two);
    }
    else {
	push @two, (shift @arr_two);
    }

    $i += 1;
}

my @two_two = reverse @one;

my @ans = ();
$i = 0;
while ( @two_two > 0 or @two ) {

    if ( ($i + 1) % 2 == 0 ) {
	push @ans, (shift @two_two);}
    else {
	push @ans, (shift @two);}
    $i += 1;
}

print (join " ", @ans);
print "\n";

<>;
@_ = sort {$a <=> $b} split ' ', <>;
push @ans, pop @_, shift @_ while @_;
print "@ans"


use warnings;
use strict;
use constant NL => qq/\n/;
use integer;

<STDIN>;
my @arr = sort { $a <=> $b } ( split qq/ /, <STDIN> );
my $i = 0;
while ( @arr > 1 ) {
    pop @arr if ($i % 2 == 0);
    shift @arr if ($i % 2 == 1);
    $i += 1;
}
print $arr [ 0 ];
print NL;

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	@_ = sort { $a <=> $b } split ' ', <>;

	print $_[ ( @_ - 1 ) / 2 ];
	}

use warnings;
use strict;
use constant NL => qq/\n/;

<STDIN>;
my @arr = sort ( split qq/ /, <STDIN> );
print $arr [ int ( $#arr / 2) ];
print NL;

use warnings;
use strict;
use constant NL => qq/\n/;

<STDIN>;
my @arr = sort ( split qq/ /, <STDIN> );
my $i = 0;
while ( @arr > 1 ) {
    pop @arr if $i % 2 == 0;
    shift @arr if $i % 2 == 1;
    $i += 1;
}
print $arr [ 0 ];
print NL;


use strict;
use warnings;
use 5.010;

my $str = <>;
chop $str;
my $rev = reverse $str;
$str .= $rev;
say $str;

$a = <STDIN>;
chomp $a;
print($a);
print(scalar reverse $a);

use strict;
use warnings;
use 5.010;

my $str = <>;
my $rev = reverse $str;
$str .= $rev;
say $str;
#


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	@_ = split ' ', <>;

	my $max = -1;
	my $ans = -1;

	for my $i ( 0 .. @_ - 1 ){
		if( $_[ $i ] > $max + 1 ){
			$ans = $i + 1;
			last;
			}
		$max < $_[ $i ] and $max = $_[ $i ];
		}

	print $ans;
	}

use strict;
use warnings;

my $input = <STDIN>;
my $n = 0+$input;
$input = <STDIN>;
my @arr = map {$_+0} split / /, $input;
my $max_mex = 0;
my $answer = -1;
my $e;
for my $i (0.. scalar @arr - 1) {
    $e = $arr[$i];
    if ($e > $max_mex) {
        $answer = $i+1;
        last;
    } else {
        $max_mex = $e + 1 if $max_mex == $e;
    }
}
print($answer);


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	my( $n, $d ) = split;

	for( 1 .. <> ){
		my( $x, $y ) = sort { $a <=> $b } split ' ', <>;
		print "NO" and next if $x + $y < $d;
		print "NO" and next if $x + $y > $n + $n - $d;
		print "NO" and next if $y - $x > $d;
		print "NO" and next if $y - $x < -$d;

		print "YES";
		}

	}


#!/usr/bin/perl

# TLE 5;
# greedy, fill

use warnings;
use strict;

$\ = $/;

my @widths = map 2 ** $_, 0 .. 19;

<>;

while(<>){
	my( $n, $w ) = split;

	@_ = split ' ', <>;

	my %h;

	map $h{ $_ } ++, @_;

	my $height = 0;

	while( keys %h ){
		$height ++;
		my $ww = $w;

		for my $i ( sort { $b <=> $a } keys %h ){
	#%	for my $i ( reverse @widths ){
			next if not exists $h{ $i };
			if( $ww - $i >= 0 ){
				$ww -= $i;
				$h{ $i } --;
				$h{ $i } == 0 and delete $h{ $i };
				redo;
				}
			}
		}

	print $height;
	}


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

my ($n, $k) = split /\s/, <>;
my @w = split /\s/, <>;

my $min = 1001;
my $min_i;
for my $i (0..$n-1){
	my $err = 0;
	my $lmin = 0;
	for my $j (0..$n-1){
		if ( $w[$i] + ($j-$i)*$k <= 0 ) {
			$err = 1;
			last;
		}

		if($w[$i] + ($j-$i)*$k != $w[$j]){
			$lmin++;
		}
	}
	if(!$err && $lmin < $min){
		$min = $lmin;
		$min_i = $i;
	}
}

print "$min\n";
for my $j (0..$n-1){
	my $delta = $w[$min_i]+($j-$min_i)*$k - $w[$j];
	if($delta != 0){
		my $sign = ($delta > 0) ? '+' : '-';
		$delta = abs($delta);
		my $pos = $j+1;
		print "$sign $pos $delta\n";
	}
}

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

my ($n, $k) = split /\s/, <>;
my @w = split /\s/, <>;

my $min = 1001;
my $min_i;
for my $i (0..$n-1){
	my $lmin = 0;
	for my $j (0..$n-1){
		if($w[$j]-$w[$i] != ($j-$i)*$k){
			$lmin++;
		}
	}
	if($lmin < $min){
		$min = $lmin;
		$min_i = $i;
	}
}

print "$min\n";
for my $j (0..$n-1){
	my $delta = $w[$min_i]+($j-$min_i)*$k - $w[$j];
	if($delta != 0){
		my $sign = ($delta > 0) ? '+' : '-';
		$delta = abs($delta);
		my $pos = $j+1;
		print "$sign $pos $delta\n";
	}
}


use strict;
use warnings;

my $input = <STDIN>;
my ($n, $m) = map {$_+0} split / /, $input;
$input = <STDIN>;
my @a1 = map {$_+0} split / /, $input;
$input = <STDIN>;
my @a2 = map {$_+0} split / /, $input;
my %hash = ($m => 1);
my %hash2 = ();
my @a = ($m);
my $oo;
while(@a) {
    $m = $a[0];
    my $ok = ff($m, \%hash, $n, \@a1, \@a2, 1);
    if ($ok == 1) {
        print "YES";
        $oo=1;
        last;
    }
    $hash2{$m} = 1;
    @a = grep {not defined $hash2{$_}} keys %hash;
}
print "NO" unless $oo;

sub ff {
    my ($station, $h, $n, $a1, $a2, $m) = @_;
    my $ok = 0;
    if ($station < $n && $a2->[$station-1]) {
        for (($station + 1) .. $n) {
            if ($a2->[$_-1]) {
                return 1 if $_ == $m;
                $ok = -1 unless defined $h->{$_};
                $h->{$_} = 1;
            }
        }
    }
    if ($station > 1) {
        for (1..$station - 1) {
            if ($a1->[$_-1] && $a1->[$station-1]) {
                return 1 if $_ == $m;
                $ok = -1 unless defined $h->{$_};
                $h->{$_} = 1;
            }
        }
    }
}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	my( $n, $s ) = split;

	my @A = split ' ', <> =~ s/1/2/rg;
	my @B = split ' ', <>;

	if( $A[ 0 ] == 0 ){
		print "NO";
		next;
		}

	$_ += shift @B for @A;

	$_ = reverse join '', @A;

	s/3\K(1+)/ 2 x length $1 /ge;

	$_ = reverse;

	print 1 < ( substr $_, $s - 1, 1 ) ? "YES" : "NO";
	}


chomp ($str1 = <STDIN>);
chomp ($str2 = <STDIN>);
if (length($str1) == length($str2)) {
	printf "YES\n" if ($str1 =~ /1/ && $str2 =~ /1/);
	printf "YES\n" if ($str1 !~ /1/ && $str2 !~ /1/);
	printf "NO\n"  if ($str1 !~ /1/ && $str2 =~ /1/);
	printf "NO\n"  if ($str1 =~ /1/ && $str2 !~ /1/);
} else {
	printf "NO\n";
}


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	my( $n, $k ) = split;
	@_ = split ' ', <>;

	my %h;

	map $h{ $_ } ++, @_;

	@_ = sort { $a <=> $b } keys %h;

	my $prev = 0;
	my @ans;

	for my $i ( 1 .. $k ){
		my $A = shift @_ // $prev;
		push @ans, $A - $prev;
		$prev = $A;
		}

	print join "\n", @ans;
	}


#use v5.24;
use warnings;
my @usb,@ps2,@merged,$nrcump=0,$costtot=0;

my $line=<stdin>;
chomp($line);
my ($usbonly,$ps2only,$both)=split(/ /,$line);
$line=<stdin>;
chomp($line);
my ($n)=split(/ /,$line);
for (my $i=1;$i<=$n;$i++){
	$line=<stdin>;
	chomp($line);
	my ($cost,$tip)=split(/ /,$line);
	if ($tip eq "USB") {push @usb, $cost;}
	else {push @ps2, $cost;}
}
@usb=sort {$a<=>$b} @usb;
@ps2=sort {$a<=>$b} @ps2;
while ($usbonly>0 && scalar @usb>0){
	$usbonly--;
	$nrcump++;
	$costtot=$costtot+shift @usb;
}
while ($ps2only>0 && scalar @ps2>0){
	$ps2only--;
	$nrcump++;
	$costtot=$costtot+shift @ps2;
}
foreach my $var (@usb) {push @merged, $var;}
foreach my $var (@ps2) {push @merged, $var;}
@merged=sort {$a<=>$b} @merged;
while ($both>0 && scalar @merged>0){
	$both--;
	$nrcump++;
	$costtot=$costtot+shift @merged;
}
print $nrcump.' '.$costtot;
<stdin>;
exit(0);

    use warnings;
    use utf8;
    use feature "say";

    my $line = <>;
    chomp($line);
    my ($usb, $ps2, $mix) = split(' ', $line);
    my $pcNr = <>;
    chomp($pcNr);
    my (@usb, @ps2);
    while ($pcNr > 0) {
      $line = <>;
      chomp($line);
      my($price, $type) = split(' ', $line);
      if ($type eq "USB") {
        push ( @usb, $price );
      } else {
        push ( @ps2, $price);
      }
      --$pcNr;
    }
    @usb = sort ( { $a <=> $b } @usb );
    @ps2 = sort ( { $a <=> $b } @ps2 );

    my $price = 0;

    for ( ; $usb > 0 && scalar(@usb); --$usb) {
            $price += shift @usb;
            ++$pcNr;
    }

    for ( ; $ps2 > 0 && scalar(@ps2); --$ps2) {
            $price += shift @ps2;
            ++$pcNr;
    }

    my @c = sort( { $a <=> $b } @usb, @ps2 );

	for ( ;scalar( @c ) && $mix > 0; --$mix) {
		$price += shift @c;
		++$pcNr;
	}

    say $pcNr." ".$price;

use v5.20;
use warnings;

sub compare {
	return $a <=> $b;
}

my ($a,$b,$c) = split(' ',<>);
my $m = int <>;

my (@A,@B);

for (1..$m) {
	my ($val,$type) = split(' ',<>);
	if ($type eq "USB") {
		push @A,$val;
    }
	else {
		push @B,$val;
	}
}

@A = sort compare @A;
@B = sort compare @B;

my $cost = 0;
my $comp = 0;

while ($a and scalar @A) {
	$comp += 1;
	$cost += shift @A;
	$a -= 1;
}

while ($b and scalar @B) {
	$comp += 1;
	$cost += shift @B;
	$b -= 1;
}

my @C = sort compare (@A,@B);

while ($c and scalar @C) {
	$comp += 1;
	$cost += shift @C;
	$c -= 1;
}

say "$comp $cost";

use v5.20;

my $input = <>;
my ($usb_comp, $ps2_comp, $both_comp) = split(/ /, $input);

my $m = <>;
my @usb, my @ps2;
for (my $i = 0; $i < $m; $i = $i + 1) {
	$input = <>;
	chomp $input;
	my ($number, $type) = split(/ /, $input);

	if ($type eq "USB") {
		push(@usb, $number);
	} else {
		push(@ps2, $number);
	}
}

my $price = 0;
my $matched = 0;
# Match as many only-usb computers to usb mouses.
@usb = sort {$a <=> $b} @usb;
my $i = 0;
while ($i < scalar @usb and $usb_comp > 0) {
	$price = $price + $usb[$i];
	$i++;
	$matched++;
	$usb_comp--;
}

# Match as many only-ps2 computers to usb mouses.
@ps2 = sort {$a <=> $b} @ps2;
my $j = 0;
while ($j < scalar @ps2 and $ps2_comp > 0) {
	$price = $price + $ps2[$j];
	$j++;
	$matched++;
	$ps2_comp--;
}

# Match the computers having both ports to cheapest mouses.
while (($j < scalar @ps2 or $i < scalar @usb) and $both_comp > 0) {
	if ($i >= scalar @usb or ($j < scalar @ps2 and $ps2[$j] < $usb[$i])) {
		$price = $price + $ps2[$j];
		$j++;
	} else {
		$price = $price + $usb[$i];
		$i++;
	}
	$matched++;
	$both_comp--;
}

print "$matched $price"

use v5.20.1;
use warnings;

=pod
my $filename = "date.in";
open(my $fh, '<:encoding(UTF-8)', $filename)
	or die "Could not open the file";
=cut

my $line = <STDIN>;
chomp($line);

my($a, $b, $c) = split(' ', $line);

$line = <STDIN>;
chomp($line);

my $m = $line;

my @usb;
my @ps;
my $model;
my $price = 0;

for(my $i = 0; $i < $m; $i++){

	$line = <STDIN>;
	($price, $model) = split(' ', $line);
	if($model eq "USB"){

		push @usb, $price;

	}elsif($model eq "PS/2"){

		push @ps, $price;

	}

}

my @usbSorted = sort {$::a <=> $::b} @usb;
my @psSorted = sort {$::a <=> $::b} @ps;

@usb = @usbSorted;
@ps = @psSorted;

my $ui = 0;
my $pi = 0;
my $cost = 0;
my $mice = 0;

while($ui < @usb && $a > 0){

	$cost += $usb[$ui];
	#say $usb[$ui], "\n";
	$ui++;
	$mice++;
	$a--;

}

while($pi < @ps && $b > 0){

	$cost += $ps[$pi];
	#say $ps[$pi], "\n";
	$pi++;
	$b--;
	$mice++;

}

while($ui < @usb && $pi < @ps && $c > 0){

	if($usb[$ui] < $ps[$pi]){

		$cost += $usb[$ui];
		#say $usb[$ui], "\n";
		$c--;
		$ui++;
		$mice++;

	}else{

		$cost += $ps[$pi];
		#say $ps[$pi], "\n";
		$pi++;
		$c--;
		$mice++;

	}

}

while($ui < @usb && $c > 0){

	$cost += $usb[$ui];
	#say $usb[$ui], "\n";
	$ui++;
	$c--;
	$mice++;

}

while($pi < @ps && $c > 0){

	$cost += $ps[$pi];
	#say $ps[$pi], "\n";
	$pi++;
	$c--;
	$mice++;

}

say $mice, " ", $cost, "\n";

sub input { $_ = <>; chomp; wantarray? split: $_ }

sub less_first { sort { $a <=> $b } @_ }

sub buy {
	my ($c, $aref) = @_;
	while ($c--) {
		last unless @$aref;
		$R++, $V += shift @$aref;
	}
}

$R = $V = 0; @c = input; $m = input;
for (1..$m) {
	($v, $t) = input;
	if ($t eq "USB") {
		push @v0, $v;
	} else {
		push @v1, $v;
	}
}

@v0 = less_first @v0;
@v1 = less_first @v1;

buy $c[0], \@v0;
buy $c[1], \@v1;
@v2 = less_first @v0, @v1;
buy $c[2], \@v2;

print $R, " ", $V;

use warnings;
use v5.20;

my @array;
my $total = 0;
my $count = 0;

my $input = <STDIN>;
chomp $input;
my($A, $B, $C) = split(' ', $input);

$input = <STDIN>;
chomp $input;
my $m = int($input);

for(my $i = 0; $i < $m; $i += 1){
    $input = <STDIN>;
    chomp $input;
    my($value, $name) = split(' ', $input);
    $value = int($value);
    push @array, {id => $value, type => $name};
}

for my $item ( sort { $a->{id} <=> $b->{id} } @array ) {
    if($item->{type} eq "USB"){
        if($A > 0){
            $total = $total + $item->{id};
            $count += 1;
            $A -= 1;
        }
        elsif($C > 0){
            $total = $total + $item->{id};
            $count += 1;
            $C -= 1;
        }
    }
    else{
        if($B > 0){
            $total = $total + $item->{id};
            $count += 1;
            $B -= 1;
        }
        elsif($C > 0){
            $total = $total + $item->{id};
            $count += 1;
            $C -= 1;
        }
    }
}
print("$count $total\n");

use strict;
use warnings;

my ($usb_nr, $ps_nr, $both_nr) = split(" ", <>);
my $nr_prices = <>;
my @prices_usb = ();
my @prices_ps = ();
my $current_price = 0;
my $current_type = "";
my $equipped_usb = 0;
my $equipped_ps = 0;
my $equipped_both = 0;
my $cost = 0;
my $bought = 0;
foreach(1 .. $nr_prices)
{
	($current_price, $current_type) = split(" ", <>);
	if($current_type eq "USB")
	{
		push(@prices_usb, $current_price);
	}
	else
	{
		push(@prices_ps, $current_price);
	}
}
@prices_usb = sort {$a <=> $b} @prices_usb;
@prices_ps = sort {$a <=> $b} @prices_ps;
#print "Prices USB: @prices_usb \n";
#print "Prices PS/2: @prices_ps \n";
$bought = 0;
foreach(0 .. scalar $usb_nr-1)
{
	if($_ < scalar @prices_usb)
	{
		#print "Equipped USB PC with $prices_usb[$_]\n";
		$equipped_usb += 1;
		$cost += $prices_usb[$_];
		$bought += 1;
	}
}
splice @prices_usb, 0, $bought;
$bought = 0;
foreach(0 .. $ps_nr-1)
{
	if($_ < scalar @prices_ps)
	{
		#print "Equipped PS/2 PC with $prices_ps[$_]\n";
		$equipped_ps += 1;
		$cost += $prices_ps[$_];
		$bought += 1;
	}
}
splice @prices_ps, 0, $bought;
my $pos_usb = 0;
my $pos_ps = 0;
while($pos_usb < scalar @prices_usb and $pos_ps < scalar @prices_ps and $equipped_both < $both_nr)
{
	if($prices_usb[$pos_usb] <= $prices_ps[$pos_ps])
	{
		#print "Equipped PC with both with USB: $prices_usb[$pos_usb]\n";
		$equipped_both += 1;
		$cost += $prices_usb[$pos_usb];
		$pos_usb += 1;
	}
	else
	{
		#print "Equipped PC with both with PS/2: $prices_ps[$pos_ps]\n";
		$equipped_both += 1;
		$cost += $prices_ps[$pos_ps];
		$pos_ps += 1;
	}
}
while($pos_usb < scalar @prices_usb and $equipped_both < $both_nr)
{
	#print "Equipped PC with both with USB: $prices_usb[$pos_usb]\n";
	$equipped_both += 1;
	$cost += $prices_usb[$pos_usb];
	$pos_usb += 1;
}
while($pos_ps < scalar @prices_ps and $equipped_both < $both_nr)
{
	#print "Equipped PC with both with PS/2: $prices_ps[$pos_ps]\n";
	$equipped_both += 1;
	$cost += $prices_ps[$pos_ps];
	$pos_ps += 1;
}
print $equipped_usb + $equipped_ps + $equipped_both, " ", $cost;

use warnings;
use utf8;
use feature "say";

my $line = <>;
chomp($line);
my ($usb, $ps2, $mix) = split(' ', $line);
my $pcNr = <>;
chomp($pcNr);
my (@usb, @ps2);
while ($pcNr > 0) {
  $line = <>;
  chomp($line);
  my($price, $type) = split(' ', $line);
  if ($type eq "USB") {
    push ( @usb, $price );
  } else {
    push ( @ps2, $price);
  }
  --$pcNr;
}
@usb = sort ( { $a <=> $b } @usb );
@ps2 = sort ( { $a <=> $b } @ps2 );

my $price = 0;
my ( $iU, $iP );

for ( $iU = 0; $usb > 0 && $iU < scalar(@usb); ++$iU, --$usb) {
        $price += $usb[$iU];
        ++$pcNr;
}

for ( $iP = 0; $ps2 > 0 && $iP < scalar(@ps2); ++$iP, --$ps2) {
        $price += $ps2[$iP];
        ++$pcNr;
}

for (; ($iU < scalar(@usb) || $iP < scalar( @ps2 )) && $mix > 0; ) {
        if ( $iU < scalar(@usb) && $iP < scalar(@ps2) && $usb[$iU] <= $ps2[$iP] || $iP >= scalar(@ps2)) {
                $price += $usb[$iU];
                ++$pcNr;
                --$mix;
                ++$iU;
        } elsif ($iP < scalar(@ps2) && $iU < scalar(@usb) && $usb[$iU] > $ps2[$iP] || $iU >= scalar(@usb)) {
                $price += $ps2[$iP];
                ++$pcNr;
                --$mix;
                ++$iP;
        }
}

say $pcNr." ".$price;

use v5.20;
use warnings;

sub compare {
	return $a <=> $b;
}

my ($a,$b,$c) = split(' ',<>);
my $m = int <>;

my (@A,@B);

for (1..$m) {
	my ($val,$type) = split(' ',<>);
	if ($type eq "USB") {
		push @A,$val;
    }
	else {
		push @B,$val;
	}
}

@A = sort compare @A;
@B = sort compare @B;

my $cost = 0;
my $comp = 0;

while ($a and @A) {
	$comp += 1;
	$cost += shift @A;
	$a -= 1;
}

while ($b and @B) {
	$comp += 1;
	$cost += shift @B;
	$b -= 1;
}

my @C = sort compare (@A,@B);

while ($c and @C) {
	$comp += 1;
	$cost += shift @B;
	$c -= 1;
}

say "$comp $cost";

use v5.20;

my $input = <>;
my ($usb_comp, $ps2_comp, $both_comp) = split(/ /, $input);

my $m = <>;
my @usb, my @ps2;
for (my $i = 0; $i < $m; $i = $i + 1) {
	$input = <>;
	chomp $input;
	my ($number, $type) = split(/ /, $input);

	if ($type eq "USB") {
		push(@usb, $number);
	} else {
		say $type;
		push(@ps2, $number);
	}
}

my $price = 0;
my $matched = 0;
# Match as many only-usb computers to usb mouses.
@usb = sort {$a <=> $b} @usb;
my $i = 0;
while ($i < scalar @usb and $usb_comp > 0) {
	say 1;
	$price = $price + $usb[$i];
	$i++;
	$matched++;
	$usb_comp--;
}

# Match as many only-ps2 computers to usb mouses.
@ps2 = sort {$a <=> $b} @ps2;
my $j = 0;
while ($j < scalar @ps2 and $ps2_comp > 0) {
	say 2;
	$price = $price + $ps2[$j];
	$j++;
	$matched++;
	$ps2_comp--;
}

# Match the computers having both ports to cheapest mouses.
while (($j < scalar @ps2 or $i < scalar @usb) and $both_comp > 0) {
	say 3;
	if ($i >= scalar @usb or ($j < scalar @ps2 and $ps2[$j] < $usb[$i])) {
		$price = $price + $ps2[$j];
		$j++;
	} else {
		$price = $price + $usb[$i];
		$i++;
	}
	$matched++;
	$both_comp--;
}

print "$matched $price"

#use v5.24;
use warnings;
my @usb,@ps2,@merged,$nrcump=0,$costtot=0;

my $line=<stdin>;
chomp($line);
my ($usbonly,$ps2only,$both)=split(/ /,$line);
$line=<stdin>;
chomp($line);
my ($n)=split(/ /,$line);
for (my $i=1;$i<=$n;$i++){
	$line=<stdin>;
	chomp($line);
	my ($cost,$tip)=split(/ /,$line);
	if ($tip eq "USB") {push @usb, $cost;}
	else {push @ps2, $cost;}
}
@usb=sort {$a<=>$b} @usb;
@ps2=sort {$a<=>$b} @ps2;
while ($usbonly>0 && scalar @usb>0){
	$usbonly--;
	$nrcump++;
	$costtot=$costtot+shift @usb;
}
while ($ps2only>0 && scalar @ps2>0){
	$ps2only--;
	$nrcump++;
	$costtot=$costtot+shift @ps2;
}
foreach my $var (@usb) {push @merged, $var;}
foreach my $var (@ps2) {push @merged, $var;}
while ($both>0 && scalar @merged>0){
	$both--;
	$nrcump++;
	$costtot=$costtot+shift @merged;
}
print $nrcump.' '.$costtot;
<stdin>;
exit(0);

use v5.20.1;
use warnings;

my $line = <STDIN>;
chomp($line);

my($a, $b, $c) = split(' ', $line);

$line = <STDIN>;
chomp($line);

my $m = $line;

my @usb = ();
my @ps = ();
my $model = "";
my $price = 0;

for(my $i = 0; $i < $m; $i++){

	$line = <STDIN>;
	($price, $model) = split(' ', $line);
	if($model eq "USB"){

		push @usb, $price;

	}elsif($model eq "PS/2"){

		push @ps, $price;

	}

}

@usb = sort @usb;
@ps = sort @ps;

my $ui = 0;
my $pi = 0;
my $cost = 0;
my $mice = 0;

while($ui < @usb && $a > 0){

	$cost += $usb[$ui];
	$ui++;
	$mice++;
	$a--;

}

while($pi < @ps && $b > 0){

	$cost += $ps[$pi];
	$pi++;
	$b--;
	$mice++;

}

while($ui < @usb && $pi < @ps && $c > 0){

	if($usb[$ui] < $ps[$pi]){

		$cost += $usb[$ui];
		$c--;
		$ui++;
		$mice++;

	}else{

		$cost += $ps[$pi];
		$pi++;
		$c--;
		$mice++;

	}

}

while($ui < @usb && $c > 0){

	$cost += $usb[$ui];
	$ui++;
	$c--;
	$mice++;

}

while($pi < @ps && $c > 0){

	$cost += $ps[$pi];
	$pi++;
	$c--;
	$mice++;

}

print $mice, " ", $cost, "\n";

sub input { $_ = <>; chomp; wantarray? split: $_ }

sub less_first { sort { $a <=> $b } @_ }

sub buy {
	my ($c, $aref) = @_;
	while ($c--) {
		last unless @$aref;
		$R++, $V += shift @$aref;
	}
}

@c = input; $m = input;
for (1..$m) {
	($v, $t) = input;
	if ($t eq "USB") {
		push @v0, $v;
	} else {
		push @v1, $v;
	}
}

@v0 = less_first @v0;
@v1 = less_first @v1;

buy $c[0], \@v0;
buy $c[1], \@v1;
@v2 = less_first @v0, @v1;
buy $c[2], \@v2;

print $R, " ", $V;

use utf8;
use feature "say";

my $line = <>;
chomp($line);
my ($usb, $ps2, $mix) = split(' ', $line);
my $pcNr = <>;
chomp($pcNr);
my (@usb, @ps2);
while ($pcNr > 0) {
  $line = <>;
  chomp($line);
  my($price, $type) = split(' ', $line);
  if ($type eq "USB") {
    push ( @usb, $price );
  } else {
    push ( @ps2, $price);
  }
  --$pcNr;
}

@usb = sort ( { $a <=> $b } @usb );
@ps2 = sort ( { $a <=> $b } @ps2 );
my $price = 0;
my ( $iU, $iP );

for ( $iU = 0; $iU < scalar ( @usb ) && $usb > 0; ++$iU, --$usb ) {
   $price += $usb[$iU];
   ++$pcNr;
}
for ( $iP = 0; $iP < scalar ( @ps2 ) && $ps2 > 0; ++$iP, --$ps2 ) {
   $price += $ps2[$iP];
   ++$pcNr;
}

for ( ; ( $iU < scalar( @usb ) || $iP < scalar ( @ps2 ) ) && $mix > 0; --$mix ) {
   if ( ({ $usb[$iU] <=> $ps2[$iP] } < 1) || $iP >= scalar ( @ps2 ) ) {
      $price += $usb[$iU];
      ++$pcNr;
      ++$iU;
   } else {
      $price += $ps2[$iP];
      ++$pcNr;
      ++$iP;
   }
}

say $pcNr." ".$price;

use warnings;
use utf8;
use feature "say";

my $line = <>;
chomp($line);
my ($usb, $ps2, $mix) = split(' ', $line);
my $pcNr = <>;
chomp($pcNr);
my (@usb, @ps2);
while ($pcNr > 0) {
  $line = <>;
  chomp($line);
  my($price, $type) = split(' ', $line);
  if ($type eq "USB") {
    push ( @usb, $price );
  } else {
    push ( @ps2, $price);
  }
  --$pcNr;
}
@usb = sort ( { $a <=> $b } @usb );
@ps2 = sort ( { $a <=> $b } @ps2 );

my $price = 0;
my ( $iU, $iP );
for ( $iU = 0; $iU < scalar ( @usb ) && $usb > 0; ++$iU, --$usb ) {
   $price += $usb[$iU];
   say $usb[$iU];
   ++$pcNr;
}
for ( $iP = 0; $iP < scalar ( @ps2 ) && $ps2 > 0; ++$iP, --$ps2 ) {
   $price += $ps2[$iP];
   say $ps2[$iP];
   ++$pcNr;
}
for ( ; ( $iU < scalar( @usb ) || $iP < scalar ( @ps2 ) ) && $mix > 0; --$mix ) {
   if ( $usb[$iU] <= $ps2[$iP]) {
      $price += $usb[$iU];
      ++$pcNr;
      ++$iU;
   } else {
      $price += $ps2[$iP];
      ++$pcNr;
      ++$iP;
   }
}

say $pcNr." ".$price;


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	my( $b, $q, $l, $m ) = split;
	my %h = map { $_ => 1 } split ' ', <>;

	my @ans;

	my $maxl = abs $l;
	my $minl = - $maxl;

	my $c = 0;

	while( $minl <= $b and $b <= $maxl ){
		$h{ $b } or push @ans, $b;
		$b *= $q;
		$c ++ > 2e4 and last;
		}

	print @ans >= 1e4 ? "inf" : 0 + @ans;
	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	my( $b, $q, $l, $m ) = split;
	my %h = map { $_ => 1 } split ' ', <>;

	my @ans;

	my $maxl = abs $l;
	my $minl = - $maxl;

	my $inf;

	if( $b == 0 ){
		$inf = $h{ 0 } ? 0 : "inf";
		}
	elsif( $q == 0 ){
		$inf = $h{ 0 } ? 0 : "inf";
		$inf or $inf += ($minl <= $b and $b <= $maxl and not $h{ $b });
		}

	if( not defined $inf and $minl <= $b and $b <= $maxl and $q =~ /^-?1$/ ){
		if( $q == 1 ){ $inf = $h{ $b } ? 0 : "inf" }
		elsif( $q == -1 ){ $inf = ($h{ $b } * $h{ 0 - $b }) ? 0 : "inf" }
		}
	elsif( not defined $inf and not( $minl <= $b and $b <= $maxl) and $q =~ /^-?1$/ ){
		$inf = 0;
		}

	while( not defined $inf and $minl <= $b and $b <= $maxl ){
		$h{ $b } or push @ans, $b;
		$b *= $q;
		}

	print defined $inf ? $inf : 0 + @ans;
	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	my( $b, $q, $l, $m ) = split;
	my %h = map { $_ => 1 } split ' ', <>;

	my @ans;

	my $maxl = abs $l;
	my $minl = - $maxl;

	my $c = 0;

	while( $minl <= $b and $b <= $maxl ){
		$h{ $b } or push @ans, $b;
		$b *= $q;
		$c ++ > 1e4 and last;
		}

	print @ans >= 1e4 ? "inf" : 0 + @ans;
	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	my( $b, $q, $l, $m ) = split;
	my %h = map { $_ => 1 } split ' ', <>;

	my @ans;

	my $maxl = abs $l;
	my $minl = - $maxl;

	my $inf;
	$b == 0 || $q == 0 and $inf = "inf";

	if( not defined $inf and $minl <= $b and $b <= $maxl and $q == 1 ){
		$inf = $h{ $b } ? 0 : "inf";
		}
	elsif( not defined $inf and not( $minl <= $b and $b <= $maxl) and $q == 1 ){
		$inf = 0;
		}

	while( not defined $inf and $minl <= $b and $b <= $maxl ){
		$h{ $b } or push @ans, $b;
		$b *= $q;
		}

	print defined $inf ? $inf : 0 + @ans;
	}


#!/usr/bin/perl

$|=1;
while(<STDIN>){
  print "NO\n";
}


chomp (my ($n, $m) = split /\s+/, <STDIN>);

chomp (my %arr = map {$_ => 1} split /\s+/, <STDIN>);

my @ans;
for (my $i = 1; $i <= 10 ** 9; $i++) {
  next if exists $arr{$i};
  if ($m >= $i) {
    $m -= $i;
    push @ans, $i;
  } else {
    last;
  }
}

printf "%s\n", scalar @ans;
printf "@ans\n";

my @n = split(/ /, <STDIN>);
my $m = $n[1];
my @A = sort {$a <=> $b} split(/ /, <STDIN>);
my $j = 0, $t = 1;
my @ans;
while(1) {
    while($A[$j] == $t) {
        $j++;
        $t++;
    }
    if($m >= $t) {
        $m -= $t;
        push @ans, $t;
        $t++;
    } else {
        print scalar @ans."\n";
        foreach $x (@ans) { print "$x "; }
        exit 0;
    }
}


#!/perl -w

use strict ;

chomp( my $n = <STDIN> );

my @beauty = split /\s+/ , <STDIN>;

my $maxbeauti = 0 ;
my $minbeauti = 10E9 + 1;
my $maxcount  = 0 ;
my $mincount  = 0 ;

foreach( @beauty ){
    if ( $_ >= $maxbeauti ){
        $maxbeauti = $_ ;
    }
    if ( $_ <= $minbeauti ){
        $minbeauti = $_ ;
    }
}
if( $minbeauti != $maxbeauti ){
    foreach( @beauty ){
        if ( $_ == $maxbeauti ){
            $maxcount ++ ;
        }
        if ( $_ == $minbeauti ){
            $mincount ++ ;
        }
    }
    print $maxbeauti - $minbeauti , " " , $mincount * $maxcount ;
}else{
    print "0" , " " , $n*($n - 1)/2 ;
}

use 5.016;
use strict;
use warnings;

<>;
my @x = sort { $a <=> $b } split ' ', <>;
my $min = $x[0];
my $max = $x[-1];
my $max_diff = $max - $min;

my $count_min = 0;
for my $x (@x) {
  if ($x > $min) {
    last;
  } else {
    $count_min++;
  }
}

my $count_max = 0;
for my $x (reverse @x) {
  if ($x < $max) {
    last;
  } else {
    $count_max++;
  }
}

my $ways = $max_diff == 0 ? $count_min * ($count_min - 1) / 2
                          : $count_min * $count_max;

say "$max_diff $ways";

#!/perl -w

use strict ;

chomp( my $n = <STDIN> );

my @beauty = split /\s+/ , <STDIN>;

my $maxbeauti = 0 ;
my $minbeauti = 10E9 + 1;
my $maxcount  = 0 ;
my $mincount  = 0 ;

foreach( @beauty ){
    if ( $_ >= $maxbeauti ){
        $maxbeauti = $_ ;
    }
    if ( $_ <= $minbeauti ){
        $minbeauti = $_ ;
    }
}

foreach( @beauty ){
    if ( $_ == $maxbeauti ){
        $maxcount ++ ;
    }
    if ( $_ == $minbeauti ){
        $mincount ++ ;
    }
}

print $maxbeauti-$minbeauti , " " , $mincount * $maxcount ;

# Perl
chomp($n = <>);
@a = sort split / /, <>;
foreach (@a) {
	$a++ if $_==$a[0] or last;
}
foreach (reverse @a) {
	$b++ if $_==$a[-1] or last;
}
print $a[-1]-$a[0], ' ', $a*$b, "\n";

use 5.016;
use strict;
use warnings;

<>;
my @x = sort { $a <=> $b } split ' ', <>;
my $min = $x[0];
my $max = $x[-1];
my $max_diff = $max - $min;

my $count_min = 0;
for my $x (@x) {
  if ($x > $min) {
    last;
  } else {
    $count_min++;
  }
}

my $count_max = 0;
for my $x (reverse @x) {
  if ($x < $max) {
    last;
  } else {
    $count_max++;
  }
}

my $ways = $count_min * $count_max;

say "$max_diff $ways";

<>;
@_=split/ /,<>;
$_>$max and $max=$_ for @_;
$min=$max;
$_<$max and $max=$_ for @_;
$min==$_ and $MIN++ for @_;
$max==$_ and $MAX++ for @_;
$max-$min and $f=$MIN*$MAX;
print $max-$min, " ", 0+$f;