S=[31,992,31744,1015808,32505856,1082401,2164802,4329604,8659208,17318416,4329631,17043521,1118480,33061951,33088575] D=dict() for i in range(5): A=list(map(int,input().split())) for j in range(5): D[A[j]]=32**i*2**j x=4096 for i in range(int(input())): u=int(input()) if u in D: x|=D[u] s=0 for y in S: if x&y==y: s+=1 print(s)