Problem:


カロリー計算

食べ物には3大栄養素と呼ばれる「たんぱく質」「脂質」「炭水化物」の３つの栄養素が含まれています。１g（グラム）あたりたんぱく質と炭水化物は 4 kcal（キロカロリー）、脂質は 9 kcal と計算されます。たとえば、下の表によると番号１のケーキにはタンパク質 7 g、脂質 14 g、炭水化物 47 g が含まれています。これをもとに含まれるカロリーを計算すると、4 &times; 7 + 9 &times; 14 + 4 &times; 47 = 342 kcal となります。その他も同様に計算されています。

番号名前たんぱく質(g)脂質(g)炭水化物(g)カロリー(kcal)

1ケーキ71447342

2ポテトチップス53555555

3どら焼き6359287

4プリン6515129

分類の対象となるお菓子の個数 n と各お菓子の情報、制限の情報を入力とし、そのお菓子一つだけなら制限を超えることがない（食べてもよい）お菓子の一覧を出力するプログラムを作成してください。

お菓子の情報は、お菓子の番号 s、そのお菓子に含まれるたんぱく質の重量 p、脂質の重量 q、炭水化物の重量 r から構成されています。制限の情報には、含むことができる最大のたんぱく質の重量 P、脂質の重量 Q、炭水化物の重量 R、および摂取することができる最大のカロリー C からなっており、タンパク質、脂質、炭水化物、カロリーのいずれかの一つでも超えると制限違反となり、「食べてはいけないお菓子」と判断されます。

食べてもよいお菓子の一覧は、食べてもよいお菓子の番号を入力順に出力してください。なお、食べてもよいお菓子がなければ”NA”と出力してください。
上の表にある4個のお菓子について、P = 10, Q = 15, R = 50, C = 400 という制限の場合、ケーキとプリンはそれぞれの栄養素及びカロリーが制限値以下なので食べてもよいお菓子に分類されますが、ポテトチップスは炭水化物とカロリー、どら焼きは炭水化物の量が制限値を超えているため食べてはいけないお菓子に分類されます。

入力

複数のデータセットの並びが与えられます。入力の終わりはゼロひとつの行で示されます。各データセットは以下の形式で与えられます。

n
s1 p1 q1 r1
s2 p2 q2 r2
:
sn pn qn rn
P Q R C

1行目にお菓子の数 n (1 ≤ n ≤ 1000)が与えられます。続く n 行に第 i のお菓子の番号 si (1 ≤ si ≤ 1000)、各栄養素の重量を表す整数 pi, qi, ri (0 ≤ pi, qi, ri ≤ 100) が与えられます。

続く行に各栄養素及びカロリーの制限値を表す整数 P, Q, R (0 ≤ P, Q, R ≤ 100), C (0 ≤ C ≤ 1700) が与えられます。

データセットの数は 100 を超えません。

出力

データセットごとに、食べてもよいお菓子の番号または ”NA” を出力します。

入力例

4
1 7 14 47
2 5 35 55
3 6 3 59
4 6 5 15
10 15 50 400
2
1 8 10 78
2 4 18 33
10 10 50 300
0

出力例

1
4
NA



Sample input:
4
1 7 14 47
2 5 35 55
3 6 3 59
4 6 5 15
10 15 50 400
2
1 8 10 78
2 4 18 33
10 10 50 300
0


Sample output:
1
4
NA

Submission status: Wrong Answer

Solution code:
while 1:
    N = int(input())
    if N == 0:break
    cals = [ list(map(int,input().split())) for i in range(N)]
    P,Q,R,C = list(map(int,input().split()))
    res = [ x for x in cals if x[1]<=P and x[2]<=Q and x[3]<=R and 4*x[1] + 9*x[2] + 4*x[3]]
    if len(res) == 0:print('NA')
    else:
        for i in res:
            print(i[0])

Submission status: Wrong Answer

Solution code:
def f(v):
    for i in range(4):
        if l[i] < v[i]:
            return
    else:
        return True

while True:
    N = int(raw_input())
    if N == 0: break
    data = {}
    for n in range(N):
        i, p, q, r = map(int, raw_input().split())
        data[i] = [p, q, r]
    l = map(int, raw_input().split())
    flag = False
    for i, v in data.items():
        c = 4*v[0]+9*v[1]+4*v[2]
        v.append(c)
        if f(v):
            print i
            flag = True
    if not flag:
        print 'NA'

