#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e4 + 10;
int T, lc[30][maxn];
char s[maxn], t[maxn];
int main() {
  scanf("%d", &T);
  while (T--) {
    scanf("%s%s", s, t);
    int n = strlen(s), m = strlen(t);
    n--;
    m--;
    for (int i = 0; i <= n + 1; i++)
      for (int j = 0; j < 26; j++) lc[j][i] = -1;
    for (int i = n; i >= 0; i--) {
      for (int j = 0; j < 26; j++) lc[j][i] = lc[j][i + 1];
      lc[s[i] - 'A'][i] = i;
    }
    for (int i = 0; i <= n; i++) {
      char cc = s[i];
      int lcc = -1;
      for (int j = n; j >= i + 1; j--)
        if (s[j] < cc) {
          cc = s[j];
          lcc = j;
        }
      if (lcc != -1) {
        swap(s[lcc], s[i]);
        break;
      }
    }
    if (strcmp(s, t) < 0)
      printf("%s\n", s);
    else
      printf("---\n");
  }
  return 0;
}
