#include using namespace std; void fast_io() { ios_base::sync_with_stdio(false); cin.tie(nullptr); } int main() { fast_io(); int n; cin >> n; vector s(n); for (int i = 0; i < n; i++) { cin >> s[i]; if (is_sorted(s[i].begin(), s[i].end())) { bool is_swaped = false; for (int j = 0; j < s[i].size() - 1; j++) { if (s[i][j] == s[i][j + 1]) { is_swaped = true; break; } } if (!is_swaped) { swap(s[i][s[i].size() - 1], s[i][s[i].size() - 2]); } continue; } string ss = s[i]; sort(ss.begin(), ss.end()); char a = '.', b = '.'; for (int j = 0; j < s[i].size(); j++) { if (s[i][j] != ss[j]) { a = s[i][j]; b = ss[j]; break; } } assert(a != '.' && b != '.'); assert(a > b); int ii = 0, jj = s[i].size() - 1; while (s[i][ii] != a) { ii++; } while (s[i][jj] != b) { jj--; } swap(s[i][ii], s[i][jj]); } string mi = *min_element(s.begin(), s.end()); int cnt = 0; for (int i = 0; i < n; i++) { if (s[i] == mi) { cnt++; } } for (int i = 0; i < n; i++) { if (s[i] == mi) { cout << n - cnt + 1 << "\n"; } else { cout << "0\n"; } } }