Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
case=0
while True:
    case+=1
    W=input()
    if W==0:break
    print "Case {}:".format(case)
    dp=[0]*1000
    w=[0]*1000
    v=[0]*1000
    n=input()
    for i in xrange(n):
        v[i],w[i]=map(int,raw_input().strip().split(","))
    for i in xrange(n):
        for j in xrange(W,-1,-1):
            if j+w[i]<=W:
                dp[j+w[i]]=max(dp[j+w[i]],dp[j]+v[i])
    print max(dp)
    print dp.index(max(dp))

Fixed submission:
case=0
while True:
    case+=1
    W=input()
    if W==0:break
    print "Case {}:".format(case)
    dp=[0]*1001
    w=[0]*1001
    v=[0]*1001
    n=input()
    for i in xrange(n):
        v[i],w[i]=map(int,raw_input().strip().split(","))
    for i in xrange(n):
        for j in xrange(W,-1,-1):
            if j+w[i]<=W:
                dp[j+w[i]]=max(dp[j+w[i]],dp[j]+v[i])
    print max(dp)
    print dp.index(max(dp))
Buggy python submission (verdict: Runtime Error):
n=5*10**4
p=[True]*n
p[0],p[1]=False,False
for i in xrange(2,int(n**0.5)+1):
    if p[i]==True:
        for j in xrange(i**2,n,i):
            p[j]=False
while True:
    n=input()
    if n==0:break
    for i in xrange(n/2+1):
        if p[i] and p[n-i]:c+=1
    print c

Fixed submission:
import itertools
N=50000+1
p=[True]*N
p[0],p[1]=False,False
for i in xrange(2,int(N**0.5)+1):
    if p[i]==True:
        for j in xrange(i**2,N,i):
            p[j]=False
plist=[i for i in xrange(N) if p[i]]
c=[0]*N*2
for i,j in itertools.combinations(plist,2):
    c[i+j]+=1
for i in xrange(N):
    if p[i]:c[i+i]+=1
while True:
    n=input()
    if n==0:break
    print c[n]

Buggy python submission (verdict: Time Limit Exceeded):
n=5*10**4
p=[True]*n
p[0],p[1]=False,False
for i in xrange(2,int(n**0.5)+1):
    if p[i]==True:
        for j in xrange(i**2,n,i):
            p[j]=False
while True:
    n=input()
    if n==0:break
    c=0
    for i in xrange(n/2+1):
        if p[i] and p[n-i]:c+=1
    print c

Fixed submission:
import itertools
N=50000+1
p=[True]*N
p[0],p[1]=False,False
for i in xrange(2,int(N**0.5)+1):
    if p[i]==True:
        for j in xrange(i**2,N,i):
            p[j]=False
plist=[i for i in xrange(N) if p[i]]
c=[0]*N*2
for i,j in itertools.combinations(plist,2):
    c[i+j]+=1
for i in xrange(N):
    if p[i]:c[i+i]+=1
while True:
    n=input()
    if n==0:break
    print c[n]

Buggy python submission (verdict: Runtime Error):
N=5000+1
p=[True]*N
p[0],p[1]=False,False
for i in xrange(2,int(N**0.5)+1):
    if p[i]==True:
        for j in xrange(i**2,N,i):
            p[j]=False
c=[0]*N
for i in xrange(N):
    for j in xrange(i/2+1):
        if p[j] and p[i-j]:c[i]+=1
while True:
    n=input()
    if n==0:break
    print c[n]

Fixed submission:
import itertools
N=50000+1
p=[True]*N
p[0],p[1]=False,False
for i in xrange(2,int(N**0.5)+1):
    if p[i]==True:
        for j in xrange(i**2,N,i):
            p[j]=False
plist=[i for i in xrange(N) if p[i]]
c=[0]*N*2
for i,j in itertools.combinations(plist,2):
    c[i+j]+=1
for i in xrange(N):
    if p[i]:c[i+i]+=1
while True:
    n=input()
    if n==0:break
    print c[n]

