Mila has a string(consisting of only lowercase characters) which she wants to gift her best friend Gila. She wants to put the string in a gift box to gift it to Gila. She calculated the width of the gift box to be k units smaller than the length of the string. The string being larger than the box, Mila would now require to remove characters from the string.

Gila once told Mila about her liking for lexicographically smallest strings. Now, Mila wants the string to be lexicographically smallest to make her best friend happy. While calculating the width of the box, Mila made a small mistake. The correction in the value of k is - If the length of the string is a power of 2, reduce k by half, else multiply k by 2.

Help Mila in finding the resulting string.

**Input:**

The first line contains a single integer T denoting the number of test cases. Then T test cases follow. Each of the test cases contains a string and an integer k as described in the problem statement.

**Output:**

For each test case, print in a new line the resulting string obtained.

If it is not possible to remove k (the value of k after correction) characters or if the resulting string is empty print -1.

**Constraints:**

1 <= T <= 10^3

1 <= |String| <=10^6

1 <= k <= 10^6

**Example:**

**Input:**

2

fooland 2

code 4

**Output:**

and

cd

**Explanation:**

For **1st test case**:

As the length of the string = 7 which is not a power of 2, hence k = 2*2 = 4. After removing 4 characters from the given string, the resulting string is ‘and’.

For **2nd test case**:

As the length of the string = 4, which is 2 to the power 2, hence k = 4/2 = 2.

Hence, resulting string after removal of 2 characters is ‘cd’.

Author: sajalagrawal

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.

vamsikrishna55 | 162 |

Vagesh Verma | 158 |

UrGuru | 133 |

abducodes | 127 |

Akhil varma | 114 |

abducodes | 683 |

harshtyagi36 | 322 |

Akhil varma | 267 |

arpit_anshuman | 258 |

highway_to_hell | 252 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4567 |

Login to report an issue on this page.