import sys,lzma,random,time,math,collections

def read(path):
    f=lzma.open(path,'rt') if path.endswith('.xz') else open(path)
    cls=[]; n=0
    for line in f:
        if not line or line[0] in 'c%0': continue
        if line[0]=='p': n=int(line.split()[2]); continue
        c=[int(x) for x in line.split() if x!='0']
        if c: cls.append(c)
    return n,cls

def solve(n,clauses,limit=10):
    occ=[[] for _ in range(n+1)]
    for i,c in enumerate(clauses):
        for l in c: occ[abs(l)].append(i)
    units=[]
    fixed=[0]*(n+1)
    for c in clauses:
        if len(c)==1:
            lit=c[0]; v=abs(lit); val=lit>0
            if fixed[v] and fixed[v]!=(1 if val else -1): return None
            fixed[v]=1 if val else -1
    start=time.time()
    best=10**9
    for restart in range(200):
        val=[False]*(n+1)
        for v in range(1,n+1):
            val[v]=(fixed[v]>0) if fixed[v] else bool(random.getrandbits(1))
        satcnt=[0]*len(clauses); unsat=[]; pos=[-1]*len(clauses)
        for i,c in enumerate(clauses):
            cnt=0
            for l in c:
                if val[abs(l)]==(l>0): cnt+=1
            satcnt[i]=cnt
            if cnt==0:
                pos[i]=len(unsat); unsat.append(i)
        def remove_unsat(ci):
            p=pos[ci]
            if p<0: return
            last=unsat[-1]; unsat[p]=last; pos[last]=p; unsat.pop(); pos[ci]=-1
        def add_unsat(ci):
            if pos[ci]>=0: return
            pos[ci]=len(unsat); unsat.append(ci)
        for step in range(2000000):
            if not unsat: return val, restart, step, time.time()-start
            if len(unsat)<best:
                best=len(unsat)
                if step%1000==0: print('best',best,'restart',restart,'step',step,'time',time.time()-start, file=sys.stderr)
            if time.time()-start>limit: return None
            ci=random.choice(unsat); c=clauses[ci]
            bestv=None; bestscore=-10**9; candidates=[]
            for l in c:
                v=abs(l)
                if fixed[v]: continue
                make=0; brk=0
                old=val[v]
                for cj in occ[v]:
                    before=satcnt[cj]
                    # after flipping: literal with var v toggles satisfaction; other literals same
                    lit_sat = False
                    for ll in clauses[cj]:
                        if abs(ll)==v:
                            lit_sat = (old==(ll>0)); break
                    after = before + (-1 if lit_sat else 1)
                    if before==0 and after>0: make+=1
                    if before>0 and after==0: brk+=1
                score=make*10-brk*20+random.random()
                if brk==0: score+=100000
                if score>bestscore: bestscore=score; candidates=[v]
                elif score==bestscore: candidates.append(v)
            if not candidates: continue
            v=random.choice(candidates); old=val[v]; val[v]=not old
            for cj in occ[v]:
                before=satcnt[cj]
                lit_sat=False
                for ll in clauses[cj]:
                    if abs(ll)==v:
                        lit_sat=(old==(ll>0)); break
                after=before + (-1 if lit_sat else 1)
                satcnt[cj]=after
                if before==0 and after>0: remove_unsat(cj)
                elif before>0 and after==0: add_unsat(cj)
    return None

for p in sys.argv[1:]:
    n,c=read(p); print('SOLVE',p,n,len(c)); r=solve(n,c,limit=float(sys.argv[-1]) if sys.argv[-1].isdigit() else 10); print('result',r[1:] if r else None)
