Buggy python submission (verdict: Time Limit Exceeded):
N, M, Q = list(map(int, input().strip().split()))
pattern_list = []
for _ in range(Q):
	pattern = tuple(map(int, input().strip().split()))
	pattern_list.append(pattern)

def create(N,M):
	S = [[] for _ in range(N+1)]
	for m in range(1,M+1):
		S[1].append([m])
	for n in range(1,N):
		for s in S[n]:
			Max = s[-1]
			for i in range(Max, M+1):
				S[n+1].append(s+[i])
	return S

highest = 0
for letters in create(N,M)[N]:
	point = 0
	for a,b,c,d in pattern_list:
		if int(c) == letters[int(b-1)]-letters[int(a-1)]:
			point += d
	highest = max([highest, point])

print(highest)

Fixed submission:
N, M, Q = list(map(int, input().strip().split()))
pattern_list = []
for _ in range(Q):
    pattern = tuple(map(int, input().strip().split()))
    pattern_list.append(pattern)

def create(N,M):
    S = [[] for _ in range(N+1)]
    for m in range(1,M+1):
        S[1].append([m])
    for n in range(1,N):
        for s in S[n]:
            Max = s[-1]
            for i in range(Max, M+1):
                S[n+1].append(s+[i])
    return S

highest = 0
for letters in create(N,M)[N]:
    point = 0
    for a,b,c,d in pattern_list:
        if c == letters[b-1]-letters[a-1]:
            point += d
    highest = max([highest, point])

print(highest)

Buggy python submission (verdict: Time Limit Exceeded):
N, M, Q = list(map(int, input().strip().split()))
pattern_list = []
shortest = 0
for _ in range(Q):
	pattern = tuple(map(int, input().strip().split()))
	shortest = max([shortest, pattern[0], pattern[1]])
	pattern_list.append(pattern)

def create(N,M):
	S = [[] for _ in range(N+1)]
	for m in range(1,M+1):
		S[1].append([m])
	for n in range(1,N):
		for s in S[n]:
			Max = s[-1]
			for i in range(Max, M+1):
				S[n+1].append(s+[i])
	return S

highest = 0
for n, n_letters in enumerate(create(N, M)):
	if n < shortest: continue
	for letters in n_letters:
		point = 0
		for a,b,c,d in pattern_list:
			if int(c) == letters[int(b-1)]-letters[int(a-1)]:
				point += d
		highest = max([highest, point])

print(highest)

Fixed submission:
N, M, Q = list(map(int, input().strip().split()))
pattern_list = []
for _ in range(Q):
    pattern = tuple(map(int, input().strip().split()))
    pattern_list.append(pattern)

def create(N,M):
    S = [[] for _ in range(N+1)]
    for m in range(1,M+1):
        S[1].append([m])
    for n in range(1,N):
        for s in S[n]:
            Max = s[-1]
            for i in range(Max, M+1):
                S[n+1].append(s+[i])
    return S

highest = 0
for letters in create(N,M)[N]:
    point = 0
    for a,b,c,d in pattern_list:
        if c == letters[b-1]-letters[a-1]:
            point += d
    highest = max([highest, point])

print(highest)

Buggy python submission (verdict: Time Limit Exceeded):
N, M, Q = list(map(int, input().strip().split()))
pattern_list = []
shortest = []
for _ in range(Q):
	pattern = tuple(map(int, input().strip().split()))
	shortest += [pattern[0], pattern[1]]
	pattern_list.append(pattern)
shortest = max(shortest)

def create(N,M):
	S = [[] for _ in range(N+1)]
	for m in range(1,M+1):
		S[1].append([m])
	for n in range(1,N):
		for s in S[n]:
			Max = s[-1]
			for i in range(Max, M+1):
				S[n+1].append(s+[i])
	return S

highest = 0
for n, n_letters in enumerate(create(N, M)):
	if n < shortest: continue
	for letters in n_letters:
		point = 0
		for a,b,c,d in pattern_list:
			if int(c) == letters[int(b-1)]-letters[int(a-1)]:
				point += d
		highest = max([highest, point])

print(highest)

Fixed submission:
N, M, Q = list(map(int, input().strip().split()))
pattern_list = []
for _ in range(Q):
    pattern = tuple(map(int, input().strip().split()))
    pattern_list.append(pattern)

def create(N,M):
    S = [[] for _ in range(N+1)]
    for m in range(1,M+1):
        S[1].append([m])
    for n in range(1,N):
        for s in S[n]:
            Max = s[-1]
            for i in range(Max, M+1):
                S[n+1].append(s+[i])
    return S

highest = 0
for letters in create(N,M)[N]:
    point = 0
    for a,b,c,d in pattern_list:
        if c == letters[b-1]-letters[a-1]:
            point += d
    highest = max([highest, point])

print(highest)