#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int total;
  string s, c;
  cin >> total;
  while (total--) {
    cin >> s >> c;
    if (s < c) {
      cout << s << endl;
      continue;
    }
    bool flag = false;
    map<char, int> tree;
    for (int i = 0; i < s.size(); i++) {
      if (tree.find(s[i]) == tree.end()) {
        tree[s[i]] = 1;
      } else {
        tree[s[i]]++;
      }
    }
    for (int i = 0; i < s.size(); i++) {
      auto j = tree.begin();
      if (s[i] != (*j).first) {
        for (int k = i + 1; k < s.size(); k++) {
          char temp = s[k];
          s[k] = s[i];
          s[i] = temp;
          if (s < c) {
            cout << s << endl;
            flag = true;
            break;
          }
          temp = s[k];
          s[k] = s[i];
          s[i] = temp;
        }
      }
      if (flag) break;
      tree[s[i]]--;
      if (tree[s[i]] == 0) {
        tree.erase(s[i]);
      }
    }
    if (!flag) {
      cout << "---" << endl;
    }
    tree.clear();
  }
  return 0;
}
