This page is still under construction.

Parts of this page are still being built. What you see may change.

Card Game

Interview

Time limit1sMemory limit512 MB

Summary
Given Bob's row of digits and Alice's digits, find the largest number Alice can form (using at least one card) that is smaller than both readings of Bob's row.
Level

Medium6 of 10

Topics
Brute force, Greedy, Sorting, Implementation
Solved
No attempts yet

Problem

Alice and Bob each have nn cards, and every card has a single digit from 11 to 99. Each player can form a number of up to nn digits using their own cards, and whoever forms the larger number wins.

Bob is too young to know how to form a large number. Instead, he lays his cards out in a row from left to right, then picks either the number formed by reading left to right or the number formed by reading right to left. For example, if the order Bob lays out on the table is [2,3,4][2, 3, 4], Bob can form 234234 by reading left to right, or 432432 by reading right to left.

Alice wants her younger brother Bob to win, so she plays by the following rules:

  • First, she waits for Bob to lay his cards on the table.
  • Whether Bob forms his number by reading left to right or right to left, Alice wants the number Bob forms to be larger than hers so her brother wins. She must use at least one card.
  • Among the ways to let Bob win, she wants to form the largest number she can.

For example, suppose Bob lays his cards in the order [2,3,4][2, 3, 4] and Alice's cards are [1,2,3][1, 2, 3].

  • Alice can form six three-digit numbers: 123123, 132132, 213213, 231231, 312312, 321321.
  • Since she does not know whether Bob will form 234234 or 432432, if Alice forms 231231, Bob wins no matter which number he forms, and this is the largest number Alice can form.

As another example, suppose Bob has [2,1,2][2, 1, 2] and Alice has [2,2,2][2, 2, 2].

  • The numbers Bob can form are the same in both directions: 212212.
  • The three-digit number Alice can form is 222222, which is larger than 212212, so Alice cannot let Bob win while using all three cards.
  • Among the two-digit numbers Alice can form is 2222, and this is the answer for this example.

Given nn and the values on the two players' cards as input, find the largest number Alice can form.

Input

The first line gives the number of test cases TT. Each test case spans three lines.

The first line gives nn.

The second line gives the digits on Bob's cards without spaces. Bob lays his cards on the table in this order.

The third line gives the digits on Alice's cards without spaces.

Output

Print the answer for each test case on its own line.

Constraints

  • 1≤T≤101 ≤ T ≤ 10
  • 2≤n≤82 ≤ n ≤ 8

Examples1

  1. Example 1

    Input
    5
    2
    99
    99
    3
    212
    222
    3
    234
    123
    4
    4123
    2345
    8
    12345678
    99999999
    
    Expected output
    9
    22
    231
    2543
    9999999