n,m,q = list(map(int,input().split()))

abcd = [[*list(map(int,input().split()))] for _ in range(q)]

from itertools import *

ans = 0

for A in combinations_with_replacement(list(range(1, m+1)), n): # nHr

    now = 0

    for a,b,c,d in abcd:

        if A[b-1] - A[a-1] == c: now += d

    ans = max(ans, now)

print(ans)

n,m,q = list(map(int,input().split()))

abcd = [[*list(map(int,input().split()))] for _ in range(q)]

from itertools import *

ans = 0

for A in combinations_with_replacement(list(range(1, m+1)), n): # nHr

    now = sum(d if A[b-1] - A[a-1] == c else 0 for a,b,c,d in abcd)

    ans = max(ans, now)

print(ans)

n, m, q = list(map(int, input().split()))
abcd = [[*list(map(int, input().split()))] for _ in range(q)]
from itertools import *

ans = 0
for A in combinations_with_replacement(list(range(1, m + 1)), n):  # nHr
    now = 0
    for a, b, c, d in abcd:
        if A[b - 1] - A[a - 1] == c:
            now += d
    ans = max(ans, now)
print(ans)

n, m, q = list(map(int, input().split()))
abcd = [[*list(map(int, input().split()))] for _ in range(q)]
from itertools import *

ans = 0
for A in combinations_with_replacement(list(range(1, m + 1)), n):  # nHr
    now = sum(d if A[b - 1] - A[a - 1] == c else 0 for a, b, c, d in abcd)
    ans = max(ans, now)
print(ans)

-    now = 0 -    for a, b, c, d in abcd: -        if A[b - 1] - A[a - 1] == c: -            now += d +    now = sum(d if A[b - 1] - A[a - 1] == c else 0 for a, b, c, d in abcd)