#include <bits/stdc++.h>
using namespace std;
long long M = 1000000007;
signed main() {
  long long t;
  cin >> t;
  for (long long u = 0; u < t; u++) {
    long long q = 0;
    string a, b;
    cin >> a >> b;
    for (long long i = 0; i < min(a.size(), b.size()); i++) {
      if (b[i] <= a[i] && q == 0) {
        for (long long j = i + 1; j < a.size(); j++) {
          if (a[j] < b[i]) {
            q++;
            char z = a[i];
            a[i] = a[j];
            a[j] = z;
            break;
          }
        }
        if (q == 0 && b[i] < a[i]) {
          for (long long j = a.size() - 1; j > i; j--) {
            if (a[j] == b[i]) {
              q++;
              char z = a[i];
              a[i] = a[j];
              a[j] = z;
              break;
            }
          }
        }
      }
      if (b[i] < a[i] || b[i] > a[i]) break;
    }
    if (a < b)
      cout << a << endl;
    else
      cout << "---" << endl;
  }
}