Buggy python submission (verdict: Time Limit Exceeded):
N=50000+1
p=[True]*N
p[0],p[1]=False,False
for i in xrange(2,int(N**0.5)+1):
    if p[i]==True:
        for j in xrange(i**2,N,i):
            p[j]=False
while True:
    n=input()
    if n==0:break
    c=0
    for i in xrange(n/2+1):
        if p[i] and p[n-i]:c+=1
    print c

Fixed submission:
import itertools
N=50000+1
p=[True]*N
p[0],p[1]=False,False
for i in xrange(2,int(N**0.5)+1):
    if p[i]==True:
        for j in xrange(i**2,N,i):
            p[j]=False
plist=[i for i in xrange(N) if p[i]]
c=[0]*N*2
for i,j in itertools.combinations(plist,2):
    c[i+j]+=1
for i in xrange(N):
    if p[i]:c[i+i]+=1
while True:
    n=input()
    if n==0:break
    print c[n]
Buggy python submission (verdict: Runtime Error):
import sys,math
for line in sys.stdin.readlines():
    line=raw_input()
    x1,y1,x2,y2,xq,yq=map(float,line.strip().split(","))
    L=((x1-x2)**2+(y1-y2)**2)**0.5
    t=math.atan2(y2-y1,x2-x1)
    R1=((x1-xq)**2+(y1-yq)**2)**0.5
    R2=((x2-xq)**2+(y2-yq)**2)**0.5
    a=math.acos((L*L+R1*R1-R2*R2)/(2*L*R1))
    x,y=x1+R1*math.cos(t+a),y1+R1*math.sin(t+a)
    if not (xq-0.00001<=x<=xq+0.00001 and yq-0.00001<=y<=yq+0.00001):
        print x,y
    else:
        print x1+R1*math.cos(t-a),y1+R1*math.sin(t-a)
#temp

Fixed submission:
import sys
for line in sys.stdin.readlines():
    x1,y1,x2,y2,xq,yq=map(float,line.strip().split(","))
    if x1==x2:
        x,y=-xq+2*x1,yq
    elif y1==y2:
        x,y=xq,-yq+2*y1
    else:
        m=(y1-y2)/(x1-x2)
        n=-m*x1+y1
        x=(2*yq*m-2*m*n+xq-m*m*xq)/(m*m+1)
        y=(xq-x)/m+yq
    print "{:.6f} {:.6f}".format(x+0,y+0)

Buggy python submission (verdict: Runtime Error):
import sys
for line in sys.stdin.readlines():
    x1,y1,x2,y2,xq,yq=map(float,line.strip().split(","))
    if x1==x2:
        x,y=-xq+2*x1,yq
    elif y1==y2:
        x,y=xq,-yq+2*y1
    else:
        m=(y1-y2)/(x1-x2)
        n=-m*x1+y1
        x,y= (2*yq*m-2*m*n+xq-m*m*xq)/(m*m+1),(xq-x)/m+yq;
    print "{:.6f} {:.6f}".format(x+0,y+0)

Fixed submission:
import sys
for line in sys.stdin.readlines():
    x1,y1,x2,y2,xq,yq=map(float,line.strip().split(","))
    if x1==x2:
        x,y=-xq+2*x1,yq
    elif y1==y2:
        x,y=xq,-yq+2*y1
    else:
        m=(y1-y2)/(x1-x2)
        n=-m*x1+y1
        x=(2*yq*m-2*m*n+xq-m*m*xq)/(m*m+1)
        y=(xq-x)/m+yq
    print "{:.6f} {:.6f}".format(x+0,y+0)
Buggy python submission (verdict: Runtime Error):
def f(n,k):
    return 0 if n==0 else (f(n-1,k)+k)%n
while True:
    n,k=map(int,raw_input().strip().split())
    if (n,k)==(0,0):
        break
    print f(n,k)+1

Fixed submission:
def f(n,k):
    return 0 if n==1 else (f(n-1,k)+k)%n
while True:
    n,k=map(int,raw_input().strip().split())
    if (n,k)==(0,0):
        break
    print f(n,k)+1
