Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
# Your code here!

def solve(n, m, q, arrays):
    from itertools import combinations_with_replacement
    pattern = list(combinations_with_replacement(range(1, m+1), n))

    maxp = 0
    for x in pattern:
        get = 0
        for array in arrays:
            if x[array[1]-1] - x[array[0]-1] == array[2]:
                get += array[3]

        maxp = max(maxp, get)

    print(maxp)

n, m, q, *aa = map(int, open(0).read().split())

from numpy import array_split
arrays = list(array_split(aa, q))

solve(n, m, q, arrays)


Fixed submission:
# coding: utf-8
# Your code here!

def solve(n, m, q, arrays):
    from itertools import combinations_with_replacement
    pattern = list(combinations_with_replacement(range(1, m+1), n))

    maxp = 0
    for x in pattern:
        get = 0
        for array in arrays:
            if x[array[1]-1] - x[array[0]-1] == array[2]:
                get += array[3]

        maxp = max(maxp, get)

    print(maxp)

n, m, q, *aa = map(int, open(0).read().split())

arrays = []
loop = 0
array = []
for x in aa:
    array.append(x)
    loop += 1
    if loop % 4 == 0:
        arrays.append(array)
        array = []

solve(n, m, q, arrays)

Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
# Your code here!

def solve(n, m, q, arrays):
    import itertools
    pattern = list(itertools.combinations_with_replacement(range(1, m+1), n))

    maxp = 0
    for x in pattern:
        get = 0
        for array in arrays:
            
            if x[array[1]-1] - x[array[0]-1] == array[2]:
                get += array[3]
        maxp = max(maxp, get)

    print(maxp)

n, m, q, *aa = map(int, open(0).read().split())

import numpy as np
arrays = list(np.array_split(aa, q))

solve(n, m, q, arrays)


Fixed submission:
# coding: utf-8
# Your code here!

def solve(n, m, q, arrays):
    from itertools import combinations_with_replacement
    pattern = list(combinations_with_replacement(range(1, m+1), n))

    maxp = 0
    for x in pattern:
        get = 0
        for array in arrays:
            if x[array[1]-1] - x[array[0]-1] == array[2]:
                get += array[3]

        maxp = max(maxp, get)

    print(maxp)

n, m, q, *aa = map(int, open(0).read().split())

arrays = []
loop = 0
array = []
for x in aa:
    array.append(x)
    loop += 1
    if loop % 4 == 0:
        arrays.append(array)
        array = []

solve(n, m, q, arrays)

Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
# Your code here!

def solve(n, m, q, arrays):
    import itertools
    pattern = list(itertools.product(range(1, m+1), repeat=n))

    maxp = 0
    for x in pattern:
        if sorted(x) != list(x):
            continue
        
        get = 0
        for array in arrays:
            if x[array[1]-1] - x[array[0]-1] == array[2]:
                get += array[3]

        maxp = max(maxp, get)

    print(maxp)

n, m, q, *aa = map(int, open(0).read().split())

import numpy as np
arrays = list(np.array_split(aa, q))

solve(n, m, q, arrays)



Fixed submission:
# coding: utf-8
# Your code here!

def solve(n, m, q, arrays):
    from itertools import combinations_with_replacement
    pattern = list(combinations_with_replacement(range(1, m+1), n))

    maxp = 0
    for x in pattern:
        get = 0
        for array in arrays:
            if x[array[1]-1] - x[array[0]-1] == array[2]:
                get += array[3]

        maxp = max(maxp, get)

    print(maxp)

n, m, q, *aa = map(int, open(0).read().split())

arrays = []
loop = 0
array = []
for x in aa:
    array.append(x)
    loop += 1
    if loop % 4 == 0:
        arrays.append(array)
        array = []

solve(n, m, q, arrays)