#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
int main() {
  int T;
  scanf("%d\n", &T);
  while (T-- > 0) {
    char s[5010], c[5010];
    scanf("%s %s", s, c);
    int end = 0, index = 0;
    char min_c = s[0], Copy[5010];
    copy(s, s + strlen(s) + 1, Copy);
    sort(Copy, Copy + strlen(s));
    if (strcmp(Copy, s))
      while (!end) {
        for (int i = index + 1; i < (int)strlen(s); ++i)
          if (s[i] <= min_c) {
            min_c = s[i];
            index = i;
          }
        for (int i = 0; i < index; ++i)
          if (min_c < s[i]) {
            swap(s[index], s[i]);
            end++;
            break;
          }
        min_c = 'Z';
      }
    strcmp(s, c) < 0 ? printf("%s\n", s) : printf("---\n");
  }
  return 0;
}