Buggy python submission (verdict: Runtime Error):
from calendar import isleap
f = lambda x: map(str, x) if x else ["NA"]
ans = []
while True:
    a, b = map(int, raw_input().split())
    if b == 0:
        break
    ans += f(filter(isleap, xrange(a, b+1))) + [""]
print("\n".join(ans).strip())

Fixed submission:
ans=[]
while True:
    a,b=map(int,raw_input().split())
    if (a,b)==(0,0):
        break
    t=[y for y in xrange(a,b+1) if (y%4==0 and not y%100==0) or y%400==0]
    if not t:
        ans.append("NA")
    else:
        ans.append("\n".join(map(str,t)))
print "\n\n".join(ans).strip()

Buggy python submission (verdict: Runtime Error):
from calendar import isleap
f = lambda x: map(str, x) if x else ["NA"]
ans = []
while True:
    a, b = map(int, input().split())
    if b == 0:
        break
    ans += f(filter(isleap, range(a, b+1))) + [""]
print("\n".join(ans).strip())

Fixed submission:
ans=[]
while True:
    a,b=map(int,raw_input().split())
    if (a,b)==(0,0):
        break
    t=[y for y in xrange(a,b+1) if (y%4==0 and not y%100==0) or y%400==0]
    if not t:
        ans.append("NA")
    else:
        ans.append("\n".join(map(str,t)))
print "\n\n".join(ans).strip()
Buggy python submission (verdict: Time Limit Exceeded):
T = 1000
memo = [[0] * (2 * T + 1) for _ in range(3)] 
#Java: int[][] memo = new int[3][2*T+1];
memo[0][0] = 1
for i in range(1,3):
    for j in range(i*T+1):
        memo[i][j] = sum([memo[i-1][k] for k in range(max(0,j-T),j+1)])

while True:
    try:
        n = input() 
    except EOFError:
        break
    cnt = 0
    for a in range(T+1):
        for b in range(T+1):
            cnt += memo[2][n-(a+b)] if 0 <= n - (a + b) <= 2 * T else 0 
            # 0 <= n - (a + b) <= 2 * T ? memo[n-(a+b)] : 0
    print cnt

#O(T^2), 2.15s

Fixed submission:
T = 1000
def f(x):
    return max(0, T + 1 - abs(T - x))
while True:
    try:
        n = input() 
    except EOFError:
        break
    cnt = 0
    for i in range(n+1):
        cnt += f(i) * f(n-i)
    print cnt

#O(n),
Buggy python submission (verdict: Time Limit Exceeded):
from itertools import combinations_with_replacement
n = input()
sq = [map(int,raw_input().split()) for u in xrange(n)]
s_sq =[[0]*(n+1) for u in xrange(n+1)]
ma = sq[0][0]
for i in xrange(n):
    for ii in xrange(n):
        s_sq[i][ii] = sq[i][ii] + s_sq[i][ii-1] + s_sq[i-1][ii] - s_sq[i-1][ii-1]
for tx,bx in combinations_with_replacement(range(n),2):
    for ty,by in combinations_with_replacement(range(n),2):
        #print "tx,ty=",tx,ty,"bx,by=",bx,by
        ma = max([ma,s_sq[by][bx]-s_sq[ty-1][bx]-s_sq[by][tx-1]+s_sq[ty-1][tx-1]])
print ma

Fixed submission:
n = input()
sq = [map(int,raw_input().split()) for u in xrange(n)]
s_sq = [[[0]*n for u in xrange(n)] for uu in xrange(n)]
#s_sq[lower_end][left_end][right_end]
ma = -1000 * 100 * 100
for y in xrange(n):
    temp = [0] * (n+1)
    for x in xrange(n):
        #make sum of the sequence left_end == 0, right_end == n
        temp[x] = temp[x-1] + sq[y][x]
    for tx in xrange(n):
        for ex in xrange(tx,n):
            s_sq[y][tx][ex] = max([0,s_sq[y-1][tx][ex]]) + temp[ex] - temp[tx-1]
            ma = max([ma,s_sq[y][tx][ex]])
