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<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pii pair<int, int>
#define eb emplace_back
#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep3(i, l, n) for (int i = l; i < n; ++i)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define out(a) cout << a << endl
#define SZ(v) (int)v.size()
#define inf (int)(1e9+7)


int main() {
    int M, T, P, R;
    while (cin >> M && M) {
        cin >> T >> P >> R;
        if (R == 0) {
            cout << T;
            rep3(i, 1, T) {
                cout << "=" << T - i;
            }
            cout << endl;
            continue;
        }
        set<int> s[T]; //解いた問題
        int time[T]; fill_n(time, T, 0);
        vector<pair<pii, pii> > v(R);
        rep(i, R) {
            //チーム、問題、時間、yes or no
            cin >> v[i].S.F >> v[i].F.F >> v[i].F.S >> v[i].S.S;
            //0indexed
            v[i].F.F--;
            v[i].S.S *= -1; //0が一番下に来ないと!!!
        }
        sort(all(v));
        //cout << endl; rep(i, R) out(v[i].F.F + 1 << " " << v[i].F.S << " " << v[i].S.F << " " << v[i].S.S);
        
        
        
        int pena = 0;
        if (v[0].S.S == 0) {
            s[v[0].F.F].insert(v[0].F.S);
            time[v[0].F.F] = v[0].S.F;
        } else {
            pena += 20;
        }
        rep3(i, 1, R) {
            if (!(v[i].F.F == v[i - 1].F.F && v[i].F.S == v[i - 1].F.S)) {
                pena = 0;
            }
            if (v[i].S.S == 0) {
                time[v[i].F.F] += pena + v[i].S.F;
                s[v[i].F.F].insert(v[i].F.S);
                pena = 0;
            } else {
                pena += 20;
            }
        }
        
        
        
        vector<pair<int, pii> > solve;
        rep(i, T) {
            solve.eb(SZ(s[i]), pii(-time[i], i + 1));
        }
        sort(all(solve));
        reverse(all(solve));
        //cout << endl; rep(i, T) out(solve[i].F << " " << solve[i].S.F << " " << solve[i].S.S + 1);
        cout << solve[0].S.S;
        rep3(i, 1, T) {
            if (solve[i].F == solve[i - 1].F && solve[i].S.F == solve[i - 1].S.F) {
                cout << "=" << solve[i].S.S;
            } else {
                cout << "," << solve[i].S.S;
            }
        }
        cout << endl;
    }
}

C++: #include<iostream>
#include<queue>
#include<tuple>

using namespace std;

signed main(){

   while(1){
      int M, T, P, R;
      cin>> M>> T>> P>> R;
      if(!(M||T||P||R)) break;

      int hoge[T][P];
      fill((int*)hoge, (int*)hoge+T*P, 0);
      for(int i=0; i<R; i++){
         int time, team, prob, judge;
         cin>> time>> team>> prob>> judge;
         time++; team--; prob--;
         if(judge==0){
            hoge[team][prob]=0-hoge[team][prob]*20+time;
         }else{
            hoge[team][prob]--;
         }
      }
      priority_queue<tuple<int, int, int>> Q;
      for(int i=0; i<T; i++){
         int ac=0, time=0;
         for(int j=0; j<P; j++){
            ac+=(hoge[i][j]>0);
            time+=max(0, hoge[i][j]);
         }
         Q.push(make_tuple(ac, -time, i));
      }

      int s, t, u;
      s=t=u=-1;
      string answer;
      for(int i=0; i<T; i++){
         int ac, time, team;
         tie(ac, time, team)=Q.top(); Q.pop();
         if(i>0){
            if(s==ac&&t==-time) answer+="=";
            else answer+=",";
         }
         answer+=to_string(team+1);
         s=ac; t=-time; u=team;
      }

      cout<< answer<< endl;
   }

   return 0;
}