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;

int main(){
  while(1){
    int M, T, P, R, i;
    scanf("%d%d%d%d", &M, &T, &P, &R);
    if(M == 0){
      return 0;
    }
    int m, t, p, j;
    vector<vector<int>> time(T, vector<int>(P, -1)), pena(T, vector<int>(P, 0));
    for(i = 0; i < R; i++){
      scanf("%d%d%d%d", &m, &t, &p, &j);
      t--;
      p--;
      if(j == 0){
        time[t][p] = m + 20 * pena[t][p];
      }
      else{
        pena[t][p]++;
      }
    }
    vector<pair<int, pair<int, int>>> score(T);
    for(i = 0; i < T; i++){
      score[i].first = i;
      int num = 0, time_sum = 0, k;
      for(k = 0; k < P; k++){
        if(time[i][k] >= 0){
          num++;
          time_sum += time[i][k];
        }
      }
      score[i].second.first = num;
      score[i].second.second = time_sum;
    }
    sort(score.begin(), score.end(), [](pair<int, pair<int, int>> a,pair<int, pair<int, int>> b){
      if(a.second.first != b.second.first){
        return a.second.first > b.second.first;
      }
      else if(a.second.second != b.second.second){
        return a.second.second < b.second.second;
      }
      else{
        return a.first > b.first;
      }
    });
    for(i = 0; i < T; i++){
      if(i == T - 1){
        printf("%d\n", score[i].first + 1);
      }
      else{
        printf("%d", score[i].first + 1);
        if(score[i].second == score[i + 1].second){
          printf("=");
        }
        else{
          printf(",");
        }
      }
    }
  }
}

C++: #include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>

using namespace std;

#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<" "; cerr<<endl; }
inline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
struct Team{
  int id;
  int solve;
  int time;
  int wa[20];
  Team(int n){
    id = n;
    solve = 0;
    time = 0;
    REP(i, 20) wa[i] = 0;
  }
  bool operator < (const Team& t) const {
    if(solve != t.solve) return solve > t.solve;
    if(time != t.time) return time < t.time;
    return id > t.id;
  }
};

int main(){
  int M, T, P, R;
  while(cin >> M >> T >> P >> R && M){
    vector<Team> teams;
    REP(i, T) teams.push_back(Team(i + 1));
    REP(i, R){
      int m, t, p, j;
      cin >> m >> t >> p >> j;
      if(j == 0){
        teams[t - 1].time += m + teams[t - 1].wa[p - 1] * 20;
        teams[t - 1].solve ++;
      }else{
        teams[t - 1].wa[p - 1] += 1;
      }
    }
    sort(teams.begin(), teams.end());
    REP(i, teams.size()){
      if(i != 0) {
        if(teams[i - 1].time == teams[i].time && teams[i - 1].solve == teams[i].solve) cout <<"=";
        else cout << ",";
      }
      cout << teams[i].id;
    }
    cout << endl;
  }
  return 0;
}

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

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

    int m,t,p,r;

    map<pair<int,int>,int>mm;
    pair<pair<int,int>,int>time[51];

    for(int i=0; i<T; i++){
      time[i].second = i;
      time[i].first.first = time[i].first.second = 0;
    }


    for(int i=0; i<R; i++){
      cin >> m >> t >> p >> r;
      t--;
      if(r != 0) mm[make_pair(t,p)]++;
      else {
	time[t].first.second -= (m + 20 * mm[make_pair(t,p)]);
	time[t].first.first++;
      }
    }

    sort(time,time+T);
    reverse(time,time+T);


    map<pair<int,int>,vector<int> >ans;

    for(int i=0; i<T; i++){
      ans[time[i].first].push_back(time[i].second);
    }

    bool flag[51];
    fill(flag,flag+T,false);

    for(int i=0; i<T; i++){
      if(flag[time[i].second])continue;
      vector<int> v = ans[time[i].first];
      for(int j=0; j<v.size(); j++){
	if(flag[v[j]])continue;
	flag[v[j]] = true;
	if(v.size() == 1){
	  if(i == 0) cout << v[j]+1;
	  else cout << ',' << v[j]+1;
	} else {
	  if(i == 0){
	    if(j == 0) cout << v[j]+1;
	    else cout << '=' << v[j]+1;
	  } else {
	    if(j == 0) cout << ',' << v[j]+1;
	    else cout << '=' << v[j]+1;
	  }
	}
      }
    }
    cout << endl;
  }
  return 0;
}