print ma
Buggy python submission (verdict: Runtime Error):
import sys
for line in sys.stdin.readlines():
    a="NA"
    for i in xrange(10):
        exp,ans=line.strip().replace("X",str(i)).split("=")
        if eval(exp)==int(ans):
            a=i
    print a

Fixed submission:
import sys
for line in sys.stdin.readlines():
    exp = line.strip()
    ans = "NA"
    # 1<=X<10
    for i in xrange(1,10):
        filled_exp = exp.replace("X",str(i))
        left,right = filled_exp.split("=")
        a,b = left.split("+")
        if int(a)+int(b) == int(right):
            ans = i
    # X==0
    filled_exp = exp.replace("X","0")
    left,right = filled_exp.split("=")
    a,b = left.split("+")
    if not any([(s[0]=="0" and len(s)>1)  for s in (a,b,right)]):
        if int(a)+int(b) == int(right):
            ans = 0
    print ans
Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heappush,heappop
MOVE = [[1,4],[0,2,5],[1,3,6],[2,7],[0,5],[1,4,6],[2,5,7],[3,6]]
GOAL = "01234567"
def h(field):    
    ret = 0
    for p,n in enumerate(map(int,field)):
        ret += dist(p,n)
    return ret
def dist(num,place):
    return abs(num%4-place%4)+(num/5+place/5)%2 #width+height
def swap(field,a,b):
    tmp = [f for f in field]
    tmp[a],tmp[b] = tmp[b],tmp[a]
    return "".join(tmp)

while True:
    try:
        field = raw_input().replace(" ","")    
    except:
        break
    hq = []
    visited = {field:True}
    heappush(hq,[0,0,field])
    while len(hq) != 0:
        f,g,field = heappop(hq)
        if field == GOAL:
            print g
            break    
        a = field.find("0")
        for b in MOVE[a]:
            tmp = swap(field,a,b)
            if tmp not in visited:
                visited[tmp] = True
                heappush(hq,[h(field)+g,g+1,tmp])

Fixed submission:
# -*- coding: utf-8 -*-
#与えられるパズルの数は1000以下です
MOVE = [[1,4],[0,2,5],[1,3,6],[2,7],[0,5],[1,4,6],[2,5,7],[3,6]]
ans = {"01234567":0}
def swap(field,a,b):
    tmp = list(field)
    tmp[a],tmp[b] = tmp[b],tmp[a]
    return "".join(tmp)

def BFS():
    global ans
    q = [[0,"01234567"]]
    while len(q) != 0:
        g, field = q.pop(0)
        a = field.find("0")
        for b in MOVE[a]:
            tmp = swap(field,a,b)
            if not tmp in ans:
                ans[tmp] = g+1
                q.append([g+1,tmp])

BFS()
while True:
    try:
        print ans[raw_input().replace(" ","")]
    except:
        break
Buggy python submission (verdict: Runtime Error):
decode = ["afkpuz",
          "bglqv.",
          "chmrw?",
          "dinsx!",
          "ejoty "]
while True:
    try:
        line = raw_input()
    except EOFError:
        break
    if len(line)%2 == 1:
        print "NA"
        continue
    try:
        print "".join(decode[j-1][i-1]
                      for i,j in zip(map(int,line[::2]),map(int,line[1::2])))
    except KeyError:
        print "NA"

Fixed submission:
decode = ["afkpuz",
          "bglqv.",
          "chmrw?",
          "dinsx!",
          "ejoty "]
while True:
    try:
        line = map(int,list(raw_input()))
    except EOFError:
        break
    if len(line)%2 == 1 or 0 in line:
        print "NA"
        continue
    try:
        print "".join(decode[j-1][i-1]
                      for i,j in zip(line[::2],line[1::2]))
    except IndexError:
        print "NA"
Buggy python submission (verdict: Time Limit Exceeded):
def dfs(l, r):
    if (l, r) in cache:
        return cache[(l, r)]
    if l == r:
        return 0
    return min(dfs(l, m) + dfs(m + 1, r) + L[l][1] * L[m][0] * L[m + 1][1] * L[m + 1][0] for m in xrange(l, r))
    
