#include <bits/stdc++.h>

using namespace std;

#define ll long long
const int N = 3e5 + 5;
unordered_map<char, vector<int>> idx;
bool taken[26];

bool canDelete(char c, int i) {
    return upper_bound(idx[c].begin(), idx[c].end(), i) != idx[c].end();
}

void solve() {
    string s;
    cin >> s;
    int n = s.size();
    for (int i = 0; i < n; ++i) {
        idx[s[i]].push_back(i);
    }
    stack<int> stk;

    for (int i = 0; i < n; ++i) {
        if (taken[s[i] - 'a'])
            continue;
        while (!stk.empty() && s[i] > stk.top() && canDelete(stk.top(), i)) {
            taken[stk.top() - 'a'] = false;
            stk.pop();
        }
        stk.push(s[i]);
        taken[s[i] - 'a'] = true;
    }
    string ans;
    while (!stk.empty()) {
        ans.push_back(stk.top());
        stk.pop();
    }
    reverse(ans.begin(), ans.end());
    cout << ans << '\n';
    idx.clear();
    memset(taken, false, sizeof(taken));
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}