#include <bits/stdc++.h>
#define ll long long
#define endl "\n"
#define pb push_back
#define all(a) (a).begin(),(a).end()
#define F first
#define S second
#define rep(i,x,y) for(ll i=x;i<y;i++)
const int N=100000;
using namespace std;
stack<char> st;
void solve()
{
    string s; cin>>s;
    vector<int> vis(26,0),cnt(26,0); vector<char> ans;
    for(auto &j: s) cnt[j-'a']++;
    for(auto &j: s)
    {
        while(!st.empty()&& !vis[j-'a'] && cnt[st.top()-'a']>0 && st.top()<j) vis[st.top()-'a']=0,st.pop();
        if(!vis[j-'a']) st.push(j),vis[j-'a']=1;
        cnt[j-'a']--;
    }
    while(!st.empty()) ans.pb(st.top()),st.pop();
    reverse(all(ans));
    for(auto &j: ans) cout<<j;
}
int main() {
  ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
  int t=1;
  cin>>t;
  for(int i=1;i<=t;i++)
  {
      //cout<<"Case #"<<i<<": ";
      solve();
      cout<<endl;
  }
  return 0;
}