#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int N; cin >> N;
    vector<string> S(N);
    vector<pair<string,int>> S2(N);
    for(auto &s : S) cin >> s;

    int idx = -1;
    for(auto s : S){
        idx++;
        string t = s;
        sort(t.begin(),t.end());

        int n = s.size();
        vector<vector<int>> C(26);
        for(int i=1; i<n; i++) C.at(s.at(i)-'a').push_back(i);

        if(t == s){
            bool ok = false;
            for(int i=0; i<26; i++){
                if(C.at(i).size() <= 1) continue;
                swap(s.at(C.at(i).at(0)),s.at(C.at(i).at(1)));
                ok = true; break;
            }
            if(!ok) swap(s.at(n-1),s.at(n-2));
            S2.at(idx) = {s,idx};
        }
        else{
            for(int i=0; i<n; i++){
                if(s.at(i) == t.at(i)) continue;
                char c = 'z'+1; int pos = n;
                for(int k=n-1; k>i; k--){
                    if(c > s.at(k)) c = s.at(k),pos = k;
                }
                swap(s.at(i),s.at(pos));
                break;
            }
            S2.at(idx) = {s,idx};
        }
    }
    sort(S2.begin(),S2.end());

    int win = 0; string s0 = S2.at(0).first;
    vector<bool> W(N);
    for(int i=0; i<N; i++){
        if(S2.at(i).first == s0) win++,W.at(S2.at(i).second) = true;
        else break;
    }
    for(auto w : W){
        if(w) cout << N+1-win << endl;
        else cout << 0 << endl;
    }
    
}