cache = {}
L = [map(int, raw_input().split()) for _ in xrange(input())]
print dfs(0, len(L) - 1)

Fixed submission:
def dfs(l, r):
    if l == r:
        return 0
    if (l, r) in cache:
        return cache[(l, r)]
    cache[(l, r)] = min(dfs(l, m) + dfs(m + 1, r) + L[l][0] * L[m][1] * L[m + 1][0] * L[r][1] for m in xrange(l, r))
    return cache[(l, r)]

cache = {}
L = [map(int, raw_input().split()) for _ in xrange(input())]
print dfs(0, len(L) - 1)
Buggy python submission (verdict: Runtime Error):
#Lupin The 4th: O(2^n)
def f(a, b, visited):
    w = W[visited] * 20
    return abs(lengths[a] - lengths[b]) * (70 + w)

def solve():
    cache = dict([((i, 0), (0, [i])) for i in xrange(n)])
    def dfs(top, visited):
        if (top, visited) in cache:
            return cache[(top, visited)]
        ret = (1 << 30, [-1])
        for i in xrange(n):
            if (visited >> i) & 1 == 1:
                tmp = dfs(i, visited ^ (1 << i))
                c = tmp[0] + f(top, i, visited)
                if c < ret[0]:
                    ret = (c, tmp[1])
        cache[(top, visited)] = (ret[0], ret[1] + [top])
        return cache[(top, visited)]
    mask = (1 << n) - 1
    ret = (1 << 30, -1)
    for i in xrange(n):
        ret = min(ret, dfs(i, mask ^ (1 << i)))
    return ret[1]

n = input()
num, lengths, weights = zip(*[map(int, raw_input().split()) for _ in xrange(n)])
cache = dict([((i, 0), 0) for i in xrange(n)])
W = [0] + [sum(weights[i] for i in xrange(n) if (b >> i) & 1 == 1)  
           for b in xrange(1, 1 << n)]
print " ".join(map(lambda i:str(num[i]), solve()))

Fixed submission:
#Lupin The 4th: O(2^n)
def f(a, b, visited):
    w = W[visited] * 20
    return float(abs(lengths[a] - lengths[b])) / (2000. / (70 + w))

def solve():
    cache = dict([((i, 0), (0., [i])) for i in xrange(n)])
    def dfs(top, visited):
        if (top, visited) in cache:
            return cache[(top, visited)]
        ret = (1 << 30, [-1])
        for i in xrange(n):
            if (visited >> i) & 1 == 1:
                tmp = dfs(i, visited ^ (1 << i))
                c = tmp[0] + f(top, i, visited)
                if c < ret[0]:
                    ret = (c, tmp[1])
        cache[(top, visited)] = (ret[0], ret[1] + [top])
        return cache[(top, visited)]
    mask = (1 << n) - 1
    ret = (1 << 30, -1)
    for i in xrange(n):
        ret = min(ret, dfs(i, mask ^ (1 << i)))
    return ret[1]

n = input()
num, lengths, weights = zip(*[map(int, raw_input().split()) for _ in xrange(n)])
W = [0] + [sum(weights[i] for i in xrange(n) if (b >> i) & 1 == 1)  
           for b in xrange(1, 1 << n)]
print " ".join(map(lambda i:str(num[i]), solve()))
Buggy python submission (verdict: Runtime Error):
from bisect import bisect,bisect_left
from fractions import gcd
MAXN = 1000002
T = 2**20*3**14*5**9
H = filter(lambda i:gcd(T,i)==i,xrange(MAXN))
while True:
    line = raw_input()
    if line == "0":
        break
    m,n = map(int,line.split())
    print bisect(H,n)-bisect_left(H,m)

Fixed submission:
from bisect import bisect_left
S = [60,80,100,120,140,160]
W = [2,5,10,15,20,25]
F = [600,800,1000,1200,1400,1600,0]
def func(x,y,h,w):
    return F[max(bisect_left(S,x+y+h),bisect_left(W,w))]
