Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
	"strings"
)

func main() {
	io := NewIo()
	defer io.Flush()
	for {
		M, T, P, R := io.NextInt(), io.NextInt(), io.NextInt(), io.NextInt()
		if M == 0 {
			break
		}
		score := make([]struct{ num, time, index int }, T)
		submit := make([][]int, T)
		for i := 0; i < T; i++ {
			score[i].index = i + 1
			submit[i] = make([]int, P)
		}

		for i := 0; i < R; i++ {
			m, t, p, j := io.NextInt(), io.NextInt()-1, io.NextInt()-1, io.NextInt()
			if j == 0 {
				score[t].num++
				score[t].time += submit[t][p]*20 + m
			} else {
				submit[t][p]++
			}
		}
		sort.SliceStable(score, func(i, j int) bool {
			return score[i].num < score[j].num || score[i].num == score[j].num && score[i].time > score[j].time
		})

		for i := T - 1; i > 0; i-- {
			io.Printf("%d", score[i].index)
			if score[i].num == score[i-1].num && score[i].time == score[i-1].time {
				io.Printf("=")
			} else {
				io.Printf(",")
			}
		}
		io.Println(score[0].index)
	}
}

// I/O template
type Io struct {
	reader    *bufio.Reader
	writer    *bufio.Writer
	tokens    []string
	nextToken int
}

func NewIo() *Io {
	return &Io{
		reader: bufio.NewReader(os.Stdin),
		writer: bufio.NewWriter(os.Stdout),
	}
}

func (io *Io) Flush() {
	err := io.writer.Flush()
	if err != nil {
		panic(err)
	}
}

func (io *Io) NextLine() string {
	var buff []byte
	for {
		line, isPrefix, err := io.reader.ReadLine()
		if err != nil {
			panic(err)
		}
		buff = append(buff, line...)
		if !isPrefix {
			break
		}
	}
	return string(buff)
}

func (io *Io) Next() string {
	for io.nextToken >= len(io.tokens) {
		line := io.NextLine()
		io.tokens = strings.Fields(line)
		io.nextToken = 0
	}
	s := io.tokens[io.nextToken]
	io.nextToken++
	return s
}

func (io *Io) NextInt() int {
	i, err := strconv.Atoi(io.Next())
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) NextFloat() float64 {
	i, err := strconv.ParseFloat(io.Next(), 64)
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) Println(a ...interface{}) {
	fmt.Fprintln(io.writer, a...)
}

func (io *Io) Printf(format string, a ...interface{}) {
	fmt.Fprintf(io.writer, format, a...)
}

func (io *Io) PrintIntSlice(a []int) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func (io *Io) PrintStringSlice(a []string) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func Log(name string, value interface{}) {
	fmt.Fprintf(os.Stderr, "%s=%+v\n", name, value)
}

func ExampleIo() {
	io := NewIo()
	defer io.Flush()
	a := make([]int, 0, 3)
	a[0], a[1], a[2] = io.NextInt(), io.NextInt(), io.NextInt()
	io.Println(a)
	io.PrintIntSlice(a)
	Log("io", a)
}

C++: #include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
#define chmin(a, b) ((a)=min((a), (b)))
#define chmax(a, b) ((a)=max((a), (b)))
#define fs first
#define sc second
#define eb emplace_back
using namespace std;

typedef long long ll;
typedef pair<int, int> Pair;
typedef tuple<int, int, int> Tuple;

const ll MOD=1e9+7;
const ll INF=1e18;
const double pi=acos(-1);
const double eps=1e-10;

int dx[]={1, 0, -1, 0};
int dy[]={0, -1, 0, 1};
int dx2[]={1, 1, 0, -1, -1, -1, 0, 1};
int dy2[]={0, -1, -1, -1, 0, 1, 1, 1};

int main(){
    while(1){
        int M, T, P, R; cin>>M>>T>>P>>R;
        if(M==0 && T==0 && P==0 && R==0) return 0;

        // 0-indexed
        // Problem, Time, Team
        vector<Tuple> team(T);
        for(int i=0; i<T; i++){
            team[i]=Tuple(0, 0, i);
        }

        vector<vector<int>> penalty(T, vector<int>(P, 0));

        for(int i=0; i<R; i++){
            int m, t, p, j; cin>>m>>t>>p>>j;
            t--, p--;

            if(j==0){
                // correct
                get<0>(team[t])++;
                get<1>(team[t])+=m; get<1>(team[t])+=penalty[t][p];
            }
            else{
                penalty[t][p]+=20;
            }
        }

        vector<vector<Pair>> count(P+1);

        for(int i=0; i<T; i++){
            count[get<0>(team[i])].push_back(Pair(get<1>(team[i]), get<2>(team[i])));
        }

        for(int i=0; i<=P; i++){
            sort(count[i].begin(), count[i].end());
        }

        int CountTeam=0;
        for(int i=P; i>=0; i--){
            //cout << "ProblemNumber = " << i << endl; 
            for(int j=0; j<count[i].size(); j++){
                //cout << "Time = " << count[i][j].fs << " " << "Team = " << count[i][j].sc+1 << endl;
                
                vector<int> allsame;
                while(j+1<count[i].size() && count[i][j].fs==count[i][j+1].fs){
                    // same correct and same time
                    if(!allsame.size()) allsame.push_back(count[i][j].sc+1);
                    allsame.push_back(count[i][j+1].sc+1);
                    j++;
                }

                for(int k=allsame.size()-1; k>=0; k--){
                    if(k==0){
                        CountTeam++;
                        if(CountTeam==T) cout << allsame[k] << endl;
                        else cout << allsame[k] << ",";
                    }
                    else{
                        CountTeam++;
                        cout << allsame[k] << "=";
                    }
                }

                if(!allsame.size()){
                    CountTeam++;
                    if(CountTeam==T) cout << count[i][j].sc+1 << endl;
                    else cout << count[i][j].sc+1 << ",";
                }
                
            }
        }
    }
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

int M,T,P,R;
int m[2000],t[2000],p[2000],j[2000];
struct team{
	int n,s,t,w[2000];
};

bool comp(const team &t1,const team &t2){
	if(t1.t==t2.t && t1.s==t2.s)return t1.n>t2.n;
	if(t1.s==t2.s)return t1.t<t2.t;
	return t1.s>t2.s;
};

team dat[2001];

int main(void){
	while(1){
		scanf("%d%d%d%d",&M,&T,&P,&R);
		if(M+T+R+P==0)break;
		memset(dat,0,sizeof(dat));
		for(int i=0;i<T;i++)dat[i].n=i+1;
		for(int i=0;i<R;i++){
			scanf("%d%d%d%d",&m[i],&t[i],&p[i],&j[i]);
			if(j[i]!=0){
				dat[t[i]-1].w[p[i]]++;
			}else{
				dat[t[i]-1].s++;
				dat[t[i]-1].t+=m[i]+dat[t[i]-1].w[p[i]]*20;
			}
		}
		sort(dat,dat+T,comp);
		printf("%d",dat[0].n);
		for(int i=1;i<T;i++){
			if(dat[i-1].s==dat[i].s && dat[i-1].t==dat[i].t)printf("=");
			else printf(",");
			printf("%d",dat[i].n);
		}
		printf("\n");
	}
	return 0;
}