Problem:
ICPC Ranking Your mission in this problem is to write a program
which, given the submission log of an ICPC (International Collegiate Programming Contest),
determines team rankings. The log is a sequence of records of program submission
in the order of submission.
A record has four fields: elapsed time, team number,
problem number, and judgment.
The elapsed time is the time elapsed from the beginning of the contest
to the submission.
The judgment field tells whether the submitted program was
correct or incorrect, and when incorrect,
what kind of an error was found. The team ranking is determined according to the following rules.
Note that the rule set shown here is one used in the real ICPC World Finals and Regionals,
with some detail rules omitted for simplification.  Teams that solved more problems are ranked higher.  Among teams that solve the same number of problems, 
ones with smaller total consumed time are ranked higher.  If two or more teams solved the same number of problems, and their
total consumed times are the same, they are ranked the same.  The total consumed time is the sum of the consumed time for each problem solved.
The consumed time for a solved problem is the elapsed time 
of the accepted submission plus 20 penalty minutes for every previously rejected submission for that problem. If a team did not solve a problem, the consumed time for the problem is zero, and thus even if there are several incorrect submissions, no penalty is given. You can assume that a team never submits a program for a problem
after the correct submission for the same problem. Input The input is a sequence of datasets each in the following format.
The last dataset is followed by a line with four zeros.  M  T  P  R   m 1  t 1  p 1  j 1   m 2  t 2  p 2  j 2  .....  m R  t R  p R  j R   The first line of a dataset contains four integers M , T , P , and R . M is the duration of the contest. T is the number of teams. P is the number of problems. R is the number of submission records.
The relations 
120 ≤ M ≤ 300,
1 ≤ T ≤ 50,
1 ≤ P ≤ 10,
and 0 ≤ R ≤ 2000
hold for these values.
Each team is assigned a team number between 1 and T , inclusive.
Each problem is assigned a problem number between 1 and P , inclusive. Each of the following R lines contains a submission record
with four integers m k , t k , p k , and j k (1 ≤ k ≤ R ). m k is the elapsed time. t k is the team number. p k is the problem number. j k is the judgment
(0 means correct, and other values mean incorrect).
The relations
0 ≤ m k ≤ M −1,
1 ≤ t k ≤ T ,
1 ≤ p k ≤ P ,
and 0 ≤ j k ≤ 10
hold for these values. The elapsed time fields are rounded off to the nearest minute. Submission records are given in the order of submission.
Therefore, if i < j ,
the i -th submission is done before the j -th submission
( m i ≤ m j ).
In some cases, you can determine the ranking of two teams
with a difference less than a minute, by using this fact.
However, such a fact is never used in the team ranking.
Teams are ranked only using time information in minutes. Output For each dataset, your program should output team numbers (from 1 to T ),
higher ranked teams first.
The separator between two team numbers should be a comma.
When two teams are ranked the same, the separator between them
should be an equal sign.
Teams ranked the same should be listed
in decreasing order of their team numbers. Sample Input 300 10 8 5
50 5 2 1
70 5 2 0
75 1 1 0
100 3 1 0
150 3 2 0
240 5 5 7
50 1 1 0
60 2 2 0
70 2 3 0
90 1 3 0
120 3 5 0
140 4 1 0
150 2 4 1
180 3 5 4
15 2 2 1
20 2 2 1
25 2 2 0
60 1 1 0
120 5 5 4
15 5 4 1
20 5 4 0
40 1 1 0
40 2 2 0
120 2 3 4
30 1 1 0
40 2 1 0
50 2 2 0
60 1 2 0
120 3 3 2
0 1 1 0
1 2 2 0
300 5 8 0
0 0 0 0 Output for the Sample Input 3,1,5,10=9=8=7=6=4=2
2,1,3,4,5
1,2,3
5=2=1,4=3
2=1
1,2,3
5=4=3=2=1