while True:
    n = input()
    if n == 0:
        break
    print sum(func(*map(int,raw_input().split())) for _ in xrange(n))
Buggy python submission (verdict: Time Limit Exceeded):
from bisect import bisect
D = range(10,61,10)
while True:
    n = input()
    if n == 0:
        break
    A = [0]*7
    for _ in xrange(n):
        A[bisect(D,input())] += 1
    print "\n".join(map(str,A))

Fixed submission:
while True:
    n = input()
    if n == 0:
        break
    A = [0]*7
    for _ in xrange(n):
        A[min(int(raw_input())/10,6)] += 1
    print "\n".join(map(str,A))

Buggy python submission (verdict: Time Limit Exceeded):
while True:
    n = input()
    if n == 0:
        break
    A = [0]*7
    for _ in xrange(n):
        A[min(input()/10,6)] += 1
    print "\n".join(map(str,A))

Fixed submission:
while True:
    n = input()
    if n == 0:
        break
    A = [0]*7
    for _ in xrange(n):
        A[min(int(raw_input())/10,6)] += 1
    print "\n".join(map(str,A))
Buggy python submission (verdict: Runtime Error):
from copy import deepcopy
answer = ""
while True:
    line = raw_input()
    while line.isspace() or not line: #!?!?!!!
        line = raw_input()
    if line == "0":
        break
    P, S = map(int, line.split())
    def check(data):
        return reduce(lambda a, b: a+b, data).count("?")
    def calc(seq):
        if "?" not in seq or not(0 <= seq.count("?") <= 1) :
            return seq
        if seq[-1] == "?":
            return seq[:-1] + sum(seq[:-1])
        a = seq[-1] - sum(s for s in seq[:-1] if s != "?")
        return [s if s != "?" else a for s in seq]
    def update(data):
        data = deepcopy(data)
        t_data = zip(*data)
        for i in xrange(P):
            for j, d in enumerate(calc(data[i])):
                data[i][j] = d
            for j, d in enumerate(calc(t_data[i])):
                data[j][i] = d
        return data
    data = [map(lambda x: int(x) if x.isdigit() else x, raw_input().split())
            for _ in xrange(P+1)]
    cdata = deepcopy(data)
    for _ in xrange(check(cdata)):
        cdata = update(cdata)
    ans = ""
    if check(cdata) == 0:
        for d in zip(data, cdata):
            for e1, e2 in zip(*d):
                if e1 == "?":
                    ans += "{}\n".format(e2)
    else:
        ans += "NO\n"
    answer += ans + "\n"
print answer.strip("\n")

Fixed submission:
import sys
from copy import deepcopy
def check(data):
    return reduce(lambda a, b: a+b, data).count("?")

def calc(seq):
    if "?" not in seq or not(0 <= seq.count("?") <= 1) :
        return seq
    if seq[-1] == "?":
        return seq[:-1] + sum(seq[:-1])
    a = seq[-1] - sum(s for s in seq[:-1] if s != "?")
    return [s if s != "?" else a for s in seq]

def update(data):
    data = deepcopy(data)
    for i in xrange(len(data)):
        for j, d in enumerate(calc(data[i])):
            data[i][j] = d
    t_data = zip(*data)
    for i in xrange(len(t_data)):
        for j, d in enumerate(calc(t_data[i])):
            data[j][i] = d
    return data

answer = ""
while True:
    line = raw_input()
    while line.isspace() or not line: #!?!?!!!
        line = raw_input()
    if line == "0":
        break
    P, S = map(int, line.split())
    data = [map(lambda x: int(x) if x.replace("-", "").isdigit() else x, raw_input().split())
            for _ in xrange(P+1)]
    cdata = deepcopy(data)
    for _ in xrange(check(cdata)):
        cdata = update(cdata)
    ans = ""
    if check(cdata) == 0:
        for d in zip(data, cdata):
            for e1, e2 in zip(*d):
                if e1 == "?":
                    ans += "{}\n".format(e2)
    else:
        ans += "NO\n"
    answer += ans + "\n"
