using System; using static System.Console; using System.Linq; using System.Collections.Generic; class Program { static int NN => int.Parse(ReadLine()); static int[] NList => ReadLine().Split().Select(int.Parse).ToArray(); static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray(); static string[] SList(long n) => Enumerable.Repeat(0, (int)n).Select(_ => ReadLine()).ToArray(); public static void Main() { Solve(); } static void Solve() { var n = NN; var s = SList(n); var t = new string[n]; var min = "|"; var minc = 0; for (var i = 0; i < n; ++i) { var tc = s[i].ToCharArray(); var pos = Enumerable.Repeat(-1, 26).ToArray(); for (var j = tc.Length - 1; j >= 0; --j) { pos[tc[j] - 'a'] = Math.Max(pos[tc[j] - 'a'], j); } var flg = false; for (var j = 0; j < tc.Length; ++j) { if (flg) break; for (var c = 0; c < tc[j] - 'a'; ++c) if (pos[c] > j) { (tc[j], tc[pos[c]]) = (tc[pos[c]], tc[j]); flg = true; break; } } for (var j = 0; j + 1 < tc.Length; ++j) { if (flg) break; if (tc[j] == tc[j + 1]) { flg = true; } } if (!flg) (tc[^2], tc[^1]) = (tc[^1], tc[^2]); t[i] = string.Concat(tc); if (string.CompareOrdinal(t[i], min) < 0) { min = t[i]; minc = 1; } else if (t[i] == min) { ++minc; } } var ans = new int[n]; for (var i = 0; i < n; ++i) if (t[i] == min) ans[i] = n + 1 - minc; WriteLine(string.Join("\n", ans)); } }