#!perl
use v5.20;
use strict;
use warnings;
use Class::Struct;
struct (
  Interval => [
    length => '$',
    cntShip => '$',
    endPos => '$'
  ]
);
while (<>) {
  my ($n, $aa, $bb, $k) = split ' ';
  my ($s, @str);
  chomp($s = <>);
  @str = split '', $s;
  my ($cnt, $i, @room);
  $cnt = 0;
  for $i ( 0..$#str ) {
    if (!$str[$i]) {
      ++$cnt;
    }
    else {
      if ($cnt) {
        push @room, Interval->new(
          length => $cnt,
          cntShip => int($cnt/$bb),
          endPos => $i-1
          );
      }
      $cnt = 0;
    }
  }
  if ($cnt) {
      push @room, Interval->new(
        length => $cnt,
        cntShip => int($cnt/$bb),
        endPos => $#str
        );
  }
  @room = sort { $a->length <=> $b->length } @room;

  #for $i ( 0..$#room ) {
  #  say '(', $room[$i]->length,
  #  ' ', $room[$i]->cntShip,
  #  ' ', $room[$i]->endPos, ')';
  #}

  my ($result, @postSum, $sum);
  $sum = 0;
  for ($i = $#room; $i >= 0; --$i) {
    $sum += $room[$i]->cntShip;
    $postSum[$i] = $sum;
  }
  my (@positions);
  for $i ( 0..$#room ) {
    if ($i != $#room && $postSum[$i+1] >= $aa) {
    #// remaining is enough.
      my $start = $room[$i]->endPos - ($room[$i]->length - 1);
      my $pos = $room[$i]->endPos;
      while ($pos >= $start + $bb - 1) {
      #say "start= ", $start, " pos= ", $pos;
        $pos -= ($bb - 1);
        push @positions, $pos+1;
        $pos--;
      }
    }
    elsif ($i != $#room && $postSum[$i+1] < $aa) {
    #// remaining is not enough, current interval is the last.
      my $remaining = $aa - $postSum[$i+1];
      my $cnt = $room[$i]->cntShip - $remaining + 1;
      my $pos = $room[$i]->endPos;
      while ($cnt--) {
        $pos -= ($bb - 1);
        push @positions, $pos+1;
        $pos--;
      }
      last;
    }
    else {
      my $cnt = $room[$i]->cntShip - $aa + 1;
      my $pos = $room[$i]->endPos;
      while ($cnt--) {
        $pos -= ($bb - 1);
        push @positions, $pos+1;
        $pos--;
      }
    }
  }
  say scalar @positions;
  say join ' ', @positions;
}