Submission status: Wrong Answer

Solution code:
while True:
    n = int(input())
    if n == 0:
        break
    s = []
    for i in range(n):
        s.append([int(i) for i in input().split()])
    
    p, q, r, c = [int(i) for i in input().split()]

    possible = []
    for i in range(n):
        if (s[i][1] <= p) & (s[i][2] <= q) & (s[i][3] <= r) & ((4 * (s[i][1] + s[i][3]) + 9 * s[i][2]) <= c):
            possible.append(i + 1)

    if possible == []:
        print('NA')
    else:
        for i in possible:
            print(i)

Submission status: Wrong Answer

Solution code:
ans = []
while True:
    n = input()
    if n == 0:
        break
    sweets = [0] * n
    flbg = 0
    for i in range(n):
        sweets[i] = map(int,raw_input().split())
        sweets[i].append(sweets[i][0]*4+sweets[i][1]*9+sweets[i][2]*4)
    limit = map(int,raw_input().split())
    for candy in sweets:
        flag = 0
        for i in range(1,4):
            if candy[i+1] > limit[i]:
                flag = 1
            if flag == 1:
                break
        if flag == 0:
            ans.append(candy[0])
    if flbg == 0:
        ans.append("NA")
for i in ans:
    print i

Submission status: Wrong Answer

Solution code:
ans = []
while True:
    n = input()
    if n == 0:
        break
    sweets = [0] * n
    flbg = 0
    for i in range(n):
        sweets[i] = map(int,raw_input().split())
        sweets[i].append(sweets[i][0]*4+sweets[i][1]*9+sweets[i][2]*4)
    limit = map(int,raw_input().split())
    for candy in sweets:
        flag = 0
        for i in range(1,4):
            if candy[i+1] > limit[i]:
                flag = 1
                flbg = 1
            if flag == 1:
                break
        if flag == 0:
            ans.append(candy[0])
    if flbg == 0:
        ans.append("NA")
for i in ans:
    print i

Submission status: Wrong Answer

Solution code:
ans = []
while True:
    n = input()
    if n == 0:
        break
    sweets = [0] * n
    flbg = 0
    for i in range(n):
        sweets[i] = map(int,raw_input().split())
        sweets[i].append(sweets[i][0]*4+sweets[i][1]*9+sweets[i][2]*4)
    limit = map(int,raw_input().split())
    for candy in sweets:
        flag = 0
        for i in range(1,4):
            if candy[i+1] > limit[i]:
                flag = 1
            if flag == 1:
                break
        if flag == 0:
            flbg = 1
            ans.append(candy[0])
    if flbg == 0:
        ans.append("NA")
for i in ans:
    print i

Submission status: Accepted

Solution code:
while 1:
    N = int(input())
    if N == 0:break
    cals = [ list(map(int,input().split())) for i in range(N)]
    P,Q,R,C = list(map(int,input().split()))
    res = [ x for x in cals if x[1]<=P and x[2]<=Q and x[3]<=R and 4*x[1] + 9*x[2] + 4*x[3] <= C]
    if len(res) == 0:print('NA')
    else:
        for i in res:
            print(i[0])

Submission status: Accepted

Solution code:
import sys
f = sys.stdin

while True:
    n = int(f.readline())
    if n == 0:
        break
    ipqr = [map(int, f.readline().split()) for _ in range(n)]
    pl, ql, rl, cl = map(int, f.readline().split())
                        
    allow = [i for i, p, q, r in ipqr if p <= pl and q <= ql and r <= rl and p * 4 + q * 9 + r * 4 <= cl]
    

    print('\n'.join(map(str, allow)) if len(allow) else 'NA')

Submission status: Accepted

Solution code:
def f(v):
    for i in range(4):
        if l[i] < v[i]:
            return
    else:
        return True

while True:
    N = int(raw_input())
    if N == 0: break
    data = [map(int, raw_input().split()) for n in range(N)]
    l = map(int, raw_input().split())
    flag = False
    for v in data:
        v.append(4*v[1]+9*v[2]+4*v[3])
        if f(v[1:]):
            print v[0]
            flag = True
    if not flag:
        print 'NA'

Submission status: Accepted

