After years of research, Ninja is finally able to invent the time machine, and now he is back to the good old days when T9 keypads were being used in mobile phones.
Being a curious person, Ninja wants to find out all possible strings that can be formed by pressing the keys of the phone.
Formally, you are given a string S, that consists of digits from 2-9 (both inclusive), your task is to find out all the possible strings that can be formed from the input string by mapping the digits to the letters as in a T9 keypad. Then, print the strings in a lexicographically sorted order.

If S = “34”, then all the possible letters that can be formed from string S are {“dg”, “dh”, “di”, “eg”, “eh”, “ei”, “fg”, “fh”, “fi”}.
The first line contains an integer 'T' which denotes the number of test cases or queries to be run. Then, the 'T' test cases follow.
The first line of each test case contains a string S.
Output format:
For each test case, print all the possible strings separated by a single space in lexicographically sorted order, that can be formed from the input string by mapping the digits to the letters as in a T9 keypad.
The output of each test case will be printed in a separate line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= |S| <= 7
where |S| denotes the length of the string S.
Time limit: 1 sec.
1
5
j k l
For the first test case, the words that can be formed from the string S are {“j”, “k”, “l”}.
2
37
79
dp dq dr ds ep eq er es fp fq fr fs
pw px py pz qw qx qy qz rw rx ry rz sw sx sy sz
For the first test case, the words that can be formed from the string S are {“dp”, “dq”, “dr”, “ds”, “ep”, “eq”, “er”, “es”, “fp”, “fq”, “fr”, “fs”}.
For the second test case, the words that can be formed from the string S is are {“pw”, “px”, “py”, “pz”, “qw”, “qx”, “qy”, “qz”, “rw”, “rx”, “ry”, “rz”, “sw”, “sx”, “sy”, “sz”}.
Think of a solution using backtracking.
Approach:
The idea is to use a backtracking algorithm. So, we traverse each digit of the string S, we have 3 options to choose if the digit belongs to {2,3,4,5,6,8} or 4 options if it belongs to {7,9}. So, go through all possible options and add a letter in the keypad that map to the current digit and add it to the current string. If there are no more digits to be checked, then add this string to the result array.
Steps:
void possibleWordsUtil(S, res, curr, index, mp):
O((N + M) * (3 ^ N * 4 ^ M)), where ‘N’ denotes the number of digits in the string ‘S’, that maps to 3 letters such as {2, 3, 4, 5, 6, 8} and ‘M’ denotes the number of digits in the string ‘S’, that maps to 4 letters such as {7, 9}.
In the worst case, a total number of (3 ^ N * 4 ^ M) strings can be formed and each string has the length in the order of N + M. Hence, the overall time complexity is O((N + M) * (3 ^ N * 4 ^ M)).
O((N + M) * (3 ^ N * 4 ^ M)), where ‘N’ denotes the number of digits in the string ‘S’, that maps to 3 letters such as {2, 3, 4, 5, 6, 8} and ‘M’ denotes the number of digits in the string ‘S’, that maps to 4 letters such as {7, 9}.
In the worst case, a total number of (3 ^ N * 4 ^ M) strings can be formed and each string has the length in the order of N + M. We have to keep all these strings in a result array. Hence, the overall space complexity is O((N + M) * (3 ^ N * 4 ^ M)).