#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long i, j, k, n, m, x, y, z, t;
  i = j = k = n = m = x = y = z = t = 0;
  cin >> t;
  while (t--) {
    y = 0;
    char c;
    string s1, s2;
    cin >> s1 >> s2;
    string s3 = s1;
    sort(s3.begin(), s3.end());
    for (i = 0; i < s1.size(); i++) {
      if (s1[i] != s3[i]) {
        c = s3[i];
        x = i;
        break;
      }
    }
    if (i == s1.size()) {
      y = 1;
    }
    if (!y)
      for (i = s1.size() - 1; i >= 0; i--) {
        if (c == s1[i]) {
          swap(s1[i], s1[x]);
        }
      }
    for (i = 0; i < min(s1.size(), s2.size()); i++) {
      if (s1[i] > s2[i]) {
        cout << "---\n";
        break;
      } else if (s2[i] > s1[i]) {
        cout << s1 << '\n';
        break;
      }
    }
    if (i == min(s1.size(), s2.size())) {
      if (s1[i] == '\0' && s2[i] != '\0')
        cout << s1 << '\n';
      else
        cout << "---" << '\n';
    }
  }
}
