import Control.Monad
import Data.List

solveSub as [] = 0
solveSub as (qs:qss) =
    if (as!!(b-1)) - (as!!(a-1)) == c
        then d + solveSub as qss
        else solveSub as qss 
    where
        a = qs!!0
        b = qs!!1
        c = qs!!2
        d = qs!!3

solve [] qss = []
solve (as:ass) qss = solveSub as qss : solve ass qss

getass 0 m i = [[]]
getass n m i = [a:as | a <- [i..m], as <- getass (n-1) m a]

main = do
    [n,m,q] <- map read . words <$> getLine :: IO [Int]
    qss <- replicateM q (map read . words <$> getLine) :: IO [[Int]]
    let ass = getass n m 1
    print $ maximum $ solve ass qss
