getInts = do
  x <- getLine
  let y = (map$toInt)$words x
  return y
    where
      toInt x = read x ::Int
getMat x
  |x==1 = do
    y <- getInts
    return [y]
  |otherwise = do
    y <- getInts
    z <- getMat (x-1)
    let w = [y] ++ z
    return w

-- [Char]あるいは String を Int にする
toInt :: (String -> Int)
toInt x = read x ::Int

score::[Int]->[Int]->Int
score x abcd
  |(x!!(abcd!!1 -1)) -(x!!(abcd!!0 -1)) == (abcd!!2) = (abcd!!3)
  |otherwise =0

scores x abcdq = sum [score x abcd | abcd <- abcdq]

--使用例
main = do
  nmq <- getInts
  abcdq <- getMat (nmq!!2)
  let x = [[z1,z2,z3,z4,z5,z6,z7,z8,z9,z0] | z1 <-[1..(nmq!!1)],z2 <-[z1..(nmq!!1)],z3 <-[z2..(nmq!!1)],z4 <-[z3..(nmq!!1)],z5 <-[z4..(nmq!!1)],z6 <-[z5..(nmq!!1)],z7 <-[z6..(nmq!!1)],z8 <-[z7..(nmq!!1)],z9 <-[z8..(nmq!!1)],z0 <-[z9..(nmq!!1)]]
  let y = maximum[scores z abcdq | z <- x]
  print y