print answer.strip("\n")
Buggy python submission (verdict: Runtime Error):
from itertools import product
L = [10, 50, 100, 500]

def solve():
    #print N
    ans = [[20] * 4, [20] * 4]
    for c in product(*[range(N[i] + 1) for i in xrange(4)]):
        #print "out", c
        r = calc(c)
        #print "in", r
        if check(c, r):
            ans = min([r, c], ans, key = lambda x: sum(x[0]))
            #print "check", ans
        #print
    return ans[1]

def calc(N):    
    r = sum(L[i] * N[i] for i in xrange(4)) - M
    #print r
    if r < 0:
        return [-1] * 4
    ret = [0] * 4
    for i in xrange(3, -1, -1):
        ret[i] += r / L[i]
        r -= ret[i] * L[i]
    return ret

def check(N1, N2):
    if N2[0] == -1:
        return False
    for c1, c2 in zip(N1, N2):
        if c1 > 0 and c2 > 0:
            return False
    return True

M = int(raw_input())
while True:
    N = map(int, raw_input().split())
    for i, n in zip(L, solve()):
        if n == 0:
            continue
        print i, n
    M = int(raw_input())
    if M == 0:
        break
    print

Fixed submission:
L = (10, 50, 100, 500)
def calc():
    ret = [0] * 4
    r = sum(L[i] * N[i] for i in xrange(4)) - M
    for i in xrange(3, -1, -1):
        ret[i] += r / L[i]
        r -= ret[i] * L[i]
    return ret

M = int(raw_input())
while M != 0:
    N = map(int, raw_input().split())
    for i, n in zip(L, map(lambda a, b: max(0, a - b), N, calc())):
        if n == 0:
            continue
        print i, n
    M = int(raw_input())
    if M == 0:
        break
    print
Buggy python submission (verdict: Runtime Error):
def toBinary(block):
    return [sum(1 << i for i,bb in enumerate(b) if bb == "#") for b in block]

def toStr(bfield):
    return ["".join("." if f & (1<<i) == 0 else "#" for i in xrange(W)) for f in bfield]

def rotated(block):
    return map(lambda x:"".join(x),zip(*[b[:] for b in block]))[::-1]

def striped(block):
    ret = [b[:] for b in block]
    while ret[-1] == "." * len(ret[0]):
        ret.pop(-1)
    while ret[0] == "." * len(ret[0]):
        ret.pop(0)
    return ret

def canPut(x, y):
    if not(0 < x + w <= W or 0 < y + h <= H):
        return False
    for f,b in zip(bfield[y:y+h], bblock):
        if (f & (b << x)) != 0:
            return False
    return True

def puted(x, y):
    ret = bfield[:]
    for i,b in zip(xrange(y,y+h), bblock):
        ret[i] = ret[i] | (b << x)
    #print "\n".join(toStr(ret)),"\n"
    return ret

def count(bfield):
    return sum(1 for f in bfield if f == (1 << W) -1)

for _ in xrange(input()):
    h, w = map(int, raw_input().split())
    block = striped(rotated(striped([raw_input() for _ in xrange(h)])))
    h, w = len(block), len(block[0])
    H, W = map(int, raw_input().split())
    field = [raw_input() for _ in xrange(H)]
    bfield = toBinary(field)
    FLAG = False
    ans = 0
    for _ in xrange(4):
        block = rotated(block)
        h, w = w, h
        #print h,w,H,W
        #print "\n".join(field), "\n"
        #print "\n".join(block), "\n"        
        bblock = toBinary(block)
        for y in xrange(0, H - h + 1):
            for x in xrange(0, W - w + 1):
                if canPut(x, y):
                    FLAG = True
                    #print x,y
                    ans = max(ans, count(puted(x, y)))
    print ans if FLAG else -1

Fixed submission:
while True:
    n = input()
    if n == 0:
        break
    L = [sum(map(int, raw_input().split())) for _ in xrange(n)]
    print "{} {}".format(max(L), min(L))