Solution (Python):
"ICPCの順位づけ"
while True:
    M, T, P, R = map(int, input().split())
    if M == 0:
        break
    submits = []  
    for i in range(R):
        submits.append(list(map(int, input().split())))

    team_scores = {}
    for i in range(1, T+1):
        team_scores[i] = [0, 0]  

    for i in range(R):
        if submits[i][3] == 0:
            add_time = 0
            for j in range(0, i):
                if submits[j][1] == submits[i][1] and submits[j][2] == submits[i][2]:
                    add_time += 20
            team_scores[submits[i][1]][0] += 1
            team_scores[submits[i][1]][1] += (submits[i][0] + add_time)

    team_scores = sorted(team_scores.items(), key=lambda x: (x[1][0], -x[1][1], x[0]), reverse=True)

    ans = ''
    for i in range(len(team_scores)):
        if i == 0:
            ans += str(team_scores[i][0])
        else:
            if team_scores[i][1] == team_scores[i-1][1]:
                ans += '='+str(team_scores[i][0])
            else:
                ans += ','+str(team_scores[i][0])

    print(ans)

Solution (Python):
"""
300 10 8 5
50 5 2 1
70 5 2 0
75 1 1 0
100 3 1 0
150 3 2 0
"""
while True:
  M,T,P,R = map(int,input().split())
  if M+T+P+R == 0:
    break
  
  Tm = [[i+1,0,0,[0 for j in range(P)]]for i in range(T)]
  
  for i in range(R):
    m,t,p,j = map(int,input().split())
    if j==0:
      Tm[t-1][1] += 1
      Tm[t-1][2] += m+Tm[t-1][3][p-1]*20
      Tm[t-1][3][p-1] = 0
    else:
      Tm[t-1][3][p-1] += 1
  Tm.sort(key=lambda x:(x[1],-x[2],x[0]), reverse=True)
  ans = []
  for i in range(T):
    ans.append(str(Tm[i][0]))
    if i<T-1:
      if Tm[i][1]==Tm[i+1][1] and Tm[i][2]==Tm[i+1][2]:
        ans.append("=")
      else:
        ans.append(",")
  print("".join(ans))

Solution (Python):
import sys
while(True):
    M,T,P,R=map(int,input().split())
    if (M,T,P,R)==(0,0,0,0):
        break
    points=[0 for i in range(T)]
    time=[0 for i in range(T)]
    penalty=[[0 for i in range(T)] for i in range(P)]
    for i in range(R):
        m,t,p,j=map(int,input().split())
        
        if j!=0:
            penalty[p-1][t-1]+=1
            continue
        points[t-1]+=1
        time[t-1]+=m+penalty[p-1][t-1]*20
    teams=[(points[i],time[i],i+1) for i in range(T)]
    teams.sort(key=lambda x:(-x[0],x[1],-x[2]))
    st=str(teams[0][2])
    for i in range(1,T):
        if (teams[i-1][0],teams[i-1][1])==(teams[i][0],teams[i][1]):
            st=st+"="
        else:
            st=st+","
        st=st+str(teams[i][2])
    print(st)

Solution (Python):
def main():
    while True:
        
        M,T,P,R = map(int, input().split(" "))
        if M == 0: break

        
        data = [[0 for i in range(P)] for j in range(T)]
        
        res = [[0, 0] for j in range(T)]
        for item in range(T):
            res[item].insert(0, item + 1)
        
        for i in range(R):
            
            m,t,p,j = map(int, input().split(" "))

            
            if j == 0: data[t - 1][p - 1] = -(data[t - 1][p - 1]) * 20 + m + 1
            else: data[t - 1][p - 1] -= 1
            
        for i in range(len(data)):
            for item in data[i]:
                if item < 0: continue
                if item != 0:
                    res[i][1] += 1
                    res[i][2] -= (item - 1)

        res = sorted(res, key = lambda x:(x[1], x[2], x[0]), reverse = True)
        
        output(res)

