Don't let the Night King win

0

0 votes
Medium
Problem

When Brienne and Arya end their little duel in a truce of sorts, Brienne asks, in astonishment, “Who taught you to do that?” “No one,” Arya responds.

"She is better than me, a Knight. How did she learn all this? I will find.", Brienne mumbled. Her only hope is Bran, who knows everything about everyone. She searched for Bran and asked him about Arya. Bran agreed to help Brienne but only if she will help Bran to defeat the Night king.

Bran has two numbers, a Stark number X and a Dead number Y of equal length N. Bran wants to make X as close as possible to Y. He can arrange the digits of the number X in any order to do so. Let that closest number formed by arranging digits of X in any order is Z. Help Brienne find |Z - Y|.

Note

1. Numbers X and Y will not contain any leading zero.

2. Z should contain the exact digits as of X, only the arrangement of digits may be different.

Input format:

There are number of test cases T.

Each test case contains three lines.

First line is the, the number of digits of X and Y i.e. N.

Second line contains the digits of Stark number X separated by space.

Last line contains the digit of Dead number Y separated by space.

Output Format:

For each test case, in new line, print the absolute value of the minimum difference between number Y and Z.

Constraints:

1 <= T <= 10000

1 <= N <= 18

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Test case 1:

Stark number X = 1212

Dead number Y = 1500

Optimal Permutation of X = Z = 1221

Answer = |Z - Y | = |1500-1221| = 279.

Here, |1500-1221| is smallest among all permutations of X.

Editor Image

?