#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())) { swap(s[i][s[i].size() - 1], s[i][s[i].size() - 2]); continue; } multiset st(s[i].begin(), s[i].end()); char a, b; for (int j = 0; j < s[i].size(); j++) { if (s[i][j] != *st.begin()) { a = s[i][j]; b = *st.begin(); break; } st.erase(st.begin()); } 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"; } } }