Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import sys
    from collections import defaultdict
    sys.setrecursionlimit(10 ** 6)
    input = sys.stdin.readline
    N, M, Q = [int(x) for x in input().strip().split()]
    abcd = [0] * Q
    for q in range(Q):
        abcd[q] = [int(x) for x in input().strip().split()]

    abcd.sort(key=lambda x:(x[0],x[1]))
    ans = 0
    for i in range(1, 2 ** Q):
        vec = [1] * N
        flag = [False] * N
        ans_ = 0
        for q in range(Q):
            # print((i >> q) % 2, end='')
            if (i >> q) % 2 == 0:
                continue
            a, b, c, d = abcd[q]
            if not flag[b-1]:
                if vec[a-1] + c > M:
                    break
                vec[b-1] = vec[a-1] + c
                ans_ += d
                flag[b-1] = True
                flag[a-1] = True
            elif vec[b-1] - vec[a-1] == c:
                ans_ += d
            else:
                break
        
        if vec != sorted(vec):
            continue
            
        ans = max(ans, ans_)

    print(ans)

if __name__ == '__main__':
    main()

Fixed submission:
def main():
    import sys
    from collections import defaultdict
    sys.setrecursionlimit(10 ** 6)
    input = sys.stdin.readline
    N, M, Q = [int(x) for x in input().strip().split()]
    abcd = [0] * Q
    for q in range(Q):
        abcd[q] = [int(x) for x in input().strip().split()]

    abcd.sort(key=lambda x:(x[0],x[1]))
    vecs = []

    def makevecs(n, vecs, A):
        if n == N:
            vecs.append(A)
            return None
        for b in range(A[-1], M+1):
            makevecs(n+1, vecs, A+[b])
        return None
        
    makevecs(1, vecs, [1])
    ans = 0
    for vec in vecs:
        ans_ = 0
        for q in range(Q):
            a, b, c, d = abcd[q]
            if vec[b-1] - vec[a-1] == c:
                ans_ += d
        
        ans = max(ans, ans_)

    print(ans)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import sys
    from collections import defaultdict
    sys.setrecursionlimit(10 ** 6)
    input = sys.stdin.readline
    N, M, Q = [int(x) for x in input().strip().split()]
    abcd = [0] * Q
    for q in range(Q):
        abcd[q] = [int(x) for x in input().strip().split()]

    abcd.sort(key=lambda x:(x[0],x[1]))
    ans = 0
    for i in range(1, 2 ** Q):
        vec = [1] * N
        flag = [False] * N
        ans_ = 0
        for q in range(Q):
            # print((i >> q) % 2, end='')
            if (i >> q) % 2 == 0:
                continue
            a, b, c, d = abcd[q]
            if not flag[b-1]:
                if vec[a-1] + c > M:
                    break
                vec[b-1] = vec[a-1] + c
                ans_ += d
                flag[b-1] = True
                flag[a-1] = True
            elif vec[b-1] - vec[a-1] == c:
                ans_ += d
            else:
                break
        
        if vec != sorted(vec):
            continue
            
        # print(bin(i), vec, ans_)
        if ans_ == 448129:
            print('★')
        ans = max(ans, ans_)

    print(ans)

if __name__ == '__main__':
    main()

Fixed submission:
def main():
    import sys
    from collections import defaultdict
    sys.setrecursionlimit(10 ** 6)
    input = sys.stdin.readline
    N, M, Q = [int(x) for x in input().strip().split()]
    abcd = [0] * Q
    for q in range(Q):
        abcd[q] = [int(x) for x in input().strip().split()]

    abcd.sort(key=lambda x:(x[0],x[1]))
    vecs = []

    def makevecs(n, vecs, A):
        if n == N:
            vecs.append(A)
            return None
        for b in range(A[-1], M+1):
            makevecs(n+1, vecs, A+[b])
        return None
        
    makevecs(1, vecs, [1])
    ans = 0
    for vec in vecs:
        ans_ = 0
        for q in range(Q):
            a, b, c, d = abcd[q]
            if vec[b-1] - vec[a-1] == c:
                ans_ += d
        
        ans = max(ans, ans_)

    print(ans)

if __name__ == '__main__':
    main()