from sys import argv, stdin, exit
from collections import defaultdict

MAXX = 1000

assert len(argv)==4, f'correct usage: python3 {argv[0]} input our_output contestants_output'

def parsuj_pocet_dlzku(popis):
    if 'x' in popis:
        p, d = [ int(_) for _ in popis.split('x') ]
    else:
        p, d = 1, int(popis)
    return (p, d)

###############################################################################################

def process_their_output(TAU, TESTY, ODPOVEDE):
    try:
        their_output = open(argv[3],'r').readlines()
    except:
        assert False, 'Vystup programu obsahuje nepovolene znaky (nie je to ASCII text).'

    their_output = [ row.strip() for row in their_output ]

    assert len(their_output) > 0, 'Program mal prazdny vystup.'

    kde = 0
    for tc in range(TAU):
        label = f'Test {tc+1}:'
        
        L, pocty_treba = TESTY[tc]

        assert kde < len(their_output), f'{label} Primalo riadkov vystupu.'
        try:
            M, kde = int( their_output[kde] ), kde+1
        except:
            assert False, f'{label} Nepodarilo sa precitat m.'

        assert M > 0, f'{label} Cislo m musi byt kladne.' 
        assert M <= ODPOVEDE[tc], f'{label} Privelke m.'

        assert kde < len(their_output), f'{label} Primalo riadkov vystupu.'
        try:
            X, kde = int( their_output[kde] ), kde+1
        except:
            assert False, f'{label} Nepodarilo sa precitat x.'

        assert X > 0, f'{label} Cislo x musi byt kladne.' 
        assert X <= MAXX, f'{label} Privelke x.'

        assert kde+X-1 < len(their_output), f'{label} Primalo riadkov vystupu.'
        try:
            instrukcie = [ their_output[kde+i].split() for i in range(X) ]
            kde += X
        except:
            assert False, f'{label} Nepodarilo sa precitat instrukcie na pilenie.'

        pocty = []
        for i in range(X):
            assert len(instrukcie[i]) > 0, f'{label} Prazdna instrukcia.'
            try:
                pocty.append( int(instrukcie[i][0]) )
                instrukcie[i] = instrukcie[i][1:]
            except:
                assert False, f'{label} Instrukcia {i+1} nezacina poctom tyci.'

        assert sum(pocty) == M, f'{label} Sucet poctov tyci v instrukciach nie je m.'

        mam_tyci = defaultdict(int)

        for i in range(X):
            dlzka_spolu = 0
            for popis in instrukcie[i]:
                try:
                    yi, di = parsuj_pocet_dlzku(popis)
                except:
                    assert False, f'{label} Nespravna syntax instrukcie.'
                assert yi >= 0, f'{label} V instrukcii musia hodnoty y_i byt nezaporne.'
                assert di >= 0, f'{label} V instrukcii musia hodnoty d_i byt nezaporne.'
                dlzka_spolu += yi * di
                mam_tyci[di] += pocty[i] * yi
            assert dlzka_spolu <= L, f'{label} Sucet dlzok tyci v instrukcii prekrocil l.'

        assert all( mam_tyci[d] >= pocty_treba[d] for d in pocty_treba ), f'{label} Nevyrobil vsetky pozadovane tyce.'

        assert M == ODPOVEDE[tc], f'{label} INTERNA CHYBA, kontaktuj prosim organizatorov.'

    assert kde == len(their_output), 'Vystup uz mal skoncit ale obsahuje dalsie riadky.'
    return # all OK

###############################################################################################

input_file = open(argv[1],'r')

TAU = int( input_file.readline() )
TESTY = []
for tc in range(TAU):
    L = int( input_file.readline() )
    N = int( input_file.readline() )
    dlzky = [ int(_) for _ in input_file.readline().split() ]
    pocty = [ int(_) for _ in input_file.readline().split() ]
    pocty = { d:p for d,p in zip(dlzky,pocty) }
    TESTY.append( (L, pocty) )

correct_file = open(argv[2],'r')
ODPOVEDE = []
for tc in range(TAU):
    M = int( correct_file.readline() )
    ODPOVEDE.append(M)
    X = int( correct_file.readline() )
    ignored = [ correct_file.readline() for x in range(X) ]

try:
    process_their_output(TAU, TESTY, ODPOVEDE)
except Exception as error_message:
    print(error_message)
    exit(1)

print('OK')
