# AOJ 0042 A Thief

# Python3 2018.6.22 bal4u



MAX = 1003



for cno in range(1, 100):

	lim = int(input())

	if lim == 0: break

	print("Case ", cno, ":", sep='')

	N = int(input())

	vw = [(0, 0)]

	for i in range(N):

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

		vw.append((v, w))



	V = [[0 for j in range(MAX)] for i in range(2)]

	W = [[0 for j in range(MAX)] for i in range(2)]		

	k1, k2 = 0, 1

	for i in range(1, N+1):

		for j in range(1, lim+1):

			v, w = vw[i]

			if w > j:

				V[k2][j] = V[k1][j]

				W[k2][j] = W[k1][j]

			else:

				d = j - w

				if V[k1][d] + v >= V[k1][j]:

					V[k2][j] = V[k1][d] + v

					W[k2][j] = W[k1][d] + w

				else:

					V[k2][j] = V[k1][j]

					W[k2][j] = W[k1][j]

		k1, k2 = k2, k1

	print(V[k1][lim], W[k1][lim], sep='\n')

# AOJ 0042 A Thief

# Python3 2018.6.22 bal4u



MAX = 1003



for cno in range(1, 100):

	lim = int(input())

	if lim == 0: break

	print("Case ", cno, ":", sep='')

	N = int(input())

	V = [[0 for j in range(MAX)] for i in range(2)]

	W = [[0 for j in range(MAX)] for i in range(2)]		

	k1, k2 = 0, 1

	for i in range(1, N+1):

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

		for j in range(1, w):

			V[k2][j] = V[k1][j]

			W[k2][j] = W[k1][j]

		for j in range(w, lim+1):

			d = j - w

			if V[k1][d] + v >= V[k1][j]:

				V[k2][j] = V[k1][d] + v

				W[k2][j] = W[k1][d] + w

			else:

				V[k2][j] = V[k1][j]

				W[k2][j] = W[k1][j]

		k1, k2 = k2, k1

	print(V[k1][lim], W[k1][lim], sep='\n')

# AOJ 0042 A Thief
# Python3 2018.6.22 bal4u
MAX = 1003
for cno in range(1, 100):
    lim = int(input())
    if lim == 0:
        break
    print("Case ", cno, ":", sep="")
    N = int(input())
    vw = [(0, 0)]
    for i in range(N):
        v, w = list(map(int, input().split(",")))
        vw.append((v, w))
    V = [[0 for j in range(MAX)] for i in range(2)]
    W = [[0 for j in range(MAX)] for i in range(2)]
    k1, k2 = 0, 1
    for i in range(1, N + 1):
        for j in range(1, lim + 1):
            v, w = vw[i]
            if w > j:
                V[k2][j] = V[k1][j]
                W[k2][j] = W[k1][j]
            else:
                d = j - w
                if V[k1][d] + v >= V[k1][j]:
                    V[k2][j] = V[k1][d] + v
                    W[k2][j] = W[k1][d] + w
                else:
                    V[k2][j] = V[k1][j]
                    W[k2][j] = W[k1][j]
        k1, k2 = k2, k1
    print(V[k1][lim], W[k1][lim], sep="\n")

# AOJ 0042 A Thief
# Python3 2018.6.22 bal4u
MAX = 1003
for cno in range(1, 100):
    lim = int(input())
    if lim == 0:
        break
    print("Case ", cno, ":", sep="")
    N = int(input())
    V = [[0 for j in range(MAX)] for i in range(2)]
    W = [[0 for j in range(MAX)] for i in range(2)]
    k1, k2 = 0, 1
    for i in range(1, N + 1):
        v, w = map(int, input().split(","))
        for j in range(1, w):
            V[k2][j] = V[k1][j]
            W[k2][j] = W[k1][j]
        for j in range(w, lim + 1):
            d = j - w
            if V[k1][d] + v >= V[k1][j]:
                V[k2][j] = V[k1][d] + v
                W[k2][j] = W[k1][d] + w
            else:
                V[k2][j] = V[k1][j]
                W[k2][j] = W[k1][j]
        k1, k2 = k2, k1
    print(V[k1][lim], W[k1][lim], sep="\n")

-    vw = [(0, 0)] -    for i in range(N): -        v, w = list(map(int, input().split(","))) -        vw.append((v, w)) -        for j in range(1, lim + 1): -            v, w = vw[i] -            if w > j: +        v, w = map(int, input().split(",")) +        for j in range(1, w): +            V[k2][j] = V[k1][j] +            W[k2][j] = W[k1][j] +        for j in range(w, lim + 1): +            d = j - w +            if V[k1][d] + v >= V[k1][j]: +                V[k2][j] = V[k1][d] + v +                W[k2][j] = W[k1][d] + w +            else: -            else: -                d = j - w -                if V[k1][d] + v >= V[k1][j]: -                    V[k2][j] = V[k1][d] + v -                    W[k2][j] = W[k1][d] + w -                else: -                    V[k2][j] = V[k1][j] -                    W[k2][j] = W[k1][j]