c=0

for W in iter(input,'0'):

 c+=1

 W=int(W)

 d=[0]*-~W

 for _ in[0]*int(eval(input())):

  v,w=list(map(int,input().split(',')))

  for i in range(W,w-1,-1):

   t=d[i-w]+v

   if d[i]<t:d[i]=t

 print(f'Case {c}:\n{d[W]}\n{d.index(d[W])}')

c=0

for W in iter(input,'0'):

 c+=1

 W=int(W)

 d=[0]*-~W

 for _ in[0]*int(eval(input())):

  v,w=list(map(int,input().split(',')))

  for i in range(W,w-1,-1):

   if d[i]<d[i-w]+v:d[i]=d[i-w]+v

 print(f'Case {c}:\n{d[W]}\n{d.index(d[W])}')

c = 0
for W in iter(input, "0"):
    c += 1
    W = int(W)
    d = [0] * -~W
    for _ in [0] * int(eval(input())):
        v, w = list(map(int, input().split(",")))
        for i in range(W, w - 1, -1):
            t = d[i - w] + v
            if d[i] < t:
                d[i] = t
    print(f"Case {c}:\n{d[W]}\n{d.index(d[W])}")

c = 0
for W in iter(input, "0"):
    c += 1
    W = int(W)
    d = [0] * -~W
    for _ in [0] * int(eval(input())):
        v, w = list(map(int, input().split(",")))
        for i in range(W, w - 1, -1):
            if d[i] < d[i - w] + v:
                d[i] = d[i - w] + v
    print(f"Case {c}:\n{d[W]}\n{d.index(d[W])}")

-            t = d[i - w] + v -            if d[i] < t: -                d[i] = t +            if d[i] < d[i - w] + v: +                d[i] = d[i - w] + v