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

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

q = []

for i in range(1, m+1):
    q.append(str(i-1))

while True:
    if len(q[0]) == n:
        break
    else:
        item = q.pop(0)
        for i in range(int(item[-1]), m):
            q.append(''.join([item, str(i)]))

ret = 0

for item in q:
    score = 0
    for t_item in t_list:
        if int(item[t_item[1]-1]) - int(item[t_item[0]-1]) == t_item[2]:
            score += t_item[3]
    ret = max(ret, score)

print(ret)


Fixed submission:
import itertools

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

q = []

for i in range(1, m+1):
    q.append(i)

ret = 0

for item in itertools.combinations_with_replacement(q, n):
    score = 0
    for t_item in t_list:
        if item[t_item[1]-1] - item[t_item[0]-1] == t_item[2]:
            score += t_item[3]
    ret = max(ret, score)

print(ret)

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

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

q = []

for i in range(1, m+1):
    q.append(str(i-1))

while True:
    if len(q[0]) == n:
        break
    else:
        item = q.pop(0)
        for i in range(int(item[-1]), m):
            q.append(''.join([item, str(i)]))

ret = 0

for item in q:
    score = 0
    for t_item in t_list:
        if int(item[t_item[1]-1]) - int(item[t_item[0]-1]) == t_item[2]:
            score += t_item[3]
    ret = max(ret, score)

print(ret)


Fixed submission:
import itertools

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

q = []

for i in range(1, m+1):
    q.append(i)

ret = 0

for item in itertools.combinations_with_replacement(q, n):
    score = 0
    for t_item in t_list:
        if item[t_item[1]-1] - item[t_item[0]-1] == t_item[2]:
            score += t_item[3]
    ret = max(ret, score)

print(ret)

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

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

q = []

for i in range(1, m+1):
    q.append(str(i-1))

while True:
    if len(q[0]) == n:
        break
    else:
        item = q.pop(0)
        for i in range(int(item[-1]), m):
            q.append(item+str(i))

ret = 0

for item in q:
    score = 0
    for t_item in t_list:
        if int(item[t_item[1]-1]) - int(item[t_item[0]-1]) == t_item[2]:
            score += t_item[3]
    ret = max(ret, score)

print(ret)


Fixed submission:
import itertools

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

q = []

for i in range(1, m+1):
    q.append(i)

ret = 0

for item in itertools.combinations_with_replacement(q, n):
    score = 0
    for t_item in t_list:
        if item[t_item[1]-1] - item[t_item[0]-1] == t_item[2]:
            score += t_item[3]
    ret = max(ret, score)

print(ret)

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

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

q = []

for i in range(1, m+1):
    q.append(str(i-1))

while True:
    if len(q[0]) == n:
        break
    else:
        item = q.pop(0)
        for i in range(int(item[-1]), m):
            q.append(item+str(i))

ret = 0

for item in q:
    score = 0
    for t_item in t_list:
        if int(item[t_item[1]-1]) - int(item[t_item[0]-1]) == t_item[2]:
            score += t_item[3]
    ret = max(ret, score)

print(ret)


Fixed submission:
import itertools

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

q = []

for i in range(1, m+1):
    q.append(i)

ret = 0

for item in itertools.combinations_with_replacement(q, n):
    score = 0
    for t_item in t_list:
        if item[t_item[1]-1] - item[t_item[0]-1] == t_item[2]:
            score += t_item[3]
    ret = max(ret, score)

print(ret)

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

t_list = []
n, m, q = map(int, input().split())
my_set = set()
for i in range(q):
    t_list.append(list(map(int, input().split())))
    my_set.add(t_list[-1][0]-1)
    my_set.add(t_list[-1][1]-1)

q = []

for i in range(1, m+1):
    q.append([i])

while True:
    if len(q[0]) == n:
        break
    else:
        item = q.pop(0)
        if len(item) not in my_set:
            tmp = copy.deepcopy(item)
            tmp.append(item[-1])
            q.append(tmp)
        else:
            for i in range(item[-1], m+1):
                tmp = copy.deepcopy(item)
                tmp.append(i)
                q.append(tmp)

ret = 0

for item in q:
    score = 0
    for t_item in t_list:
        if item[t_item[1]-1] - item[t_item[0]-1] == t_item[2]:
            score += t_item[3]
    ret = max(ret, score)

print(ret)


Fixed submission:
import itertools

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

q = []

for i in range(1, m+1):
    q.append(i)

ret = 0

for item in itertools.combinations_with_replacement(q, n):
    score = 0
    for t_item in t_list:
        if item[t_item[1]-1] - item[t_item[0]-1] == t_item[2]:
            score += t_item[3]
    ret = max(ret, score)

print(ret)

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

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

q = []

for i in range(1, m+1):
    q.append([i])

while True:
    if len(q[0]) == n:
        break
    else:
        item = q.pop(0)
        for i in range(item[-1], m+1):
            tmp = copy.deepcopy(item)
            tmp.append(i)
            q.append(tmp)

ret = 0

for item in q:
    score = 0
    for t_item in t_list:
        if item[t_item[1]-1] - item[t_item[0]-1] == t_item[2]:
            score += t_item[3]
    ret = max(ret, score)

print(ret)


Fixed submission:
import itertools

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

q = []

for i in range(1, m+1):
    q.append(i)

ret = 0

for item in itertools.combinations_with_replacement(q, n):
    score = 0
    for t_item in t_list:
        if item[t_item[1]-1] - item[t_item[0]-1] == t_item[2]:
            score += t_item[3]
    ret = max(ret, score)

print(ret)