n = int(input()) S = [input() for _ in range(n)] for i in range(n): T = S[i] mi = chr(200) m = len(T) ind = -1 for j in range(1, m): if T[j - 1] > T[j] and T[j] <= mi: mi = T[j] ind = j elif T[j] == mi: ind = j if ind == -1: change = True for j in range(1, m): if T[j] == T[j - 1]: change = False break if change: T = T[:-2] + T[-1] + T[-2] S[i] = T continue for j in range(m): if T[j] > mi: T = T[:j] + T[ind] + T[j + 1 : ind] + T[j] + T[ind + 1 :] S[i] = T break mi = min(S) c = S.count(mi) ans = [0] * n for i in range(n): if S[i] == mi: ans[i] = n + 1 - c print(*ans, sep="\n")