#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
int n;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  while (n--) {
    string cad1, cad2;
    cin >> cad1 >> cad2;
    bool flag = false;
    if (cad1 < cad2) {
      cout << cad1 << "\n";
      continue;
    }
    for (int i = 0; i < cad1.size(); i++) {
      for (int j = i + 1; j < cad1.size(); j++) {
        if (cad1[i] <= cad1[j]) continue;
        swap(cad1[i], cad1[j]);
        if (cad1 < cad2) {
          cout << cad1 << "\n";
          flag = true;
          break;
        }
        swap(cad1[j], cad1[i]);
      }
      if (flag) break;
    }
    if (!flag) {
      cout << "---\n";
    }
  }
}
