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 <vector>
#include <algorithm>

using namespace std;

int main(){
    int M, T, P, R;
    while( true ){
        cin >> M >> T >> P >> R;
        
        if( M == 0 )
            break;
        
        vector<int> tans(T, 0);
        vector<int> ttime(T, 0);
        vector<int> pen(T * P, 0);
        
        int m, t, p, j;
        for(int i = 0; i < R; i++){
            
            cin >> m >> t >> p >> j;
            
            if(j == 0){
                tans[t-1] += 1;
                ttime[t-1] += m;
                ttime[t-1] += pen[(t-1)*P+p-1];
            }
            else{
                pen[(t-1)*P+p-1] += 20;
            }
        }
        
        vector<int> tscore(T, 0);
        vector<int> tss(T, 0);
        for(int i = 0; i < T; i++){
            tscore[i] = tans[i] * 50000 - ttime[i];
        }
        
        vector<int> trank(T);
        
        tss = tscore;
        sort( tss.begin(), tss.end() );
        tss.erase( unique( tss.begin(), tss.end() ), tss.end() );
        for(int i = 0; i < T; i++){
            trank[i] = find( tss.begin(), tss.end(), tscore[i] ) - tss.begin();
        }
        bool Fl = true;
        for(int i = T - 1; i >= 0; i--){
            bool flag = true;
            for(int j = T - 1; j >= 0; j--){
                if(trank[j] == i){
                    if(flag){
                        if(Fl){
                            cout << j + 1;
                            Fl = false;
                        }
                        else
                            cout << "," << j + 1;
                        flag = false;
                    }
                    else{
                        cout << "=" << j + 1;
                    }
                }
            }
        }
        cout <<endl;
        
    }
}

C++: #include <bits/stdc++.h>
using namespace std;

struct team{
	  int id;
	  int ac,time;
	  vector<int> wa;
	  team(int i, int a, int t, int P){
			id = i; ac = a; time = t;
			wa = vector<int>(P+1,0);
	  }
	  bool operator < (const team& tgt) const {
			if( ac != tgt.ac )
				  return ac < tgt.ac;
			if( time != tgt.time )
				  return time > tgt.time;
			return id < tgt.id;
	  }

};

int main(){
	  while(1){
			int M,T,P,R; cin >> M >> T >> P >> R;
			if(!M) break;
			vector<team> teams;
			for(int i=0; i<T; i++)
				  teams.push_back( team(i+1,0,0,P) );
	  
			for(int i=0; i<R; i++){
				  int m,t,p,j;
				  cin >> m >> t >> p >> j;
				  if( j == 0 ){
						teams[ t-1 ].ac ++;
						teams[ t-1 ].time += m;
						teams[ t-1 ].time += teams[ t-1 ].wa[ p ]*20 ;
						
				  }
				  else{
						teams[ t-1 ].wa[ p ]++;
				  }
			}
			sort(teams.begin(),teams.end());
			reverse(teams.begin(),teams.end());
			
			team pre = team(-1,-1,-1,P);
			for(int i=0; i<T; i++){
				  team corr = teams[i];
				  if( pre.ac == corr.ac and pre.time == corr.time )
						cout << "=" << corr.id;
				  else
						if(i!=0)
							  cout << "," << corr.id;
						else
							  cout << corr.id;
				  pre = corr;
			}
			cout << endl;
	  
	  }
}

C++: #include <cstdio>
#include <algorithm>
#include <utility>

using namespace std;

const int max_num_teams = 50;
const int max_num_probs = 10;

// time, num_errors
pair<int, int> probrems[max_num_teams + 1][max_num_probs + 1];
// num_corrects, time
struct team {
    int team_id;
    int num_corrects;
    int time;
};

bool operator<(const team& l, const team& r) {
    if (l.num_corrects == r.num_corrects) {
        if (l.time == r.time) {
            return l.team_id > r.team_id;
        }
        return l.time < r.time;
    }
    return l.num_corrects > r.num_corrects;
}

bool same_place(const team& l, const team& r) {
    return l.num_corrects == r.num_corrects && l.time == r.time;
}

team teams[max_num_teams];

int main() {
    for (;;) {
        int M, T, P, R;
        scanf("%d%d%d%d", &M, &T, &P, &R);

        if (!M && !T && !P && !R) { break; }

        for (int i = 1; i <= max_num_teams; i++) {
            for (int j = 1; j <= max_num_probs; j++) {
                probrems[i][j] = make_pair(-1, 0);
            }
            teams[i].team_id = i;
            teams[i].num_corrects = 0;
            teams[i].time = 0;
        }

        for (int i = 0; i < R; i++) {
            int m, t, p, j;
            scanf("%d%d%d%d", &m, &t, &p, &j);
            if (j == 0) { // Correct answer
                probrems[t][p].first = m;
            } else { // Wrong answer
                probrems[t][p].second++;
            }
        }

        for (int i = 1; i <= T; i++) {
            for (int j = 1; j <= P; j++) {
                if (probrems[i][j].first != -1) {
                    teams[i].num_corrects++;
                    teams[i].time += probrems[i][j].first;
                    teams[i].time += probrems[i][j].second * 20;
                }
            }
        }

        sort(&teams[1], &teams[T + 1]);

        for (int i = 1; i <= T - 1; i++) {
            printf("%d", teams[i].team_id);
            if (same_place(teams[i], teams[i + 1])) {
                printf("=");
            } else {
                printf(",");
            }
        }
        printf("%d\n", teams[T].team_id);
    }
    return 0;
}