Solution code:
while True:
	n = input()
	if n == 0 : break

	calorieSet = []
	for _ in xrange(n):
		num,protein,lipid,carbohydrate = map(int,raw_input().split())
		sumCalorie = protein * 4 + carbohydrate * 4 + lipid * 9
		calorieSet.append((num, protein, lipid, carbohydrate, sumCalorie))
	
	limmit = map(int,raw_input().split())
	good = []
	for var in calorieSet:
		judge = True
		for cal,lim in zip(var[1:], limmit):
			if cal > lim:
				judge = False
				break
		if judge:
			good.append(var[0])
			
	if good == []:
		print "NA"
	else:
		for var in good:
			print var

Submission status: Accepted

Solution code:
class Candy(object):
    def __init__(self, num, p, q, r):
        self.num = num
        self.p = p
        self.q = q
        self.r = r
    def can_eat(self, P, Q, R, C):
        if self.p > P or self.q > Q or self.r > R:
            return False
        elif 4*self.p + 9*self.q + 4*self.r > C:
            return False
        else:
            return True

while True:
    n = input()
    if n == 0:
        break
    Candylist = []
    for i in range(n):
        data = raw_input().split(" ")
        c = Candy(data[0], int(data[1]), int(data[2]), int(data[3]))
        Candylist.append(c)
    lim = raw_input().split(" ")
    eat_num = []
    for i in range(n):
        if Candylist[i].can_eat(int(lim[0]), int(lim[1]), int(lim[2]), int(lim[3])):
            eat_num.append(Candylist[i].num)
    if len(eat_num) == 0:
        print "NA"
    else:
        for i in eat_num:
            print i

Submission status: Accepted

Solution code:
while True:
	n = int(input())
	if n == 0:
		break
	data = [map(int,input().split()) for i in range(n)]
	p,q,r,c = map(int,input().split())

	ans = [i for i,pp,qq,rr in data if pp<=p and qq<=q and rr<=r and pp*4+qq*9+rr*4<=c]

	if len(ans):
		for i in ans:
			print (i)
	else :
		print ("NA")

Submission status: Accepted

Solution code:
while 1:
    n=int(input())
    if n==0:break
    a=[[0]*5for _ in range(n)]
    for i in range(n):
        a[i][:4]=map(int,input().split())
        a[i][4]=4*(sum(a[i][1:4:2]))+9*a[i][2]
    p,q,r,c=map(int,input().split())
    f=1
    for i in range(n):
        if a[i][1]<=p and a[i][2]<=q and a[i][3]<=r and a[i][4]<=c:print(a[i][0]);f=0
    if f:print('NA')

Submission status: Accepted

Solution code:
while 1:
    n=int(input())
    if n==0:break
    a=[list(map(int,input().split())) for _ in range(n)]
    p,q,r,c=map(int,input().split())
    b=[x[0] for x in a if x[1]<=p and x[2]<=q and x[3]<=r and 4*(x[1]+x[3])+9*x[2]<=c]
    if len(b):print(*b,sep='\n')
    else:print('NA')

Submission status: Accepted

Solution code:
while True:
    n = int(input())
    if n == 0:
        break
    ls = [list(map(int, input().split())) for _ in range(n)]
    P, Q, R, C = map(int, input().split())
    b = True
    for l in ls:
        if P >= l[1] and Q >= l[2] and R >= l[3] and 4*l[1]+9*l[2]+4*l[3] <= C:
            print(l[0])
            b = False
    if b: print("NA")

Submission status: Accepted

Solution code:
while True:
    n = int(input())
    if n == 0:
        break
    s = []
    for i in range(n):
        s.append([int(i) for i in input().split()])
    
    p, q, r, c = [int(i) for i in input().split()]

    possible = []
    for i in range(n):
        if (s[i][1] <= p) & (s[i][2] <= q) & (s[i][3] <= r) & ((4 * (s[i][1] + s[i][3]) + 9 * s[i][2]) <= c):
            possible.append(s[i][0])

    if possible == []:
        print('NA')
    else:
        for i in possible:
            print(i)

Submission status: Accepted

Solution code:
# AOJ 0239: Calorie Counting
# Python3 2018.6.25 bal4u

while 1:
	n = int(input())
	if n == 0: break
	s, p, q, r = [0]*1002, [0]*1002, [0]*1002, [0]*1002
	for i in range(n):
		s[i], p[i], q[i], r[i] = map(int, input().split())
	P, Q, R, C = map(int, input().split())
	f = False
	for i in range(n):
		c = ((p[i]+r[i]) << 2) + (q[i] << 3) + q[i]
		if p[i] > P or q[i] > Q or r[i] > R or c > C: pass
		else:
			print(s[i])
			f = True
	if not f: print("NA")

