import Data.List
import Data.Ord
import Data.Maybe

data Record = Record {tid::Int, p::Int, score::Int}
instance Eq Record where
 (==) (Record tid1 p1 t1) (Record tid2 p2 t2) = p1 == p2 && t1 == t2
instance Ord Record where
 (<=) (Record tid1 p1 t1) (Record tid2 p2 t2) = 
  (p1 > p2) || (p1 == p2 && t1 < t2) || (p1 == p2 && t1 == t2 && tid1 > tid2)
instance Show Record where
 show (Record tid _ _) = show tid

main = interact $ unlines . map (show' . group . sort . judge) . read' . lines
 where
  read' ("0 0 0 0":_) = []
  read' (mtpr:xs) = (m,t,p,logs) : read' xs'
   where
    [m,t,p,r] = map read (words mtpr)
    (logs',xs') = splitAt r xs
    logs = map (map read . words) logs'
  show' [rs] = wordsBy "=" (map show . reverse . sortBy (comparing tid) $ rs)
  show' (rs:rss) = wordsBy "=" (map show . reverse . sortBy (comparing tid) $ rs) ++ "," ++ show' rss

wordsBy _ [] = []
wordsBy _ [x] = x
wordsBy punc (x:xs) = x ++ punc ++ wordsBy punc xs

judge (m,t,p,logs) = map rec (foldl' total initr logs)
 where
  rec (t,pr,s,_) = Record t (length pr) s
  fst' (x,_,_,_) = x
  initr = [(tid,[],0,replicate p 0) | tid<-[1..t]]
  total acc [time,tid,pid,stat]
   | stat /= 0 = let (t,pr,s,pe) = query tid acc in (t,pr,s,wrong pe) : (filter ((/=tid).fst') acc)
   | otherwise = let (t,pr,s,pe) = query tid acc in (t,pid:pr,score pe s,pe) : (filter ((/=tid).fst') acc)
   where
    score pe s' = s' + time + (pe!!(pid-1)*20)
    wrong = map (\(i,w) -> if i==pid then w+1 else w) . zip [1..]
    query i = fromJust . find ((==i) . fst')