description: You are given strings S and T, consisting of lowercase English letters. It is guaranteed that T is a permutation of the string abc. 
  
  Find string S', the lexicographically smallest permutation of S such that T is not a subsequence of S'.
  
  String a is a permutation of string b if the number of occurrences of each distinct character is the same in both strings.
  
  A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) elements.
  
  A string a is lexicographically smaller than a string b if and only if one of the following holds:
  
    * a is a prefix of b, but a ≠ b;
    * in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b.
  
  Input
  
  Each test contains multiple test cases. The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. Description of the test cases follows.
  
  The first line of each test case contains a string S (1 ≤ |S| ≤ 100), consisting of lowercase English letters.
  
  The second line of each test case contains a string T that is a permutation of the string abc. (Hence, |T| = 3).
  
  Note that there is no limit on the sum of |S| across all test cases.
  
  Output
  
  For each test case, output a single string S', the lexicographically smallest permutation of S such that T is not a subsequence of S'.
  
  Example
  
  Input
  
  
  7
  abacaba
  abc
  cccba
  acb
  dbsic
  bac
  abracadabra
  abc
  dddddddddddd
  cba
  bbc
  abc
  ac
  abc
  
  
  Output
  
  
  aaaacbb
  abccc
  bcdis
  aaaaacbbdrr
  dddddddddddd
  bbc
  ac
  
  Note
  
  In the first test case, both aaaabbc and aaaabcb are lexicographically smaller than aaaacbb, but they contain abc as a subsequence.
  
  In the second test case, abccc is the smallest permutation of cccba and does not contain acb as a subsequence.
  
  In the third test case, bcdis is the smallest permutation of dbsic and does not contain bac as a subsequence.
solution: ##########################################################################
  #     Author : nesco   
  #     Created: Mon Dec 27 2021 10:43:58
  #     File   : A. Forbidden Subsequence
  ##########################################################################
  import sys,math,cmath,time,collections
  from string import ascii_lowercase
  ##########################################################################
  ################# ---- THE ACTUAL CODE STARTS BELOW ---- #################
  def solve():
      s = insr()
      t = insr()
  
      if t != "abc":
          print(sort_string(s))
      else:
          r = ""
          st = string_counter(s)
          for i in range(26):
              if i == 1:
                  if st[0] == 0 or st[1] == 0 or st[2] == 0:
                      r = r + ascii_lowercase[1]*st[1]
                  else:
                      r = r + ascii_lowercase[2]*st[2]
              elif i == 2:
                  if st[0] == 0 or st[1] == 0 or st[2] == 0:
                      r = r + ascii_lowercase[2]*st[2]
                  else:
                      r = r + ascii_lowercase[1]*st[1]
              elif st[i] != 0:
                  r = r + ascii_lowercase[i]*st[i]
          print(r)
  
  ################## ---- THE ACTUAL CODE ENDS ABOVE ---- ##################
  ##########################################################################
   
  def main():
      global tt
      t = inp()
      for tt in range(1,t + 1):
          solve()
   
  #---------------------- USER DEFINED INPUT FUNCTIONS --------------------#
  def inp():   # input integer
      return(int(input()))
  def inlt():  # input list of integers
      return(list(map(int,input().split())))
  def inls():  # input list of strings
      return(list(map(str,input().split())))
  def insr():  # input string
      return(input().strip())
  def invr():  # input array
      return(map(int,input().split()))
   
  #------------------ USER DEFINED PROGRAMMING FUNCTIONS ------------------#
  # set to list converter
  def convert(set):
      return list(set)
  
  # outputs unique values from list
  def unique(list):
      list_set = set(list)
      unique_list = convert(list_set)
      return unique_list
  
  # sorting string
  def sort_string(string):
      r = sorted(string)
      return "".join(r)
  
  # reverse string
  def reverse_string(string):
      return string[::-1]
  
  # list of all prime numbers up to n
  def seive(n):
      a = []
      prime = [True for i in range(n+1)] 
      p = 2
      while (p * p <= n): 
          if (prime[p] == True): 
              for i in range(p ** 2,n + 1, p): 
                  prime[i] = False
          p = p + 1
      for p in range(2,n + 1): 
          if prime[p]: 
              a.append(p)
      return(a)
  
  def counter(a):
      q = [0] * max(a)
      for i in range(len(a)):
          q[a[i] - 1] = q[a[i] - 1] + 1
      return(q)
   
  # counts number of elements 
  def counter_elements(a):
      q = dict()
      for i in range(len(a)):
          if a[i] not in q:
              q[a[i]] = 0
          q[a[i]] = q[a[i]] + 1
      return(q)
  
  # counts number of letters in string
  def string_counter(a):
      q = [0] * 26
      for i in range(len(a)):
          q[ord(a[i]) - 97] = q[ord(a[i]) - 97] + 1
      return(q)
   
  # returns factorial of n
  def factorial(n,m = 1000000007):
      q = 1
      for i in range(n):
          q = (q * (i + 1)) % m
      return(q)
  
  # returns all (possible) factors of n
  def factors(n):
      q = []
      for i in range(1,int(n ** 0.5) + 1):
          if n % i == 0: q.append(i); q.append(n // i)
      return(list(sorted(list(set(q)))))
  
  # returns prime factors of n
  def prime_factors(n):
      q = []
      while n % 2 == 0: q.append(2); n = n // 2
      for i in range(3,int(n ** 0.5) + 1,2):
          while n % i == 0: q.append(i); n = n // i
      if n > 2: q.append(n)
      return(list(sorted(q)))
  
  # transpose matrix or vector (list)
  def transpose(a):
      n,m = len(a),len(a[0])
      b = [[0] * n for i in range(m)]
      for i in range(m): 
          for j in range(n): 
              b[i][j] = a[j][i]
      return(b)
  
  # returns boolean if x is power of two
  def power_two(x):
      return (x and (not(x & (x - 1))))
  
  # returns ceil of division
  def ceil(a, b):
      return -(-a // b)
  #-----------------------------------------------------------------------#
      
  main()