#!/usr/local/bin/perl

## See how many we can throw away

use strict;
use warnings;

my $fileName = shift;

open(IFILE, $fileName) or die("Couldn't open the file $fileName\n");

my %hyps = ();
my %chains = ();
my $seg = 1;
my $hypc = 0;
my $segc = 0;
while(my $str = <IFILE>){
	chomp($str);
	if($str eq ''){
		print STDERR $seg," $hypc $segc\n";
		$seg++;
		$segc = 0;
		next;
	}
	my $scoreStr = <IFILE>;
	$hypc++;
	my @scores = split /\s+/, $scoreStr;
	my $throw = 0;
# 	print "\n\n",join(' ',@scores),"\n";
# 	foreach my $h (keys %{$hyps{$seg}}){
# 		my @hs = @{${$hyps{$seg}}{$h}};
# 		my @diff = map {$hs[$_] - $scores[$_]} 0..$#hs;
# 		my @signp = grep {$_ >= 0} @diff;
# 		my @signn = grep {$_ <= 0} @diff;
# 		my @equal = grep {$_ == 0} @diff;
# 		if($#equal == 5){
# 			$throw = 0;
# 		}
# 		elsif($#signp == 5){
# 			die('Error') if ($throw == 3);
# 			push @{$chains{join(' ',@hs)}},join(' ',@scores);
# # 			print "Throw1 --> in Seg $seg\n";
# # 			print join(' ',@scores),"\n",$str,"\n",join(' ',@hs),"\n",$h,"\n";
# 			$throw = 0;
# 			last;
# 		}
# 		elsif($#signn == 5){
# # 			print STDERR "Throw3 --> in Seg $seg $hypc\n";
# # 			print join(' ',@hs),"\n";
# # 			delete ${$hyps{$seg}}{$h};
# 			push @{$chains{join(' ',@scores)}},join(' ',@hs);
# 			push @{$chains{join(' ',@scores)}},@{$chains{join(' ',@hs)}} if(defined $chains{join(' ',@hs)});
# 			$segc--;
# # 			$throw = 3;
# 		}
# 	}
	if($throw != 1){
		${$hyps{$seg}}{$hypc.' '.$str} = \@scores;
		$segc++;
	}
}
close IFILE;
print STDERR $hypc,"\n";

# foreach my $head (keys %chains){
# 	print $head,"\n";
# 	foreach (@{$chains{$head}}){
# 		print "$_\n";
# 	}
# 	print "\n\n";
# }
foreach my $seg (sort {$a <=> $b} keys %hyps){
	my @sortedHyps = sort {
				$a =~ /^(\d+) /;
				my $anum = $1;
				$b =~ /^(\d+) /;
				my $bnum = $1;
				return $anum <=> $bnum;
			} keys %{$hyps{$seg}};
	foreach my $hyp (@sortedHyps){
		$hyp =~ /^(\d+) (.+)$/;
		print "$2\n",join(' ',@{${$hyps{$seg}}{$hyp}}),"\n";
	}
	print "\n";
}