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
	}

	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)
		}
	}
}

Python: while True:
  n = int(input())
  if n == 0:
    break
  result = []
  for _ in range(n):
    lst = input().split()
    result.append(((lst[1:].count("0"), lst[1:].count("2"), lst[1:].count("1")), lst[0]))
  result.sort(key=lambda x:-(x[0][0] * 10 + x[0][1]))
  for r in result:
    print(r[1])

Python: while True:
    n = int(input())
    if n == 0: break
    tmp = [list(input().split()) for _ in range(n)]
    score = {'0': 2, '1': 0, '2': 1}
    ans = []
    for t in tmp:
        tmpv = 0
        for v in t[1:]:
            tmpv += score[v]
        ans.append(t[0] + str(tmpv))
    for i in range(len(ans)):
        for j in range(i+1, len(ans))[::-1]:
            if ans[j][1] > ans[j-1][1]:
                ans[j], ans[j-1] = ans[j-1], ans[j]
    print('\n'.join([a[0] for a in ans]))

Python: # Aizu Problem 0196: Baseball Championship
#
import sys, math, os

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input.txt", "rt")



while True:
    n = int(input())
    if n == 0:
        break
    teams = []
    for i in range(n):
        rec = input().split()
        name = rec[0]
        rec = [int(_) for _ in rec[1:]]
        wins = rec.count(0)
        losses = rec.count(1)
        draws = rec.count(2)
        teams.append((i, name, wins, draws, losses))
    teams = sorted(teams, key=lambda x: (-x[2], x[4], x[0]))
    for team in teams:
        print(team[1])

Python: while True:
    num = int(input())
    if num==0:
        break
    L = []
    for i in range(num):
        n, *k = input().split()
        L.append( (n, k.count("0"), k.count("1"), i) )
    L.sort(key=lambda x:(-x[1],x[2],x[3]) )
    
    for x in L:
        print(x[0])

Python: # AOJ 0196 Baseball Championship
# Python3 2018.6.21 bal4u

while 1:
	n = int(input())
	if n == 0: break
	team = []
	for i in range(n):
		r = list(input())
		t = r.pop(0)
		w = l = 0
		for p in r:
			if p.isdigit() == False: pass
			elif int(p) == 0: w += 1      # 勝ち数
			elif int(p) == 1: l += 1      # 負け数
		team.append((t, i, w, l))
	for i in sorted(team, key=lambda x:(-x[2],x[3],x[1])): print(*i[0])

Python: while True:
    n = int(input())
    if n == 0:
        break
    sets = []
    for i in range(n):
        hokan = input().split()
        sets.append([hokan.count("0"),hokan.count("2"),n-i,hokan[0]])
    sets.sort(reverse = True)
    for i in range(n):
        print(sets[i][3])

Python: WIN = "0"
LOSE = "1"

while True:

    input_count = int(input())

    if input_count == 0:
        break

    input_list = [input().split(" ") for _ in range(input_count)]

    count_list = [(item[0], item.count(WIN), item.count(LOSE)) for item in input_list]
    count_list.sort(key=lambda item: (-item[1], item[2]))

    output = [item[0] for item in count_list]
    print("\n".join(output))

Python: while True:
    n = int(input())
    if n == 0: break
    tmp = [list(input().split()) for _ in range(n)]
    score = {'0': 2, '1': 0, '2': 1}
    ans = []
    for t in tmp:
        tmpv = 0
        for v in t[1:]:
            tmpv += score[v]
        ans.append(t[0] + str(tmpv))
    for i in range(len(ans)):
        for j in range(i+1, len(ans))[::-1]:
            if ans[j][1] > ans[j-1][1]:
                ans[j], ans[j-1] = ans[j-1], ans[j]
    for a in ans:
        print(a[0])

Python: if __name__ == '__main__':

	while True:
		try:
			n = int(input())

			if n == 0:
				break

			A = []

			for _ in range(n):
				cmd = input().split()
				A.append((cmd.count("0"),cmd.count("1"),cmd.count("2"),cmd[0]))

			B = sorted(A,key=lambda x:(-x[0],x[1],x[2]))

			for y in B:
				print(y[3])

		except EOFError:
			break

Python: while True:
    n = int(input())
    if n == 0:
        break
    ls = []
    for i in range(n):
        v = input().split()
        sum = 0
        for j in range(1,n):
            v[j] = int(v[j])
            if v[j] == 0:
                sum += 100
            if v[j] == 1:
                sum -= 1
        ls.append([sum,-i,v[0]])
    ls = sorted(ls)
    ls.reverse()
    for i in range(n):
        print(ls[i][2])

Python: from collections import Counter

while True:
    n=int(input())
    if n==0:
        break

    scores=[]
    for i in range(n):
        s=input().split(" ")
        team=s[0]
        score=s[1:]
        c=Counter(score)
        c=dict(c)
        result=[team,c.get('0',0),c.get('1',0),c.get('2',0)]
        scores.append(result)
    scores=sorted(scores,key=lambda w: w[1]*100+w[3],reverse=True)
    for s in scores:
        print(s[0])

Python: while True:
    n = int(input())
    if n == 0: break
    tmp = [list(input().split()) for _ in range(n)]
    score = {'0': 2, '1': 0, '2': 1}
    ans = []
    for t in tmp:
        tmpv = 0
        for v in t[1:]:
            tmpv += score[v]
        ans.append(t[0] + str(tmpv))
    for i in range(len(ans)):
        for j in range(i+1, len(ans))[::-1]:
            if ans[j][1] > ans[j-1][1]:
                ans[j], ans[j-1] = ans[j-1], ans[j]
    print('\n'.join(str(a[0]) for a in ans))

Python: 
class team:
    def __init__(self,name,result):
        self.name = name
        self.win = 0
        self.lose = 0
        for i in range(len(result)):
            if result[i] == 0:
                self.win += 1
            elif result[i] == 1:
                self.lose +=1
            else:
                pass

while True:
    results = []
    n = int(input())

    if n==0:
        break

    for i in range(n):
        lst = input().split()
        name = lst[0]
        lst = lst[1:]
        result = list(map(int,lst))

        results.append(team(name,result))

    results = sorted(results,key = lambda w:w.lose)
    results = sorted(results,key = lambda w:w.win, reverse = True)

    for i in range(n):
        print(results[i].name)

Python: while 1:
    n = int(input())
    if n == 0:
        break

    result = []
    for i in range(n):
        data = list(map(str, input().split()))
        name = data.pop(0)
        win = data.count("0")
        lose = data.count("1")
        result.append([name, win, lose])

    result = sorted(result, key=lambda x: (-x[1], x[2]))

    for r in result:
        print(r[0])