N = int(input()) # K = 1にăȘる。 S = [] for i in range(N): s = input() n = len(s) T = [s[j] for j in range(n)] TT = T[:] TT[-1],TT[-2] = TT[-2],TT[-1] t = ''.join(TT) C = [[-1] for j in range(26)] for j in range(n): C[ord(s[j]) - 97].append(j) TTT = T[:] f = 0 for j in range(n): if f: break c = ord(s[j]) - 97 if c == 0: continue for k in range(c): if C[k][-1] > j: l = C[k][-1] TTT[j],TTT[l] = TTT[l],TTT[j] f = 1 break Q = [t] if f: Q.append(''.join(TTT)) else: for j in range(26): if len(C[j]) >= 3: Q.append(s) break Q.sort() S.append(Q[0]) T = S[:] T.sort() t = T[0] ans = [] for i in range(N): if S[i] == t: ans.append(1) else: ans.append(0) sco = N + 1 - sum(ans) for i in range(N): if ans[i]: ans[i] = sco for res in ans: print(res)