Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"sort"
	"strings"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)

type Team struct {
	Name string
	Win, Lose, Draw int
}
type lessFunc func(p1, p2 *Team) bool
type multiSorter struct {
	teams []Team
	less    []lessFunc
}
func (ms *multiSorter) Sort(teams []Team) {
	ms.teams = teams
	sort.Sort(ms)
}
func OrderedBy(less ...lessFunc) *multiSorter {
	return &multiSorter{
		less: less,
	}
}
func (ms *multiSorter) Len() int {
	return len(ms.teams)
}
func (ms *multiSorter) Swap(i, j int) {
	ms.teams[i], ms.teams[j] = ms.teams[j], ms.teams[i]
}
func (ms *multiSorter) Less(i, j int) bool {
	p, q := &ms.teams[i], &ms.teams[j]
	var k int
	for k = 0; k < len(ms.less)-1; k++ {
		less := ms.less[k]
		switch {
		case less(p, q):
			return true
		case less(q, p):
			return false
		}
	}
	return ms.less[k](p, q)
}

func main() {
	win := func(c1, c2 *Team) bool {
		return c1.Win > c2.Win
	}
	lose := func(c1, c2 *Team) bool {
		return c1.Lose < c2.Lose
	}
	draw := func(c1, c2 *Team) bool {
		return c1.Draw < c2.Draw
	}

	for scanner.Scan() {
		n, _ := strconv.Atoi(scanner.Text())
		if n == 0 {
			break
		}
		teams := make([]Team, n)
		for i := 0; i < n; i++ {
			scanner.Scan()
			buf := strings.Split(scanner.Text(), " ")
			name := buf[0]
			tmp := make([]int, n-1)
			for i := range tmp {
				tmp[i], _ = strconv.Atoi(buf[i+1])
			}
			cntwin, cntlose, cntdraw := 0, 0, 0
			for _, v := range tmp {
				switch v {
				case 1:
					cntlose++
				case 0:
					cntwin++
				case 2:
					cntdraw++
				}
			}
			teams[i] = Team{name, cntwin, cntlose, cntdraw}
		}
		OrderedBy(win, lose, draw).Sort(teams)
		for _, t := range teams {
			fmt.Println(t.Name)
		}
	}
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <queue>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1<<30;

struct hoge {
  int w, l;
  char c;
  hoge(char c, int w, int l) : c(c), w(w), l(l) {}
};
bool operator<(const hoge &a, const hoge &b) {
  return a.w != b.w ? a.w > b.w : a.l < b.l;
}

int main() {
  int n;
  while(cin >> n, n)  {
    vector<hoge> v;
    REP(j,n) {
      char c;
      cin >> c;
      int a = 0, b = 0;
      REP(i,n-1) {
        int r;
        cin >> r;
        if (r == 0)
          a++;
        else if (r == 1)
          b++;
      }
      v.push_back(hoge(c,a,b));
    }
    sort(ALL(v));
    FOR(it,v)
      cout << it->c << endl;
  }
}

C++: #include <iostream>
using namespace std;
int main()
{
        while(true){
                int n;
                char t;
                int r;
                char x[10];
                cin >> n;
                if(n==0)
                        break;
                int win[10],lose[10];
                for(int i=0 ;i<10 ;i++){
                                win[i] = 0;
                                lose[i] = 0;
                }
                for(int i=0 ;i<n ;i++){
                        cin >> t;

                        for(int j=1 ;j<n ;j++){
                                cin >> r;
                                if(r==0){
                                        win[i]++;
                                }
                           if(r==1){
                                        lose[i]++;
                                }

                        }
                        x[i] = t;
                }
                char y=0,z=0;
                for(int i=0 ;i<n-1 ;i++){
                                for(int j=i+1 ;j<n ;j++){
                                if(win[i] < win[j]){
                                        z=win[i];
                                        win[i]=win[j];
                                        win[j]=z;
                                        y=x[i];
                                        x[i]=x[j];
                                        x[j]=y;
                                }
                                else if(win[i] == win[j]){
                                        if(lose[i] > lose[j]){
                                                z=win[i];
                                                win[i]=win[j];
                                                win[j]=z;
                                                y=x[i];
                                                x[i]=x[j];
                                                x[j]=y;
                                        }
                                }
								else if(win[i] == win[j] && lose[i] == lose[j]){
									if(x[i] <x[j]){
										y=x[i];
                                        x[i]=x[j];
                                        x[j]=y;
									}
								}
                        }
                }

                for(int i=0 ;i<n ;i++){
                        cout << x[i] << endl;
                }
        }
		return 0;
}

C++: #include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
struct data{
    int w,l,p;
    char name;
    data(int a,int b,int c,char d):w(a),l(b),p(c),name(d){}
    data(){}
    bool operator<(const data &d)const{
        if(w!=d.w)return w<d.w;
        if(l!=d.l)return l>d.l;
        return p<d.p;
    }
};
int main(){
    int n;
    while(scanf("%d",&n),n){
        vector<data>V;
        for(int i=0;i<n;i++){
            char c;
            scanf(" %c",&c);
            int w,l;
            w=l=0;
            for(int j=0;j<n-1;j++){
                int val;
                scanf("%d",&val);
                if(val==2)continue;
                if(val)w++;
                else l++;
            }
            V.push_back(data(w,l,i,c));
        }
        sort(V.begin(),V.end());
        for(int i=0;i<n;i++){
            printf("%c\n",V[i].name);
        }

    }
    return 0;
}

C++: #include "bits/stdc++.h"

#define REP(i,num) for(int i=0;i<(num);++i)
#define LOOP(i) while(i--)
#define ALL(c) c.begin(),c.end()
#define PRINTALL(c) for(auto pitr=c.begin();pitr!=c.end();++pitr){cout<<*pitr;if(next(pitr,1)!=c.end())cout<<' ';}cout<<endl;
#define PAIRCOMP(c,comp) [](const pair<ll,ll>& lhs,const pair<ll,ll>& rhs){return lhs.c comp rhs.c;}

using namespace std;
using ll = long long;

constexpr ll atcoder_mod = 1e9+7;

template<typename T=int>
T in(){T x; cin >> x; return (x);}
template<typename T=int,typename C=vector<T>>
C vecin(int N){C x(N);REP(i,N){x[i]=in<T>();}return move(x);}

void vout(){cout << endl;}
template<typename Head,typename... Tail>
void vout(Head&& h,Tail&&... t){cout << ' ' << h;vout(forward<Tail>(t)...);}
void out(){cout << endl;}
template<typename Head,typename... Tail>
void out(Head&& h,Tail&&... t){cout << h;vout(forward<Tail>(t)...);}

class Data{
public:
	char name;
	int win{0},draw{0},lose{0};
};

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);
	
	while(true){
		int N=in();
		if(!N) break;

		vector<Data> T(N);
		REP(i,N){
			T[i].name=in<char>();
			REP(j,N-1){
				int v=in();
				if(!v) T[i].win++;
				else if(v==1) T[i].lose++;
				else T[i].draw++;
			}
		}
		sort(ALL(T),[](Data& l,Data& r){return l.win==r.win?l.lose<r.lose:l.win>r.win;});
		REP(i,N){
			out(T[i].name);
		}
	}
	return 0;
}