{-# LANGUAGE PartialTypeSignatures #-}
module Main (main) where

import Control.Monad
import qualified Data.ByteString.Char8 as C8
import qualified Data.Char as C
import qualified Data.List as L

main :: IO ()
main = do
  [n,m,q] <- readInts
  replicateM q readInts >>= output . solve n m

solve :: Int -> Int -> [[Int]] -> _
solve n m xs = maximum . map go $ genAscList m n
  where
    go as = sum [ d | [a,b,c,d] <- xs, (as!!(b-1)) - (as!!(a-1)) == c ]

genAscList :: Int -> Int -> [[Int]]
genAscList = gen 1
  where
    gen _ m 0 = [ [] ]
    gen a m n = [ i:is | i <- [a..m], is <- gen i m (pred n) ]

output :: _ -> IO ()
output = print

-- utils
readInts :: IO [Int]
readInts = L.unfoldr (C8.readInt . C8.dropWhile C.isSpace) <$> C8.getLine