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 <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <algorithm>

using namespace std;

struct Team {
    Team(int team_number) {
        tn_ = team_number;
        n_ = 0;
        t_ = 0;
        pc_ = 0;
    }
    int tn_;
    int n_;
    int t_;
    int pc_;
    vector<int> p_list_;
};

int main (int argc, char *argv[]) {
    int M, T, P, R;
    int m, t, p, j;
    vector<Team> tv;

    while (cin>>M>>T>>P>>R) {
        for (int i = 0; i < T; ++i) {
            tv.push_back(Team(i+1));
        }
        for (int i = 0; i < R; ++i) {
            cin>>m>>t>>p>>j;
            if (j != 0) {
                tv[t-1].p_list_.push_back(p);
            }
            else {
                for (auto e : tv[t-1].p_list_) {
                    if (e == p) {
                        tv[t-1].pc_++;
                    }
                }
                tv[t-1].n_++;
                tv[t-1].t_ += m;
            }
        }
        for (auto& e : tv) {
            e.t_ += e.pc_ * 20;
        }

        // for (auto e : tv) {
        //     cout<<"-----------"<<endl;
        //     cout<<e.tn_<<endl;
        //     cout<<e.n_<<endl;
        //     cout<<e.t_<<endl;
        //     cout<<e.pc_<<endl;
        //     for (auto c : e.p_list_) {
        //         cout<<c<<" ";
        //     }
        //     cout<<endl;
        //     cout<<"-----------"<<endl;
        // }

        sort(tv.begin(), tv.end(), [](const auto& lhs, const auto& rhs) -> bool{
            if (lhs.n_ != rhs.n_ ) {
                return lhs.n_ > rhs.n_;
            }
            else if (lhs.n_ == rhs.n_ && lhs.t_ != rhs.t_) {
                return lhs.t_ < rhs.t_;
            }
            else return lhs.tn_ > rhs.tn_;
        });

        const int size = static_cast<int>(tv.size());
        for (int i = 0; i < size; ++i) {
            cout<<tv[i].tn_;
            if(i != size -1) {
                if (tv[i].n_ != tv[i+1].n_ || tv[i].t_ != tv[i+1].t_ ) {
                    cout<<",";
                }
                else cout<<"=";
            }
        }
        cout<<endl;
        tv.clear();
    }

    return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
using namespace std;

#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;

#define INF 1000000000

struct team{
  int ac, t, n; // number of accepts, consumed time, team number
  int fails[50];
};

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

int main(){
  int M,T,P,R;
  while(cin>>M>>T>>P>>R, M|T|P|R){
    vector<team> teams(T);

    // initialize each teams info
    rep(i,T){
      teams[i].ac = teams[i].t = 0;
      teams[i].n = i+1;
      rep(j,P) teams[i].fails[j]=0;
    }

    // read input and set team info
    rep(i,R){
      int m,t,p,j;
      scanf("%d %d %d %d", &m, &t, &p, &j);
      t--; p--;
      if(j==0){ //AC
	teams[t].ac++;
	teams[t].t += m + teams[t].fails[p]*20;
      } else { //WA
	teams[t].fails[p]++;
      }
    }

    sort(all(teams), sorter);

    // output results
    printf("%d", teams[0].n);
    repl(i,1,T){
      if(teams[i-1].ac==teams[i].ac && teams[i-1].t==teams[i].t)
	printf("=");
      else
	printf(",");
      printf("%d", teams[i].n);
    }
    printf("\n");
  }
}

C++: #include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Team{
public:
  int id, pr, pe;
  Team(){}
  Team(int id, int pr, int pe): id(id), pr(pr), pe(pe){}
  bool operator < (const Team &t) const {
    if(pr != t.pr) return pr > t.pr;
    if(pe != t.pe) return pe < t.pe;
    return id > t.id;
  }
  bool operator == (const Team &t) const {
    return pr == t.pr && pe == t.pe;
  }
};

const int N = 50;

int M, T, P, R;
bool sc[N][10];
int cnt[N][10];
Team team[N];

main(){
  while(cin >> M >> T >> P >> R && (M|T|P|R)){
    for(int i=0;i<T;i++) team[i] = Team(i, 0, 0);
    fill(sc[0], sc[N], false);
    fill(cnt[0], cnt[N], 0);
    for(int i=0;i<R;i++){
      int m, t, p, j;
      cin >> m >> t >> p >> j;
      t--; p--;
      if(j) cnt[t][p]++;
      else if(!sc[t][p]){
        sc[t][p] = true;
        team[t].pr++;
        team[t].pe += m + 20 * cnt[t][p];
      }
    }
    sort(team, team+T);
    cout << team[0].id+1;
    for(int i=1;i<T;i++) cout << (team[i-1] == team[i] ? '=' : ',') << team[i].id+1;
    cout << endl;
  }
}