Submission status: Accepted

Solution code:
while 1:
    n = int(input())
    if n == 0:break
    s = []
    for _ in range(n):s.append(tuple(map(int, input().split())))
    l = tuple(map(int, input().split()))
    e = []
    for i in range(n):
        if s[i][1] <= l[0] and s[i][2] <= l[1] and s[i][3] <= l[2] and s[i][1] * 4 + s[i][2] * 9 + s[i][3] * 4 <= l[3]:e.append(s[i][0])
    if e != []:
        for i in e:print(i)
    else:print("NA")

Submission status: Accepted

Solution code:
while True:
  n = int(input())
  if n == 0:
    break
  lst = [list(map(int, input().split())) for _ in range(n)]
  lp, lq, lr, lc = map(int, input().split())
  flag = True
  for s, p, q, r in lst:
    if p <= lp and q <= lq and r <= lr and 4 * p + 9 * q + 4 * r <= lc:
      print(s)
      flag = False
  if flag:
    print("NA")

Submission status: Accepted

Solution code:
while True:
  a=int(input())
  I,P,Q,R,C=[],[],[],[],[]
  if a == 0: break
  for i in range(a):
    i,p,q,r=map(int,raw_input().split())
    I.append(i)
    P.append(p)
    Q.append(q)
    R.append(r)
    t=4*(p+r)+q*9
    C.append(t)
  x,y,z,o=map(int,raw_input().split())
  hoge=0
  for i in range(a):
    if C[i]>o or P[i]>x or Q[i] > y or R[i] > z:
      continue
    else:
      print I[i]
      hoge+=1
  else:
    if hoge==0: print "NA"

Submission status: Accepted

Solution code:
while True:
    f=False
    n=input()
    if n==0:break
    L=[map(int,raw_input().split()) for i in range(n)]
    P,Q,R,C=map(int,raw_input().split())
    for i,p,q,r in L:
        c=(p+r)*4+q*9
        if p<=P and q<=Q and r<=R and c<=C:
            print i
            f=True
    if f==False:
        print "NA"

Submission status: Accepted

Solution code:
while 1:
	n=input()
	if n==0:break
	s=[map(int,raw_input().split()) for i in range(n)]
	for i in range(n):
		s[i].append(4*s[i][1]+9*s[i][2]+4*s[i][3])
	r=map(int,raw_input().split())
	flag=0
	for i in s:
		if i[1]<=r[0] and i[2]<=r[1] and i[3]<=r[2] and i[4]<=r[3]:
			print i[0]
			flag=1
	if flag==0:print "NA"

Submission status: Accepted

Solution code:
while 1:
	n=input()
	if n==0:break
	s=[map(int,raw_input().split()) for i in range(n)]
	r=map(int,raw_input().split())
	flag=0
	for i in s:
		if i[1]<=r[0] and i[2]<=r[1] and i[3]<=r[2] and 4*(i[1]+i[3])+9*i[2]<=r[3]:
			print i[0]
			flag=1
	if flag==0:print "NA"

Submission status: Accepted

Solution code:
while 1:
    n = int(input())
    count = 0
    if n == 0:
        break
    L = [list(map(int,input().split())) for i in range(n+1)]

    for i in range(n):
        if L[i][1] <= L[n][0] and L[i][2] <= L[n][1]  and L[i][3] <= L[n][2] and L[i][1] * 4 + L[i][2] * 9 +L[i][3] * 4 <= L[n][3] :
            print(L[i][0])
            count += 1

    if count == 0:
        print("NA")

Submission status: Accepted

Solution code:
while True:
    n = int(input())
    if n == 0:
        break
    foods = []
    for _ in range(n):
        foods.append(list(map(int,input().split())))
    limit = list(map(int,input().split()))
    ans = []
    for food in foods:
        if food[1] <= limit[0] and food[2] <= limit[1] and food[3] <= limit[2] and (food[1]+food[3])*4+food[2]*9 <= limit[3]:
            ans.append(food[0])
    if ans == []:
        print("NA")
    else:
        for num in ans:
            print(num)

Submission status: Accepted