def output(listt):
    print(str(listt[0][0]), end = "")
    for i in range(1, len(listt)):
        if listt[i][1] == listt[i - 1][1] and listt[i][2] == listt[i - 1][2]:
            print("=" + str(listt[i][0]), end = "")
        else: print("," + str(listt[i][0]), end = "")
    print("")
main()

Solution (Python):
from collections import defaultdict


class Team:
    def __init__(self, num):
        self.num = num
        self.score = 0
        self.time = 0
        self.penalty = defaultdict(int)


while True:
    _, T, _, R = map(int, input().split())
    if T == 0:break
    teams = [Team(i + 1) for i in range(T)]
    for _ in range(R):
        m, t, p, j = map(int, input().split())
        if j != 0:
            teams[t - 1].penalty[p] += 20
        if j == 0:
            teams[t - 1].score += 1
            teams[t - 1].time += m + teams[t - 1].penalty[p]

    teams.sort(key=lambda team: (-team.score, team.time, -team.num))
    ans = str(teams[0].num)
    pre_score = teams[0].score
    pre_time = teams[0].time
    for i in range(1, T):
        team = teams[i]
        ans += "=" if team.score == pre_score and team.time == pre_time else ","
        ans += str(team.num)
        pre_score = team.score
        pre_time = team.time
    print(ans)

Solution (Python):
import sys

input = sys.stdin.readline

for e in iter(input, '0 0 0 0\n'):
    M, T, P, R = map(int, e.split())

    result = [[0, M, i] + [0] * P for i in range(T + 1)]

    for _ in range(R):
        m, t, p, j = map(int, input().split())
        team_data = result[t]
        if j:
            team_data[-p] += 1
        else:
            team_data[0] += 1
            team_data[1] -= m + 20 * team_data[-p]

    result.sort(reverse=True)
    result.pop()

    for td1, td2 in zip(result[:-1], result[1:]):
        if td1[0] == td2[0] and td1[1] == td2[1]:
            print(td1[2], end='=')
        else:
            print(td1[2], end=',')
    print(result[-1][2])

Solution (Python):

while 1:
    m,t,p,r=map(int,input().split())
    if m==0:
        break
    teamdata=[[0,0,i+1,set()] for i in range(t)]
    data=[]
    for i in range(r):
        a,b,c,d=map(int,input().split())
        if d==0:
            teamdata[b-1][0]+=1
            teamdata[b-1][1]+=a
            teamdata[b-1][3].add(c)
        data.append([a,b,c,d])
    for a,b,c,d in data:
        if d!=0 and (c in teamdata[b-1][3]):
            teamdata[b-1][1]+=20
    teamdata=sorted(teamdata, key=lambda x:(-x[0],x[1],-x[2]))
    for i in range(t):
        print(teamdata[i][2],end=('\n' if i==t-1 else ',' if teamdata[i][0]>teamdata[i+1][0] or teamdata[i][1]<teamdata[i+1][1] else '='))

Solution (Python):
while True:
    M, T, P, R = map(int, input().split())
    if M == T == P == R == 0:
        quit()
    a_list = [[0, 0, i] for i in range(T)]
    wa = [[0 for j in range(P)] for i in range(T)]
    for i in range(R):
        m, t, p, j = map(int, input().split())
        t -= 1
        p -= 1
        if j == 0:
            a_list[t][0] += m + wa[t][p]*20
            a_list[t][1] += 1
        else:
            wa[t][p] += 1
    a_list.sort(key=lambda x: x[2], reverse=True)
    a_list.sort(key=lambda x: x[0])
    a_list.sort(key=lambda x: x[1], reverse=True)
    temp = [-1, -1]
    ans = ''
    for i in range(len(a_list)-1):
        ans += str(a_list[i][2]+1)
        if a_list[i][0] == a_list[i+1][0] and a_list[i][1] == a_list[i+1][1]:
            ans += '='
        else:
            ans += ','
    ans += str(a_list[-1][2]+1)
    print(ans)