Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

type Team struct {
	Name string
	Win, Lose, Draw, Index 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
	}
	index := func(c1, c2 *Team) bool {
		return c1.Index < c2.Index
	}

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

C++: #include <iostream>
#include <algorithm>
using namespace std;
int n, a, d[10]; char c[10];
int main() {
	while (true) {
		scanf("%d", &n);
		if (n == 0)break;
		for (int i = 0; i < n; i++) {
			cin >> c[i]; d[i] = 0;
			for (int j = 0; j < n - 1; j++) {
				scanf("%d", &a);
				if (a == 0) d[i] += 10;
				if (a == 1) d[i] -= 1;
			}
		}
		for (int i = 0; i < n; i++) {
			for (int j = i + 1; j < n; j++) {
				if (d[i] < d[j]) {
					swap(c[i], c[j]);
					swap(d[i], d[j]);
				}
			}
		}
		for (int i = 0; i < n; i++) printf("%c\n", c[i]);
	}
	return 0;
}

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

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int,int> pint;
#define mp make_pair

template<class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; }
template<class T> void chmin(T &t, T f) { if (t > f) t = f; }
template<class T> void chmax(T &t, T f) { if (t < f) t = f; }
int in() { int x; scanf("%d", &x); return x; }

vector<pair<int,string> > team(20);

int main() {
	int n;
	int score;
	while(n=in()){
		int i;
		for(i=0;i<n;i++){
			team[i].first=0;
			cin>>team[i].second;
//cout<<team[i].second<<endl;
			int j;
			for(j=0;j<n-1;j++){
				score=in();
				if(score==0)team[i].first+=100;
				if(score==2)team[i].first++;
//cout<<" "<<score<<endl;
			}
		}
		stable_sort(&team[0],&team[n],greater<pair<int,string> >());
		for(i=0;i<n;i++){
			cout<<team[i].second<<endl;
		}
	}
	return 0;
}

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

class team
{
	public:

		team(){c=w=l=0;}

		char c;
		int w,l;

		bool operator<(const team& t) const
		{
			return w!=t.w ? w>t.w : l<t.l;
		}
};

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

		for(int j=0; j<n; j++)
		{
			team t;
			cin >> t.c;

			for(int i=0; i<n-1; i++)
			{
				cin >> s;
				if(s==0) { t.w++; }
				if(s==1) { t.l++; }
			}
			v.push_back(t);
		}
		stable_sort(v.begin(), v.end());
		for(int i=0; i<v.size(); i++)
		{
			cout << v[i].c << endl;
		}
	}

	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
typedef pair<pair<int, int>, char> p;
int main() {
	int n;
	while (cin >> n, n) {
		p a[20];
		for (int i = 0; i < n; i++) {
			cin >> a[i].second;
			a[i].first.first = 0;
			a[i].first.second = n;
			for (int j = 1; j < n; j++) {
				int k; cin >> k;
				if (!k)a[i].first.first++;
				else if (k == 1)a[i].first.second--;
			}
		}
		for (int i = n; i >= 1; i--)
			for (int j = 1; j < i; j++)
				if (a[j - 1].first < a[j].first)swap(a[j - 1], a[j]);
		for (int i = 0; i < n; i++)
			cout << a[i].second << endl;
	}
	return 0;
}

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

using namespace std;

#define rep(i,a) for(int i = 0 ; i < a ; i ++)
#define loop(i,a,b) for(int i = a ; i < b ; i ++)

class state{
public :
	char t;
	int w,l,id;
	bool operator<(const state & a)const{
		if (w!=a.w) return w > a.w;
		if (l!=a.l) return l < a.l;
		return id<a.id;
	}	
};

main(){
	int n;
	while(cin>>n,n){
		state data[n];
		rep(i,n){
			cin>>data[i].t;
			data[i].id=i;
			data[i].w =0;
			data[i].l =0;
			rep(j,n-1){
				int tmp;
				cin>>tmp;
				if(tmp==0)data[i].w++;
				else if(tmp==1)data[i].l++;
			}
		}
		sort(data,data+n);
		rep(i,n)cout<<data[i].t<<endl;
	}
}

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

struct team{
	char t;
	int w, l;
};

bool large(team t1, team t2){
	return ((t1.w > t2.w) || (t1.w == t2.w && t1.l < t2.l));
}

int main(){
	while(1){
		int n;
		cin >> n;
		if(n == 0) break;
		team teams[10] = {0};
		for(int i = 0;i < n;i++){
			cin >> teams[i].t;
			for(int j = 0;j < n - 1;j++){
				int r;
				cin >> r;
				if(r == 0) teams[i].w++;
				if(r == 1) teams[i].l++;
			}
		}
		stable_sort(teams, teams + n, large);
		for(int i = 0;i < n;i++){
			cout << teams[i].t << endl;
		}
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
int main(){
  int n;
  while(cin >> n){
    if(n==0) break;
    char t[n];
    char kt;
    int tt[2][n];
    int k;
    int h;
    for(int i=0;i<2;i++){
      for(int j=0;j<n;j++){
	tt[i][j] = 0;
      }
    }
    for(int i=0;i<n;i++){
      cin >> t[i];
      for(int j=0;j<n-1;j++){
	cin >> h;
	if(h==0) tt[0][i] += 1;
	if(h==1) tt[1][i] += 1;
      }
    }
    for(int i=n-1;i>0;i--){
      for(int j=0;j<i;j++){
	if(tt[0][j]<tt[0][j+1]){
	  k = tt[0][j];
	  tt[0][j] = tt[0][j+1];
	  tt[0][j+1] = k;
	  k = tt[1][j];
	  tt[1][j] = tt[1][j+1];
	  tt[1][j+1] = k;
	  kt = t[j];
	  t[j] = t[j+1];
	  t[j+1] = kt;
	}
	else if(tt[0][j]==tt[0][j+1]&&tt[1][j]>tt[1][j+1]){
    	  k = tt[0][j];
	  tt[0][j] = tt[0][j+1];
	  tt[0][j+1] = k;
	  k = tt[1][j];
	  tt[1][j] = tt[1][j+1];
	  tt[1][j+1] = k;
	  kt = t[j];
	  t[j] = t[j+1];
	  t[j+1] = kt;
	}
      }
    }
    for(int i=0;i<n;i++) cout << t[i] << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<utility>
#include<algorithm>
#include<queue>

using namespace std;

priority_queue<pair<pair<pair<int, int>,int>,char>> results;

int main() {
	int n;
	while (true) {
		cin >> n; if (n == 0)break;
		char name;
		int win, lose;
		for (int i = 0; i < n;++i) {
			cin >> name;
			win = 0;lose = 0;
			for (int j = 0; j < n - 1; ++j) {
				int r;
				cin >> r;
				if (r == 2)continue;
				else if (r == 0)win++;
				else lose++;
			}results.emplace(make_pair(make_pair(make_pair(win,-lose),-i),name));
		}
		while(!results.empty()) {
			cout << results.top().second << endl;
			results.pop();
		}
	}
	return 0;
}