Solution code:
while True:
    n = int(input())
    if n == 0:
        break
    L = []
    for _ in range(n):
        s, p, q, r = [int(x) for x in input().split()]
        c = p*4 + q*9 + r*4
        L.append((s,p,q,r,c))
    lp,lq,lr,lc = [int(x) for x in input().split()]
    
    M = []
    for s, p, q, r, c in L:
        if p <= lp and q <= lq and r <=lr and c <=lc:
            M.append(s)
    #M.sort()
    
    if len(M) > 0:
        print(*M, sep="\n")
    else:
        print("NA")

Submission status: Accepted

Solution code:
while True:
  n = int(input())
  if n == 0:break
  a = [list(map(int, input().split())) for _ in range(n)]
  p, q, r, c = map(int, input().split())
  b = [x[0] for x in a if x[1] <= p and x[2] <= q and x[3] <= r and 4 * (x[1]+x[3])+9*x[2] <= c]
  if len(b):print(*b, sep='\n')
  else:print('NA')

Submission status: Accepted

Solution code:
while True:
    n = int(input())
    if n == 0: break
    l = [map(int,input().split()) for i in range(n)]
    P, Q, R, C = map(int,input().split())
 
    ans = [i for i,p,q,r in l if p<=P and q<=Q and r<=R and p*4+q*9+r*4<=C]
 
    if len(ans):
        for i in ans:
            print (i)
    else :
        print ("NA")

Submission status: Accepted

Solution code:
# AOJ 0239: Calorie Counting
# Python3 2018.6.25 bal4u
 
while 1:
    n = int(input())
    if n == 0: break
    s, p, q, r = [0]*1002, [0]*1002, [0]*1002, [0]*1002
    for i in range(n):
        s[i], p[i], q[i], r[i] = map(int, input().split())
    P, Q, R, C = map(int, input().split())
    f = False
    for i in range(n):
        c = ((p[i]+r[i]) << 2) + (q[i] << 3) + q[i]
        if p[i] > P or q[i] > Q or r[i] > R or c > C: pass
        else:
            print(s[i])
            f = True
    if not f: print("NA")

Submission status: Accepted

Solution code:
while True:
  n = int(input())
  if n == 0:
    break
  lst = [list(map(int, input().split())) for _ in range(n)]
  lp, lq, lr, lc = map(int, input().split())
  flag = True
  for s, p, q, r in lst:
    if p <= lp and q <= lq and r <= lr and 4 * p + 9 * q + 4 * r <= lc:
      print(s)
      flag = False
  if flag:
    print("NA")

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Calorie Counting
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0239

