#!/usr/bin/perl

use strict;
use warnings;
use diagnostics;

my(%profondeurs);
my(@Fld);

my($arg);
foreach $arg (@ARGV) {
    # while (length($arg=$ARGV[0]) && shift) {
    $profondeurs{$arg}=1 if $arg =~ /^[0-9]+$/;
}

my($affiche) = 1;

my($line);

while (defined($line=<STDIN>)) {
    chomp $line;	# strip record separator
    @Fld = split(' ', $line, 9999);

    if ($line=~/^[ \t]/) {
	if ($affiche == 1) {
	    print $line, "\n";
	}
    } else {
	if (!$Fld[0] || ($Fld[0] ne 1 && $Fld[0] ne 2 && $Fld[0] ne 3 &&
	    $Fld[0] ne 4 && $Fld[0] ne 5)) {
	    print $line, "\n";
	    $affiche = 1;
	} elsif ($Fld[0] == 4) {
	    show($Fld[3], $line);
	} else {
	    show($Fld[6], $line);
	}
    }
}

sub show {
    my($prof) = shift;
    my($ligne) = shift;

    if (defined($profondeurs{$prof})) {
	print $ligne, "\n";
	$affiche = 1;
    } else {
	$affiche = 0;
    }
}