#!perl
use strict;
use warnings;
use v5.18;
while (<>) {
	my ($n, $a, $b, $k) = split;
	my $s = <>; chomp $s;
	$s = '1' . $s . '1';
	my @str = split '', $s;
	my @positions;
	my $q;
	for ( my $i = 0; $i < $#str; $i = $q ) {
		my $j = $i;
		while ($j < $#str && 1 == $str[$j]) { $j++; }
		$q = $j;
		while ($q < $#str && 0 == $str[$q]) { $q++; }
		for (my $pos = $j - 1 + $b; $pos < $q; $pos += $b) {
			if ($a == 1) { push @positions, $pos }
			else { $a--; }
		}
	}
	say scalar @positions;
	say join ' ', @positions;
}

#!perl
use v5.20;
use strict;
use warnings;
use Class::Struct;
struct (
  Interval => [
    length => '$',
    cntShip => '$',
    endPos => '$'
  ]
);
while (<>) {
  my ($n, $aa, $bb, $k) = split ' ';
  my ($s, @str);
  chomp($s = <>);
  @str = split '', $s;
  my ($cnt, $i, @room);
  $cnt = 0;
  for $i ( 0..$#str ) {
    if (!$str[$i]) {
      ++$cnt;
    }
    else {
      if ($cnt) {
        push @room, Interval->new(
          length => $cnt,
          cntShip => int($cnt/$bb),
          endPos => $i-1
          );
      }
      $cnt = 0;
    }
  }
  if ($cnt) {
      push @room, Interval->new(
        length => $cnt,
        cntShip => int($cnt/$bb),
        endPos => $#str
        );
  }
  @room = sort { $a->length <=> $b->length } @room;

  #for $i ( 0..$#room ) {
  #  say '(', $room[$i]->length,
  #  ' ', $room[$i]->cntShip,
  #  ' ', $room[$i]->endPos, ')';
  #}

  my ($result, @postSum, $sum);
  $sum = 0;
  for ($i = $#room; $i >= 0; --$i) {
    $sum += $room[$i]->cntShip;
    $postSum[$i] = $sum;
  }
  my (@positions);
  for $i ( 0..$#room ) {
    if ($i != $#room && $postSum[$i+1] >= $aa) {
    #// remaining is enough.
      my $start = $room[$i]->endPos - ($room[$i]->length - 1);
      my $pos = $room[$i]->endPos;
      while ($pos >= $start + $bb - 1) {
      #say "start= ", $start, " pos= ", $pos;
        $pos -= ($bb - 1);
        push @positions, $pos+1;
        $pos--;
      }
    }
    elsif ($i != $#room && $postSum[$i+1] < $aa) {
    #// remaining is not enough, current interval is the last.
      my $remaining = $aa - $postSum[$i+1];
      my $cnt = $room[$i]->cntShip - $remaining + 1;
      my $pos = $room[$i]->endPos;
      while ($cnt--) {
        $pos -= ($bb - 1);
        push @positions, $pos+1;
      }
      last;
    }
    else {
      my $cnt = $room[$i]->cntShip - $aa + 1;
      my $pos = $room[$i]->endPos;
      while ($cnt--) {
        $pos -= ($bb - 1);
        push @positions, $pos+1;
      }
    }
  }
  say scalar @positions;
  say join ' ', @positions;
}


<>;

print sort { $a . $b cmp $b . $a } map s/\n//r, <>

<>;

print sort {
		($A, $B) = ($a, $b);
		$max = length $A > length $B ? $A : $B;
		map { $_ x= 50; substr $_, 0, $max } $A, $B;
		$A cmp $B
	} map s/\n//r, <>;

<>;

print join '+', sort {
		($A, $B) = ($a, $b);
		$max = length $A > length $B ? $A : $B;
		map { $_ x= 50; substr $_, 0, $max } $A, $B;
		$A cmp $B
	} map s/\n//r, <>;

<>;

%h = map { chomp; $_ => 1 } @_ = <>;

for (sort map $_ .= (chop) x (52 - length), @_){
	for $i (reverse 1 .. 50){
		$s = substr $_, 0, $i;
		$h{ $s } and do { $_ = $s ; delete $h{ $s }; last }
		}
	print
	}

<>;

print map y/~//dr, sort map s/\n//r . '~', <>;


$_ = <>, chomp, m!http://[\w.]+!, undef $h{ $& }{ $' } for 1 .. <>;

push @{ $g{ join ' ', sort keys %{ $h{ $_ } }  } }, $_ for keys %h;
push @ans, grep / /, join ' ', @{ $g{ $_ } } for keys %g;

print join $/, ~~ @ans, @ans

#!/usr/bin/env perl

use strict;
use warnings;
no warnings 'uninitialized';

my $N = <> + 0;

my $npaths = 0;
my %map_paths = ();

my %host_paths = ();

for (my $i = 0; $i < $N; ++$i) {
   my $url = <>;
   chomp $url;
   $url =~ m{http://([.a-z]+)(.*)};
   my ($hostname, $path) = ($1, $2);
   #print $hostname, ' ', $path, "\n";

   my $id;
   if ( not exists $map_paths{ $path } ) {
      $id = $map_paths{ $path } = $npaths++
   }
   else {
      $id = $map_paths{ $path }
   }
   $host_paths{ $hostname }{$id} = 1
}

my %paths_hosts = ();
while (my ($key, $path_set) = each %host_paths) {
   my $paths = join(':', sort keys %$path_set);
   #print $key, ' ', $paths, "\n";
   push( @{$paths_hosts{ $paths }}, $key )
}

my @res;
while (my ($key, $list) = each %paths_hosts) {
   next if @$list <= 1;
   push(@res, join(' ', map('http://' . $_, @$list)))
}

print scalar @res, "\n";
print join("\n", @res), "\n"

use warnings;
use strict;

my %host2path;
my %path2host;

my $n = <>;
chomp $n;
$n += 0;

for (my $i = 0; $i < $n; $i++){
    my $url = <>;
    $url =~ m!^(http://[\w\.]+)(/[\w\./]*)?\n$! or die "bad url";
    my $host = $1;
    my $path = $2 || '';
    #print STDERR "host = \'$host\' path = \'$path\'\n";
    if ($host2path{$host}) {
        ${$host2path{$host}}{$path} = undef;
        }
    else {
        $host2path{$host} = { $path => undef };
        }
    }

while (my ($host, $pathes) = each %host2path){
    my $ppp = join '#', sort keys %$pathes;
    #print STDERR "ppp = \'$ppp\' host = \'$host\'\n";
    if ($path2host{$ppp}){
        push @{$path2host{$ppp}}, $host;
        }
    else {
        $path2host{$ppp} = [ $host ]
        }
    }

my @res = ();
while (my ($ppp, $hosts) = each %path2host){
    push @res, $hosts if (scalar(@$hosts) > 1);
    }

print scalar(@res), "\n";
print join(' ', @$_), "\n" foreach (@res);

exit(0);

#!/usr/bin/env perl

use strict;
use warnings;
no warnings 'uninitialized';

my $N = <> + 0;

my $npaths = 0;
my %map_paths = ();

my %host_paths = ();

for (my $i = 0; $i < $N; ++$i) {
   my $url = <>;
   chomp $url;
   $url =~ m{http://([.a-z]+)(.*)};
   my ($hostname, $path) = ($1, $2);
   #print $hostname, ' ', $path, "\n";

   my $id;
   if ( not exists $map_paths{ $path } ) {
      $id = $map_paths{ $path } = $npaths++
   }
   else {
      $id = $map_paths{ $path }
   }
   push( @{$host_paths{ $hostname }}, $id )
}

my %paths_hosts = ();
while (my ($key, $list) = each %host_paths) {
   my $paths = join(':', sort @$list);
#  print $key, ' ', $paths, "\n"
   push( @{$paths_hosts{ $paths }}, $key )
}

my @res;
while (my ($key, $list) = each %paths_hosts) {
   next if @$list <= 1;
   push(@res, join(' ', map('http://' . $_, @$list)))
}

print scalar @res, "\n";
print join("\n", @res), "\n"


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

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

	print 0 + eval join '|', @_;
	}


#!/usr/bin/perl
($n,$m) = split /\D/, <>;
flag:for (1..$n) {
    chomp($str = <>);
    @arr = split(//, $str);
    @arr = sort { $a <=> $b } @arr;
    if ($arr[0] == $arr[$#arr]) {
        $color = $arr[0];
        if (!defined $colors{$color}) {
            $colors{$color} = $color;
            if ( (keys %colors) > 10) {
                print "NO";
                goto end;
            }
        }
        if (defined $pcolor) {
            if ($pcolor eq $color) {
                print "NO";
                goto end;
            }
        }
        $pcolor = $color;
    } else {
        print "NO";
        goto end;
    }
}
print "YES";
end:
__END__

#!/usr/bin/perl

$PI=3.14159265358979323846;
$MOD=1000000007;

#    <>=~/ /;
#    <>=~/ (\d+) /;
#    <>=~/ (\d+) (\d+) /;

while(<>=~/ /){
 	$t=-1; $f=0;
	for $i(1..$`+0){
		$_=<>;
		chomp;
		/./;
		$t==$& and $f++;
		$t=$&;
		s/(.)\1*//;
		$_ and $f++;
		}
	print ($f?"NO\n":"YES\n");
	}

#***********SUBS************

sub num {$a <=> $b}
sub num_rev {$b <=> $a}
sub rev {$b cmp $a}

sub p{print}
sub n{print "\n"}
sub pn{p,n}
sub pn_mas {pn for @_}

# Transpose "charbox"
sub transpose{
    $i=@m=@_;
    chomp @m;
    while ($i--){
        $t[$i]=~s/$/chop/e for @m
    }
return @t
}

# Transpose "value sets"
sub transpose_space{
    my @t;
    for (@_){
        my $j=0;
        s/\d+/($t[$j++].="$& ")?$&:$&/eg;
        }
    chop @t;
return @t
}

sub div{
    my ($n, $k)=@_;
return ($n - $n % $k) / $k
}

sub uniq{
    my @m=sort @_;
    push @u, my $i=shift @m;
    for (@m){
        $i eq $_ or push @u, $_;
        $i=$_;
        }
return @u
}

sub max{
    my $max=shift;
    $max<$_ and $max=$_ for @_;
return $max
}

sub min{
    my $min=shift;
    $min>$_ and $min=$_ for @_;
return $min
}

# count (kas_ieskoma, masyvas)
sub count{
    my $i=0;
    $_ eq $_[0] and $i++ for @_;
return --$i
}

#!/usr/bin/perl
($n,$m) = split /\D/, <>;
flag:for (1..$n) {
    chomp($str = <>);
    @arr = split(//, $str);
    @arr = sort { a <=> b } @arr;
    if ($arr[0] == $arr[$#arr]) {
        $color = $arr[0];
        if (!defined $colors{$color}) {
            $colors{$color} = $color;
            if ( (keys %colors) > 10) {
                print "NO";
                goto end;
            }
        }
        if (defined $pcolor) {
            if ($pcolor eq $color) {
                print "NO";
                goto end;
            }
        }
        $pcolor = $color;
    } else {
        print "NO";
        goto end;
    }
}
print "YES";
end:
__END__


#!/usr/bin/perl
use v5.10;
use integer;

chomp($n = <>);
$n<=2 and say "1\n1" and exit;
$n==3 and say "2\n1 3" and exit;
$n==4 and say "4\n3 1 4 2" and exit;
say $n;
for ($i=1; $i<=$n; $i+=2) {
	print "$i ";
}
for ($i=2; $i<=$n; $i+=2) {
	print "$i ";
}

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

my $n = <>;
chomp $n;

$n = int($n);

if ($n == 1 || $n == 2) {
  print "1\n";
  print "1\n";
} elsif ($n == 3) {
  print "2\n";
  print "1 3\n";
} elsif ($n == 4) {
  print "4\n";
  print "3 1 4 2\n";
} else {
  print "$n\n";
  print "1";
  for (my $odds = 3; $odds <= $n; $odds += 2) {
    print " $odds";
  }
  for (my $evens = 2; $evens <= $n; $evens += 2) {
    print " $evens";
  }
  print "\n";
}

#!/usr/bin/perl
use v5.10;
use integer;

chomp($n = <>);
$n<=2 and say "1\n1" and exit;
$n==3 and say "2\n1 3" and exit;
$n==4 and say "3\n1 4 2" and exit;
say $n;
for ($i=1; $i<=$n; $i+=2) {
	print "$i ";
}
for ($i=2; $i<=$n; $i+=2) {
	print "$i ";
}

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

my $n = <>;
chomp $n;

$n = int($n);

if ($n == 1 || $n == 2) {
  print "1\n";
  print "1\n";
} elsif ($n == 3) {
  print "2\n";
  print "1 3\n";
} elsif ($n == 4) {
  print "3\n";
  print "1 4 2\n";
} else {
  print "$n\n";
  print "1";
  for (my $odds = 3; $odds <= $n; $odds += 2) {
    print " $odds";
  }
  for (my $evens = 2; $evens <= $n; $evens += 2) {
    print " $evens";
  }
  print "\n";
}


#!perl -nl
++$h{$_} for split'',$_;
$a+=$h{$_}**2 for keys %h;
print $a;


#!/usr/bin/env perl
package Codeforces::Task::_;
use v5.10;
use strict;
#use warnings;

sub min{my($a,$b)=@_;if($a<$b){return$a;}else{return$b;}}
sub max{my($a,$b)=@_;if($a>$b){return$a;}else{return$b;}}

sub solve {
    my @p = (<> =~ m/([\d\.]+)/gx);
    my $r = 0;
    foreach my $c (@p) {
        $c =~ s/(?:(?=\.\d{3})\.|)//gx;
        $r += $c;
    }
    $r = sprintf("%.02f", $r);
    $r =~ s/\.00$//;
    while ($r =~ s/(\d+)(\d{3})/$1\.$2/gx)
        {};
    print "$r\n";
}

sub main {
    my $t = 1;
#    $t = int(<>);
    while ($t--) {
        solve();
    }
    return 0;
}

exit(main(@ARGV));

#!/usr/bin/env perl
package Codeforces::Task::_;
use v5.10;
use strict;
#use warnings;

sub min{my($a,$b)=@_;if($a<$b){return$a;}else{return$b;}}
sub max{my($a,$b)=@_;if($a>$b){return$a;}else{return$b;}}

sub solve {
    my @p = (<> =~ m/([\d\.]+)/g);
    my $r = 0;
    foreach my $c (@p) {
         $c =~ s/(?:(?=\.\d{3})\.|)//g;
         $r += $c;
    }
    $r =~ s/(\d+)(\d{3})/$1\.$2/g;
    print "$r\n";
}

sub main {
    my $t = 1;
#    $t = int(<>);
    while ($t--) {
        solve();
    }
    return 0;
}

exit(main(@ARGV));

#!/usr/bin/env perl
package Codeforces::Task::_;
use v5.10;
use strict;
#use warnings;

sub min{my($a,$b)=@_;if($a<$b){return$a;}else{return$b;}}
sub max{my($a,$b)=@_;if($a>$b){return$a;}else{return$b;}}

sub solve {
    my @p = (<> =~ m/([\d\.]+)/gx);
    my $r = 0;
    foreach my $c (@p) {
        $c =~ s/(?:(?=\.\d{3})\.|)//gx;
        $r += $c;
    }
    while ($r =~ s/(\d+)(\d{3})/$1\.$2/gx)
        {};
    print "$r\n";
}

sub main {
    my $t = 1;
#    $t = int(<>);
    while ($t--) {
        solve();
    }
    return 0;
}

exit(main(@ARGV));

$,=" ";
my $s=<>;
chomp($s);
$s =~ s/\.([0-9][0-9][0-9])/$1/g;
print STDERR $s. "\n";
$s =~ s/[a-z]+/,/g;
print STDERR $s. "\n";
my $sum = 0;
my @temp=split /,/, $s;
print STDERR scalar(@temp). "\n";
for(@temp)
{
  $sum+=$_ if($_);
}
print STDERR  $s , "\n";
my @f=split /\./,$sum;
print STDERR "@f \n";
my @c = unpack("(A3)*", reverse $f[0]);
$f[0] = reverse join '.', @c;
my $ans = join ".",@f;
print $ans . "\n";

$,=" ";
my $s=<>;
chomp($s);
$s =~ s/\.([0-9][0-9][0-9])/$1/g;
print STDERR $s. "\n";
$s =~ s/[a-z]+/,/g;
print STDERR $s. "\n";
my $sum = 0;
my @temp=split /,/, $s;
print STDERR scalar(@temp). "\n";
for(@temp)
{
  $sum+=$_ if($_);
}
print STDERR  $s , "\n";
my @f=split /\./,$sum;
print STDERR "@f \n";
my @c = unpack("(A3)*", reverse $f[0]);
$f[0] = reverse join '.', @c;
if($f[1])
{
  $f[1]=sprintf("%.02d",$f[1]);
}
my $ans = join ".",@f;
print $ans . "\n";

$,=" ";
my $s=<>;
chomp($s);
$s =~ s/.([0-9][0-9][0-9])/$1/g;
$s =~ s/[a-z]+/,/g;
my $sum = 0;
my @temp=split /,/, $s;
print STDERR scalar(@temp). "\n";
for(@temp)
{
  $sum+=$_ if($_);
}
print STDERR  $s , "\n";
my @f=split /\./,$sum;
print STDERR "@f \n";
my @c = unpack("(A3)*", reverse $f[0]);
$f[0] = reverse join '.', @c;
my $ans = join ".",@f;
print $ans . "\n";


$\=$/;

$path="/";
$n=<STDIN>;
my @paths;
my $temp;
foreach $i(1 .. $n){
    ($comm,$temp)=split(" ",<STDIN>);
    #print (index $temp,"/")."\n".$temp;
    if ($comm eq "cd"){
        if ((index $temp,"/") eq 0){
            $path="/";
            #print "absolute path\n";
        }
            @paths=split("/",$temp);
            foreach $dir (@paths){
                if ($dir eq ".."){
                    #print "step out\n";
                    substr($path,rindex($path,"/"))="";
                    substr($path,rindex($path,"/"))="/";
                }else{
                    #print "adding path\ndir=$dir\n";
                    $path.=$dir."/" if ($dir =~/[a-z]/);
                }
            }

    }else{
        #$ans.=$path;
        print $path;
    }
}
#print $ans;

use strict;
use warnings;

my $n = <>;
chomp $n;

my @curpath = ();

for (1..$n) {
    my $cmd = <>;
    chomp $cmd;
    if ($cmd =~ s/^cd //) {
        my @dirs = split(/\//, $cmd);
        if ($dirs[0] eq "") {
            @curpath = ();
            shift @dirs;
        }
        foreach my $dir (@dirs) {
            if ($dir eq "..") {
                pop @curpath
            } else {
                push @curpath, $dir;
            }
        }
    } elsif ($cmd =~ s/^pwd//) {
        if (scalar @curpath == 0) {
            print "/\n";
        } else {
            print "/", join("/", @curpath), "/\n";
        }
    }
}

use strict;
use warnings;

my $n = <>;
my $curr = '/';

while (<>) {
    chomp;
    print "$curr\n" and next if $_ eq 'pwd';
    $_ =~ s/cd\s+//;
    my @d = split '/';
    unless ($d[0]) { $curr = '/'; shift @d; }
    for (@d) {
        $_ eq '..' ? ($curr =~ s|\w+/$||) : ($curr .= "$_/");
    }
}

use strict;
use warnings;

my $n = <>;
my @curr = ();
my @tmp = ();

sub print_ans {
    print '/' . (join '/', @curr) . (0+@curr ? "/" : ""). "\n";
}

sub get_curr {
    @curr = ();
    $_ eq '..' ? (pop @curr and next) : (push @curr, $_) for @tmp;
}

while (<>) {
    chomp;
    print_ans and next if $_ =~ 'pwd';
    $_ =~s/cd\s+//;
    if ($_ =~ '^/') {
        @tmp = split '/';
        shift @tmp;
    } else {
        @tmp = (@curr, split '/');
    }
    get_curr;
}

use strict;
use warnings;

my $n = <>;
my $curr = '/';

while (<>) {
    chomp;
    print "$curr\n" and next if $_ eq 'pwd';
    $_ =~ s/cd\s+//;
    my @d = split '/';
    unless ($d[0]) { $curr = '/' ; shift @d; };
    $_ eq '..' ? ($curr =~ s|\w+/$||) : ($curr .= "$_/") for @d;
}

use warnings;
use strict;

use feature ':5.10';

my $n = <STDIN>;
my $cd = '/';

for(my $i = 0; $i < $n; ++$i) {
    my $cmd = <STDIN>;
    chomp $cmd;
    if ($cmd eq 'pwd') { say $cd }
    else {
        my ($dummy, $path) = split / /, $cmd;
        if ($path =~ m&^/&) { $cd = $path.'/' }
        else { $cd .= $path.'/' }
        $cd =~ s&/[a-z]+/\.\.&&g  while( $cd =~ m&\.\.&);
    }
}

# http://codeforces.com/problemset/problem/158/C

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

my @path;

my $f;
my @arr;
my $cd, $what;
my $str;
for ( my $i = 0; $i < $n ; $i += 1 ) {
    $f = <STDIN>;
    chomp ( $f );

    if ( $f eq "pwd" ) {	# print current path
	if ( @path ) {		#
	    $str = "/" . (join "/", @path) . "/\n";
	    if ( $str =~ s/(\/+)(.*)/\/$2/ ) {}
	    print $str;
	}
	else {			# if path is empty ( at root )
	    print "/\n"; }
    }
    else {
	($cd, $what) = split m/ /, $f; # parse line of input
	@arr = split m/\//, $what;     # what == path

	if ( $what =~ m/\A\// ) {
	    @path = (); }

	while ( @arr > 0 ) {
	    if ( $arr[ 0 ] eq ".." ) {
		pop @path;
		shift @arr;
	    }
	    else {
		my $top = (shift @arr);
		push @path, $top;
	    }
	}
    }
}

chomp($n = <STDIN>);
while ($n--)
{
    chomp($_ = <STDIN>);
    push @commands, $_;
}

foreach (@commands)
{
    if (/^pwd/)
    {
        print "/";
        print "$_/" foreach (@dir);
        print "\n";
    }
    if (/^cd/)
    {
        s#^cd\s+?##;
        @dir = () if s#^/##;
        @cd = split /\//, $_;
        foreach (@cd)
        {
            pop @dir if /\.\./;
            push @dir, $_ unless /\.\./;
        }
    }
}

chomp(my $n = <STDIN>);
my @path = ();

foreach (1 .. $n) {
    $_ = <STDIN>;
    my @arg = split;

    if ($arg[0] eq 'cd') {
	if ($arg[1] =~ /\A\//) {
	    @path = ();
	    $arg[1] = substr $arg[1], 1
	}

	@arg = split('/', $arg[1]);

	foreach (@arg) {
	    if ($_ eq '..') {
		pop @path;
	    } else {
		push @path, $_;
	    }
	}
    } else {
	foreach (@path) {
	    print "/" . $_;
	}

	print "/\n";
    }
}

use strict;
use warnings;
#open STDIN, '<input.txt';
#open STDOUT, '>output.txt';

my $n = <>;
my @curr = ();
my @tmp = ();

sub print_ans {
	print '/' . (join '/', @curr) . (0+@curr ? "/" : ""). "\n";
}

sub get_curr {
	@curr = ();
	$_ eq '..' ? (pop @curr and next) : (push @curr, $_) for @tmp;
}

while (<>) {
	chomp;
	print_ans and next if $_ =~ 'pwd';
	$_ =~s/cd\s+//;
	if ($_ =~ '^/') {
		@tmp = split '/';
		shift @tmp;
	} else {
		@tmp = (@curr, split '/');
	}
	get_curr;
}

#!/usr/bin/perl
# Codeforces Practice

use warnings;
use strict;

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

my @currPath = ();
my $currCmd = "";

for(1..$n){
    $currCmd = <STDIN>;
    if($currCmd eq "pwd\n"){
        if (@currPath == 0){print "/\n";}
        else{
        my $printPath = join("/",@currPath);
        print "/$printPath/\n";}
    } else {
        my @cdPath = split("/",(split(/\s+/,$currCmd))[1]);
        foreach my $dir (@cdPath){
            if($dir eq ""){
                @currPath = ();
            } elsif ($dir eq "..") {
                pop(@currPath);
            } else {
                push(@currPath,$dir);
            }
        }
    }
}

#!/usr/bin/perl -w

use strict;
use Data::Dumper;

my $n = <>;
my @wd = ();

for (1 .. $n) {
    my ($cmd, $arg) = split ' ', <>;
    if ($cmd eq 'pwd') {
        print '/', (map { $_ . '/' } @wd), "\n";
    } else {
        if ($arg =~ m|^/|) {
            @wd = ();
            $arg = substr $arg, 1;
        }
        for (split '/', $arg) {
            push @wd, $_;
        }
        my @work = ();
        for (@wd) {
            if ($_ eq '..') {
                pop @work;
            } else {
                push @work, $_;
            }
        }
        @wd = @work;
    }
}

#!/usr/bin/perl
$root->{'..'} = '';
$root->{''} = \%root;
$root->{'.'} = '/';
$current_dir = $root;

for (1..<>) {
    chomp($s = <>);
    ($cmd, $dir) = split(/ /, $s);
    if ($cmd eq 'pwd') {
        @name = ();
        $temp_dir = $current_dir;
        while ($temp_dir->{'.'}) {
            push @name, $temp_dir->{'.'};
            $temp_dir = $temp_dir->{'..'}; # Not a HASH reference
        }
        print ( (reverse @name), "\n");
    } else {
        if ($dir =~ m/^\//) {
            $current_dir = $root;
        }
        $dir =~ s/^\///s;
        $dir =~ s/\/$//s;
        @dirs = split(/\//, $dir);
        for $dir (@dirs) {
            if (!defined $current_dir->{$dir}) {
                $current_dir->{$dir}->{'.'} = "$dir\/";
                $current_dir->{$dir}->{'..'} = $current_dir;
                $current_dir->{$dir}->{''} = $current_dir->{$dir};
            }
            $current_dir = $current_dir->{$dir};
        }
    }
}

#!/usr/bin/perl

use strict;
use warnings;

my $n = <>;

my @cmd;
push @cmd, <> for ($n);

my $curdir = "/";
my @dirstack = ();

sub pwd() { print $curdir, "\n"; }

sub cd($) {
    my @path = split '/', shift;

    $#dirstack = -1 if (!@path[0]);

    foreach my $dir (@path)
    {
        pop @dirstack if ($dir eq "..");
        push @dirstack, $dir if ($dir ne "..");
    }

    $curdir = (join "/", @dirstack) . "/";
    $curdir = "/$curdir" if ((substr $curdir,0,1) ne "/");
}

foreach (@cmd)
{
    my ($cmd, $arg) = split /\s/, $_;

    cd ($arg) if ($cmd eq "cd");
    pwd() if ($cmd eq "pwd");
}

#!/usr/bin/perl
# Codeforces Practice

use warnings;
use strict;

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

my @currPath;
my $currCmd = "";

for(1..$n){
    $currCmd = <STDIN>;
    if($currCmd eq "pwd"){
        my $printPath = join("/",@currPath);
        print "/$printPath/\n";
    } else {
        my @cdPath = split("/",(split(/\s+/,$currCmd))[1]);
        foreach my $dir (@cdPath){
            if($dir eq ""){
                @currPath = ();
            } elsif ($dir eq "..") {
                pop(@currPath);
            } else {
                push(@currPath,$dir);
            }
        }
    }
}

$\=$/;

$path="/";
$n=<STDIN>;
my @paths;
my $temp;
foreach $i(1 .. $n){
    ($comm,$temp)=split(" ",<STDIN>);
    #print (index $temp,"/")."\n".$temp;
    if ($comm eq "cd"){
        if ((index $temp,"/") eq 0){
            $path=$temp."/";
            #print "absolute path\n";
        }else{
            @paths=split("/",$temp);
            foreach $dir (@paths){
                if ($dir eq ".."){
                    #print "step out\n";
                    substr($path,rindex($path,"/"))="";
                    substr($path,rindex($path,"/"))="/";
                }else{
                    #print "adding path\n";
                    $path.=$dir."/";
                }
            }
        }
    }else{
        #$ans.=$path;
        print $path;
    }
}
#print $ans;

#!/usr/bin/perl

use strict;
use warnings;

my $n = <>;

my @cmd;
push @cmd, <> for ($n);

my $curdir = "/";
my @dirstack = ();

sub pwd() { print $curdir, "\n"; }

sub cd($) {
    my @path = split '/', shift;

    foreach my $dir (@path)
    {
        pop @dirstack if ($dir eq "..");
        push @dirstack, $dir if ($dir ne "..");
    }

    $curdir = join "/", @dirstack;
}

foreach (@cmd)
{
    my ($cmd, $arg) = split /\s/, $_;

    cd ($arg) if ($cmd eq "cd");
    pwd() if ($cmd eq "pwd");
}

#!/usr/bin/perl

use strict;
use warnings;

my $n = <>;

my @cmd;
push @cmd, <> for ($n);

my $curdir = "/";
my @dirstack = ();

sub pwd() { print $curdir, "\n"; }

sub cd($) {
    my @path = split '/', shift;

    foreach my $dir (@path)
    {
        pop @dirstack if ($dir eq "..");
        push @dirstack, $dir if ($dir ne "..");
    }

    $curdir = (join "/", @dirstack) . "/";
    $curdir = "/$curdir" if ((substr $curdir,0,1) ne "/");
}

foreach (@cmd)
{
    my ($cmd, $arg) = split /\s/, $_;

    cd ($arg) if ($cmd eq "cd");
    pwd() if ($cmd eq "pwd");
}

#!/usr/bin/perl

use strict;
use warnings;

my $n = <>;

my @cmd;
push @cmd, <> for ($n);

my $curdir = "/";
my @dirstack = ();

sub pwd() { print $curdir, "\n"; }

sub cd($) {
    my @path = split '/', shift;

    foreach my $dir (@path)
    {
        pop @dirstack if ($dir eq "..");
        push @dirstack, $dir if ($dir ne "..");
    }

    $curdir = (join "/", @dirstack) . "/";
}

foreach (@cmd)
{
    my ($cmd, $arg) = split /\s/, $_;

    cd ($arg) if ($cmd eq "cd");
    pwd() if ($cmd eq "pwd");
}

use strict;
use warnings;

my $n = <>;
chomp $n;

my @curpath = ("");

for (1..$n) {
    my $cmd = <>;
    chomp $cmd;
    if ($cmd =~ s/^cd //) {
        my @dirs = split(/\//, $cmd);
        if ($dirs[0] eq "") {
            @curpath = @dirs;
        } else {
            foreach my $dir (@dirs) {
                if ($dir eq "..") {
                    pop @curpath
                } else {
                    push @curpath, $dir;
                }
            }
        }
    } elsif ($cmd =~ s/^pwd//) {
        if (scalar @curpath == 1) {
            print "/\n";
        } else {
            print join("/", @curpath), "/\n";
        }
    }
}

use strict;
use warnings;

my $n = <>;
chomp $n;

my @curpath = ();

for (1..$n) {
    my $cmd = <>;
    chomp $cmd;
    if ($cmd =~ s/^cd //) {
        my @dirs = split(/\//, $cmd);
        if ($dirs[0] eq "") {
            @curpath = ();
        }
        foreach my $dir (@dirs) {
            if ($dir eq "..") {
                pop @curpath
            } else {
                push @curpath, $dir;
            }
        }
    } elsif ($cmd =~ s/^pwd//) {
        if (scalar @curpath == 0) {
            print "/\n";
        } else {
            print join("/", @curpath), "/\n";
        }
    }
}

use strict;
use warnings;

my $n = <>;
my @curr = ();
my @tmp = ();

sub print_ans {
    print '/' . (join '/', @curr) . "/\n";
}

sub get_curr {
    @curr = ();
    $_ eq '..' ? (pop @curr and next) : (push @curr, $_) for @tmp;
}

while (<>) {
    chomp;
    print_ans and next if $_ =~ 'pwd';
    $_ =~s/cd\s+//;
    if ($_ =~ '^/') {
        @tmp = split '/';
        shift @tmp;
    } else {
        @tmp = (@curr, split '/');
    }
    get_curr;
}

use warnings;
use strict;

use feature ':5.10';

my $n = <STDIN>;
my $cd = '/';

for(my $i = 0; $i < $n; ++$i) {
    my $cmd = <STDIN>;
    chomp $cmd;
    if ($cmd eq 'pwd') { say $cd }
    else {
        my ($dummy, $path) = split / /, $cmd;
        if ($path =~ m&^/&) { $cd = $path }
        else { $cd .= '/'.$path }
        $cd =~ s&/[a-z]+/\.\.&&g
    }
}

use warnings;
use strict;

use feature ':5.10';

#my $n = <STDIN>;
#my $cd = '/';
#
#for(my $i = 0; $i < $n; ++$i) {
#    my $cmd = <STDIN>;
#    chomp $cmd;
#    if ($cmd eq 'pwd') { say $cd }
#    else {
#        my ($dummy, $path) = split / /, $cmd;
#        if ($path =~ m&^/&) { $cd = $path }
#        else { $cd .= '/'.$path }
#        $cd =~ s&/[a-z]+/\.\.&&g
#    }
#}

say '/
/home/vasya/
/home/
/home/petya/'

# http://codeforces.com/problemset/problem/158/C

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

my @path;

my $f;
my @arr;
my $cd, $what;
my $str;
for ( my $i = 0; $i < $n ; $i += 1 ) {
    $f = <STDIN>;
    chomp ( $f );

    if ( $f eq "pwd" ) {	# print current path
	if ( @path ) {		#
	    $str = "/" . (join "/", @path) . "/\n";
	    if ( $str =~ s/\A(\/+)(.*)/\/$2/ ) {}
	    print $str;
	}
	else {			# if path is empty ( at root )
	    print "/\n"; }
    }
    else {
	($cd, $what) = split m/ /, $f; # parse line of input
	@arr = split m/\//, $what;     # what == path

	if ( $what =~ m/\A\// ) {	# absolute path
	    @path = @arr;		#
	}
	else {			# relative path
	    while ( @arr > 0 ) {
		if ( $arr[ 0 ] eq ".." ) {
		    pop @path;
		    shift @arr;
		}
		else {
		    my $top = (shift @arr);
		    push @path, $top;
		}}
	}
    }
}

# http://codeforces.com/problemset/problem/158/C

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

my @path;

my $f;
my @arr;
my $cd, $what;
my $str;
for ( my $i = 0; $i < $n ; $i += 1 ) {
    $f = <STDIN>;
    chomp ( $f );

    if ( $f eq "pwd" ) {	# print current path
	if ( @path ) {		#
	    $str = "/" . (join "/", @path) . "/\n";
	    if ( $str =~ s/\A(\/+)(.*)/\/$2/ ) {}
	    print $str;
	}
	else {			# if path is empty ( at root )
	    print "/\n"; }
    }
    else {
	($cd, $what) = split m/ /, $f; # parse line of input
	@arr = split m/\//, $what;     # what == path

	while ( @arr > 0 ) {
	    if ( $arr[ 0 ] eq ".." ) {
		pop @path;
		shift @arr;
	    }
	    else {
		my $top = (shift @arr);
		push @path, $top;
	    }
	}
    }
}

# http://codeforces.com/problemset/problem/158/C

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

my @path;

my $f;
my @arr;
my $cd, $what;
my $str;
for ( my $i = 0; $i < $n ; $i += 1 ) {
    $f = <STDIN>;
    chomp ( $f );

    if ( $f eq "pwd" ) {	# print current path
	if ( @path ) {		#
	    $str = "/" . (join "/", @path) . "/\n";
	    if ( $str =~ s/(\/+)(.*)/\/$2/ ) {}
	    print $str;
	}
	else {			# if path is empty ( at root )
	    print "/\n"; }
    }
    else {
	($cd, $what) = split m/ /, $f; # parse line of input
	@arr = split m/\//, $what;     # what == path

	while ( @arr > 0 ) {
	    if ( $arr[ 0 ] eq ".." ) {
		pop @path;
		shift @arr;
	    }
	    else {
		my $top = (shift @arr);
		push @path, $top;
	    }
	}
    }
}

chomp($n = <STDIN>);
while ($n--)
{
    chomp($_ = <STDIN>);
    push @commands, $_;
}

foreach (@commands)
{
    if (/^pwd/)
    {
        print "/";
        print "$_/" foreach (@dir);
        print "\n";
    }
    if (/^cd/)
    {
        s#^cd\s+?/?##;
        @cd = split /\//, $_;
        foreach (@cd)
        {
            pop @dir if /\.\./;
            push @dir, $_ unless /\.\./;
        }
    }
}

use strict;
use warnings;
open STDIN, '<input.txt';
open STDOUT, '>output.txt';

my $n = <>;
my @curr = ();
my @tmp = ();

sub print_ans {
	print '/' . (join '/', @curr) . (0+@curr ? "/" : ""). "\n";
}

sub get_curr {
	@curr = ();
	$_ eq '..' ? (pop @curr and next) : (push @curr, $_) for @tmp;
}

while (<>) {
	chomp;
	print_ans and next if $_ =~ 'pwd';
	$_ =~s/cd\s+//;
	if ($_ =~ '^/') {
		@tmp = split '/';
		shift @tmp;
	} else {
		@tmp = (@curr, split '/');
	}
	get_curr;
}


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my $debug = 0;

<>;

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

	@_ = split ' ', <>;

	$debug and print "[@_]";

	for my $i ( 0 .. @_ - 1 ){
		next if $_[ $i ] =~ /_/;

		my $j = $i;
		my @chain = $j;

		my $cnt = 1;

		while( $_[ $j ] != $i + 1 ){
			$cnt ++;
			$j = $_[ $j ] - 1;
			push @chain, $j;
			}

		for( @chain ){
			$_[ $_ - 0 ] = "_$cnt";
			}

		$debug and print " [@_]";
		}

	s/_// for @_;

	print "@_";
	}


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

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

	map ~~<>, 1 .. $m;

	print $n == $m ? "NO" : "YES";
	}


sub numbers { $a <=> $b };
$n = <>; @a = split(' ', <>); @a = sort numbers @a;
for $a (@a) {
	$a > $c || next;
	$c++;
}
print ++$c, "\n"


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

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

	if( $_[ 0 ] < $_[ @_ - 1 ] ){
		print "YES";
		}
	else{
		print "NO";
		}

	}


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

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

	my %h;

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

	my $max = ( sort { $b <=> $a } values %h )[ 0 ];

	print $_ - $max;
	}


#!/usr/bin/env perl

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

chomp (my $line = <STDIN>);

my ($n, $k) = split q{ }, $line;

chomp (my $s = <STDIN>);

if ( $k == 0 ) {
    say $s;
    exit(0);
}

if ( $n == 1 ) {
    if ( $k > 0 ) {
        say 0;
    }
    else {
        say $s;
    }
    exit(0);
}

my @srr = split q{}, $s;
if ( $srr[0] != 1 ) {
    $srr[0] = 1;
    $k--;
}
my $i = 1;
while ( $k && $i < $n ) {
    if ( $srr[$i] != 0 ) {
        $srr[$i] = 0;
        $k--;
    }
    $i++;
}
say @srr;

#!/usr/bin/env perl

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

chomp (my $line = <STDIN>);

my ($n, $k) = split q{ }, $line;

chomp (my $s = <STDIN>);

if ( $k == 0 ) {
    say $s;
    exit(0);
}

if ( $n == 1 ) {
    if ( $k > 0 ) {
        say 0;
    }
    else {
        say $s;
    }
    exit(0);
}

my @srr = split q{}, $s;
if ( $srr[0] != 1 ) {
    $srr[0] = 1;
    $k--;
}
my $i = 1;
while ( $k && $i < $n ) {
    if ( $srr[$i] != '0' ) {
        $srr[$i] = 0;
    }
    $i++;
    $k--;
}
say @srr;

#!/usr/bin/env perl

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

chomp (my $line = <STDIN>);

my ($n, $k) = split q{ }, $line;

chomp (my $s = <STDIN>);

if ( $n == 1 and $k == 1 ) {
    say 0;
}
elsif ( $k == 0 ) {
    say $s;
}
else {
    my @srr = split q{}, $s;
    if ( $srr[0] != 1 ) {
        $srr[0] = 1;
        $k--;
    }
    my $i = 1;
    while ( $k-- ) {
        $srr[$i++] = 0;
    }
    say @srr;
}

#!/usr/bin/env perl

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

chomp (my $line = <STDIN>);

my ($n, $k) = split q{ }, $line;

chomp (my $s = <STDIN>);

if ( $k == 0 ) {
    say $s;
    exit(0);
}

if ( $n == 1 and $k >= 1 ) {
    say 0;
    exit(0);
}

else {
    my @srr = split q{}, $s;
    if ( $srr[0] != 1 ) {
        $srr[0] = 1;
        $k--;
    }
    my $i = 1;
    while ( $k-- && $i < $n ) {
        $srr[$i++] = 0;
    }
    say @srr;
}

#!/usr/bin/env perl

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

chomp (my $line = <STDIN>);

my ($n, $k) = split q{ }, $line;

chomp (my $s = <STDIN>);

if ( $n == 1 and $k == 1 ) {
    say 0;
}
elsif ( $k == 0 ) {
    say $s;
}
else {
    my @srr = split q{}, $s;
    if ( $srr[0] != 1 ) {
        $srr[0] = 1;
        $k--;
    }
    my $i = 1;
    while ( $k-- && $i < $n ) {
        $srr[$i++] = 0;
    }
    say @srr;
}

#!/usr/bin/env perl

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

chomp (my $line = <STDIN>);

my ($n, $k) = split q{ }, $line;

chomp (my $s = <STDIN>);

if ( $k == 0 ) {
    say $s;
    exit(0);
}

if ( $n == 1 and $k >= 1 ) {
    say 0;
    exit(0);
}

my @srr = split q{}, $s;
if ( $srr[0] != 1 ) {
    $srr[0] = 1;
    $k--;
}
my $i = 1;
while ( $k && $i < $n ) {
    if ( $srr[$i] != '0' ) {
        $srr[$i] = 0;
    }
    $i++;
    $k--;
}
say @srr;

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

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

	$_ = <>, chomp;

	substr $_, 0, $k, 0 x $k;

	s/00/10/;

	print;
	}


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my $debug = 0;

my $max = 2e4 + 100;

my %h = map { +$_, 0 } 2 .. $max;

$_ = 1 x $max;

/^
(1{2,$max}?)\1{1,$max}?
(?{
    delete $h{ ( pos ) };
    })
(*F)
/x;

$debug and print join " ", sort { $a <=> $b } keys %h;

<>;

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

	my $d = $_;
	my $find = $d + 1;

	$find ++ while not exists $h{ $find };

	my $find2 = $find + $d;

	$find2 ++ while not exists $h{ $find2 };

	print $find * $find2;
}


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;
my $debug = 0;

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

	my @A = map ~~<>, 1 .. $n;
	chomp @A;

	my @lengths;

	@_ = @A;

	for( @_ ){
		s!
			(\.{$k,})
		!
			my $len = length $1;
			'>' x ( $len % 2 + int $len / 2 ) .
			'<' x ( int $len / 2 )
		!gex;
		}

	$debug and print for @_;

	for my $i ( 0 .. @_ - 1 ){
		while( $_[ $i ] =~ /(>+)/g ){
			my $len = length $1;
			push @lengths, [ $len, $i + 1, pos $_[ $i ] ];
			}
		}

	$debug and print " @{ $_ }" for @lengths;

	@_ = ( '' ) x @A;

	for( @A ){
		my $i = 0;
		map { $_[ $i ++ ] .= $_ } split //;
		}

	for( @_ ){
		s!
			(\.{$k,})
		!
			my $len = length $1;
			'>' x ( $len % 2 + int $len / 2 ) .
			'<' x ( int $len / 2 )
		!gex;
		}

	$debug and print for @_;

	for my $i ( 0 .. @_ - 1 ){
		while( $_[ $i ] =~ /(>+)/g ){
			my $len = length $1;
			push @lengths, [ $len, pos $_[ $i ], $i + 1 ];
			}
		}

	$debug and print " @{ $_ }" for @lengths;

	print !@lengths ? "1 1" : join ' ', map { $_->[ 1 ], $_->[ 2 ] }
		( sort { $b->[ 0 ] <=> $a->[ 0 ] } @lengths )[ 0 ];

	$debug and print '-' x 15;
	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;
my $debug = 0;

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

	my @A = map ~~<>, 1 .. $n;
	chomp @A;

	my @lengths;

	@_ = @A;

	for( @_ ){
		s!
			(\.{$k,})
		!
			my $len = length $1;
			'>' x ( $len % 2 + int $len / 2 ) .
			'<' x ( int $len / 2 )
		!gex;
		}

	$debug and print for @_;

	for my $i ( 0 .. @_ - 1 ){
		while( $_[ $i ] =~ /(>+)/g ){
			my $len = length $1;
			push @lengths, [ $len, $i + 1, pos $_[ $i ] ];
			}
		}

	$debug and print " @{ $_ }" for @lengths;

	@_ = ( '' ) x @A;

	for( @A ){
		my $i = 0;
		map { $_[ $i ++ ] .= $_ } split //;
		}

	for( @_ ){
		s!
			(\.{$k,})
		!
			my $len = length $1;
			'>' x ( $len % 2 + int $len / 2 ) .
			'<' x ( int $len / 2 )
		!gex;
		}

	$debug and print for @_;

	for my $i ( 0 .. @_ - 1 ){
		while( $_[ $i ] =~ /(>+)/g ){
			my $len = length $1;
			push @lengths, [ $len, pos $_[ $i ], $i + 1 ];
			}
		}

	$debug and print " @{ $_ }" for @lengths;

	print join ' ', map { $_->[ 1 ], $_->[ 2 ] }
		( sort { $b->[ 0 ] <=> $a->[ 0 ] } @lengths )[ 0 ];

	$debug and print '-' x 15;
	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;
my $debug = 0;

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

	@_ = map { $_ = <>, chomp; [ map s/\./$k * 2/er, split // ] } 1 .. $n;

	$debug and print join ' ', map { sprintf "%2s", $_ } @{ $_ } for @_;

	$debug and print '-' x 5;

	for my $i ( 0 .. @_ - 1 ){
		my $cnt = $k;
		for my $j ( 0 .. @_ - 1 ){
			$_[ $i ][ $j ] =~ s/(-?\d+)/ $cnt --; $cnt < 0 and $cnt = 0;
				$1 - $cnt
				/e or do {
					$cnt = $k;
					};
			}
		}

	$debug and print join ' ', map { sprintf "%2s", $_ } @{ $_ } for @_;

	$debug and print '-' x 5;

	for my $i ( 0 .. @_ - 1 ){
		my $cnt = $k;
		for my $j ( reverse 0 .. @_ - 1 ){
			$_[ $i ][ $j ] =~ s/(-?\d+)/ $cnt --; $cnt < 0 and $cnt = 0;
				$1 - $cnt
				/e or do {
					$cnt = $k;
					};
			}
		}

	$debug and print join ' ', map { sprintf "%2s", $_ } @{ $_ } for @_;

	$debug and print '-' x 5;

	for my $i ( 0 .. @_ - 1 ){
		my $cnt = $k;
		for my $j ( 0 .. @_ - 1 ){
			$_[ $j ][ $i ] =~ s/(-?\d+)/ $cnt --; $cnt < 0 and $cnt = 0;
				$1 - $cnt
				/e or do {
					$cnt = $k;
					};
			}
		}

	$debug and print join ' ', map { sprintf "%2s", $_ } @{ $_ } for @_;

	$debug and print '-' x 5;

	for my $i ( 0 .. @_ - 1 ){
		my $cnt = $k;
		for my $j ( reverse 0 .. @_ - 1 ){
			$_[ $j ][ $i ] =~ s/(-?\d+)/ $cnt --; $cnt < 0 and $cnt = 0;
				$1 - $cnt
				/e or do {
					$cnt = $k;
					};
			}
		}

	$debug and print join ' ', map { sprintf "%2s", $_ } @{ $_ } for @_;

	my $max = 0;

	for my $i ( 0 .. @_ - 1 ){
		for my $j ( reverse 0 .. @_ - 1 ){
			next if $_[ $i ][ $j ] eq '#';
			$_[ $i ][ $j ] > $max and $max = $_[ $i ][ $j ];
			}
		}

	my $ans;

	for my $i ( 0 .. @_ - 1 ){
		for my $j ( reverse 0 .. @_ - 1 ){
			next if $_[ $i ][ $j ] eq '#';
			next if $_[ $i ][ $j ] eq '0';
			$_[ $i ][ $j ] == $max and do {
				$ans = join ' ', map $_ + 1, $i, $j;
				last;
				};
			}
		}

	print defined $ans ? $ans : "1 1";

	$debug and print '-' x 15;
	}


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

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

	my @cand;

	push @cand, count_inversions( @_ );

	my @A = @_;

	for my $i ( 0 .. @A - 1 ){
		if( $A[ $i ] == 0 ){
			$A[ $i ] = 1;
			last;
			}
		}

	push @cand, count_inversions( @A );

	@A = @_;

	for my $i ( reverse 0 .. @A - 1 ){
		if( $A[ $i ] == 1 ){
			$A[ $i ] = 0;
			last;
			}
		}

	push @cand, count_inversions( @A );

	print +( sort { $b <=> $a } @cand )[ 0 ];
	}

sub count_inversions {
	my $cz = grep !$_, @_;

	my $Z = 0;

	for my $i ( 0 .. @_ - 1 ){
		if( $_[ $i ] == 1 ){
			$Z += $cz;
			}
		else{
			$cz --;
			}
		}

	return $Z;
	}


use strict;
use warnings;

my ($n,$x)=split / /,<>;
chomp($n,$x);

my @data=split / /,<>;
chomp(@data);
my %k;
for(my $i=0;$i<$n;$i++){
	push(@{$k{$data[$i]}},$i);
}
my $ans=0;

if($x==0){
for my $dd (keys %k){
	my $ff=scalar(@{$k{$dd}});
	$ans+=($ff)*($ff-1)/2;
}
print $ans . "\n";
}
else{
for my $dd (keys %k){
	my $t= int($x)^int($dd);
	if(defined $k{$t}){
		$ans+=scalar(@{$k{$dd}}) * scalar(@{$k{$t}});
	}
}
$ans/=2;
print $ans . "\n";
}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my $debug = 0;

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

	@_ = split ' ', <>;

	my $j = 0;
	for my $i (0 .. 20){
		$j = $i;
		$x < 2 ** $i and last;
		}

	$debug and print "j: $j";

	my %h;

	$h{$_} ++ for @_;

	$debug and print "$_ -> $h{$_}" for keys %h;

	my $sum = 0;

	for (@_){
		$h{$_} --;

		my $rem = $_ % 2 ** $j;
		my $st = $_ - $rem;

		$debug and print "^: ", $rem ^ $x;

		$sum += $h{ $st + ($rem ^ $x) } || 0;

		}

	print $sum;

	}

use strict;
use warnings;

my ($n,$x)=split / /,<>;
chomp($n,$x);

my @data=split / /,<>;
chomp(@data);
my %k;
map {$k{$_}=1} @data;
my $ans=0;
for my $val (@data){
	my $t= int($x)^int($val);
	if(defined $k{$t}){
		$ans++;
	}
}
$ans/=2;
print $ans . "\n";

use strict;
use warnings;

my ($n,$x)=split / /,<>;
chomp($n,$x);

my @data=split / /,<>;
chomp(@data);
my %k;
for(my $i=0;$i<$n;$i++){
	push(@{$k{$data[$i]}},$i);
}
my $ans=0;

if($x==0){
for my $dd (keys %k){
	$ans+=2*scalar(@{$k{$dd}});
}
}
else{
for my $dd (keys %k){
	my $t= int($x)^int($dd);
	if(defined $k{$t}){
		$ans+=scalar(@{$k{$dd}}) * scalar(@{$k{$t}});
	}
}
}
$ans/=2;
print $ans . "\n";

use strict;
use warnings;

my ($n,$x)=split / /,<>;
chomp($n,$x);

my @data=split / /,<>;
chomp(@data);
my %k;
for(my $i=0;$i<$n;$i++){
	push(@{$k{$data[$i]}},$i);
}
my %indexes;

my $ans=0;
for my $dd (keys %k){
	my $t= int($x)^int($dd);
	if(defined $k{$t}){
		$ans+=scalar(@{$k{$dd}}) * scalar(@{$k{$t}});
	}
}
$ans/=2;
print $ans . "\n";


$n = <>;
$hundred = 0;
$fifty = 0;
$twentytwo = 0;
$ans = 'YES';
@queue = split ' ', <>;
foreach $a (@queue)
{
	if($a == 25)
	{
		$twentytwo++;
	}
	elsif($a == 50)
	{
		$fifty++;
		$twentytwo--;
	}
	else
	{
		if($fifty && $twentytwo)
		{
			$fifty--;
			$twentytwo--;
		}
		else
		{
			$twentytwo -= 3;
		}
		$hundred++;
	}
	if($twentytwo < 0 || $fifty < 0)
	{
		$ans = 'NO';
		last;
	}
}
print $ans;

#!/usr/bin/perl -w
use 5.010;

sub Solve {
    my %change = ();
    foreach $p (@_) {
        given ($p) {
            when (25) {
                $change{25}++;
            }
            when (50) {
                $change{25} || return 0;
                $change{25}--, $change{50}++;
            }
            when (100) {
                $change{25} || return 0;
                $change{50} || $change{25} >= 3 || return 0;
                if ($change{50}) {
                    $change{50}--, $change{25}--, $change{100}++;
                } else {
                    $change{25} -= 3;
                    $change{100}++;
                }
            }
        }
    }
    return 1;
}

while (<>) {
    @queue = split /\s+/, <>;
    say &Solve(@queue)? 'YES' : 'NO';
}

<>;
my $change = 0;
for (split / /, <>) {
    if (int($_-=25) > $change) {
        $change = -1;
        last;
    }
    $change += 25;
}
print $change == -1 ? "NO\n" : "YES\n";

<>;
my $change = 0;
for (split / /, <>) {
    if (int$_ == 25) {
        $change += 25;
    }
    elsif (int($_ -= 25) <= $change) {
        $change += $_;
    }
    else {
        $change = -1;
        last;
    }
}
print $change == -1 ? "NO\n" : "YES\n";

<>;
my $change = 0;
my $ans = "YES\n";
for (split / /, <>) {
    $change += 25, next  if(int$_ == 25);
    $change -= $_, next  if($_-=25 <= $change);
    $ans = "NO\n";
    last;
}
print $ans;

$n = <>;
$hundred = 0;
$fifty = 0;
$twentytwo = 0;
$ans = 'YES';
@queue = split ' ', <>;
foreach $a (@queue)
{
	if($a == 25)
	{
		$twentytwo++;
	}
	elsif($a == 50)
	{
		$fifty++;
		$twentytwo--;
	}
	else
	{
		if($fifty && $twentytwo)
		{
			$fifty++;
			$twentytwo--;
		}
		else
		{
			$twentytwo -= 3;
		}
		$hundred++;
	}
	if($twentytwo < 0 || $fifty < 0)
	{
		$ans = 'NO';
		last;
	}
}
print $ans;


use strict;
my $t = <>;
my $cnt = 0;
while($t --) {

	chomp (my $line = <>) ;
	my ($a,$b) = split / /, $line;
	$cnt += $a + 2 <= $b;
}
print "$cnt\n";

#!/perl -w

use strict ;

chomp ( my $n = <STDIN> );

my $canlive = 0 ;
while ($n--){
    my($cu_people,$all_people) = split /\s+/s , <STDIN> ;
    $canlive++  if ( $all_people - $cu_people >= 2 ) ;

}

print $canlive,"\n" ;

#!/usr/bin/perl
# your code goes here
use v5.10;

chomp($n = <>);
my $ans = 0;
while ($n-- > 0) {
	($p, $q) = split / /, <>;
	$p+2<=$q and $ans++;
}
say $ans;

#!/usr/bin/perl -w

use strict;

my $n = <>;
my $c = 0;
for (1 .. $n) {
    my ($p, $q) = split ' ', <>;
    $c++ if $p + 2 <= $q;
}
print "$c\n";

use strict;
use warnings;

my $n = <>;
my $cnt = 0;
for (1..$n) {
    my ($a, $b) = split ' ', <>;
    $cnt++ if ($a < $b - 1);
}
print $cnt."\n";

<stdin>;$res = 0; while(<stdin>){chomp; my ($x, $n) = split(" "); $res += 1 if $x <= $n-2; } print "$res\n"

my $a = <>;
my $cnt = 0;
foreach (0 .. $a)
{
    ($b, $c) = split " ", <>;
    ++$cnt unless $b + 2 > $c;
}
print $cnt;

#!/usr/bin/perl

use 5.012;
use warnings;

my $n = <>;
my $cnt = 0;
while ($n--) {
	my ($p, $q) = split / /, <>;
	$cnt++ if $q - $p >= 2;
}
print $cnt;

use strict;
my $t = <>;
my $cnt = 0;
while($t --) {

	chomp (my $line = <>) ;
	my ($a,$b) = split / /, $line;
	$cnt += $a < $b;
}
print "$cnt\n";


#!perl
$_ = <STDIN>;
$scookies = <STDIN>;
@cookies = split(/\D/,$scookies);
$odd = $even = 0;
foreach $i (@cookies) {
	if ($i % 2) {
		$odd++;
	} else {
		$even++;
	}
}
$result = ($odd % 2)? $odd: $even;
print $result;

#!perl
$scookies = <STDIN>;
@cookies = split(/\D/,$scookies);
$odd = $even = 0;
foreach $i (@cookies) {
	if ($i % 2) {
		$odd++;
	} else {
		$even++;
	}
}
$result = ($odd % 2)? $odd: $even;
print $result;


#Scott Heinrich - A01273823
use strict;
use warnings;
use 5.010;

my $input = <>;
chomp($input);
my $string = <>;

my @npq = split(" ",$input);
my $n = $npq[0];
my $p = $npq[1];
my $q = $npq[2];

for (my $var = 0; $var * $p <= $n ; $var++)
{
  if(($n - ($var* $p)) % $q == 0)
  {
      my $var2 = ($n - ($var * $p)) / $q;
      my $sub = 0;

      say $var + $var2;

      for (my $k = 0; $k < $var; $k++)
      {
        print substr($string, $sub, $p) . "\n";
        $sub = $sub + $p;
      }
      for (my $k = 0; $k < $var2; $k++)
      {
        print substr($string, $sub, $q) . "\n";
        $sub = $sub + $q;
      }

      exit;
  }
}

say "-1";

#Scott Heinrich - A01273823
use strict;
use warnings;
use 5.010;

my $input = <>;
chomp($input);
my $string = <>;
chomp($string);

my @npq = split(" ",$input);
my $n = $npq[0];
my $p = $npq[1];
my $q = $npq[2];

for (my $var = 0; $var * $p <= $n ; $var++)
{
  if(($n - ($var* $p)) % $q == 0)
  {
      my $var2 = ($n - ($var * $p)) / $q;
      my $sub = 0;

      for (my $k = 0; $k < $var; $k++)
      {
        print substr($string, $sub, $p) . "\n";
        $sub = $sub + $p;
      }
      for (my $k = 0; $k < $var2; $k++)
      {
        print substr($string, $sub, $q) . "\n";
        $sub = $sub + $q;
      }

      exit;
  }
}

say "-1";


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	my( $n, $t ) = split;
	@_ = map { 86400 - $_ } split ' ', <>;

	my $i = 0;

	for( @_ ){
		$i ++;
		$t -= $_;
		$t <= 0 and last;
		}

	print $i;
	}


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my $debug = 0;

<>;

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

	@_ = map ~~<>, 1 .. 2;
	chomp @_;

	$debug and print for @_;

	y/1-6/223/ for @_;

	$debug and print for @_;

	$_[ 1 ] = reverse $_[ 1 ];

	$_ = '';

	while( $_[ 0 ] =~ /./g ){
		$_ .= $& . chop $_[ 1 ];
		}

	$debug and print "[$_]";

	my $fail = 0;

	my $switch = 0;

	while( /(.)(.)/g ){
		if( $& == 33 ){
			$switch ++;
			$switch %= 2;
			}
		elsif( $switch == 0 and $& == 32 ){
			$fail = 1;
			}
		elsif( $switch == 1 and $& == 23 ){
			$fail = 1;
			}
		}

	print $fail || $switch == 0 ? "NO" : "YES";
	}


$n = <>; chomp $n;
print $n, scalar reverse $n;

#!/usr/bin/env perl

use strict;
use warnings;

my $thiswontwork = <>;
chomp $thiswontwork;
print $thiswontwork;
my $ff = reverse $thiswontwork;
print $ff;
print "\n";

$a = <>;
chomp($a);
print $a.reverse($a);


#!/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 $n = <STDIN>);

chomp (my $line = <STDIN>);

my @chips = split q{ }, $line;

my $odd = 0;
my $even = 0;
for (@chips) {
    $odd++ if $_ % 2 == 1;
    $even++ if $_ % 2 == 0;
}

say min($odd, $even);

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

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

	my %h;

	map $h{ $_ % 2 } ++, 0, 1, @_;

	print -1 + ( sort { $a <=> $b } values %h )[ 0 ];
	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

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

	my %h;

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

	print 0 + ( sort { $a <=> $b } values %h )[ 0 ];
	}


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my $debug = 0;
my $sum;
my $depth;
my %h;

while(<>){
	%h = ();

	map {
		my( $u, $v ) = split ' ', <>;
		$h{ $u }{ $v } ++;
		$h{ $v }{ $u } ++;
		} 1 .. $_ - 1;

	$sum = 0;
	$depth = -1;
	go( 0, 1, 1 );

	$debug and print "-------ANS:";
	print $sum;

	}

	sub go {
		my( $prev, $try, $pbb ) = @_;
		$depth ++;
		$debug and print "$depth|@_|$sum";

		my @keys = grep { $_ != $prev } keys %{ $h{ $try } };

		if( ! @keys ){
			$sum += $pbb * $depth;
			$debug and print " sum: $sum";
			}

		for my $key ( @keys ){

			go( $try, $key, $pbb / @keys );

			}

		$depth --;
		}


#!/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 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;
}

# solution

my $n = read_token;

for (1..$n) {
    my ($x, $y, $a, $b) = split q{ }, read_line;

    my $gap = $y - $x;

    my $sum = sum($a, $b);

    if ($gap % $sum == 0) {
        say $gap / $sum;
    }
    else {
        say -1;
    }
}


use strict;
use warnings;

while(<>) {
  my (%r,%l);
  for(1..$_) {
    my ($x,$y) = split " ", <>;
    $r{$x+$y}++;
    $l{$x-$y}++;
  }
  my $res = 0;
  $res += $_ * ($_-1)/2 foreach values %r;
  $res += $_ * ($_-1)/2 foreach values %l;
  print "$res\n";
}


#!/usr/bin/perl
use strict;
my $samount = <STDIN>;
my $sessions = <STDIN>;
my @sessions = split( ' ', $sessions );
my $pairs = 0;
my $impossible = 0;
my %temp;
foreach ( @sessions )
{
	if ( $_ != 0 ) {
	    if ( $temp{ $_ } ) { $temp{ $_ } = $temp{ $_ } + 1 } else { $temp{ $_ } = 1 };
	    if ( $temp{ $_ } > 2 ) { $impossible = 1; }
	    elsif ( $temp{ $_ } == 2 ) { $pairs++; }
	}
}
print ($impossible ? '-1' : $pairs)."\n";

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

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

my %calls;

my @data = split(' ', $n);

foreach (@data) {
	if ($_ != 0) {
		$calls{$_}++;
	}
}

my $total = 0;

foreach (values %calls) {
	if ($_ > 2) {
		print "-1\n";
		exit(0);
	} if ($_ == 2) {
		$total++;
	}
}

print "$total\n";


#!/bin/perl

my ($n) = split /\s/, <>;
my @in = split /\s/, <>;
my @first, @second;
@in = sort {$a <=> $b} @in;
push @in, @in[-1];
for (my $i = 0; $i < $n; ++$i) {
    my $k = @in[$i];
    push @first, $k;
    if (@in[$i] == @in[$i + 1]) {
        push @second, $k;
        while ($i < $n && @in[$i] == @in[$i + 1]) {
            ++$i;
        }
    }
}
@second = reverse @second;
shift @second if @second && @second[0] == @first[-1];
print @first + @second . "\n" . join(" ", (@first, @second)) . "\n";

#!/usr/bin/perl
use strict;
my $m=<STDIN>;
chomp($m);
my $in=<STDIN>;
chomp($in);
my @arr=split(' ',$in);
@arr=sort {$a<=>$b}@arr;
my @output;
@arr=reverse(@arr);
my $a=$b=$arr[0];
push @output,$arr[0];
for my $i (1..$m-1){
	if($arr[$i]<$a){
	unshift @output,$arr[$i];
	$a=$arr[$i];
	}
	elsif($arr[$i]<$b)
	{
	push @output,$arr[$i];
	$b=$arr[$i];
	}
}
my $sz=scalar(@output);
print "$sz\n";
for my $i (0..$sz-1)
{
print "$output[$i] ";
}
print "\n";
exit;

#!/usr/bin/perl

#方針
#多少処理が遅くてもいいので、
#パッと見てわかるプログラムを目指す。
#perl -e '<>;@d=sort{$b<=>$a}split(" ",<>);@v=($d[0]);map{($_<$v[$#v]&&push(@v,$_))or$_<$v[0]&&unshift(@v,$_)}@d;$n=@v;print $n,"\n","@v","\n"'

use strict;
use warnings;

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

my @vals = split " ", $line;

#降順でソート
@vals = sort{$b <=> $a} @vals;

#初期化。一番大きい値だけを持つ配列を作る。
my $val = shift @vals;
my @answer = ($val);

#値を大きい順に検索し、
#右端より小さければ右端に結合、
#左端より小さければ左端に結合を繰り返す。
foreach $val (@vals){
    if($val < $answer[$#answer]){
        push(@answer, $val);
    }elsif($val < $answer[0]){
        unshift(@answer, $val);
    }
}

$num = @answer;
print $num,"\n";
print "@answer\n";

#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;

my $line1 = <>;
my $line2 = <>;
chomp($line1);
chomp($line2);

my $m = $line1;
my @cards = split(/ /, $line2);

my $max_value = 0;
my $res_num = $m;
my @cards_num = ();

foreach (@cards) {
    if (defined($cards_num[$_])) {
        $cards_num[$_]++;
        if ($cards_num[$_] > 2) { $res_num--; }
    } else {
        $cards_num[$_] = 1;
    }
    if ($_ > $max_value) { $max_value = $_; }
}

if ($cards_num[$max_value] > 1) { $res_num--; };
$cards_num[$max_value] = 1;

print "$res_num\n";
$cards_num[$max_value] = 1;
for (my $i = 1; $i <= $max_value; $i++) {
    if (defined($cards_num[$i])) {
        print "$i";
        if ($res_num > 1) {
            print " ";
            $res_num--;
        } else {
            print "\n";
            exit;
        }
        if ($cards_num[$i] == 1) { $cards_num[$i] = (undef); }
    }
}
for (my $i = $max_value; $i >= 1; $i--) {
    if (defined($cards_num[$i])) {
        print "$i";
        if ($res_num > 1) {
            print " ";
            $res_num--;
        } else {
            print "\n";
            exit;
        }
    }
}

#!/bin/perl

my ($n) = split / /, <>;
my @in = split / /, <>;
my @first, @second;
@in = sort @in;
push @in, @in[-1];
for (my $i = 0; $i < $n; ++$i) {
    my $k = @in[$i];
    push @first, $k;
    if (@in[$i] == @in[$i + 1]) {
        push @second, $k;
        while ($i < $n && @in[$i] == @in[$i + 1]) {
            ++$i;
        }
    }
}
@second = reverse @second;
shift @second if @second && @second[0] == @first[-1];
print @first + @second . "\n" . join(" ", (@first, @second)) . "\n";

#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;

my $line1 = <>;
my $line2 = <>;
chomp($line1);
chomp($line2);

my $m = $line1;
my @cards = split(/ /, $line2);

my $max_value = 0;
my $res_num = $m;
my @cards_num = ();

foreach (@cards) {
    if (defined($cards_num[$_])) {
        if ($cards_num[$_] > 2) { $res_num--; }
        else { $cards_num[$_] ++; }
    } else {
        $cards_num[$_] = 1;
    }
    if ($_ > $max_value) { $max_value = $_; }
}
if ($cards_num[$max_value] > 1) {
    $res_num--;
    $cards_num[$max_value] = 1;
}

print "$res_num\n";
for (my $i = 1; $i <= $max_value; $i++) {
    if (defined($cards_num[$i])) {
        print "$i";
        if ($res_num > 1) {
            print " ";
            $res_num--;
        } else {
            print "\n";
            exit;
        }
        if ($cards_num[$i] == 1) { $cards_num[$i] = (undef); }
    }
}
for (my $i = $max_value; $i >= 1; $i--) {
    if (defined($cards_num[$i])) {
        print "$i";
        if ($res_num > 1) {
            print " ";
            $res_num--;
        } else {
            print "\n";
            exit;
        }
    }
}

#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;

my $line1 = <>;
my $line2 = <>;
chomp($line1);
chomp($line2);

my $m = $line1;
my @cards = split(/ /, $line2);

my $max_value = 0;
my $res_num = $m;
my @cards_num = ();

foreach (@cards) {
    if (defined($cards_num[$_])) {
        $cards_num[$_]++;
    } else {
        $cards_num[$_] = 1;
    }
    if ($_ > $max_value) { $max_value = $_; }
}
$res_num = $#cards_num * 2 - 1;

print "$res_num\n";
$cards_num[$max_value] = 1;
for (my $i = 1; $i <= $max_value; $i++) {
    if (defined($cards_num[$i])) {
        print "$i";
        if ($res_num > 1) {
            print " ";
            $res_num--;
        } else {
            print "\n";
            exit;
        }
        if ($cards_num[$i] == 1) { $cards_num[$i] = (undef); }
    }
}
for (my $i = $max_value; $i >= 1; $i--) {
    if (defined($cards_num[$i])) {
        print "$i";
        if ($res_num > 1) {
            print " ";
            $res_num--;
        } else {
            print "\n";
            exit;
        }
    }
}

#!/usr/bin/perl
use strict;
my $m=<STDIN>;
my @arr;
for my $i (0..$m-1)
{
        $arr[$i]=<STDIN>;
       chomp($arr[$i]);
}
@arr=sort {$a<=>$b}@arr;
my @output;
@arr=reverse(@arr);
my $a=$b=$arr[0];
push @output,$arr[0];
for my $i (1..$m-1){
        if($arr[$i]<$a){
        unshift @output,$arr[$i];
        $a=$arr[$i];
        }
        elsif($arr[$i]<$b)
        {
        push @output,$arr[$i];
        $b=$arr[$i];
        }
}
my $sz=scalar(@output);
print "$sz\n";
for my $i (0..$sz-1)
{
print "$output[$i] ";
}
print "\n";
exit;

#!/usr/bin/perl
use strict;
my $m=<STDIN>;
my @arr;
for my $i (0..$m-1)
{
        $arr[$i]=<STDIN>;
       chomp($arr[$i]);
}
@arr=sort {$a<=>$b}@arr;
my @output;
@arr=reverse(@arr);
my $a=$b=$arr[0];
push @output,$arr[0];
for my $i (1..$m-1){
        if($arr[$i]<$a){
        unshift @output,$arr[$i];
        $a=$arr[$i];
        }
        elsif($arr[$i]<$b)
        {
        push @output,$arr[$i];
        $b=$arr[$i];
        }
}
my $sz=@output;
print "$sz\n";
my $sz=@output;
print "$sz\n";
for my $i (0..$#output)
{
print "$output[$i] ";
}
print "\n";
exit;


#!/usr/bin/perl -w

use strict;

chomp(my $n = <>);
my @a = (0, 0);
my @b = (0, 0);
for (1 .. $n) {
    chomp($_ = <>);
    my ($server, $succ, $fail) = split;
    $a[$server - 1] += $succ;
    $b[$server - 1] += 10;
}
for my $i (0 .. 1) {
    my $res = $a[$i] * 2 >= $b[$i] ? 'LIVE' : 'DEAD';
    print $res, "\n";
}


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

while(<>){
	chomp;

	print join ' ', 1, reverse 2 .. $_;
	}


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my $debug = 0;

while( my $n = <> ){
	$debug and print '-' x 15;
	chomp $n;

	my $s = 0;

	@_ = ();
#	my @xhor = ();
#	my @xver = ();

#	for my $i ( 0 .. $n ** 2 - 1 ){
#		push @{ $_[ $i / $n ] }, sprintf "%0${n}b", $s;
#		$xhor[ $i / $n ] ^= $i;
#		$xver[ $i % $n ] ^= $i;
#		$s ++;
#		}

	for my $i ( 0 .. $n ** 2 - 1 ){
	#	push @{ $_[ 4 * int( $i / ( 4 * $n ) ) + $i % 4 ] }, sprintf "%0${n}b", $s;
		push @{ $_[ 4 * int( $i / ( 4 * $n ) ) + $i % 4 ] }, $s;
	#	$xhor[ $i / $n ] ^= $i;
	#	$xver[ $i % $n ] ^= $i;
		$s ++;
		}

#	$debug and print "@{ $_ }" for @_;

#	$debug and print "@xhor";
#	$debug and print join " ", map { sprintf "%0${n}b", $_ } @xhor;
#	$debug and print "@xver";
#	$debug and print join " ", map { sprintf "%0${n}b", $_ } @xver;

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


<>;

$_ = chop( $e = <> );

s/\n\K/chop $e/e, s/$/chop $e/em while $e;

print split

<>;

$_ = chop( $e = <> );

s/\n\K/chop $e/e, s/(?=\n)/chop $e/e while $e;

print split


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

while(<>){
	print do {
		if( m/^E+N$/ || m/^NE+$/ || m/^E+NE+$/ ){
			"NO";
			}
		else{
			"YES";
			}
		}
	}


#!/usr/bin/perl
use v5.10;

chomp($n = <>);
$_==1 and say -1 and exit foreach (split / /, <>);
say 1;

#!/usr/bin/perl
use v5.10;

chomp($n = <>);
$==1 and say -1 and exit foreach (split / /, <>);
say 1;


# http://codeforces.com/problemset/problem/446/A
# http://codeforces.com/blog/entry/13036

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

my @left, @right;
my $cnt = 1;
$left[0] = 1;

for ( my $i = 1; $i < @seq; $i += 1 ) {
    if ( $seq[ $i-1 ] < $seq[ $i ] ) {
	$cnt += 1;
	$left[ $i ] = $cnt; }
    else {
	$cnt = 1;
	$left[ $i ] = $cnt; }}

$right[ @seq-1 ] = 1;
$cnt = 1;
for ( my $i = @seq-2; $i >= 0; $i -= 1 ) {
    if ( $seq[ $i ] < $seq[ $i+1 ] ) {
	$cnt += 1;
	$right[ $i ] = $cnt; }
    else {
	$cnt = 1;
	$right[ $i ] = $cnt; }}

my $mx = 0;
for ( my $i = 0; $i < @seq-2; $i += 1 ) {
    if ( $seq[ $i ] + 1 < $seq[ $i+2 ] ) {
	if ( $left[ $i ] + 1 + $right[ $i+2 ] > $mx ) {
	    $mx = $left[ $i ] + 1 + $right[ $i+2 ]; }}}

my $ww = 0;
for (@left) {
    if ( $_ > $ww ) {
	$ww = $_; }}
if ( $ww < @left ) {
    $ww += 1; }
if ( $ww > $mx ) { $mx = $ww; }

if ( $n < 3 ) { print "$n\n"; }
else {
print $mx;
print "\n"; }

# http://codeforces.com/problemset/problem/446/A
# http://codeforces.com/blog/entry/13036

my $n = <STDIN>;
my $f = <STDIN>;
chomp ( $n, $f );
my @seq = split m/ /, $f;
my @left, @right;
my $cnt = 1;
$left[0] = 1;

for ( my $i = 1; $i < @seq; $i += 1 ) {
    if ( $seq[ $i-1 ] < $seq[ $i ] ) {
	$cnt += 1;
	$left[ $i ] = $cnt; }
    else {
	$cnt = 1;
	$left[ $i ] = $cnt; }}

$right[ @seq-1 ] = 1;
$cnt = 1;
for ( my $i = @seq-2; $i >= 0; $i -= 1 ) {
    if ( $seq[ $i ] < $seq[ $i+1 ] ) {
	$cnt += 1;
	$right[ $i ] = $cnt; }
    else {
	$cnt = 1;
	$right[ $i ] = $cnt; }}

my $mx = 0;
for ( my $i = 0; $i < @seq-2; $i += 1 ) {
    if ( $seq[ $i ] + 1 < $seq[ $i+2 ] ) {
	if ( $left[ $i ] + 1 + $right[ $i ] > $mx ) {
	    $mx = $left[ $i ] + 1 + $right[ $i ]; }}}

if ( $n < 3 ) { print "$n\n"; }
else {
print $mx;
print "\n"; }


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

my $debug = 0;

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

	my $n = $_;

	my $length = 2;

	my @re = split ' ', join '', map ~~<>, 1 .. $length;

	$debug and print "\$n: $n, \@re: @re";

	my $letters = join '', 'a' .. 'c';

	my $prependent = ',';

	my $str = join '', map "${prependent}$_", ( $letters ) x $length;

	$debug and print $str;

	my $match_one_letter = "${prependent}.*([^$prependent]).*";

	my $match_letters = $match_one_letter x $length;

	$debug and print "$match_letters";

	my %strings;

	$str =~ /
		$match_letters
		(?{ $strings{ $1 . $2 } ++ })
		(*F)
		/x;

	/(.).*\1/ and delete $strings{ $_ } for sort keys %strings;

	$debug and print join ' ', sort keys %strings;

	my %variants;

	for my $string ( sort keys %strings ){
		my( $third_letter ) = $letters =~ s/[$string]//gr;
		$variants{ join '', map { $_ x $n } $string, $third_letter } ++;
		$variants{ join '', map { $_ x $n } $third_letter, $string } ++;
		$variants{ scalar( $string . $third_letter ) x $n } ++;
		}

	$debug and print for sort keys %variants;

	my $find;

	for my $variant ( sort keys %variants ){
		my $ok = 1;
		for my $re ( @re ){
			$variant =~ /$re/ and $ok = 0;
			}

		last if $ok and $find = $variant;
		}

	print defined $find ? "YES\n$find" : "NO";
	}


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

while(<>){
	@_ = map ~~<>, 1 .. 8;

	if( grep m/^R+$/, @_ ){
		print 'R';
		}
	else{
		print 'B';
		}
	}


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;
my $debug = 0;

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

	my @A = map { [ split //, <> =~ s/\n//r ] } 1 .. $n;

	$debug and print "@{$_}" for @A;

	my $sx;
	my $sy;

	OUT:
	for my $i ( 0 .. $n - 1 ){
		for my $j ( 0 .. $m - 1 ){
			$A[ $i ][ $j ] eq 'S' and do {
				$sx = $i; $sy = $j;
				$A[ $i ][ $j ] = '.';
				last OUT;
				};
			}
		}

	$debug and print '-' x 5;
	$debug and print "@{$_}" for @A;
	$debug and print "[$sx|$sy]";

	my @dirs = split //, <> =~ s/\n//r;

	$debug and print "[@dirs]";

	my @v = qw( U D L R );
	my $vars = 0;

	for my $i1 ( @v ){
		for my $i2 ( @v ){
			for my $i3 ( @v ){
				for my $i4 ( @v ){
					next if "$i1$i2$i3$i4" =~ /(.).*\1/;
					my @i = split //, "$i1$i2$i3$i4";

					my $ssx = $sx;
					my $ssy = $sy;

					my $fail = 0;

					for my $j ( @dirs ){
						my( $dx, $dy ) = ( 0, 0 );
						$i[ $j ] eq 'D' and $dy += 1;
						$i[ $j ] eq 'U' and $dy -= 1;
						$i[ $j ] eq 'L' and $dx -= 1;
						$i[ $j ] eq 'R' and $dx += 1;

						$ssx += $dx;
						$ssy += $dy;

						if( $ssx < 0 || $ssx >= $n || $ssy < 0 || $ssy >= $m ||
							$A[ $ssx ][ $ssy ] eq '#'
							){
							$fail = 1;
							last;
							}

						if( $A[ $ssx ][ $ssy ] eq 'E' ){
							$vars ++;
							last;
							}
						}
					}
				}
			}
		}

	print $vars;

	$debug and print '-' x 20;
	}


$s.=tr/aeiou// while <>;
print "575" eq $s?"YES":"NO"

#!/usr/bin/env perl

use strict;
use warnings;

my $line1 = <>;
my $line2 = <>;
my $line3 = <>;

$line1 =~ s/[^aeiou]//g;
$line2 =~ s/[^aeiou]//g;
$line3 =~ s/[^aeiou]//g;

printf "%s\n", ((length($line1) == 5 &&
                 length($line2) == 7 &&
                 length($line3) == 5) ? 'YES' : 'NO');

sub occ{
@arr=@_;
@one=grep{$_ eq "a" or $_ eq "i" or $_ eq "u" or $_ eq "o" or $_ eq "e" }@arr;
$size=@one;
return $size;
}
@arr=split('',<>);
@arr1=split('',<>);
@arr2=split('',<>);
if(occ(@arr)==5 and occ(@arr1)==7 and occ(@arr2)==5){
print "YES";
}
else{
print "NO";
}

while(<>) {
  chomp;
  $s.=$_;
}
print $s=~tr/aeiou// == 17?"YES":"NO"


use strict;
open STDIN, '<input.txt';
open STDOUT, '>output.txt';
my $n = <>;
my $m = <>;
my @a;
for(my $i = 0; $i < $n; $i++){
   $a[$i] = <>;
}
@a = sort @a;
my $ans = 0;
my $j = $#a;

while($m > 0 && $j >= 0){
  $m -= $a[$j];
  $ans++;
  $j--;
}
print $ans;


chomp (my $n = <STDIN>);

my ($x, $y, $xp, $yp);

chomp (($xp, $yp) = split /\s+/, <STDIN>);
chomp (($xp, $yp) = split /\s+/, <STDIN>);

my $prevDir = 'N';
my $curDir = 'N';

my $ans = 0;
for (my $i = 1; $i < $n; $i++) {
  chomp (($x, $y) = split /\s+/, <STDIN>);

  $curDir = 'N' if ($y > $yp);
  $curDir = 'E' if ($x > $xp);
  $curDir = 'W' if ($x < $xp);
  $curDir = 'S' if ($y < $yp);

  $ans++ if (
    (($prevDir eq 'N') && ($curDir eq 'W')) ||
    (($prevDir eq 'E') && ($curDir eq 'N')) ||
    (($prevDir eq 'W') && ($curDir eq 'S')) ||
    (($prevDir eq 'S') && ($curDir eq 'E'))
  );

  $prevDir = $curDir;
  ($xp, $yp) = ($x, $y);
}

printf "$ans\n";


#!/usr/bin/env perl

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

my ($n, $t) = split q{ }, <STDIN>;
my $idx;

my @time_and_gap = ( (undef)x$n );
for ( @time_and_gap ) {
    $_ = [ split q{ }, <STDIN> ];
}

my $time;
my $gap;
my $s;
my $mod;
my $div;
my $min = 1000000;
my $min_i = 101;
my $i = 0;
for ( @time_and_gap ) {
    $i++;
    $time = $_->[0];
    $gap = $_->[1];

    if ( $time < $t ) {
        $s = $t - $time;
        $mod = $s % $gap;
        $div = int ( $s/$gap );
        if ( $mod ) {
            $div++;
        }
        $time += $div * $gap;
    }

    if ( $time < $min ) {
        $min = $time;
        $min_i = $i;
    }

}

say $min_i;

#!/usr/bin/env perl

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

my ($n, $t) = split q{ }, <STDIN>;
my $idx;

my @time_and_gap = ( (undef)x$n );
for ( @time_and_gap ) {
    $_ = [ split q{ }, <STDIN> ];
}

my $time;
my $gap;
my $s;
my $div;
my $min = 1000000;
my $min_i = 101;
my $i = 0;
for ( @time_and_gap ) {
    $i++;
    $time = $_->[0];
    $gap = $_->[1];

    if ( $time < $t ) {
        $s = $t - $time;
        $div = int ( $s/$gap );
        $time += $div * $gap;
    }
    if ( $time < $t ) {
        $time += $gap;
    }
    if ( $time < $t ) {
        next;
    }

    if ( $time < $min ) {
        $min = $time;
        $min_i = $i;
    }

}

say $min_i;

#!/usr/bin/env perl

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

my ($n, $t) = split q{ }, <STDIN>;
my $idx;

my @time_and_gap = ( (undef)x$n );
for ( @time_and_gap ) {
    $_ = [ split q{ }, <STDIN> ];
}

my $time;
my $gap;
my $s;
my $div;
my $min = 1000000;
my $min_i = 101;
my $i = 0;
for ( @time_and_gap ) {
    $i++;
    $time = $_->[0];
    $gap = $_->[1];

    while ( $time < $t ) {
        $time += $gap;
    }

    if ( $time < $min ) {
        $min = $time;
        $min_i = $i;
    }

}

say $min_i;

#!/usr/bin/env perl

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

my ($n, $t) = split q{ }, <STDIN>;

my @time_and_gap = ( (undef)x$n );
for ( @time_and_gap ) {
    $_ = [ split q{ }, <STDIN> ];
}

sub canTake {
    my $t = $_[0];
    my $idx = 1;
    for ( @time_and_gap ) {
        $_->[0] += $_->[1] if $_->[0] < $t;
        if ( $_->[0] == $t ) {
            return $idx;
        }
        $idx++;
    }
    canTake($t+1);
}

say canTake($t);

#!/usr/bin/env perl

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

my ($n, $t) = split q{ }, <STDIN>;

my @time_and_gap = ( (undef)x$n );
for ( @time_and_gap ) {
    $_ = [ split q{ }, <STDIN> ];
}

sub canTake {
    my $t = $_[0];
    my $idx = 1;
    for ( @time_and_gap ) {
        $_->[0] += $_->[1] if $_->[0] < $t;
        if ( $_->[0] == $t ) {
            say $t;
            say $_->[0];
            return $idx;
        }
        $idx++;
    }
    canTake($t+1);
}

say canTake($t);


$t = <STDIN>;
chomp $t;
@test = <STDIN>;
chomp @test;

foreach (@test) {
    #print $_;
$loop = $_/2;
@coins = ();
$coins[0] = 2;
for ($i=1;$i<$_;$i = $i + 1) {
    push (@coins, $coins[$i-1]*2);
}
#foreach $coin (@coins) {
#    print "\n $coin";
#}
#print $loop;
$sum1 = 0;
$sum2 = 0;
if ($loop == 1) {
    $ans = $coins[1]-$coins[0];
    print "$ans\n";
}else{
    for ($j=0;$j<=$loop-2;$j=$j+1) {
        $sum1 = $sum1  + $coins[$j];
    }
    $sum1 = $sum1 + $coins[$_-1];
        for ($k=$loop-1;$j<=$_-2;$j=$j+1) {
        $sum2 = $sum2  + $coins[$j];
    }
    $ans = $sum1 - $sum2;
    #print "$sum1 $sum2";
    print "$ans\n";
}
}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

while(<>){
	chomp;

	my $left = 0;

	$left += $_ for map { 2 ** $_ } 1 .. $_ / 2 - 1, $_;

	my $right = 0;

	$right += $_ for map { 2 ** $_ } $_ / 2 .. $_ - 1;

	print $left - $right;
	}


chomp($_ = <STDIN>);
@nums = split / /;
$x = $nums[1];
chomp($_ = <STDIN>);
@nums = split / /;
@nums = sort {$a <=> $b} @nums;
$r = 0;
foreach $n (@nums) {
	$r += $x*$n;
	if ($x > 1) {
		$x--;
	}
}
print $r;


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

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

	my @L;
	my @N;

	for( @_ ){
		if( $_ % 2 == 0 ){
			push @L, $_;
			}
		else{
			push @N, $_;
			}
		}

	@L = sort { $b <=> $a } @L;
	@N = sort { $b <=> $a } @N;

	my $A = 0;
	my $B = 0;

	my $move = 0;

	while( @L or @N ){

		if( 0 ){ ; }
		elsif( $move == 0 ){
			if( @L and @N ){
				if( $L[ 0 ] > $N[ 0 ] ){
					$A += shift @L;
					}
				else{
					shift @N;
					}
				}
			elsif( @L ){
				$A += shift @L;
				}
			elsif( @N ){
				shift @N;
				}
			}
		elsif( $move == 1 ){
			if( @L and @N ){
				if( $L[ 0 ] < $N[ 0 ] ){
					$B += shift @N;
					}
				else{
					shift @L;
					}
				}
			elsif( @L ){
				shift @L;
				}
			elsif( @N ){
				$B += shift @N;
				}
			}

		$move = 1 - $move;
		}

	print qw( Tie Alice Bob )[ $A <=> $B ];
	}


use 5.016;
use strict;
use warnings;

sub min {
  my $min = shift;
  for my $x (@_) {
    $min = $x if $x < $min;
  }
  $min;
}

my %last;
my %max_dist;

chomp (my $s = <>);
for my $i (0..length($s) - 1) {
  my $c = substr $s, $i, 1;

  my $dist = $i - ($last{$c} // -1);
  $max_dist{$c} = $dist if !exists $max_dist{$c} || $dist > $max_dist{$c};

  $last{$c} = $i;
}

for my $c (keys %max_dist) {
  my $dist = length($s) - $last{$c};
  $max_dist{$c} = $dist if $dist > $max_dist{$c};
}

say min(values %max_dist);

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(<>){
	chomp;
	@_ = ();

	for my $c ( 'a' .. 'z' ){

		/$c/ and push @_, 1 + ( sort { $b <=> $a } map length, grep $_, split $c )[ 0 ];
		}

	print 0 + ( sort { $a <=> $b } @_ )[ 0 ];
	}

$_ = <>, chomp;

$m = ~0;

for $c ( 'a' .. 'z' ){
	( $q ) = sort { $b <=> $a } map length, split $c;
	$q < $m and $m = $q;
	}

print 1 + $m

# read input
my $str = <STDIN>;
chomp ( $str );

# insert each character into an index in the array
my @arr = split m//, $str;

# insert the keys and initial values into the hashes
my %prev;
my %max;
for (@arr) {
    $prev{ $_ } = $max{ $_ } = -1; }

# iterate through the string
for ( my $j = 0; $j < (@arr); $j += 1 ) {
    if ( $j - $prev{ $arr[ $j ] } > $max{ $arr[ $j ] } ) {
	$max{ $arr[ $j ] } = $j - $prev{ $arr[ $j ] };}
    $prev{ $arr[ $j ] } = $j;}

# don't forget the last substrings
for ( my $i = 0; $i < (@arr); $i += 1 ) {
    if ( (@arr) - $prev{ $arr[ $i ] } > $max{ $arr[ $i ] } ) {
	$max{ $arr[ $i ] } = @arr - $prev{ $arr[ $i ] }; }}

# find the minimum maximum value
my $min = 100000000;
for (@arr) {
    if ( $max{ $_ } < $min && $max{ $_ } > 0 ) {
	$min = $max{ $_ }; }}

# print answer
print "$min\n";

# read input
my $str = <STDIN>;
chomp ( $str );

# insert each character into an index in the array
my @arr = split m//, $str;

# insert the keys and initial values into the hashes
my %prev;
my %max;
for (@arr) {
    $prev{ $_ } = $max{ $_ } = -1; }

# iterate through the string
for ( my $j = 0; $j < (@arr); $j += 1 ) {
    if ( $j - $prev{ $arr[ $j ] } > $max{ $arr[ $j ] } ) {
	$max{ $arr[ $j ] } = $j - $prev{ $arr[ $j ] };}
    $prev{ $arr[ $j ] } = $j;}

# don't forget the last substrings
for ( my $i = 0; $i < (@arr); $i += 1 ) {
    if ( (@arr-1) - $prev{ $arr[ $i ] } > $max{ $arr[ $i ] } ) {
	$max{ $arr[ $i ] } = @arr - 1 - $prev{ $arr[ $i ] }; }}

# find the minimum maximum value
my $min = 100000000;
for (@arr) {
    if ( $max{ $_ } < $min && $max{ $_ } > 0 ) {
	$min = $max{ $_ }; }}

# print answer
print "$min\n";

use 5.016;
use strict;
use warnings;

sub min {
  my $min = shift;
  for my $x (@_) {
    $min = $x if $x < $min;
  }
  $min;
}

my %last;
my %max_dist;

chomp (my $s = <>);
for my $i (0..length($s) - 1) {
  my $c = substr $s, $i, 1;

  my $dist = $i - ($last{$c} // 0);
  $max_dist{$c} = $dist if !exists $max_dist{$c} || $dist > $max_dist{$c};

  $last{$c} = $i;
}

for my $c (keys %max_dist) {
  my $dist = length($s) - $last{$c};
  $max_dist{$c} = $dist if $dist > $max_dist{$c};
}

say min(values %max_dist);


use bigint;
chomp(my $t = <STDIN>);
foreach (1..$t)
{
    chomp (my $n = <STDIN>);
    my $c = ($n + 3) / 4;
    print '9' x ($n - $c) . '8' x $c, "\n";
}

use strict;

my $try = <>;

while($try--)
{
	my $n = <>;
	for(my $i = 0; $i < $n-1; $i++){
		print "9";
        }
	print "8\n";
}


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(!eof){
	my $left = 1e9;
	my $right = 0;
	my $left_2 = 1e9;
	my $right_2 = 0;
	my $n = <>;
	for my $i (1 .. $n){
		my( $l, $r )= split ' ', <>;
		$r < $left and $left = $r;
		$l > $right_2 and $right_2 = $l;
		}
	my $m = <>;
	for my $i (1 .. $m){
		my( $l, $r )= split ' ', <>;
		$r < $left_2 and $left_2 = $r;
		$l > $right and $right = $l;
		}

	print +( sort { $a <=> $b } 0, $right - $left, $right_2 - $left_2 )[ 2 ];

	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(!eof){
	my $left = 3e5;
	my $right = 0;
	my $left_2 = 3e5;
	my $right_2 = 0;
	my $n = <>;
	for my $i (1 .. $n){
		my( $l, $r )= split ' ', <>;
		$r < $left and $left = $r;
		$l > $right_2 and $right_2 = $l;
		}
	my $m = <>;
	for my $i (1 .. $m){
		my( $l, $r )= split ' ', <>;
		$r < $left_2 and $left_2 = $r;
		$l > $right and $right = $l;
		}

	print +( sort { $a <=> $b } 0, $right - $left, $right_2 - $left_2 )[ 2 ];

	}

#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

while(!eof){
	my $left = 3e5;
	my $right = 0;
	my $n = <>;
	for my $i (1 .. $n){
		my $r = ( split ' ', <> )[ 1 ];
		$r < $left and $left = $r;
		}
	my $m = <>;
	for my $i (1 .. $m){
		my $l = ( split ' ', <> )[ 0 ];
		$l > $right and $right = $l;
		}

	print +( sort { $a <=> $b } 0, $right - $left )[ 1 ];

	}


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

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

	@_ = map { eval join '/', split } map ~~<>, 1 .. $n;

	print $m * ( sort { $a <=> $b } @_ )[ 0 ];
	}

print +( split )[ 1 ] * ( sort { $a <=> $b } map { eval s! !/!r } <> )[ 0 ] while <>

print +( split ) * ( sort { $a <=> $b } map { eval s! !/!r } <> )[ 0 ] for <>

print +( split ) * ( sort { $b <=> $a } map { eval s! !/!r } <> ) while <>


use strict;
use warnings;
my $totalNum =  <STDIN>;
my %finResults;
my @hisResults;

my @inputs;

#calc final score and keep historic record.
for(my $i = 0; $i < $totalNum; $i++){
    my $input = <STDIN>;
    @inputs = split( " ", $input);
    $finResults{$inputs[0]} += $inputs[1];
    my @record = ($inputs[0], $finResults{$inputs[0]} );
    push @hisResults, \@record;
}
#Sort based on final score to find candidate winners.
my @finals = sort {$finResults{$a} <=> $finResults{$b} } keys %finResults;
if (scalar @finals == 1){  #if there is only one candidate, he is the winner.
    return $finals[0];
}
my $finalScore = $finResults{ $finals[scalar @finals - 1] };
my %candidateWiners;
$candidateWiners{$finals [scalar @finals - 1]} = 1;
for(my $i = scalar @finals -2; $i >= 0; $i--){
    if($finResults{ $finals[$i] } < $finalScore){
        last;
    }
    else{
        $candidateWiners{$finals[$i] } = 1;
    }
}

#Check the historic data to find the final winner.
foreach my $hisRes (@hisResults){
    if ($hisRes->[1] >= $finalScore &&  exists($candidateWiners{$hisRes->[0]})){
        print $hisRes->[0];
        last;
    }
}

$n=<STDIN>;
%hash;
@list;
$i=0;
$max=-99999999999999;
$winner;
while($n--)
{
        $tmp=<STDIN>;
        @tmp2=split(" ",$tmp);
        $name=$tmp2[0]; $score=$tmp2[1];
        if($hash{$name})
        {
            $hash{$name}+=$score;
        }
        else
        {
            $hash{$name}=$score;
        }
        $list[$i++]=$name;
        $list[$i++]=$hash{$name};
}
#print $max,"\n";
$flag=1;
foreach $key (%hash)
        {
    #        print $hash{$key},"\n";
            if($hash{$key}>$max)
            {
                $max=$hash{$key};
                $winner=$key;
            }
        }

for($j=0;$j<$i && $flag;$j+=2)
{
    #print "$list[$j] $list[$j+1] $hash{$list[$j]} $max\n";
    if($list[$j+1]>=$max && $hash{$list[$j]}==$max && $flag)
    {
        print $list[$j];
        $flag=0;
    }
}

use strict;
use warnings;

use Data::Dumper;

my $totalNum =  <STDIN>;
my %finResults;
my @hisResults;

my @inputs;
for(my $i = 0; $i < $totalNum; $i++){
    my $input = <STDIN>;
    @inputs = split( " ", $input);
    $finResults{$inputs[0]} += $inputs[1];
    my @record = ($inputs[0], $finResults{$inputs[0]} );
    push @hisResults, \@record;
#    if (! exists $hisResults{$inputs[0]}){
#        $hisResults{$inputs[0]} = [$inputs[1]];
#    }
#    else{
#        my $arrayRef = $hisResults{$inputs[0]};
#        if($$arrayRef[$#$arrayRef] < $finResults{$inputs[0]} ){
#            push @$arrayRef, $finResults{$inputs[0]};
#        }
#
#    }
}

my @finals = sort {$finResults{$a} <=> $finResults{$b} } keys %finResults;

if (scalar @finals == 1){
    return $finals[0];
}
my $finalScore = $finResults{ $finals[scalar @finals - 1] };
my %candidateWiners;
$candidateWiners{$finals [scalar @finals - 1]} = 1;
for(my $i = scalar @finals -2; $i >= 0; $i--){
    if($finResults{ $finals[$i] } < $finalScore){
#        print "UUUUUUUUUU: $i,  NNNNN: $finResults{ $finals[$i]}\n";
        last;
    }
    else{
        $candidateWiners{$finals[$i] } = 1;
    }
}
#print Dumper \@finals ;
#
#
#
#print Dumper \%candidateWiners;
#print "HHHHHH: finalScore $finalScore \n";
#print Dumper \%finResults ;
#print Dumper \@hisResults;

foreach my $hisRes (@hisResults){
    if ($hisRes->[1] >= $finalScore &&  exists($candidateWiners{$hisRes->[0]})){
        print $hisRes->[0];
        last;
    }
}

#foreach my $candidate (sort {$finResults{$a} cmp $finResults{$b} }
#           keys %finResults)
#{
#     print "$candidate $finResults{$candidate} \n";
#}

#print Dumper \%hisResults;

my $n = <>;
my (@name, @point);
my @table = [];
my ($ans, $max) = (0, 0);
my %hash;
for my $i (1..$n) {
    ($name[$i], $point[$i]) = split " ", <>;
    $hash{$name[$i]} = 0 if(not exists $hash{$name[$i]});
    $hash{$name[$i]} += $point[$i];
    push @table, [$name[$i], $hash{$name[$i]}];
}
$max = $_ > $max ? $_ : $max foreach(values %hash);
# print "\n-----------\n";
for my $i (1..$n) {
    if ($hash{$name[$i]} == $max && $table[$i][1] >= $max) {
        print $name[$i] ."\n";
        last;
    }
}

use strict;
use warnings;

my $players = {};
my $score_log = [];

my $n_inputs = <>;

for ( 1 .. $n_inputs ) {
  my $line = <>;
  my ( $name, $score, @rest ) = split /\s+/s, $line;

  if ( not exists $players->{$name} ) {
    $players->{$name} = 0;
  }
  my $new_score = $players->{$name} + $score;
  push @{ $score_log }, { name => $name, score => $new_score };
  $players->{ $name } = $new_score;
}
my %winning_players;
my ( $winning_score, ) = sort { $b <=> $a } values %{ $players };
for ( grep { $winning_score == $players->{$_} } keys %{ $players } ) {
  $winning_players{ $_ } = 1;
}
for my $entry ( @{ $score_log } ) {
  next unless exists $winning_players{ $entry->{name} };
  next unless $entry->{score} >= $winning_score;
  printf "%s\n", $entry->{name};
  exit 0;
}

#!/usr/bin/perl

use strict;
use warnings;

my $n = <>;
chomp $n;

my @round;
my %score;

for (my $i = 0; $i < $n; $i++) {
    my $line = <>;
    chomp $line;
    push @round, $line;
    my @tuple = split ' ', $round[$i];
    if (not defined($score{$tuple[0]})) {
        $score{$tuple[0]} = $tuple[1];
    } else {
        $score{$tuple[0]} += $tuple[1];
    }
}

my $max = 0;
foreach (keys(%score)) {
    $max = $score{$_} if ($score{$_} > $max);
}

my %t;
for (my $i = 0; $i < $n; $i++) {
    my @tuple = split ' ', $round[$i];
    if (not defined($t{$tuple[0]})) {
        $t{$tuple[0]} = $tuple[1];
    } else {
        $t{$tuple[0]} += $tuple[1];
    }

    if ($score{$tuple[0]} == $max and $t{$tuple[0]} >= $max) {
        print $tuple[0];
        last;
    }
}

use strict;
use warnings;

my $n = <>;
my $name;
my $point;
my $ans;
my $max = 0;
my %hash;
for (1..$n) {
    ($name, $point) = split " ", <>;
    if (exists $hash{$name}) {
        $hash{$name} += $point;
    }
    else {
        $hash{$name} = $point;
    }
    if ($hash{$name} > $max) {
        $max = $hash{$name};
        $ans = $name;
    }
}
print $ans . "\n";

my $n = <>;
my (@name, @point);
my @table = [];
my ($ans, $max) = (0, 0);
my %hash;
for my $i (1..$n) {
    ($name[$i], $point[$i]) = split " ", <>;
    $hash{$name[$i]} = 0 if(not exists $hash{$name[$i]});
    $hash{$name[$i]} += $point[$i];
    push @table, [$name[$i], $point[$i]];
}
$max = $_ > $max ? $_ : $max foreach(values %hash);
# print "\n-----------\n";
for my $i (1..$n) {
    if ($hash{$name[$i]} == $max && $table[$i][1] >= $max) {
        print $name[$i] ."\n";
        last;
    }
}

my $n = <>;
my @name;
my @point;
my $ans;
my $max = 0;
my %hash;
for my $i (1..$n) {
    ($name[$i], $point[$i]) = split " ", <>;
    next if ($point[$i] < 0);
    $hash{$name[$i]} = 0 if(not exists $hash{$name[$i]});
    $hash{$name[$i]} += $point[$i];
    $max = $hash{$name[$i]} if ($hash{$name[$i]} > $max);
}
for my $i (1..$n) {
    if ($point[$i] >= $max) {
        print $name[$i] ."\n";
        last;
    }
}

my $n = <>;
my @name;
my @point;
my $ans;
my $max = 0;
my %hash;
my %table;
for my $i (1..$n) {
    ($name[$i], $point[$i]) = split " ", <>;
    $hash{$name[$i]} = 0 if(not exists $hash{$name[$i]});
    $hash{$name[$i]} += $point[$i];
}
$max = $_ > $max ? $_ : $max foreach(values %hash);
# print "\n-----------\n";
for my $i (1..$n) {
    next if ($point[$i] < 0);
    $table{$name[$i]} = 0 if(not exists $table{$name[$i]});
    $table{$name[$i]} += $point[$i];
    # print "$hash{$name[$i]} $table{$name[$i]}\n";
    if ($hash{$name[$i]} == $max && $table{$name[$i]} >= $max) {
        print $name[$i] ."\n";
        last;
    }
}

my $n = <>;
my $name;
my $point;
my $ans;
my $max = 0;
my %hash;
for (1..$n) {
    ($name, $point) = split " ", <>;
    if ($point < 0) {
        $hash{$name} = -1000;
        next;
    }
    if (exists $hash{$name} and $hash{$name} > 0) {
        $hash{$name} += $point;
    }
    else {
        $hash{$name} = $point;
    }
    if ($hash{$name} > $max) {
        $max = $hash{$name};
        $ans = $name;
    }
}
print $ans . "\n";

my $n = <>;
my @name;
my @point;
my $ans;
my $max = 0;
my %hash;
for my $i (1..$n) {
    ($name[$i], $point[$i]) = split " ", <>;
    next if ($point[$i] < 0);
    $hash{$name[$i]} = 0 if(not exists $hash{$name[$i]});
    $hash{$name[$i]} += $point[$i];
    $max = $hash{$name[$i]} if ($hash{$name[$i]} > $max);
}

undef %hash;
for my $i (1..$n) {
    next if ($point[$i] < 0);
    $hash{$name[$i]} = 0 if(not exists $hash{$name[$i]});
    $hash{$name[$i]} += $point[$i];
    if ($hash{$name[$i]} >= $max) {
        print $name[$i] ."\n";
        last;
    }
}

use strict;
use warnings;

my $players = {};
my $score_log = [];

my $n_inputs = <>;

for ( 1 .. $n_inputs ) {
  my $line = <>;
  my ( $name, $score, @rest ) = split /\s+/s, $line;

  if ( not exists $players->{$name} ) {
    $players->{$name} = 0;
  }
  my $new_score = $players->{$name} + $score;
  push @{ $score_log }, { name => $name, score => $new_score };
  $players->{ $name } = $new_score;
}
my %winning_players;
my ( $winning_score, ) = sort { $b <=> $a } values %{ $players };
for ( grep { $winning_score == $players->{$_} } keys %{ $players } ) {
  $winning_players{ $_ } = 1;
}
for my $entry ( @{ $score_log } ) {
  next unless exists $winning_players{ $entry->{name} };
  next unless $entry->{score} > $winning_score;
  printf "%s\n", $entry->{name};
  exit 0;
}

use strict;
use warnings;

my $scores = {};
my $players = {};

my $n_inputs = <>;

for ( 1 .. $n_inputs ) {
  my $line = <>;
  my ( $name, $score, @rest ) = split /\s+/s, $line;

  if ( not exists $players->{$name} ) {
    $players->{$name} = 0;
  }
  my $new_score = $players->{$name} + $score;
  $scores->{ $new_score } = $name unless exists $scores->{ $new_score };
  $players->{ $name } = $new_score;
}
my ( $winning_score, ) = sort { $b <=> $a } values %{ $players };

printf "%s\n", $scores->{ $winning_score };

$n=<STDIN>;
%hash;
$max=0;
$winner;
while($n--)
{
        $tmp=<STDIN>;
        @tmp2=split(" ",$tmp);
        $name=$tmp2[0]; $score=$tmp2[1];
        $hash{$name}+=$score;
      #  print "# $max\n";
        if($max<$hash{$name}){
     #       print "2 $name\n";
            $max=$hash{$name};
    #        print "$max\n";
            $winner=$name;
        }
}
print $winner;

$n=<STDIN>;
%hash;
$max=-99999999999999;
$winner;
while($n--)
{
        $tmp=<STDIN>;
        @tmp2=split(" ",$tmp);
        $name=$tmp2[0]; $score=$tmp2[1];
        if($hash{$name})
        {
            $hash{$name}+=$score;
        }
        else
        {
            $hash{$name}=$score;
        }
}
#print $max,"\n";
$flag=1;
foreach $key (%hash)
        {
  #          print $hash{$key},"\n";
            if($hash{$key}>$max)
            {
                $max=$hash{$key};
                $winner=$key;
            }
        }
print $winner;

$n=<STDIN>;
%hash;
$max=-99999999999999;
$winner;
while($n--)
{
        $tmp=<STDIN>;
        @tmp2=split(" ",$tmp);
        $name=$tmp2[0]; $score=$tmp2[1];
        if($hash{$name})
        {
            $hash{$name}+=$score;
        }
        else
        {
            $hash{$name}=$score;
        }
      #  print "# $max\n";
        if($max<$hash{$name}){
     #       print "2 $name\n";
            $max=$hash{$name};
    #        print "$max\n";
            $winner=$name;
        }
}
print $winner;

$n=<STDIN>;
%hash;
@list;
$i=0;
$max=-99999999999999;
$winner;
while($n--)
{
        $tmp=<STDIN>;
        @tmp2=split(" ",$tmp);
        $name=$tmp2[0]; $score=$tmp2[1];
        if($hash{$name})
        {
            $hash{$name}+=$score;
        }
        else
        {
            $hash{$name}=$score;
        }
        $list[$i++]=$name;
        $list[$i++]=$hash{$name};
}
#print $max,"\n";
$flag=1;
foreach $key (%hash)
        {
    #        print $hash{$key},"\n";
            if($hash{$key}>$max)
            {
                $max=$hash{$key};
                $winner=$key;
            }
        }

for($j=0;$j<$i;$j+=2)
{
   # print "$list[$j] $list[$j+1] $max\n";
    if($list[$j+1]==$max && $flag)
    {
        print $list[$j];
        $flag=0;
    }
}

$n=<STDIN>;
%hash;
$max=-99999999999999;
$winner;
while($n--)
{
        $tmp=<STDIN>;
        @tmp2=split(" ",$tmp);
        $name=$tmp2[0]; $score=$tmp2[1];
        $hash{$name}+=$score;
      #  print "# $max\n";
        if($max<$hash{$name}){
     #       print "2 $name\n";
            $max=$hash{$name};
    #        print "$max\n";
            $winner=$name;
        }
}
print $winner;

$n=<STDIN>;
%hash;
$max=-99999999999999;
$winner;
while($n--)
{
        $tmp=<STDIN>;
        @tmp2=split(" ",$tmp);
        $name=$tmp2[0]; $score=$tmp2[1];
        if($hash{$name})
        {
            $hash{$name}+=$score;
        }
        else
        {
            $hash{$name}=$score;
        }
      #  print "# $max\n";
        if($max<$hash{$name}){
     #       print "2 $name\n";
            $max=$hash{$name};
    #        print "$max\n";
            $winner=$name;
        }
}
$flag=1;
foreach $key (%hash)
        {
            if($hash{$key}>=$max && $key==$winner && $flag)
            {
                print $key,"\n";
                $flag=0;
            }
        }
#print $winner;

use strict;
use warnings;
use Data::Dumper;
my $totalNum =  <STDIN>;
my %finResults;
my @hisResults;
my @inputs;
for(my $i = 0; $i < $totalNum; $i++){
    my $input = <STDIN>;
    @inputs = split( " ", $input);
    $finResults{$inputs[0]} += $inputs[1];
    my @record = ($inputs[0], $finResults{$inputs[0]} );
    push @hisResults, \@record;
}
my @finals = sort {$a <=> $b} values %finResults;
my $finalScore = $finals[scalar @finals - 1];
foreach my $hisRes (@hisResults){
    if ($hisRes->[1] >= $finalScore){
        print $hisRes->[0];
        last;
    }
}

use strict;
use warnings;

use Data::Dumper;

my $totalNum =  <STDIN>;
my %finResults;
my @hisResults;

my @inputs;
for(my $i = 0; $i < $totalNum; $i++){
    my $input = <STDIN>;
    @inputs = split( " ", $input);
    $finResults{$inputs[0]} += $inputs[1];
    my @record = ($inputs[0], $finResults{$inputs[0]} );
    push @hisResults, \@record;
#    if (! exists $hisResults{$inputs[0]}){
#        $hisResults{$inputs[0]} = [$inputs[1]];
#    }
#    else{
#        my $arrayRef = $hisResults{$inputs[0]};
#        if($$arrayRef[$#$arrayRef] < $finResults{$inputs[0]} ){
#            push @$arrayRef, $finResults{$inputs[0]};
#        }
#
#    }
}

my @finals = sort {$a <=> $b} values %finResults;

#print Dumper \@finals ;

my $finalScore = $finals[scalar @finals - 1];
#print "HHHHHH: finalScore $finalScore \n";
#print Dumper \%finResults ;
#print Dumper \@hisResults;

foreach my $hisRes (@hisResults){
    if ($hisRes->[1] >= $finalScore){
        print $hisRes->[0];
        last;
    }
}

#foreach my $candidate (sort {$finResults{$a} cmp $finResults{$b} }
#           keys %finResults)
#{
#     print "$candidate $finResults{$candidate} \n";
#}

#print Dumper \%hisResults;

use strict;
use warnings;

use Data::Dumper;

my $totalNum =  <STDIN>;
my %finResults;
my @hisResults;

my @inputs;
for(my $i = 0; $i < $totalNum; $i++){
    my $input = <STDIN>;
    @inputs = split( " ", $input);
    $finResults{$inputs[0]} += $inputs[1];
    my @record = ($inputs[0], $finResults{$inputs[0]} );
    push @hisResults, \@record;
#    if (! exists $hisResults{$inputs[0]}){
#        $hisResults{$inputs[0]} = [$inputs[1]];
#    }
#    else{
#        my $arrayRef = $hisResults{$inputs[0]};
#        if($$arrayRef[$#$arrayRef] < $finResults{$inputs[0]} ){
#            push @$arrayRef, $finResults{$inputs[0]};
#        }
#
#    }
}

my @finals = sort {$finResults{$a} <=> $finResults{$b} } keys %finResults;

#print Dumper \@finals ;

my $finalScore = $finals[scalar @finals - 1];
#print "HHHHHH: finalScore $finalScore \n";
#print Dumper \%finResults ;
#print Dumper \@hisResults;

foreach my $hisRes (@hisResults){
    if ($hisRes->[1] >= $finResults{$finalScore} && $hisRes->[0] eq $finalScore){
        print $hisRes->[0];
        last;
    }
}

#foreach my $candidate (sort {$finResults{$a} cmp $finResults{$b} }
#           keys %finResults)
#{
#     print "$candidate $finResults{$candidate} \n";
#}

#print Dumper \%hisResults;


#!/usr/bin/perl

use warnings;
use strict;

$\ = $/;

<>;

while(<>){
	$_ = <>, chomp;

	my( $L ) = reverse sort split '';

	my $A = 'a';

	my $ans;

	for my $i ( 1 .. 26 ){
		if( $A eq $L ){
			$ans = $i;
			last;
			}
		$A ++;
		}

	print $ans;
	}