Buggy python submission (verdict: Time Limit Exceeded):
import itertools

N, M, Q = map(int, input().split())

Q_list = []
for _ in range(Q):
    q = list(map(int, input().split()))
    Q_list.append(q)

A_list = []
for a in itertools.combinations_with_replacement(range(1, M+1), N):
    A_list.append(a)

answer = 0
score_list = []
for A in A_list:
    score = 0
    for i in range(Q):
        a, b, c, d = Q_list[i][0], Q_list[i][1], Q_list[i][2], Q_list[i][3]
        a -=1
        b -=1
        if A[b] - A[a] == c:
            score += d
    answer = max(answer, score)

print(answer)

Fixed submission:
import itertools

N, M, Q = map(int, input().split())

Q_list = []
for _ in range(Q):
    a, b, c, d = list(map(int, input().split()))
    Q_list.append((a, b, c, d))

A_list = []
for a in itertools.combinations_with_replacement(range(1, M+1), N):
    A_list.append(a)

answer = 0
score_list = []
for A in A_list:
    score = 0
    for i in range(Q):
        a, b, c, d = Q_list[i]
        a -=1
        b -=1
        if A[b] - A[a] == c:
            score += d
    answer = max(answer, score)

print(answer)

Buggy python submission (verdict: Time Limit Exceeded):
import itertools

N, M, Q = map(int, input().split())

Q_list = []
for _ in range(Q):
    q = list(map(int, input().split()))
    Q_list.append(q)

A_list = []
for a in itertools.combinations_with_replacement(range(1, M+1), N):
    A_list.append(a)

score_list = []
for A in A_list:
    score = 0
    for i in range(Q):
        a, b, c, d = Q_list[i][0], Q_list[i][1], Q_list[i][2], Q_list[i][3]
        a -=1
        b -=1
        if A[b] - A[a] == c:
            score += d
    score_list.append(score)

print(max(score_list))

Fixed submission:
import itertools

N, M, Q = map(int, input().split())

Q_list = []
for _ in range(Q):
    a, b, c, d = list(map(int, input().split()))
    Q_list.append((a, b, c, d))

A_list = []
for a in itertools.combinations_with_replacement(range(1, M+1), N):
    A_list.append(a)

answer = 0
score_list = []
for A in A_list:
    score = 0
    for i in range(Q):
        a, b, c, d = Q_list[i]
        a -=1
        b -=1
        if A[b] - A[a] == c:
            score += d
    answer = max(answer, score)

print(answer)

Buggy python submission (verdict: Runtime Error):
import itertools

N, M, Q = map(int, input().split())

Q_list = []
for _ in range(Q):
    q = list(map(int, input().split()))
    Q_list.append(q)

A_list = []
for a in itertools.combinations_with_replacement(range(1, M+1), N):
    if a[0]<= a[1] and a[1] <= a[2]:
        A_list.append(a)

score_list = []
for A in A_list:
    score = 0
    for i in range(Q):
        a, b, c, d = Q_list[i][0], Q_list[i][1], Q_list[i][2], Q_list[i][3]
        a -=1
        b -=1
        if A[b] - A[a] == c:
            score += d
    score_list.append(score)

print(max(score_list))

Fixed submission:
import itertools

N, M, Q = map(int, input().split())

Q_list = []
for _ in range(Q):
    a, b, c, d = list(map(int, input().split()))
    Q_list.append((a, b, c, d))

A_list = []
for a in itertools.combinations_with_replacement(range(1, M+1), N):
    A_list.append(a)

answer = 0
score_list = []
for A in A_list:
    score = 0
    for i in range(Q):
        a, b, c, d = Q_list[i]
        a -=1
        b -=1
        if A[b] - A[a] == c:
            score += d
    answer = max(answer, score)

print(answer)