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

plist = []

for _ in range(q):

    plist.append(list(map(int, input().split())))



alist = []





def bfs(depth: int, l: list):

    global n, m, alist

    if depth == n:

        alist.append(l[1:])

        return

    c = l[depth]

    cl = l.copy()

    cl.append(c)

    bfs(depth+1, cl.copy())

    while c < m:

        c += 1

        cl = l.copy()

        cl.append(c)

        bfs(depth + 1, cl.copy())





bfs(0, [1])

pmax = 0

for a in alist:

    point = 0

    for p in plist:

        if (a[p[1]-1] - a[p[0]-1] == p[2]):

            point += p[3]

    pmax = max(pmax, point)



print(pmax)

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

plist = []

for _ in range(q):

    plist.append(list(map(int, input().split())))



alist = []





def bfs(depth: int, l: list):

    global n, m, alist

    if depth == n:

        alist.append(l[1:])

        return

    c = l[depth]

    cl = l.copy()

    cl.append(c)

    bfs(depth+1, cl)

    while c < m:

        c += 1

        cl = l.copy()

        cl.append(c)

        bfs(depth + 1, cl)





bfs(0, [1])

pmax = 0

for a in alist:

    point = 0

    for p in plist:

        if (a[p[1]-1] - a[p[0]-1] == p[2]):

            point += p[3]

    pmax = max(pmax, point)



print(pmax)

n, m, q = list(map(int, input().split()))
plist = []
for _ in range(q):
    plist.append(list(map(int, input().split())))
alist = []


def bfs(depth: int, l: list):
    global n, m, alist
    if depth == n:
        alist.append(l[1:])
        return
    c = l[depth]
    cl = l.copy()
    cl.append(c)
    bfs(depth + 1, cl.copy())
    while c < m:
        c += 1
        cl = l.copy()
        cl.append(c)
        bfs(depth + 1, cl.copy())


bfs(0, [1])
pmax = 0
for a in alist:
    point = 0
    for p in plist:
        if a[p[1] - 1] - a[p[0] - 1] == p[2]:
            point += p[3]
    pmax = max(pmax, point)
print(pmax)

n, m, q = list(map(int, input().split()))
plist = []
for _ in range(q):
    plist.append(list(map(int, input().split())))
alist = []


def bfs(depth: int, l: list):
    global n, m, alist
    if depth == n:
        alist.append(l[1:])
        return
    c = l[depth]
    cl = l.copy()
    cl.append(c)
    bfs(depth + 1, cl)
    while c < m:
        c += 1
        cl = l.copy()
        cl.append(c)
        bfs(depth + 1, cl)


bfs(0, [1])
pmax = 0
for a in alist:
    point = 0
    for p in plist:
        if a[p[1] - 1] - a[p[0] - 1] == p[2]:
            point += p[3]
    pmax = max(pmax, point)
print(pmax)

-    bfs(depth + 1, cl.copy()) +    bfs(depth + 1, cl) -        bfs(depth + 1, cl.copy()) +        bfs(depth + 1, cl)