#include <bits/stdc++.h>
using namespace std;
string a, b, c;
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int T;
  cin >> T;
  while (T--) {
    cin >> a >> c;
    b = a;
    sort(b.begin(), b.end());
    if (b >= c)
      cout << "---" << endl;
    else {
      if (a == b || a < c)
        cout << a << endl;
      else {
        for (int i = 0; i < a.size(); i++) {
          if (a[i] != b[i]) {
            for (int j = a.size() - 1; j >= 0; j--) {
              if (a[j] == b[i]) {
                swap(a[j], a[i]);
                break;
              }
            }
            break;
          }
        }
        if (a < c)
          cout << a << endl;
        else
          cout << "---" << endl;
      }
    }
  }
  return 0;
}
