#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5100;
const int MAXM = 100100;
const int INF = 1 << 30;
string a, b;
int n, m;
int ma[MAXN][30];
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    cin >> a >> b;
    if (a < b) {
      cout << a << endl;
    } else {
      n = a.length();
      m = b.length();
      for (int i = 0; i <= n; i++)
        for (int j = 0; j < 26; j++) {
          ma[i][j] = 0;
        }
      for (int i = 1; i <= n; i++) {
        for (int j = 0; j < 26; j++) {
          ma[i][j] = ma[i - 1][j];
        }
        ma[i][a[i - 1] - 'A']++;
      }
      int p = 0, i, j;
      bool flag = false;
      while (p < n && p < m) {
        int lexi = a[p] - 'A';
        int lexi_b = b[p] - 'A';
        for (int k = 0; k <= lexi - 1; k++) {
          if (lexi_b < k) break;
          if (ma[n][k] - ma[p + 1][k] > 0) {
            flag = true;
            i = p;
            for (int q = n - 1; q >= p + 1; q--) {
              if (a[q] - 'A' == k) {
                j = q;
                break;
              }
            }
            break;
          }
        }
        if (flag || a[p] != b[p]) {
          break;
        }
        p++;
      }
      if (flag) {
        swap(a[i], a[j]);
        if (a < b)
          cout << a << endl;
        else
          cout << "---" << endl;
      } else
        cout << "---" << endl;
    }
  }
  return 0;
}
