#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const long long INF = 1e18;
const long long MAX = 100001;
void solve() {
  string str, s;
  cin >> str >> s;
  map<char, int> m;
  int ii = 0;
  for (char ch : str) m[ch] = ++ii;
  long long flag = 1;
  for (int i = 0; i < (min(s.length(), str.length())); i++) {
    if (str[i] < s[i]) {
      cout << str << '\n';
      return;
    }
    if (flag)
      for (int j = 0; j < (str[i] - 'A'); j++) {
        if (m['A' + j]) {
          flag = 0;
          swap(str[i], str[m['A' + j] - 1]);
          break;
        }
      }
    if (str[i] < s[i]) {
      cout << str << '\n';
      return;
    }
    if (str[i] > s[i]) {
      cout << "---\n";
      return;
    }
    if (m[str[i]] == i + 1) m[str[i]] = 0;
  }
  if (str.length() >= s.length())
    cout << "---\n";
  else
    cout << str << '\n';
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long t;
  t = 1;
  cin >> t;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}
