The Reversed World

All submissions for this problem are available.
Given three numbers A, B and C all having N digits, little Johnny needs to write a program to permute the digits of the number C so that the new number is greater than both A and B.
This problem would be so easy if Johnny's teacher didn't require that Johnny's program must be correct in both the real world and the reversed world. What happens once the world is reversed? One thing Johnny knows for sure is that all the numbers are also reversed. The second thing, the "greater than" relation becomes the
"smaller than" relation.
For the sake of clarity, we will use a^{R} to denote the number a in the reversed world (for instance, 113^{R} = 311).
Thus Johnny task is now permute the digits of C so that the new number is greater than both A and B. In additional, the reverse of the new number should be smaller than both A^{R} and B^{R}. If there are multiple possible solutions, Johnny needs to find the smallest one.
As usual, please help Little Johnny with his task!
Input
The first line contains a number T (about 10) which is the number of the test cases. Each test case has the following form.
The first line contains N (1 <= N <= 1000000).
The second line contains the number A.
The third line contains the number B.
The fourth line contains the number C.
Each test case's input is separated by a blank line.
Output
For each test case, print the solution in a single line. If there is no solution, print the number 1.
Example
Input: 2 2 12 23 32 3 124 324 335 Output: 1 353
Author:  admin 
Tester:  friggstad 
Editorial  http://discuss.codechef.com/problems/REVERSED 
Tags  admin, april10, hard 
Date Added:  11032010 
Time Limit:  0.26 sec 
Source Limit:  50000 Bytes 
Languages:  C, JAVA, PYTH, CS2, PAS fpc, PAS gpc, RUBY, PHP, HASK, SCALA, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TEXT 
Comments
 Please login at the top to post a comment.
SUCCESSFUL SUBMISSIONS
Fetching successful submissions