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: # cook your dish here
  # cook your dish here
  #-------------------imports-------#
  import sys
  import io,os
  import string
  from io import BytesIO, IOBase
  from os import path
  from collections import Counter,OrderedDict
  import itertools
  
  from itertools import chain, combinations
  import math
  import random
  import bisect
  from functools import reduce
  from sys import stdin,stdout
  from math import gcd,floor,sqrt,log
  from collections import defaultdict as dd
  from bisect import bisect_left as bl,bisect_right as br
  
  
  #------------------ONLINE JUDGE OR NOT-------#
  
  if(path.exists('input.txt')):
      sys.stdin = open("input.txt","r")
      sys.stdout = open("output.txt","w")
  else:
      input = sys.stdin.readline
      # input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
  
  
  
  sys.setrecursionlimit(100000000)
  
  
  
  #---------------------pre made and input functions-------#
  simp    =lambda: int(input())
  strng  =lambda: input().strip()
  jn     =lambda x,l: x.join(map(str,l))
  strl   =lambda: list(input().strip())
  mul    =lambda: map(int,input().strip().split())
  mulf   =lambda: map(float,input().strip().split())
  seq    =lambda: list(map(int,input().strip().split()))
  ceil   =lambda x: int(x) if(x==int(x)) else int(x)+1
  ceildiv=lambda x,d: x//d if(x%d==0) else x//d+1
  flush  =lambda: stdout.flush()
  stdstr =lambda: stdin.readline()
  stdint =lambda: int(stdin.readline())
  stdpr  =lambda x: stdout.write(str(x))
  
  
  #------------functions---------------------
  
  #---------------------input-------
  
  a = simp()
  
  
  def if_its_binaryform_palindrome(n):
  
      res = 0
  
      k = n
      while k > 0:
          #to finish the job
          res = (res << 1) | (k & 1)
          k = k >> 1  
  
      return n == res
  
  # prime = list_of_prime_sieve(10000000)        
    
  def solve(s,t):
  
      first = t[0]
      second = t[1]
      third = t[2]
  
      if 'a' not in s or 'b' not in s or 'c' not in s:
          sortedd = sorted(s)
          s = ''.join(sortedd)
  
  
          return s
  
  
      if s[0]=='a' and t[0]!='a':
          sortedd = sorted(s)
          s = ''.join(sortedd)
          # s.sort()
          return s
      elif t[0]!='a':   
          sortedd = sorted(s)
          s = ''.join(sortedd)
          # s.sort()
          return s 
  
      sortedd = sorted(s)
      s = ''.join(sortedd)    
      # s.sort()
          
      d = Counter(s)  
  
      r = ''
      if t[1]=='b':
          rr = ''
          for i,j in d.items():
              if i=='a':
                  r+=i*j
              elif i=='b' and r[-1]=='a':
                  rr+=i*j
              elif i=='c':
                  r+=i*j
                  r+=rr
              else:
                  r+=i*j
  
          return r
      elif t[1]=='c':
          rr = ''
          for i,j in d.items():
              if i=='a':
                  r+=i*j
              elif i=='c' and r[-1]=='a':
                  rr+=i*j
              elif i=='b':
                  r+=i*j
                  r+=rr
              else:
                  r+=i*j
          return r
  
      # elif t[1]=='a':
      #     rr = ''
      #     for i,j in d.items():
      #         r+=i*j
  
      # return r       
  
                          
  
  
                      
                      
  
  
      # a a a a b b c
  
  
      # abc
  
      # t = 'abc'    
  
  
  
  for i in range(a):
      t = strng()
      d = strng()
      print(solve(t,d))
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  def seive(n):
      """ preprocessing  """
   
      prime = [1 for i in range(n + 1)]
      p = 2
      while (p * p <= n):
           
          # this is prime number.
          if (prime[p] == 1):
               
              # Update all multiples of p in array
              for i in range(p ** 2, n + 1, p):
                  prime[i] = 0
                  
          p += 1
      prime[0]= 0
      prime[1]= 0
      
      # print(prime)
      for i in range(1,n+1):
          prime[i] += prime[i-1]
          
      return prime
  
  def list_of_prime_sieve(n):
  
      #an array of True True
      primes = [True]*(n+1)
      #changing o and 1 to False
      primes[0]==primes[1]==False
  
      #now for every true we find
      for i in range(2,int(n**(1/2))+1):
          if primes[i]==True:
              #we change their multiples to False
              for j in  range(i*i,n+1,i):
                  primes[j]=False
  
  
      list_primes = []
      for i in range(2,len(primes)):
          if primes[i]==True:
              list_primes.append(i)
  
      return list_primes
  
  def count_primes(a):
      return len(list_of_prime_sieve(a))    
  
  
  def gcdoftwo(a,b):
  
      #log(min(a,b))
      if a == 0:
          return b
      else:
          return gcdoftwo(b%a,a)
  
  def lcm(a,b): return (a*b//(gcdoftwo(a,b)))        
  
  def gcdofList(A): 
      res = A[0]
      for c in A[1::]:
          res = gcdoftwo(res , c) 
          if res == 1:
              return res
      return res         
  
  def binpow(a,b):
  
      #to calc a^b in log(n) time
  
  
      res = 1
  
  
      while b>0:
  
          if b&1:
              res = res*a
          a = a*a
          b>>=1
      return res        
  
  def fibo_in_logn(n):
      if n == 0:
          return [0, 1]
  
      p = fibo_in_logn(n >> 1)
      c = p.first * (2 * p.second - p.first)
      d = p.first * p.first + p.second * p.second
      if n & 1:
          return {d, c + d}
      else:
          return {c, d}
  
  
  
  
  
  def x_n_mod_m(a,b,m):
  
      #to calculate x^n mod m
  
      a  = a%m 
  
      res = 1
      while b>0:
          if b&1:
              res = res*a%m 
          a = a*a%m
          b>>=1
  
      return res
  
  
  
  
  def change_array(count,t):
  
      emp = []
      for i in range(len(t)):
          emp.append(count[t[i]])
      return emp    
  
  
  
  
  # prime = []
  
  def simpleSieve(limit):
  
      mark = [True for i in range(limit + 1)]
      p = 2
      while (p * p <= limit):
          if (mark[p] == True):
              for i in range(p * p, limit + 1, p):
                  mark[i] = False
          p += 1
      for p in range(2, limit):
          if mark[p]:
              prime.append(p)
              # print(p,end = " ")
      return prime        
  
  def segmentedSieve(n):
      limit = int(math.floor(math.sqrt(n)) + 1)
      t = []
      t += simpleSieve(limit)
  
      low = limit
      high = limit * 2
      while low < n:
          if high >= n:
              high = n
  
          mark = [True for i in range(limit + 1)]
  
          for i in range(len(prime)):
  
              loLim = int(math.floor(low / prime[i]) *
                                          prime[i])
              if loLim < low:
                  loLim += prime[i]
              for j in range(loLim, high, prime[i]):
                  mark[j - low] = False
  
          # t = []        
          for i in range(low, high):
              if mark[i - low]:
                  t.append(i)
                  # print(i, end = " ")
  
          low = low + limit
          high = high + limit
  
      return t
  
  
  
  
  mod=1000000007
  
  def occurrences(string, sub):
      count = start = 0
      while True:
          start = string.find(sub, start) + 1
          if start > 0:
              count+=1
          else:
              return count
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
      
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  