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).Sort(teams)
		for _, t := range teams {
			fmt.Println(t.Name)
		}
	}
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<functional>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<numeric>
#include<limits>

#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()

using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ll, char> plc;


int main()
{
	int n; 
	while (cin >> n, n) {
		vector<plc> v(n);

		for (int i = 0; i < n; i++) {
			cin >> v[i].second;
			int num = 0;
			for (int j = 0; j < n - 1; j++) {
				int x; cin >> x;
				if (x == 0)num += 100000;
				if (x == 1)num--;
			}
			v[i].first = num;
		}

		for (int i = 0; i < n-1; i++) {
			for (int j = i + 1; j < n; j++) {
				if (v[i].first < v[j].first)
					swap(v[i], v[j]);
			}
		}

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

C++: // #define _CRT_SECURE_NO_WARNINGS
// #define _USE_MATH_DEFINES
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <locale>
#include <cctype>
#include <sstream>
using namespace std;

typedef long long LL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef map<int, int> MAPII;
typedef multimap<int, char, greater<int> > MuMAPIC;
typedef vector<pair<int, int> > VPII;
typedef multimap<int, string, greater<int> > MuMIS;

#define MP make_pair
#define fastIO  cin.tie(0); ios::sync_with_stdio(false);
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
//for gcc (未test)
// #define FOREACH_IT(it,c) for(typeof(c)::iterator it=(c).begin(); it!=(c).end(); ++it)
//for Visual Studio
#define foreach_it(type,it,c) for(type::iterator it=c.begin(),c_end=c.end();it!=c_end;++it)
#define DUMP_VVI(b) FOR(i,0,b.size()){FOR(j,0,b[i].size())printf("%d ",b[i][j]);puts("");}

// ------------------- include, typedef, define END. -------------------

void solveAndOutput(MuMAPIC &result){
	foreach_it(MuMAPIC, it, result){
		cout << (*it).second << endl;
	}
}

int main(){
	/* 方針
	 * A:381 , B:370 , C:40 , ...
	 * 百の位は勝ち数, 十の位は(9-負け数), 一の位は引き分け数.
	 * 入力チーム数が10以下（= 繰り上がり繰り下がりがないこと）を前提としている.
	 */

	int n = 0, in = 0;
	char in_char;
	while (cin >> n, n){
		MuMAPIC result;

		// 各team入力
		FOR(teamSum, 0, n){
			// seisekiCount[0] = win, [1]=lose, [2]=hikiwake
			VI seisekiCount(3, 0);
			int temp_mapin = 0;

			cin >> in_char;

			FOR(i, 0, n - 1){
				cin >> in;
				seisekiCount[in]++;
			}
			temp_mapin = seisekiCount[0] * 100 + (9 - seisekiCount[1]) * 10 + seisekiCount[2];
			result.insert(MP(temp_mapin, in_char));
		}
		// all teams input END.
		solveAndOutput(result);
	}
	return 0;
}

C++: #define _USE_MATH_DEFINES
#define INF 10000000
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>

using namespace std;

typedef long long ll;
typedef pair<int,string> P;

static const double eps = 1e-8;

class DATA{
public:
	string name;
	int win;
	int lose;
	int draw;
	DATA() : win(0),lose(0),draw(0){}
};

class mycmp{
public:
	bool operator()(const DATA d1, const DATA d2){
		if(d1.win > d2.win) return true;
		if(d1.win == d2.win && d1.lose < d2.lose) return true;
		return false;
	}
};

int main(){
	int n;
	while(~scanf("%d",&n)){
		if(n==0) break;

		vector<DATA> teams;
		for(int i=0;i<n;i++){
			char name[32];
			scanf("%s",&name);
			DATA data;
			data.name = name;
			for(int j=0;j<n-1;j++){
				int result;
				scanf("%d",&result);
				if(result==0) data.win++;
				else if(result==1) data.lose++;
				else if(result==2) data.draw++;
			}

			teams.push_back(data);
		}

		sort(teams.begin(),teams.end(),mycmp());

		for(int i=0;i<teams.size();i++){
			printf("%s\n",teams[i].name.c_str());
		}
	}
}

C++: #include <iostream>
#include <map>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
	while (1) {
		int n;
		cin >> n;
		if (n == 0) return 0;
		vector<pair<string, int>>v(n);
		for (int i = 0; i < n; i++) {
			cin >> v[i].first;
			for (int j = 0; j < n - 1; j++) {
				int num;
				cin >> num;
				if (num == 0) v[i].second += 100;
				if (num == 1) v[i].second -= 1;
			}
		}
		for (int i = 0; i < n; i++) {
			for (int j = n - 1; j >= i + 1; j--) {
				if (v[j].second < v[j - 1].second) swap(v[j], v[j - 1]);
			}
		}
		for (int i = n - 1; i >= 0; i--) {
			cout << v[i].first << endl;
		}
	}
	return 0;
}

C++: #include<stdio.h>
int main(void)
{
	int n,p[100];
	int i,j,temp;
	int w[100],l[100],a[100];
	char t[100],m;

	scanf("%d",&n);
	while(n!=0){
		for(i=0;i<n;i++){
			w[i]=0;
			l[i]=0;
			scanf(" %c",&t[i]);
			a[i]=i;
			for(j=0;j<n-1;j++){
				scanf("%d",&p[j]);
				if(p[j]==0){
					w[i]++;
				}
				else if(p[j]==1){
					l[i]++;
				}
			}
		}
		for(i=0;i<n-1;i++){
			for(j=i+1;j<n;j++){
				if(w[i]<w[j]){
					temp=w[i];
					w[i]=w[j];
					w[j]=temp;
					temp=l[i];
					l[i]=l[j];
					l[j]=temp;
					m=t[i];
					t[i]=t[j];
					t[j]=m;
					temp=a[i];
					a[i]=a[j];
					a[j]=temp;
				}
				else if(w[i]==w[j]){
					if(l[i]>l[j]){
						temp=w[i];
						w[i]=w[j];
						w[j]=temp;
						temp=l[i];
						l[i]=l[j];
						l[j]=temp;
						m=t[i];
						t[i]=t[j];
						t[j]=m;
						temp=a[i];
						a[i]=a[j];
						a[j]=temp;
					}
					else if(l[i]==l[j] && a[i]>a[j]){
						temp=w[i];
						w[i]=w[j];
						w[j]=temp;
						temp=l[i];
						l[i]=l[j];
						l[j]=temp;
						m=t[i];
						t[i]=t[j];
						t[j]=m;
						temp=a[i];
						a[i]=a[j];
						a[j]=temp;
					}
				}
			}
		}
		for(i=0;i<n;i++){
			printf("%c\n",t[i]);
		}
		scanf("%d",&n);
	}
	return 0;
}