"""
import sys


def solve(foods, limit):
    ans = []
    for s, p, q, r in foods:
        total_calorie = p*4 + q*9 + r*4
        if all([x <= y for x, y in zip([p, q, r, total_calorie], limit)]):
            ans.append(s)
    return ans if ans else ['NA']


def main(args):
    while True:
        n = int(input())
        if n == 0:
            break
        foods = [list(map(int, input().split())) for _ in range(n)]
        limit = list(map(int, input().split()))
        ans = solve(foods, limit)
        print(*ans, sep='\n')


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

while True:
    N = int(input())
    if N == 0:
        break
    
    line = []
    for l in range(N):
        line.append(input())
    P,Q,R,C = [int(i) for i in input().split()]
    
    flag = False
    for l in range(N):
        s,p,q,r = [int(i) for i in line[l].split()]
        if p <= P and q <= Q and r <= R and 4*p+9*q+4*r <= C:
            print(s)
            flag = True
    if flag == False:
        print("NA")

Submission status: Accepted

Solution code:
while 1:
    n=int(input())
    if n==0:break
    treat_list=[]
    treat_limit=[]
    count=0
    for i in range(n):
        treat_list.append(list(map(int,input().split())))
    treat_limit=list(map(int,input().split()))
    for i in treat_list:
        if i[1]<=treat_limit[0] and i[2]<=treat_limit[1] and i[3]<=treat_limit[2] and (i[1]+i[3])*4+i[2]*9<=treat_limit[3]:
            print(i[0])
            count+=1
    if count==0:print("NA")

Submission status: Accepted

Solution code:
while 1:
    n = int(input())
    if n == 0:
        break

    data = []
    for _ in range(n):
        s, p, q, r = map(int, input().split())
        data.append([s, p, q, r])

    P, Q, R, C = map(int, input().split())

    flag = False
    for d in data:
        cal = d[1] * 4 + d[2] * 9 + d[3] * 4
        if d[1] <= P and d[2] <= Q and d[3] <= R and cal <= C:
            flag = True
            print(d[0])

    if not flag:
        print("NA")

Submission status: Accepted

Solution code:
ans = []
readline = open(0).readline
writelines = open(1, 'w').writelines

while 1:
    N = int(readline())
    if N == 0:
        break
    items = [map(int, readline().split()) for i in range(N)]
    P, Q, R, C = map(int, readline().split())
    c = 0
    for s, p, q, r in items:
        if p <= P and q <= Q and r <= R and 4*p+9*q+4*r <= C:
            ans.append("%d\n" % s)
            c += 1
    if not c:
        ans.append("NA\n")
writelines(ans)

Submission status: Runtime Error

Solution code:
def f(v):
    for i in range(4):
        if l[i] < v[i]:
            return
    else:
        return True

while True:
    N = int(raw_input())
    if N == 0: break
    data = {}
    for n in range(N):
        i, p, q, r = map(int, raw_input().split())
        data[i] = [p, q, r]
    l = map(int, raw_input().split())
    flag = False
    for i, v in data.items():
        c = 4*v[0]+9*v[1]+4*v[2]
        v.append(c)
        if f(v):

Submission status: Runtime Error

Solution code:
ans = []
while True:
    n = input()
    if n == 0:
        breakans = []
while True:
    n = input()
    if n == 0:
        break
    sweets = [0] * n
    flbg = 0
    for i in range(n):
        sweets[i] = map(int,raw_input().split())
        sweets[i].append(sweets[i][0]*4+sweets[i][1]*9+sweets[i][2]*4)
    limit = map(int,raw_input().split())
    for candy in sweets:
        flag = 0
        for i in range(1,4):
            if candy[i+1] > limit[i]:
                flag = 1
                flbg = 1
            if flag == 1:
                break
        if flag == 0:
            ans.append(candy[0])
    if flbg == 0:
        ans.append("NA")
for i in ans:
    print i
    sweets = [0] * n
    flbg = 0
    for i in range(n):
        sweets[i] = map(int,raw_input().split())
        sweets[i].append(sweets[i][0]*4+sweets[i][1]*9+sweets[i][2]*4)
    limit = map(int,raw_input().split())
    for candy in sweets:
        flag = 0
        for i in range(1,4):
            if candy[i+1] > limit[i]:
                flag = 1
            if flag == 1:
                break
        if flag == 0:
            ans.append(candy[0])
    if flbg == 0:
        ans.append("NA")
for i in ans:
    print i

Submission status: Runtime Error

Solution code:
#include<iostream>

using namespace std;

int main()
{
  while(1)
  {
    int n;
    int table[1001][4];
    cin >> n;
    if(!n)
      break;
    for(int i = 0; i < n; i++)
    {
      cin >> table[i][0] >> table[i][1] >> table[i][2] >> table[i][3];
      table[i][4] = table[i][1] * 4 + table[i][2] * 9 + table[i][3] * 4;
    }
    int lim[5];
    bool b = true;
    cin >> lim[1] >> lim[2] >> lim[3] >> lim[4];
    for(int i = 0; i < n; i++)
    {
      bool f = true;
      for(int j = 1; j < 5; j++)
      {
       	if(table[i][j] > lim[j])
	{
          f = 0;
          break;
	}
      }
      if(f)
      {
       	cout << table[i][0] << endl;
	b = 0;
      }
    }
    if(b)
      cout << "NA" << endl;
  }
}

Submission status: Runtime Error

Solution code:
#include<iostream>

using namespace std;

int main()
{
  while(1)
  {
    int n;
    int table[1001][5];
    cin >> n;
    if(!n)
      break;
    for(int i = 0; i < n; i++)
    {
      cin >> table[i][0] >> table[i][1] >> table[i][2] >> table[i][3];
      table[i][4] = table[i][1] * 4 + table[i][2] * 9 + table[i][3] * 4;
    }
    int lim[5];
    bool b = true;
    cin >> lim[1] >> lim[2] >> lim[3] >> lim[4];
    for(int i = 0; i < n; i++)
    {
      bool f = true;
      for(int j = 1; j < 5; j++)
      {
       	if(table[i][j] > lim[j])
	{
          f = 0;
          break;
	}
      }
      if(f)
      {
       	cout << table[i][0] << endl;
	b = 0;
      }
    }
    if(b)
      cout << "NA" << endl;
  }
}