NZEC error in python.Problem code :T22 Problem name : Friends Meeting. Please help

T=int(input())

R=[]

for i in range(T):

N=int(input())

A=list(map(int,input().split()))

P=[]

def prime(n):

    for i in range(2,n):

        if(n%i==0):

            return -1

            break
    else:

        return 1   
for i in range(len(A)):
    y=prime(A[i])
    if(y==1):
        p=0
        P.append(A[i])
    else:
        pass      
P.sort()
if(p==0 and prime(P[0]*P[1])==1):
    R.append(P[0]*P[1])
else:
    R.append(-1)

for i in range(len®):

print(R[i])

It is possible for P to have 0 or 1 element. In that case P[0]*P[1] will raise IndexError.
Moreover if all the numbers are composite then the statement p=0 inside the loop will not execute, which will raise NameError.