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<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<cctype>
#include<climits>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<memory>
#include<functional>

using namespace std;

#define ALL(g) (g).begin(),(g).end()
#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i,n) REP(i,0,n)
#define F(i,j,k) fill(i[0],i[0]+j*j,k)
#define P(p) cout<<(p)<<endl;
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define INF 1<<25
#define pb push_back

typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<double> vd;
typedef pair<int,int> pii;
typedef pair<long,long> pll;
typedef long long ll;

using namespace std;

struct data {
    int time, team, number, judge;
};

int penalties[51][11];
int times[51];
int point[51];
int ranking[51];

int main()
{
    int M, T, P, R;

    while (cin >> M >> T >> P >> R, M|T|P|R) {
        fill_n((int *)penalties, 51 * 11, 0);
        fill_n((int *)times, 51, 0);
        fill_n((int *)point, 51, 0);
        for (int i = 1; i <= T; i++) ranking[i] = i;
        int m, t, p, j;
        for (int i = 0; i < R; i++) {
            cin >> m >> t >> p >> j;
            if (j == 0) {
                point[t]++;
                times[t] += m + penalties[t][p];
            } else {
                penalties[t][p] += 20;
            }
        }
        for (int i = 1; i <= T; i++) {
            for (int j = i; j <= T; j++) {
                if (point[i] < point[j]) {
                    int a = ranking[i];
                    ranking[i] = ranking[j];
                    ranking[j] = a;
                    a = point[i];
                    point[i] = point[j];
                    point[j] = a;
                    a = times[i];
                    times[i] = times[j];
                    times[j] = a;
                }
            }
        }
        for (int i = 1; i <= T; i++) {
            for (int j = i; j <= T; j++) {
                if (point[i] == point[j]) {
                    if (times[i] > times[j]) {
                        int a = ranking[i];
                        ranking[i] = ranking[j];
                        ranking[j] = a;
                        a = point[i];
                        point[i] = point[j];
                        point[j] = a;
                        a = times[i];
                        times[i] = times[j];
                        times[j] = a;
                    }
                    if (times[i] == times[j]) {
                        if (ranking[i] < ranking[j]) {
                            int a = ranking[i];
                            ranking[i] = ranking[j];
                            ranking[j] = a;
                            a = point[i];
                            point[i] = point[j];
                            point[j] = a;
                            a = times[i];
                            times[i] = times[j];
                            times[j] = a;
                        }
                    }
                }
            }
        }
        for (int i = 1; i <= T; i++) {
            cout << ranking[i];
            if (i+1 <= T) {
                if (point[i] == point[i+1] && times[i] == times[i+1]) {
                    cout << "=";
                } else {
                    cout << ",";
                }
            }
        }
        cout << endl;
    }

    return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

struct data{
    int team, solve, tim;
    int pena[11] = {};
    data() {}
    data(int t) : team(t) {solve = 0, tim = 0;}
    void add(int p, int r){
        solve++;
        tim += r + pena[p]*20;
    }

    bool operator <(const data other) const{
        if(solve != other.solve){
            return solve > other.solve;
        }else if(tim != other.tim){
            return tim < other.tim;
        }else{
            return team > other.team;
        }
    }

    bool operator ==(const data other){
        return solve == other.solve && tim == other.tim;
    }
};

int main(){
    int M, T, P, R;

    while(cin >> M >> T >> P >> R, M+T+P+R){
        vector<data> v(T);
        for(int i = 0; i < T; i++)  v[i] = data(i+1);

        int m, t, p, j;
        for(int i = 0; i < R; i++){
            cin >> m >> t >> p >> j;
            if(j == 0){
                v[t-1].add(p, m);
            }else{
                v[t-1].pena[p]++;
            }
        }

        sort(v.begin(), v.end());

        cout << v[0].team;
        for(int i = 1; i < T; i++){
            if(v[i] == v[i-1])  cout << "=";
            else                cout << ",";
            cout << v[i].team;
        }
        cout << endl;
    }
    return 0;
}