#include "bits/stdc++.h"
using namespace std;
 
/** Template Begins **/

#define ll          long long 
#define IOS         ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl        '\n'
 
const int N = 5e6+7;

/** Template Ends **/


signed main() 
{ 
    IOS;
    int t;  cin >> t;
    while (t--) {
        string s;   cin >> s;
        int n = s.size();
        int cnt[26];
        int taken[26];
        memset(taken, 0, sizeof taken);
        memset(cnt, 0, sizeof cnt);
        for (char c: s) cnt[c - 'a']++;

        stack<char> st;
        for (char c: s) {
            cnt[c - 'a']--;
            if (taken[c - 'a']) continue;
            else {
                while (!st.empty() && c > st.top() && cnt[st.top() - 'a'] > 0) {
                    char rem = st.top();
                    st.pop();
                    taken[rem - 'a'] = false;
                }
                st.push(c);
                taken[c - 'a'] = true;
            }
        }
        string ans;
        while (!st.empty()) {
            ans += st.top();
            st.pop();
        }
        reverse(ans.begin(), ans.end());
        cout << ans << endl;
    }

    return 0;
}