#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <bitset>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <unordered_map>

using namespace std;
#define fi first
#define se second
#define re register
typedef long long ll;
typedef pair<ll, ll> PII;
typedef unsigned long long ull;
const int N = 3e5 + 10, M = 1e6 + 5, INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int stk[N], top;
void solve() {
    int T; cin >> T; while (T--) {
        string s; cin >> s;
        vector<int> cnt(26), vis(26); top = 0;
        for (int i = 0; i < s.size(); i++) cnt[s[i]-'a']++;
        for (int i = 0; i < s.size(); i++) {
            if (vis[s[i]-'a']) {
                cnt[s[i]-'a']--;
                continue;
            }
            while (top && s[stk[top]] < s[i] && cnt[s[stk[top]]-'a']) {
                vis[s[stk[top]]-'a'] = 0;
                //cnt[s[stk[top]]-'a']--;
                top--;
            }
            stk[++top] = i;
            vis[s[stk[top]]-'a'] = 1;
            cnt[s[stk[top]]-'a']--;
        }
        for (int i = 1; i <= top; i++) cout << s[stk[i]];
        cout << endl;
    }
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#ifdef ACM_LOCAL
    freopen("input", "r", stdin);
    freopen("output", "w", stdout);
#endif
    solve();
    return 0;
}