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

int main() {
    int t;
    scanf("%d",&t);

    while (t--) {
        string s;
        vector<char> stack;
        map<char,int> hm;
        set<char> visited;
        cin>>s;
        for (int i=0;i<=s.size();i++) hm[s[i]]++;
        for (int i=0;i<s.size();i++) {
            if (visited.find(s[i])==visited.end()) {
                int len=stack.size();
                while (len && stack[len-1]<=s[i] && hm[stack[len-1]]>1) {
                    hm[stack[len-1]]--;
                    visited.erase(stack[len-1]);
                    stack.pop_back();
                    len--;
                }
                stack.push_back(s[i]);
                visited.insert(s[i]);
            } else hm[s[i]]--;
            
            // for (auto& j:stack) cout<<j;
            // cout<<" "<<s[i]<<" "<<hm[s[i]]<<endl;
        }
        string ans;
        for (auto& i:stack) {
            ans+=i;
        }
        cout<<ans<<endl;
    }
}