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

Python: import sys
import operator
f = sys.stdin

while True:
    n = int(f.readline())
    if n == 0:
        break
    teams = [f.readline().split() for i in range(n)]
    
    teams = [(t[0], -t.count('0'), t.count('1'), i) for i, t in enumerate(teams)]
    
    teams.sort(key=operator.itemgetter(1,2,3))
    
    print('\n'.join([t[0] for t in teams]))

Python: def parse(i):
    l = input().strip().split()
    m = l.pop(0)
    return (-l.count('0'), l.count('1'), i, m)


n = int(input())

while n:
    print('\n'.join(str(t[3]) for t in sorted([parse(_) for _ in range(n)])))
    n = int(input())

Python: while True:
    n = int(input())
    if n == 0:
        break
    d = []
    for i in range(n):
        tmp = input().split()
        t = tmp[0]
        w = sum(1 for c in tmp[1:] if not int(c))
        l = sum(1 for c in tmp[1:] if int(c) == 2)
        d.append([t, w, l, n-i])
    ans = sorted(d, key=lambda x: (x[1], x[2], x[3]))
    for a in ans[::-1]:
        print(a[0])

Python: # -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0196

"""
import sys
from sys import stdin
from heapq import heappop, heappush
input = stdin.readline


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

        pq = []
        for seq in range(n):
            temp = input().split()
            team_name = temp[0]
            temp = temp[1:]
            win_count = temp.count('0')
            lose_count = temp.count('1')
            heappush(pq, (-win_count, lose_count, seq, team_name))

        while pq:
            _, _, _, team_name = heappop(pq)
            print(team_name)


if __name__ == '__main__':
    main(sys.argv[1:])

Python: def solve():
    from sys import stdin
    f_i = stdin
    
    ans = ""
    
    while True:
        n = int(f_i.readline())
        
        if n == 0:
            break
        
        result = []
        
        for i in range(n):
            score = f_i.readline()
            
            t = score[0]
            win = score.count('0')
            lose = score.count('1')
            
            result.append((-win, lose, i, t))
        
        result.sort()
        
        ans += '\n'.join(map(lambda x: x[-1], result))
        ans += '\n'
    
    print(ans, end='')

solve()

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().split())
		t = r.pop(0)
		w = l = 0
		for p in r:
			if 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
    result = [input().split() for _ in range(n)]
    result = [[r[0], r.count('0'), r.count('1')] for r in result]
    result.sort(key=lambda x: [-x[1], x[2]])
    result = [r[0] for r in result]
    print('\n'.join(result))

Python: import sys
f = sys.stdin

while True:
    n = int(f.readline())
    if n == 0:
        break
    teams = [f.readline().split() for i in range(n)]
    
    teams = [(t[0], -t.count('0'), t.count('1'), i) for i, t in enumerate(teams)]
    
    teams.sort(key=operator.itemgetter(1,2,3))
    
    print('\n'.join([t[0] for t in teams]))

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: 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: def solve():
    N = int(input())
    if N == 0: return -1
    score = []
    for i in range(N):
        s = input().split()
        win, draw = 0, 0
        for j in range(1, len(s)):
            if s[j] == '1': continue
            elif s[j] == '0': win += 1 
            else : draw += 1
        score.append((s[0], win, draw))
    res = sorted(score, reverse=True, key=lambda x:(x[1], x[2]))
    for s in res: print(s[0])

while True:
    if solve() != None: break

Python: while True:
    L = []
    n = int(input())
    if n == 0:
        break
    for i in range(n):
        r =  list(input().split())
        t = r.pop(0)
        w = l = 0
        for a in r:
            if int(a) == 0:
                w += 1
            elif int(a) == 1:
                l += 1
        L.append((t,w,l,i))

    for i in sorted(L, key = lambda x:(-x[1],x[2],x[3])):
        print(i[0])

Python: while 1:
    N = int(input())
    if N == 0:
        break
    L = []
    for i in range(N):
        t, *rs = input().split()
        w = 0; l = 0
        for e in map(int, rs):
            if e == 0:
                w += 1
            elif e == 1:
                l += 1
        L.append((-w, l, i, t))
    L.sort()
    for _, _, _, t in L:
        print(t)

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