#include <bits/stdc++.h>
using namespace std;
int main() {
  int t, l, i, j, l1, l2, f;
  string s, c, tmp;
  cin >> t;
  while (t--) {
    cin >> s;
    cin >> c;
    l1 = s.length();
    l2 = c.length();
    f = 0;
    tmp = s;
    sort(tmp.begin(), tmp.end());
    for (i = 0; i < l1; i++) {
      if (s[i] != tmp[i]) {
        for (j = l1 - 1; j > i; j--) {
          if (s[j] == tmp[i]) {
            f = 1;
            swap(s[i], s[j]);
            break;
          }
        }
      }
      if (f == 1) break;
    }
    if (s.compare(c) < 0)
      cout << s;
    else
      cout << "---";
    cout << endl;
  }
}
