#include <bits/stdc++.h>
using namespace std;
long long int INF = 1000000007;
long long int exp(long long int x, long long int n) {
  if (n == 0) return 1;
  if (n == 1) return x % INF;
  if (n % 2 == 0)
    return exp((x * x) % INF, n / 2);
  else
    return (x * exp((x * x) % INF, n / 2)) % INF;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int t, i, j;
  cin >> t;
  while (t--) {
    string s, c;
    cin >> s >> c;
    string k = s;
    sort(s.begin(), s.end());
    long long int ind = -1;
    for (i = 0; i < s.length(); i++) {
      if (s[i] != k[i]) {
        for (long long int j = i; j < s.length(); j++) {
          if (s[i] == k[j]) {
            ind = j;
          }
        }
        break;
      }
    }
    if (ind != -1) {
      char temp = k[i];
      k[i] = s[i];
      k[ind] = temp;
      i = 0;
      if (lexicographical_compare(k.begin(), k.end(), c.begin(), c.end())) {
        cout << k << endl;
      } else
        cout << "---" << endl;
    } else {
      if (lexicographical_compare(k.begin(), k.end(), c.begin(), c.end())) {
        cout << k << endl;
      } else
        cout << "---" << endl;
    }
  }
  return 0;
}
