Maximize permutations
Submissions: 507   Accuracy:

16.94%

  Difficulty: Easy   Marks: 2

Given two numbers N and M. Construct maximal number by permuting (changing order) the digits of N, not exceeding M. It is allowed to leave N as it is.

Input:

The first line of the input contains T i.e number of test cases. Each line of the test case contains Two numbers N, M  separated by space.

Output:

Each new line of the output contains only one single integer i.e Maximize Permutation if possible else -1.

Constraints:

1<=T<=500

1<=N<=10^5

1<=M<=10^5

Example:

Input:

2
123  222
3921  10000

output:

213
9321

Explanation:
Tese Case 1: here are total 3! permutations possible for N = 123, But the only permutation that satisfies the given condition is 213.

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Nishant Singh
Author: Nishant_Singh


If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.



Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.


to report an issue on this page.