import java.io.*; import java.util.*; import java.util.function.*; import java.util.stream.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); String[] mystrings = new String[n]; TreeMap counts = new TreeMap<>(); for (int i = 0; i < n; i++) { mystrings[i] = getChange(sc.next()); counts.put(mystrings[i], counts.getOrDefault(mystrings[i], 0) + 1); } int score = n + 1 - counts.firstEntry().getValue(); String result = Arrays.stream(mystrings).map(x -> String.valueOf(x.equals(counts.firstKey()) ? score : 0)) .collect(Collectors.joining("\n")); System.out.println(result); } static String getChange(String s) { char[] arr = s.toCharArray(); List> idxes = new ArrayList<>(); for (int i = 0; i < 26; i++) { idxes.add(new ArrayDeque<>()); } for (int i = 0; i < arr.length; i++) { idxes.get(arr[i] - 'a').add(i); } boolean enable = false; boolean[] visited = new boolean[26]; for (int i = 0; i < arr.length - 1 && !enable; i++) { int current = arr[i] - 'a'; if (visited[current]) { continue; } for (int j = 0; j < current && !enable; j++) { if (idxes.get(j).size() > 0) { enable = true; int next = idxes.get(j).pollLast(); char tmp = arr[i]; arr[i] = arr[next]; arr[next] = tmp; } } visited[current] = true; idxes.get(current).pollFirst(); } if (!enable) { int left = arr.length - 2; int right = arr.length - 1; char tmp = arr[left]; arr[left] = arr[right]; arr[right] = tmp; } return new String(arr); } } class Scanner { BufferedReader br; StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); public Scanner() { try { br = new BufferedReader(new InputStreamReader(System.in)); } catch (Exception e) { } } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String next() { try { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } } catch (Exception e) { e.printStackTrace(); } finally { return st.nextToken(); } } }