

Two strings ‘A’ and ‘B’ are given. A string ‘C’ has to be formed using these two strings according to the following rules:
1. Append string ‘A’ to C one time.
2. Append string ‘B’ to C two times.
3. Append string ‘A’ to C three times.
4. Append string ‘B’ to C four times.
5. Append string ‘A’ to C five times.
And so on….
Your task is to return the ‘K’th character to the newly formed string ‘C’.
For ExampleIf ‘A’ = “AB” and B = “CD” and K = 7.
The formed string will be “ABCDCDABABAB…...”.
So, the 7th character is A.
The first line of the input contains an integer, 'T,’ denoting the number of test cases.
The first line of each test case contains two strings, 'A’ and ’B’.
The second line contains ‘K’ representing the character number to be found.
Output Format:
For each test case, print a character denoting the ‘K’th character.
Print the output of each test case 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 <= length of ‘A’,length of ‘B’ <= 100.
1 <= ‘K’ <= 10^15
Time limit: 1 sec
2
AB
CD
7
ABC
A
10
A
B
For the first test case,
The formed string will be “ABCDCDABABABCDCDCDCD…...”.
The 7th character is A. Hence, the answer is A.
For the second test case:
The formed string will be “ABCAAABCABCABCAAAA……..” .
The 10th character is B. Hence, the answer is B.
2
ABCDD
F
6
ACE
X
3
F
E
Try to maintain the size of the formed string.
In this approach, we will first declare a string ‘STRING_SIZE’ to store the size of the newly formed string.
Now, we will update the ‘STRING_SIZE’ according to the given conditions until ‘STR’ is less than ‘K’. After that, we will check if the last inserted strings are strings ‘A’ or ‘B’.We will find the number of extra added characters as ‘EXTRA’.
We will now remove the extra strings and decrease ‘EXTRA’. Now the ‘EXTRA’ + 1th character from the last of the last inserted string is the ‘K’th character of the formed string.
Algorithm:
O(√K ), where ‘K’ is the position of the character.
In the worst case, if the size of strings ‘A’ and ‘B’ is one. The size of the formed string will be like 1+2+3+4….. which is equal to i*(i+1)/2 = ‘K’.So we will be iterating for i of order √K.Hence the overall time complexity is O(√K ).
O(1).
In this approach, we have used constant space. Hence the overall space complexity is O(1).