We have a problem: There is a fixed initial batting lineup of 7 players in order: ["Rohit", "Dhawan", "Kohli", "Yuvraj", "Raina", "Dhoni", "Sir Jadeja"]. They play one ball at a time. After a player plays a ball, Jadeja makes a new copy of that player, and both the original and the copy get back into the batting lineup (presumably at the end of the lineup? The description: "both the same players will get back in this batting line up." It likely means that after playing, the player (the original) and the new copy are added to the end of the batting order (i.e., they get back into the lineup). So effectively, the batting order behaves like a queue: Initially, we have 7 players in order. At each step, the first player in the queue plays the ball, then we remove him from the front, and then we add two copies of that player to the back of the queue. This process continues.
We need to output the name of the player who plays the K-th ball (1-indexed). K up to 1e9, T up to 1000, so we need an efficient solution.
We can model the process: It's a classic queue process where each element "reproduces" into two copies. The sequence of players played forms an infinite sequence. We need to find the player at position K.
Observation: This is similar to a binary tree or a process of replicating. The initial queue is [0,1,2,3,4,5,6] indices mapping to names. At each step, we pop the front element X, and then push two X's at the back. So the queue evolves.
This process generates a sequence of players played: a0, a1, a2, ... where a_n is the player popped at step n+1.
We need a_n for n = K-1.
We can try to find pattern: The queue at any time is essentially the BFS order of an infinite binary tree? Actually, consider that each player generates two copies of itself, and those copies will be played later. This resembles the process of generating a sequence by repeatedly taking the first element and appending two copies. This is known to produce the "binary carry" or "binary representation" type of sequence. Another viewpoint: The order of play is determined by the binary representation of the index. Let's test small indices.
Let’s denote players as A0= Rohit, A1=Dhawan, A2=Kohli, A3=Yuvraj, A4=Raina, A5=Dhoni, A6=Sir Jadeja.
We simulate manually for small K to see pattern.
Initial queue: [0,1,2,3,4,5,6]
Step 1: pop 0 -> play 0. Then push 0,0. Queue: [1,2,3,4,5,6,0,0]
Step 2: pop 1 -> play 1. Push 1,1. Queue: [2,3,4,5,6,0,0,1,1]
Step 3: pop 2 -> play 2. Push 2,2. Queue: [3,4,5,6,0,0,1,1,2,2]
Step 4: pop 3 -> play 3. Push 3,3. Queue: [4,5,6,0,0,1,1,2,2,3,3]
Step 5: pop 4 -> play 4. Push 4,4. Queue: [5,6,0,0,1,1,2,2,3,3,4,4]
Step 6: pop 5 -> play 5. Push 5,5. Queue: [6,0,0,1,1,2,2,3,3,4,4,5,5]
Step 7: pop 6 -> play 6. Push 6,6. Queue: [0,0,1,1,2,2,3,3,4,4,5,5,6,6]
Step 8: pop 0 (first of the two 0's) -> play 0. Push 0,0. Queue: [0,1,1,2,2,3,3,4,4,5,5,6,6,0,0]? Actually careful: After step 7, queue: [0,0,1,1,2,2,3,3,4,4,5,5,6,6]. Then step 8: pop first element: 0. Play 0. Then push two 0's. So queue becomes: remaining [0,1,1,2,2,3,3,4,4,5,5,6,6] + [0,0] => [0,1,1,2,2,3,3,4,4,5,5,6,6,0,0]
Step 9: pop 0 (the first element now) -> play 0. So 9th ball is Rohit, as per sample output: yes sample says for K=9 output Rohit.
So our simulation matches sample.
Now we need to compute for large K efficiently.
Let's denote f(n) = player index for the n-th ball (1-indexed). We have 7 initial players.
This is a classic "pop and push two copies" process. For a general initial queue of size M (here M=7), the process yields a sequence that can be described in terms of binary expansions. In fact, the order is like: the initial elements are processed in order, each generating a binary tree of occurrences. More precisely, if we consider that each initial player (seed) spawns an infinite sequence of occurrences at positions determined by a base-M representation? Actually, let's think.
We can think of the process as a queue that always contains some number of players. At each step, we remove the head, and then add two copies of that player to the tail. So the queue behaves like a FIFO with generation. This is reminiscent of generating the sequence of "breadth-first traversal" of an infinite binary tree where the root is the first element? But note that the initial queue has 7 elements, not a single root. Perhaps we can treat each initial player as the root of a binary tree, and the sequence is the concatenation of BFS traversals of these trees interleaved? Actually, because the queue is initially [0,1,2,3,4,5,6], and the process adds copies after each pop, the resulting sequence is exactly the order of a breadth-first search on a 7-ary (or binary) structure? Let's explore.
Alternatively, we can think in terms of the following recurrence: Let's define a function that maps each occurrence to a "parent". The first 7 occurrences are the original players. Then each occurrence i (i>=1) after the initial ones is a copy of some earlier occurrence. Actually, after a player plays, it spawns two future occurrences. The timing of those future occurrences depends on the queue length.
This is similar to a queue that performs a transformation that is linear: If we track the number of occurrences of each player over time, we can compute the order by simulating in chunks? Since K is up to 1e9, we need O(log K) or O(1) per test.
Observing pattern: The sequence after the initial 7 steps repeats with some doubling? Let's list the first few played indices:
Ball index (1-indexed) and player index:
1:0
2:1
3:2
4:3
5:4
6:5
7:6
8:0
9:0
10:1? Let's compute from simulation after step 9 we have queue after step 9? Actually after step 9, we played 0. Let's continue to see next ones.
After step 8: queue after step 8 we had: [0,1,1,2,2,3,3,4,4,5,5,6,6,0,0]
Step 9: pop first element 0 -> play 0. Then push 0,0. New queue: remove first 0 -> remaining [1,1,2,2,3,3,4,4,5,5,6,6,0,0] then add [0,0] => [1,1,2,2,3,3,4,4,5,5,6,6,0,0,0,0]
Step 10: pop 1 -> play 1. Push 1,1 => queue becomes [1,2,2,3,3,4,4,5,5,6,6,0,0,0,0,1,1]? Actually starting: after step9 queue = [1,1,2,2,3,3,4,4,5,5,6,6,0,0,0,0] (wait check: After step9 we had: [1,1,2,2,3,3,4,4,5,5,6,6,0,0,0,0]. That is 16 elements? Let's count: we had after step9: originally after step8 queue length? Actually step8: after step8 we had 15 elements? Let's recalc carefully.
We start with initial 7. Each step, we remove one element and add two, so net +1 element to queue length. So after n steps (starting from step 0 before any ball?), let's define L_n = length of queue after completing n steps (i.e., after playing ball n and adding copies). Initially before any ball, queue length = 7. After playing ball 1, we remove one and add two, so net +1 => length = 8. In general, after playing ball i, length = 7 + i. So after step 7, length = 7+7 = 14. Indeed after step7 we had queue: [0,0,1,1,2,2,3,3,4,4,5,5,6,6] which is 14 elements. After step8: we remove one and add two, so length = 14+1 = 15. After step9: length = 15+1 = 16. After step10: length = 16+1 = 17, etc. So yes.
Now after step9 queue = [1,1,2,2,3,3,4,4,5,5,6,6,0,0,0,0]. Actually we need to verify the exact order. After step8 we had: [0,1,1,2,2,3,3,4,4,5,5,6,6,0,0] (15 elements). Then step9: pop first 0, add two 0s. So new queue = remaining from after pop: [1,1,2,2,3,3,4,4,5,5,6,6,0,0] (that's 14 elements?) Let's count: after step8 we had 15 elements: let's list them: index0:0,1:1,2:1,3:2,4:2,5:3,6:3,7:4,8:4,9:5,10:5,11:6,12:6,13:0,14:0. So after popping first, we have elements at indices 1..14: that's 14 elements: [1,1,2,2,3,3,4,4,5,5,6,6,0,0]. Then we add two 0s, so total 16 elements: [1,1,2,2,3,3,4,4,5,5,6,6,0,0,0,0]. Yes.
Step10: pop first 1, play 1, then push two 1s. So remaining after pop: [1,2,2,3,3,4,4,5,5,6,6,0,0,0,0] (15 elements) then add [1,1] => [1,2,2,3,3,4,4,5,5,6,6,0,0,0,0,1,1] (17 elements). So ball10: player 1 (Dhawan). Good.
We can continue but pattern may be deduced.
Observation: This process is similar to the "binary carry" where the player for ball K can be determined by representing K-1 in binary? Not exactly.
Alternative viewpoint: The process of a queue where each element, when dequeued, produces two copies, results in the sequence of players being the sequence of "run-length encoding"? Might be related to the concept of "symmetric binary expansion" for the Calkin-Wilf tree? No.
We can model the position of each initial player's occurrences as follows: The first occurrence of player i is at ball i+1 (since initial order). Then each time that player (or a copy) plays, it creates two future occurrences. So the future occurrences are like a binary tree. However, due to interleaving among different players, it's like multiple binary trees interleaved in a round-robin fashion.
Let's think in terms of "generations". Define generation 0 as the original players: they appear at positions 1 to 7. When a player of generation g plays, it creates two copies, which we can consider generation g+1. The order in which these copies will play is determined by the queue order: they are appended to the queue after the current player. So the queue maintains a breadth-first ordering across all trees? Indeed, if we start with generation 0 nodes (seven roots) in order, and each time we process a node, we append its two children to the end, then the order of processing (the ball order) is exactly a breadth-first search (BFS) traversal of a forest of 7 infinite binary trees, where each node (player) has two children of the same player type. Moreover, the forest is processed in the order: first root 0, then root 1, ..., root 6, then the children of root 0 (two nodes), then children of root 1 (two nodes), then children of root 2, etc. This is typical BFS of a forest with fixed order of children.
So the sequence of balls corresponds to BFS order (level order) of these 7 infinite binary trees, where each node is labeled by the root's player index. In BFS of a forest, you first list all roots in order, then all nodes at depth 1 (i.e., children of roots) in order of their parents, then all nodes at depth 2, etc.
Thus, the ball index of the j-th node (in BFS order) can be determined. Since each node has exactly 2 children, the number of nodes at depth d for each root is 2^d. Therefore, total nodes in the forest up to depth D (including depth 0) is 7 * (1 + 2 + 4 + ... + 2^D) = 7 * (2^{D+1} - 1). But careful: BFS order goes depth by depth: first all depth 0 nodes (7 of them), then all depth 1 nodes (72 = 14), then depth 2 (74 = 28), etc.
Thus, the position (ball index) of the first node at depth d is 1 + 7*(2^d - 1)? Actually sum_{i=0}^{d-1} 72^i = 7(2^d - 1). The depth 0 nodes (roots) occupy indices 1..7. Then depth 1 nodes occupy indices 8..21? Let's compute: 7 roots (1-7) + 14 depth1 = 21. So index 8 to 21 are depth1 nodes. depth2: 28 nodes, indices 22 to 49? Check: 7+14=21; 21+28=49. Yes. So index range for depth d (starting at d=0) is [7*(2^d - 1) + 1, 7*(2^{d+1} - 1)].
Now, within a given depth d, the nodes are ordered by the parent order: For each root in original order (0..6), all its descendants at depth d appear in contiguous blocks? Actually BFS: we traverse roots in order, and for each root we process its children left then right? But here each node, when processed, adds two children to the end: first the left child? Actually when we "push two copies" we add two copies of the player to the back. The order of those two copies is both are identical but they are added sequentially: first push first copy, then second copy? The problem says "both the same players will get back in this batting line up". It doesn't specify order between the two copies; but presumably both are added, so they appear one after the other. In the simulation, we added two copies, and they appear consecutively. There's no distinction between left and right; both are identical. So the order of children for a given parent: both children are the same player, so the order among them doesn't matter. However, the relative ordering of children of different parents: Since we process parents in the order they appear, and when we process a parent, we add its two children to the end of the queue. So children of an earlier parent will be added earlier, thus will appear earlier in the queue (i.e., will be played earlier) than children of a later parent. Therefore, the BFS order at depth d: first all children of root 0 (2 nodes), then all children of root 1 (2 nodes), ... then all children of root 6 (2 nodes). Then for depth 2: first children of the first node at depth 1 (which is from root 0's left? Actually the first node at depth 1 is the first child of root 0, then second child of root 0, then first child of root 1, etc. But note: both children of a parent are identical and are added sequentially; the order of these two children among themselves: the first copy added will be played before the second copy. So we can treat them as two positions. Then for depth 2, the order is: first, the children of the first depth1 node (two nodes), then children of the second depth1 node (two nodes), then children of the third depth1 node, etc., in the order that the depth1 nodes were processed. And since the depth1 nodes are processed in BFS order (i.e., the order they appear in the queue, which is exactly the order they were added, which is: first child of root0, second child of root0, first child of root1, second child of root1, ..., first child of root6, second child of root6), so depth2 nodes will be in that parent order.
Thus, the labeling of nodes in the BFS order corresponds to a mapping: each node can be identified by a path from a root: root index r (0..6), and a binary string representing the sequence of child choices (0 for first child, 1 for second child, maybe) of length d (depth). And the BFS order enumerates these nodes in lexicographic order? Actually, BFS of a forest of complete binary trees, where we order children as first child then second child, and we order roots in given order, yields an ordering: first all nodes at depth 0 sorted by root index; then all nodes at depth 1 sorted by root index then by child index; then all nodes at depth 2 sorted by root index, then by child index at depth1, then child index at depth2? But BFS doesn't sort by depth and then by root and then by child index in a nested manner; it processes nodes in order of increasing depth, but within the same depth, the order is the order they are enqueued. That order is: start with roots in order; then when processing root 0, we enqueue its two children (first child, then second child). Then process root 1, enqueue its two children, etc. So after processing all roots, the queue contains: children of root0 (first then second), children of root1 (first then second), ..., children of root6 (first then second). So the order of depth1 nodes is exactly: for root r from 0 to 6, then child index c=0 then 1? Actually careful: we process roots sequentially. After processing root0, we enqueue two children: call them (r=0, child0) and (r=0, child1) in that order. Then we process root1, enqueue (r=1, child0) and (r=1, child1). So the queue becomes: (0,0), (0,1), (1,0), (1,1), ... (6,0), (6,1). So indeed depth1 nodes are sorted by root index, and for each root, by child index (0 then 1). Now, when we process these depth1 nodes, we will enqueue their children (depth2). The order we process depth1 nodes is the order they are in the queue: (0,0), (0,1), (1,0), (1,1), ..., (6,0), (6,1). For each such node, we enqueue its two children (which are depth2). So after processing all depth1 nodes, the queue will contain: children of (0,0) (two), children of (0,1) (two), children of (1,0) (two), ... children of (6,1) (two). Thus depth2 nodes order is: (0,0,0), (0,0,1), (0,1,0), (0,1,1), (1,0,0), (1,0,1), (1,1,0), (1,1,1), ..., (6,1,0), (6,1,1). This is exactly lexicographic order of (root index, then binary string of length d) where binary string is interpreted with most significant bit being the first child choice? Actually the order is: for each root r, iterate over all binary strings of length d in standard order (0...0,0...1,...,1...1). But is that true for d=2? For root 0: binary strings: 00,01,10,11? But above we got for root0: (0,0,0),(0,0,1),(0,1,0),(0,1,1) which corresponds to binary strings: (0,0) -> children: from (0,0) yields two: (0,0,0) and (0,0,1); then (0,1) yields (0,1,0),(0,1,1). So indeed for a fixed root, the order of nodes at depth d is exactly the lexicographic order of the binary string of length d, where bits are in order from most significant (depth1 child index) to least (depth d child index). This is because BFS processes nodes level by level, but within a level, it's effectively performing a breadth-first traversal of each tree, which results in ordering by root, then by binary strings in increasing order (like binary numbers). However, note that BFS across the forest interleaves different roots? Actually, it does not interleave across roots at the same depth? It does: all depth d nodes from root0 appear before those from root1? Yes, because at depth1 we had all children of root0 first, then root1, etc. At depth2, we have all grandchildren of root0 first, then root1, etc. So indeed, the ordering across roots is: first all nodes of root0 at that depth, then all of root1, etc. So the overall order is: for each root r in increasing order, for each binary string s (length d) in lexicographic order (0...0 to 1...1), the node (r, s) appears.
Thus, the ball index for node (r, s) where s is a binary string of length d (depth) can be computed as:
First, count how many nodes appear before this node in the BFS order.
BFS order enumeration:
-
All nodes of depth < d: total = 7*(2^d - 1). Because sum_{i=0}^{d-1} 72^i = 7(2^d - 1). These occupy indices 1 through 7*(2^d - 1).
-
Then within depth d, nodes are arranged first by root index r from 0 to 6, and within each root, by binary strings of length d in increasing order.
Number of nodes at depth d before root r: for roots with index less than r, each contributes 2^d nodes. So that's r * 2^d nodes.
Then within root r, before the particular binary string s, we need to count how many binary strings of length d are less than s in lexicographic order. That's essentially the integer value of s (interpreted as binary number with bits from most significant to least) . Since s is a d-bit binary number (with bits b1 b2 ... bd, where b1 is the child index at depth1, etc.) The number of strings less than s is exactly the integer value of s (treating s as a d-bit number with leading zeros allowed). Because lexicographic order of all 2^d binary strings from 0 to 2^d-1 (with d bits, including leading zeros) corresponds to the numeric order. So count = value(s).
Thus, overall, the index (1-indexed) of the ball for node (r, s) is:
K = 7*(2^d - 1) + r * 2^d + value(s) + 1
Because after the previous nodes (depth<d) count = 7*(2^d - 1) nodes, plus the ones before this node within depth d: r * 2^d + value(s). Then add 1 to get the next index.
So K = 7*(2^d - 1) + r * 2^d + v + 1, where v = integer in [0, 2^d - 1] representing the binary string.
Simplify: K = 7*(2^d - 1) + r * 2^d + v + 1 = 72^d - 7 + r2^d + v + 1 = (7 + r) * 2^d + v - 6.
Wait: 72^d - 7 + r2^d + v + 1 = (7+r)*2^d + v - 6. Let's check: -7+1 = -6, yes. So K = (7+r)*2^d + v - 6.
But is that correct? Let's test with small known values.
For root r=0, depth d=0 (i.e., s is empty string, v=0?). For depth 0, we treat 2^0=1, value(s)=0. Then formula gives: K = 7*(1 - 1) + 01 + 0 + 1 = 1. Good. For root r=1 depth0: K=70 + 1*1 +0+1 =2. Works.
Now test for depth 1: e.g., r=0, depth1, first child (v=0? Actually for depth1, s is a 1-bit string; child0 corresponds to v=0, child1 v=1). For r=0, v=0 (first child), d=1: K = 7*(2-1) + 02 + 0 +1 = 71 + 0 + 1 = 8. According to simulation, ball8 was Rohit (0) indeed. For r=0, v=1 (second child): K = 7 + 0 + 1 +1? Actually 7*(2-1)=7, plus r*2^d=0, plus v=1, plus 1 = 9. Ball9 was Rohit (0). Good.
For r=1, v=0: K = 7 + (12)=2? Wait compute: 7(2-1)=7, + r2^d=12=2, + v=0 +1 = 10. Indeed ball10 is Dhawan (1). Good.
For r=1, v=1: K = 7 + 2 + 1 +1 = 11? Actually 7 + 2 + 1 +1 = 11. Ball11 should be Dhawan? Let's see simulation: after ball10, ball11? We haven't simulated but we expect 11: from our earlier queue after step10, the next ball is? After step10 we had queue after pop and push. But we can trust formula.
For r=6, depth1, v=1: K = 7 + 62=12? Actually compute: 7 + 62=12, + v=1 +1 = 14? That gives 7 + 12 + 1 +1? Wait recalc: 7*(2-1)=7; r2^d = 62=12; v=1; +1 => total = 7+12+1+1=21. So ball21 should be Sir Jadeja? Let's check: depth1 total nodes =14, so indices 8-21. The last index 21 should be second child of root6. That seems plausible.
Now for depth 2: test r=0, v=0 (binary 00). d=2: K = 7*(4-1)=73=21, + r4=0, + v=0, +1 = 22. So ball22 should be Rohit. That matches earlier: depth2 starts at index 22. Good.
So formula appears correct.
Thus, given K, we need to find d, r, v such that:
K = 7*(2^d - 1) + r * 2^d + v + 1, where 0 <= r <= 6, 0 <= v <= 2^d - 1, and d >= 0 integer.
Alternatively, we can think: Let N = K - 1 (zero-indexed). Then:
N = 7*(2^d - 1) + r * 2^d + v.
So N ∈ [0, ∞). Let's denote base = 7*(2^d - 1). For each d, the range of N for depth d is from base to base + 72^d - 1. Because r from 0..6, v from 0..2^d-1 gives total 72^d nodes at depth d. So N ∈ [7*(2^d - 1), 7*(2^{d+1} - 1) - 1]? Actually max N at depth d = 7*(2^d - 1) + (6 * 2^d + (2^d - 1)) = 7*(2^d - 1) + 72^d - 1 = 7(2^{d+1} - 1) - 1. So indeed depth d nodes occupy zero-indexed N in [7*(2^d - 1), 7*(2^{d+1} - 1) - 1].
Thus, we can find d by determining which range K lies in. Since d can be up to about log2(K/7) maybe up to 30 (since 2^30 ~ 1e9), so we can compute iteratively or by using integer arithmetic.
Given K (1-indexed), compute N = K - 1.
We need to find d such that 7*(2^d - 1) <= N < 7*(2^{d+1} - 1). This is equivalent to:
(2^d - 1) <= N/7 < (2^{d+1} - 1). Not exactly because integer division. But we can find d by doubling.
Simplify: Let A = N + 7. Then? Let's try to transform.
N = 7*(2^d - 1) + offset, where offset = r * 2^d + v, with 0 <= offset < 7*2^d.
So N + 7 = 72^d + offset. So N + 7 = 72^d + (r*2^d + v) = (7 + r)*2^d + v.
Thus, K = N+1, so K + 6 = N + 7 = (7+r)*2^d + v.
Since 0 <= v < 2^d, we see that v is the remainder when (K+6) is divided by 2^d? Actually, (K+6) = (7+r)*2^d + v. But (7+r) ranges from 7 to 13. So (K+6) div 2^d = 7+r, and remainder = v.
But we also have constraint 0 <= r <= 6, so 7 <= (K+6) // 2^d <= 13.
Alternatively, from N + 7 = (7+r)*2^d + v, with 0 <= v < 2^d.
Thus, if we can find d such that when we divide (K+6) by 2^d, the quotient lies between 7 and 13 inclusive. And then r = quotient - 7, v = remainder.
But careful: (K+6) = K + 6. Since K up to 1e9, K+6 up to ~1e9, so d up to 30 (2^30 ~ 1e9). So we can iterate d from 0 upwards until we find such d. However, T up to 1000, log2(1e9) ~ 30, so iterating d up to 30 per test case is fine. But we need to be careful about overflow? Not an issue in 64-bit. So we can simply:
For a given K:
N = K - 1
Find d: start d=0, while True:
low = 7 * ((1 << d) - 1)
high = 7 * ((1 << (d+1)) - 1) - 1 // N upper bound inclusive? Actually N < high+1, so we can check if N < 7*((1<<(d+1))-1). Equivalent: while N >= 7*((1<<(d+1))-1) then d++.
Alternatively compute d = floor(log2((N+7)/7))? Let's derive formula.
Since N+7 = 72^d + offset, and offset < 72^d, we have (N+7) / (72^d) between 1 and 2? Not exactly. Let's compute bounds: If N is in depth d, then 7(2^d - 1) <= N < 7*(2^{d+1} - 1). Adding 7: 72^d <= N+7 < 72^{d+1}. So (N+7) / (7) is in [2^d, 2^{d+1}). So d = floor(log2((N+7)/7)). But careful: (N+7)/7 may not be integer. But floor(log2(...)) works. However, we must ensure that when (N+7) is exactly 72^d, it gives d, but if it's exactly 72^{d+1}, then it's at the upper bound, but N+7 < 7*2^{d+1}, so it's okay. So we can compute d = floor(log2( (N+7) / 7 )). But due to integer division, we can avoid floating point. We can compute by dividing by powers of two.
Since T is 1000 and K up to 1e9, we can just precompute powers of two and iterate d from 0 to maybe 30. Simpler: We can find d by starting d=0, while 7*((1<<(d+1))-1) <= N: d+=1. That is, while the upper bound of depth d (i.e., max N for depth d) is less than N, move to next depth. Actually, we want the smallest d such that N < 7*((1<<(d+1))-1)? Wait, depth d nodes satisfy N < 7*(2^{d+1} - 1). For d=0: 7*(2^1 -1)=7, so N < 7 => N max 6, which are indices 0..6 (balls 1..7). If N >= 7, then we go to d=1: bound is 7*(2^2-1)=73=21, N < 21? If N between 7 and 20 inclusive, then d=1. If N>=21, then d=2: bound 7(2^3-1)=7*7=49, etc. So we can compute:
d = 0
while N >= 7*((1<<(d+1))-1):
d += 1
But careful: For N=7, condition: 7 >= 7*((1<<(1+1))-1)=73=21? No 7<21, so loop stops and d=0? But N=7 corresponds to ball index 8? Actually N=7 is ball 8, which is depth 1. Wait: For N=7, is it depth 1? Let's check: depth 1 range N: from 7(2^1 -1)=7 to 7*(2^2-1)-1 = 73-1 = 20. So yes, N=7 is depth 1. But our condition N >= 7((1<<(d+1))-1) with d=0: 7*((1<<1)-1) = 7*(2-1)=7. So N >= 7, so loop condition true, we increment to d=1. Then with d=1, check condition N >= 7*((1<<2)-1)=7*(4-1)=21? 7>=21 false, so stop. d=1, correct. So the condition should be while N >= 7*((1<<(d+1))-1) (i.e., while N is at least the lower bound of next depth? Actually for d=0, the threshold for moving to d=1 is if N >= 7*(2^1 -1) = 7. So indeed the condition uses (d+1) because we want to check if N is not less than the lower bound of depth d+1? Wait, we want to find d such that 7*(2^d -1) <= N < 7*(2^{d+1} -1). So if we start d=0, check if N < 7*(2^{d+1} -1) = 7*(2^1 -1)=7. If N >=7, then d is not correct, we need to increase d. So while N >= 7*(2^{d+1} -1) actually increments d when N is at least the upper bound of current d? Let's test: d=0, bound upper = 7*(2^1 -1)=7. If N >=7, then N is not less than upper bound, so we need to go to d=1. So condition while N >= 7*(2^{d+1} -1) works: for d=0, if N>=7, we go to d=1. For d=1, check if N >= 7*(2^{2} -1)=21, if so go to d=2, else stop. So yes, loop while N >= 7*((1<<(d+1))-1) increments d until the condition fails, meaning N < the next depth's lower bound? Actually careful: For d=1, upper bound is 7*(2^2 -1)=21. If N=20, condition N>=21 false, stop; d=1 correct. If N=21, then N is exactly 21, which is the lower bound of depth 2? Because depth 2 starts at N=21 (since 7*(2^2 -1)=21). Indeed, for N=21, that's the first ball of depth 2 (ball 22). So we want d=2 for N=21. Condition: while N >= 7*((1<<(d+1))-1). For d=1, we check N>=21, N=21 >=21 true, so we increment to d=2. Then for d=2, check N>= 7*((1<<3)-1)=7*7=49? 21>=49 false, stop, d=2 correct. So it works.
Thus we can compute d by that loop.
After we have d, compute offset = N - 7*((1<<d)-1). This offset lies in [0, 7*2^d - 1].
Then r = offset // (2^d) (integer division), and v = offset % (2^d). The player index is r (since root index r). But careful: root indices are 0..6, mapping to names. So answer is names[r].
Thus solution is straightforward.
But wait: Is there any nuance about Sir Jadeja being the one who does the copying? That doesn't affect the process; it's just a story. The process is as described.
Thus for each test case:
- Read K.
- N = K-1.
- Find d = 0; while N >= 7*((1 << (d+1)) - 1): d += 1.
- offset = N - 7*((1 << d) - 1).
- block_size = 1 << d.
- r = offset // block_size.
- player_index = r (0-indexed).
- Output corresponding name.
We need to ensure we handle up to K=1e9. d max: we need to compute 7*((1<<(d+1))-1) up to > 1e9. (1<<30) = 1073741824 ≈1e9, times 7 ~ 7.5e9 > 1e9, so d up to maybe 29 or 30. Actually we need to ensure loop doesn't overflow 64-bit, but 7*((1<<31)-1) might be ~ 7*2^31 ~ 14e9, still within 64-bit (which can hold up to 9e18). So safe using 64-bit (Python int is arbitrary precision anyway). So okay.
Let's test with sample: K=3 -> N=2. d=0: while N >= 7*((1<<1)-1)=7? 2>=7 false, so d=0. offset = 2 - 7*((1<<0)-1)=2-0=2. block_size=1. r=2//1=2 => player 2 -> Kohli. correct.
K=9 -> N=8. d=0: while N>=7? 8>=7 true => d=1. d=1: check while N>=7*((1<<2)-1)=21? 8>=21 false. So d=1. offset = 8 - 7*((1<<1)-1) = 8 - 7*(2-1)=8-7=1. block_size=2. r=1//2=0? Actually integer division: 1//2 = 0. So r=0 -> Rohit. correct. v = 1%2 = 1, which indicates second child.
Thus works.
Edge cases: K=1 => N=0, d=0 (0>=7? false). offset=0, r=0 -> Rohit. K=7 => N=6, d=0, offset=6, r=6 -> Sir Jadeja. correct.
Now, what about K beyond 7? All good.
But is there any off-by-one for d when using shift? Since Python's ints are fine.
Now check K=8: N=7, d=0: 7>=7 true -> d=1. d=1: while 7>=21 false. offset = 7 - 7*(2-1)=7-7=0, r=0//2=0 -> Rohit. correct.
Now we need to implement for T up to 1000. Very easy.
However, there is a nuance: The batting lineup initially is exactly as given, and the process described: "Now they are playing one ball at a time and once they play a ball, Jadeja will make a new copy of that player (including himself) and both the same players will get back in this batting line up." It says "both the same players will get back in this batting line up." That implies the original player and the new copy are both added to the lineup. Our queue model (pop first, then push two copies) matches that: the original player is added back (as one of the two copies) or does the original player stay? Let's parse: "once they play a ball, Jadeja will make a new copy of that player (including himself) and both the same players will get back in this batting line up." The phrase "both the same players" suggests that after playing, we have the original player and the newly created copy, and both get into the lineup. So the original player is not removed permanently; they go back to the lineup along with the copy. So our model is correct: the player who just played remains (as a copy? Actually, we pop him from the front, but then we push two copies. That effectively means the player returns to the back, but we push two copies, not one original plus one copy? It's ambiguous: If we pop the player and then push two copies, we are effectively replacing the original with two copies. But is the original considered to be one of those copies? In the queue, the original is removed and two copies are added. That means the player is duplicated, but the original is not preserved as a distinct entity; however, both copies are identical, so it's as if the player continues to exist in two forms. But the description says "both the same players will get back". That could be interpreted as the original player and the new copy are both added back (so the original remains, and a copy is added). In that case, after playing, the original should stay (not be removed) and also a copy is added. But the batting lineup is a queue: they take turns. When a player plays a ball, they are at the front of the lineup, and after playing, they go to the back? Typically in cricket batting order, once a player gets out, they don't bat again, but here it's a magic copy process: they play a ball, then Jadeja creates a copy, and both the same players (i.e., the original and the copy) will get back in the batting lineup. This implies that after playing, the original player goes back to the lineup (maybe to the end), and the copy is also added to the lineup. So the original is not removed; it's simply moved from the front to the back, and an extra copy is added. So the net effect is that the player who just played is placed at the back, and an additional copy of him is also placed at the back. So the queue operation would be: remove the head (to play), then after the ball, that player is still alive and returns, so we need to add two instances: the original player (which we just removed) plus a new copy. But if we removed him from the front to play, then we need to add two copies. So effectively it's the same as: pop front, then push two copies. The original is one of the two copies. So indeed our model matches: we popped the player (original), and then we push two copies of that player (one representing the original that returns, and the other the new copy). So it's consistent.
Thus the queue model stands.
Now, verify that our BFS interpretation correctly models the queue order. Yes.
Thus algorithm: For each K:
- set N = K - 1.
- d = 0
- while N >= 7 * ((1 << (d+1)) - 1):
d += 1
- offset = N - 7 * ((1 << d) - 1)
- block = 1 << d
- r = offset // block
- output names[r]
That is O(log K) per test, which is fine.
But can we make it O(1) by using log2 and division? Possibly, but not necessary.
One caveat: For very large K, the while loop might loop up to 30 iterations, which is trivial.
We need to be careful with Python's shift: (1 << (d+1)) may become huge if d large, but d is small.
Edge case: K = 10^9, let's compute d maximum. Compute 7 * (2^{d+1} - 1) > 1e9. Solve 2^{d+1} > (1e9/7) + 1 ≈ 142857143 +1 = 142857144, so 2^{d+1} > ~1.428e8 => d+1 >= 28 because 2^28 = 268435456 > 1.428e8, so d+1=28 => d=27. Actually 2^27=134217728, 2^28=268435456. (1e9/7) ≈ 142857143, so 2^27=134M < 142M, so need 2^{d+1} > 2142M? Wait condition N < 7(2^{d+1}-1). For K=1e9, N=999999999. We need d such that 7*(2^d -1) <= N < 7*(2^{d+1} -1). For d=27: 2^27=134217728, compute lower bound: 7*(134217728-1)=939524096-7? Actually 1342177287 = 939524096, minus 7 = 939524089? Let's compute: 7(2^27 - 1) = 7134217727 = 939524089. Upper bound: 7(2^28 - 1) = 7268435455 = 1879048185? Actually 2684354557 = 1879048185, which is > 1e9. So for d=27, N=999,999,999 is > 939,524,089 and < 1,879,048,185, so d=27. So maximum d is 27 for K up to 1e9. So loop up to 27.
Thus fine.
Now implement.
But let's double-check with a few more values manually.
K=14? Let's simulate quickly: after the initial 7, we played balls 8-21 for depth1. So ball14 is within depth1. Let's compute: N=13, d: while N >= 7*((1<<(d+1))-1). d=0: 13>=7 true -> d=1; d=1: check 13>=21 false, so d=1. offset =13 - 7*(2-1)=13-7=6. block=2, r=6//2=3 -> player 3 (Yuvraj). Let's see if that matches. Depth1 order: [0,0,1,1,2,2,3,3,4,4,5,5,6,6] for balls 8-21. So ball8=0,9=0,10=1,11=1,12=2,13=2,14=3,15=3,... So ball14 is 3 (Yuvraj). Yes.
K=22: N=21, d: d=0: 21>=7 true -> d=1; d=1: 21>=21 true -> d=2; d=2: 21>=? 7*((1<<3)-1)=77=49, false. So d=2. offset = 21 - 7(4-1)=21 - 7*3=21-21=0. block=4, r=0//4=0 -> player 0 (Rohit). ball22 should be Rohit, as earlier.
K=49? Actually 49 is the last of depth2? depth2 nodes: indices 22 to 49? Wait depth2: from N=21 to 48? Because 7*(2^3-1)-1 = 77-1=48. So K=49 corresponds to N=48? Let's check: depth2 total nodes =28, so indices 22-49 inclusive? Actually if N start at 21 for first depth2, then last N = 21+28-1 = 48, so K = 49. So ball49 is last of depth2. Let's compute: N=48, d: while? d=0: 48>=7 true->d=1; d=1: 48>=21 true->d=2; d=2: 48>=49? 48>=49 false, so d=2. offset = 48 - 7(4-1)=48-21=27. block=4, r=27//4=6 (since 64=24), remainder 3. So r=6 (Sir Jadeja). So ball49 is Sir Jadeja. That matches expectation: depth2 nodes are grouped by root; for root6, the last block of 4 nodes would be indices? root6 offset range: from 64=24 to 24+3=27 (offset). So offset=27 corresponds to root6, v=3 (binary 11), which is last node. So ball49 is Sir Jadeja.
All consistent.
Thus solution is simple.
We should also consider if there is any twist: Could the process start with Jadeja making copies after each ball, but maybe Jadeja himself is not copied when he plays? The problem says "including himself", so yes he is also copied. So consistent.
One more nuance: The batting lineup is fixed as given, but after each ball, the player who just played is copied, and both return. Our model says we remove the player from the front and add two copies to the back. But what about the case when the player is Jadeja, does he make a copy of himself? Yes, as per statement, he makes a new copy of that player (including himself). So it's same.
Thus, answer stands.
Now, we need to output the name as string. Mapping: index 0: "Rohit", 1: "Dhawan", 2: "Kohli", 3: "Yuvraj", 4: "Raina", 5: "Dhoni", 6: "Sir Jadeja". Exactly as listed.
Double-check order: The list: [ "Rohit", "Dhawan", "Kohli", "Yuvraj", "Raina" , "Dhoni", "Sir Jadeja"] So index 0 to 6.
Now, we must consider if K can be as large as 10^9, and T up to 1000, the while loop per test case is okay.
But we could also precompute the thresholds for d up to, say, 30, and then binary search. But simple while is fine.
Potential pitfalls: In Python, we must be careful with integer shifts: 1 << d for d up to 60 is fine. But we only go up to ~30.
One more check: For K=1, our while condition: N=0, check 0>=7? false, d=0. offset=0 - 0=0, r=0, output Rohit.
Now, test K=100: Compute manually? Let's just trust.
Thus, final solution is:
def solve():
import sys
data = sys.stdin.read().split()
if not data: return
it = iter(data)
T = int(next(it))
names = ["Rohit", "Dhawan", "Kohli", "Yuvraj", "Raina", "Dhoni", "Sir Jadeja"]
out_lines = []
for _ in range(T):
K = int(next(it))
N = K - 1
d = 0
# while N >= 7 * ((1 << (d+1)) - 1):
# might be faster to compute bound incrementally
# Let's compute bound = 7 * ((1 << (d+1)) - 1)
while N >= 7 * ((1 << (d+1)) - 1):
d += 1
offset = N - 7 * ((1 << d) - 1)
block = 1 << d
r = offset // block
out_lines.append(names[r])
sys.stdout.write("\n".join(out_lines))
But wait: Need to ensure that for large d, the condition 7 * ((1 << (d+1)) - 1) does not overflow? In Python, it's arbitrary precision, fine.
But is it possible that for very large K (like 10^9) the loop will iterate many times? Actually we start d=0 and increment until condition fails. For K=1e9, d will go to 27. That's 27 iterations, which is fine for 1000 tests (27k operations). So okay.
Alternatively, we can compute d directly via integer log2. For potential micro-optimization, we can precompute a list of thresholds: thresholds[d] = 7 * ((1 << (d+1)) - 1) which is the lower bound for depth d+1 (or the value to compare). Then we can do binary search or linear scan. But linear scan is simple.
Given the constraints, it's okay.
But we must be cautious: The while loop condition uses d+1; at d=0, (1<<1)-1=1, times 7 =7. So for N=7, condition true, d becomes 1. Then at d=1, (1<<2)-1=3, times 7=21. So for N=7, condition 7>=21 false, loop stops. So d=1, correct. So condition is correct.
Now, confirm with N=21: d=0: 21>=7 true -> d=1; d=1: 21>=21 true -> d=2; d=2: 21>=49 false -> d=2, correct.
Thus algorithm is correct.
However, there is also a direct formula: The player index can be found by writing K+6 in binary? Let's explore for fun. From K+6 = (7+r)*2^d + v. Since 7+r is between 7 and 13, which in binary is 3 to 4 bits. Could maybe derive using bit manipulation but not needed.
Now, one more important nuance: The initial lineup includes "Sir Jadeja". The problem statement might have "Sir Jadeja" as the last element. In sample output they print "Sir Jadeja"? Actually sample output only shows Rohit and Kohli. But we need to output exactly as listed: "Sir Jadeja". So careful about spaces: "Sir Jadeja" includes a space. The list given: [ "Rohit", "Dhawan", "Kohli", "Yuvraj", "Raina" , "Dhoni", "Sir Jadeja"]. So yes "Sir Jadeja" with a space.
Thus final answer.
But I'd like to double-check the problem statement: "Team India is playing too much cricket in this season. Team players are getting better in their performance with match by match. Sir Jadeja has become a Trump Card for M.S Dhoni. There is a hidden reason of his special position in the team. He can make new copies of a person by some magic. Due to this, players can get many chances to bat. Now there is a fixed batting line up of top 7 batsman decided by Dhoni as : [ "Rohit", "Dhawan", "Kohli", "Yuvraj", "Raina" , "Dhoni", "Sir Jadeja"]. Now they are playing one ball at a time and once they play a ball, Jadeja will make a new copy of that player (including himself) and both the same players will get back in this batting line up. In this way they will get a chance to play a ball turn by turn. Your task is very simple. You are required to output the name of a player as given in the above list who will play a K^th ball."
Thus, reading confirms.
Now let's also consider if the order of addition might be the two copies added after the player has played, but maybe the original player does not go to the back? Actually "both the same players will get back in this batting line up" suggests after playing, the original and the copy are placed back into the lineup. If the original is already at the front and just played, they'd go back to the lineup (likely to the end), and the copy also goes to the end. So our model matches.
Thus final solution as above.
But before finalizing, test with some random values by simulating small K to verify formula.
We'll write a quick simulation for K up to maybe 50 and compare.
I'll do mental simulation already for some.
But just to be thorough, let's compute a few more:
K=1: Rohit
K=2: Dhawan
K=3: Kohli
K=4: Yuvraj
K=5: Raina
K=6: Dhoni
K=7: Sir Jadeja
K=8: Rohit
K=9: Rohit
K=10: Dhawan
K=11: Dhawan
K=12: Kohli
K=13: Kohli
K=14: Yuvraj
K=15: Yuvraj
K=16: Raina
K=17: Raina
K=18: Dhoni
K=19: Dhoni
K=20: Sir Jadeja
K=21: Sir Jadeja
K=22: Rohit
K=23: Rohit? Wait depth2: first block of 4 for root0: indices 22,23,24,25? Actually with d=2, block=4. For root0, offset from 0 to 3. So offset 0 -> K=22, offset 1 -> K=23, offset 2 -> K=24, offset 3 -> K=25. So K=22: Rohit (v=0), K=23: Rohit? Actually v=1? No offset 1 gives v=1, but still r=0, so Rohit. So indeed both are Rohit. Let's confirm with formula: For K=23, N=22, d=2, offset=22-21=1, r=0, v=1, so Rohit. So K=22,23,24,25 are all Rohit? Wait r=0 gives Rohit, yes. But earlier depth1 had two Rohit, depth2 has four Rohit? That makes sense: each Rohit at depth1 generated two copies, so total Rohit at depth2 = 2*2=4. So yes, Rohit appears 4 times in depth2. So consistent.
Now K=26: offset=5? Actually 26 => N=25, offset=25-21=4? Wait compute: N=25, offset=4? 25-21=4, block=4, r=4//4=1, remainder 0, so player1 (Dhawan). So K=26 is Dhawan. So order: after Rohit block (4), next block of 4 for Dhawan: indices 26-29? Actually offset range for root1: from 4 to 7. So K=26 (offset 4), 27 (offset5), 28 (offset6), 29 (offset7) all Dhawan? Yes. So good.
Thus formula works.
Now edge: K=50? Actually depth2 ends at K=49. K=50 is start of depth3? Let's compute: depth3 lower bound: 7*(2^3-1)=77=49? Wait 2^3=8, 8-1=7, times 7 =49. So N start =49? Actually 7(2^3-1)=49. That is lower bound N for depth3. So ball index = N+1 = 50. So K=50 is first ball of depth3. Then d=3. offset=0, block=8, r=0, so Rohit. So ball50 Rohit.
Thus fine.
Thus solution is ready.
But is there any chance that K is 0? No, K >= 1.
Now, implement.
We can precompute d threshold array to avoid repeated shifting? Not necessary.
But careful: In the while loop, the expression 7 * ((1 << (d+1)) - 1) involves shift of (d+1). Starting with d=0, compute 1<<1=2, minus1=1, times7=7. So loop runs while N >= 7. That will increase d until N < 7*(2^{d+1}-1). This is O(d). For large d (like 27) it's fine.
Alternatively, we could compute d = floor(log2((N+7)//7))? Let's see if we can compute directly: Since N+7 is between 72^d and 72^{d+1}-1? Actually from earlier: 72^d <= N+7 < 72^{d+1}. So dividing by 7: 2^d <= (N+7)/7 < 2^{d+1}. So d = floor(log2((N+7)//? But (N+7)/7 may not be integer. We can compute d = floor(log2((N+7) // 7))? Actually if we take integer division (N+7)//7, this integer is between? Let's test: N+7 = 72^d + offset, offset < 72^d. So (N+7)//7 = floor((72^d + offset)/7) = 2^d + floor(offset/7). But offset can be up to 72^d - 1, so floor(offset/7) can be up to 2^d - 1. So (N+7)//7 can be as high as 2^d + (2^d - 1) = 2^{d+1} - 1. So it may be as high as almost 2^{d+1}. Thus floor(log2((N+7)//7)) could be d or d+1. For example, N=21 (depth2 lower bound). N+7=28, (N+7)//7=4. log2(4)=2, which gives d=2, correct. For N=20 (depth1 last), N+7=27, //7=3, log2(3)=1, gives d=1, correct. For N=48 (depth2 last), N+7=55, //7=7, log2(7)=2, gives d=2, correct. For N=49 (depth3 first), N+7=56, //7=8, log2(8)=3, gives d=3, correct. So floor(log2((N+7)//7)) appears to give d correctly, because when (N+7)//7 equals 2^{d+1} exactly? That would be when N+7 = 72^{d+1}. But N+7 < 72^{d+1} (strict), so (N+7)//7 <= 2^{d+1} - 1, except if N+7 is exactly multiple? Actually if N+7 = 72^{d+1} - something, the integer division might be 2^{d+1} - 1 if the remainder is enough? But the maximum (N+7) = 72^{d+1} - 1, then (N+7)//7 = floor((72^{d+1} - 1)/7) = 2^{d+1} - 1 (since 72^{d+1} /7 = 2^{d+1}, minus 1/7). So the integer division yields 2^{d+1} - 1. So floor(log2(2^{d+1} - 1)) = d (since 2^{d+1} - 1 is less than 2^{d+1} but greater than or equal to 2^d for d>=0? Actually for d=0: 2^{1}-1=1, log2(1)=0, correct. For d=1: 2^2-1=3, log2(3)=1, correct. So floor(log2((N+7)//7)) will give d exactly. But is there any case where (N+7)//7 could be less than 2^d? No, because N+7 >= 7*2^d, so (N+7)//7 >= 2^d. So floor(log2(...)) yields at least d. But it could yield d+1 if (N+7)//7 >= 2^{d+1}? Since maximum is 2^{d+1}-1, floor(log2(2^{d+1}-1)) = d. So it never yields d+1. So indeed d = floor(log2((N+7)//7)). That gives a direct formula. Let's test edge: N=0 (K=1): N+7=7, //7=1, log2(1)=0 => d=0. Good. N=6 (K=7): N+7=13, //7=1, log2(1)=0 => d=0. But wait: N=6 is depth0? Actually depth0 includes N=0..6, so d should be 0. So correct. N=7: N+7=14, //7=2, log2(2)=1 => d=1. Good. So we can compute d using bit_length: (N+7)//7 is an integer X. Then d = X.bit_length() - 1. Because bit_length gives number of bits needed to represent X, i.e., floor(log2(X)) + 1. So d = X.bit_length() - 1.
Let's test: X=1 => bit_length=1 => d=0. X=2 => bit_length=2 => d=1. X=3 => bit_length=2 => d=1. X=4 => bit_length=3 => d=2. X=7 => bit_length=3 => d=2. X=8 => bit_length=4 => d=3. Works.
Thus we can compute:
X = (N + 7) // 7
d = X.bit_length() - 1
But careful: For N=20, N+7=27, //7=3, bit_length=2 => d=1 correct. N=21, N+7=28, //7=4, bit_length=3 => d=2 correct. N=48, N+7=55, //7=7, bit_length=3 => d=2 correct. N=49, N+7=56, //7=8, bit_length=4 => d=3 correct.
So this gives d directly without loop. Since Python's bit_length is efficient, we can use this.
But we must ensure that (N+7)//7 correctly maps to X. For N=0, X=1 -> d=0. Good.
Thus we can compute:
N = K - 1
X = (N + 7) // 7
d = X.bit_length() - 1
But wait, what about N=6? X = (6+7)//7 = 13//7 = 1, d=0. Good. N=7? X=14//7=2, d=1. Good. So it's consistent.
Now, after computing d, we need to compute offset = N - 7 * ((1 << d) - 1). But note that using the above d derived from X might be off if X is exactly 2^d? Actually X is between 2^d and 2^{d+1} - 1 inclusive. So d from bit_length minus 1 yields the floor(log2(X)), which is exactly d. Because if X is between 2^d and 2^{d+1}-1, then floor(log2(X)) = d. So it's fine.
Thus offset = N - 7*(2**d - 1). Then block = 1 << d. r = offset // block.
But we must confirm that this d is indeed the correct depth. For N in depth d, we have 7*(2^d - 1) <= N < 7*(2^{d+1} - 1). Our X = (N+7)//7. Let's check: Since N+7 is in [72^d, 72^{d+1}) (maybe inclusive/exclusive). Because N >= 7*(2^d - 1) => N+7 >= 72^d. And N < 7(2^{d+1} - 1) => N+7 < 72^{d+1}. So indeed (N+7)//7 is in [2^d, 2^{d+1} - 1] because when N+7 is exactly 72^d, division gives 2^d; when N+7 is 7*2^{d+1} - 1, division gives 2^{d+1} - 1. So X is indeed in [2^d, 2^{d+1} - 1], inclusive. So d = floor(log2(X)). Good.
Thus we have an O(1) per test solution using bit_length.
But caution: For very large K (e.g., 10^9), X = (K-1+7)//7 = (K+6)//7. This is about up to 1.43e8, bit_length ~ 28, fine.
So we can implement.
Now let's test some borderline: K=7*(2^{d+1} - 1) which is the first ball of depth d+1? Actually that ball index is 7*(2^{d+1} - 1) + 1? Let's compute K = 7*(2^{d+1} - 1) + 1? Wait we need to be careful: For depth d, the last ball index is 7*(2^{d+1} - 1). Because N_max = 7*(2^{d+1} - 1) - 1, so K_max = N_max+1 = 7*(2^{d+1} - 1). Then the next ball (first of depth d+1) is K = 7*(2^{d+1} - 1) + 1. So for K = 7*(2^{d+1} - 1) + 1, N = 7*(2^{d+1} - 1). Then X = (N+7)//7 = (7*(2^{d+1} - 1) + 7)//7 = (72^{d+1})//7 = 2^{d+1}. Then X = 2^{d+1}, its bit_length = d+2? Actually 2^{d+1} in binary is 1 followed by d+1 zeros. Its bit_length is d+2? Wait: 2^{n} in Python is represented as integer with bit_length = n+1? Let's test: 2^0 = 1, bit_length=1; 2^1 = 2, bit_length=2; 2^2 = 4, bit_length=3; so indeed bit_length of 2^m is m+1. So for X = 2^{d+1}, bit_length = (d+1)+1 = d+2. Then d_calc = bit_length - 1 = d+1. So we get d = d+1, which is correct because for the first ball of depth d+1, we want d+1. So our formula yields correct d for that boundary case as well. Check: K = 7(2^{d+1} - 1) + 1. For d=0: K = 7*(2-1)+1=7+1=8, which is first of depth1. Then N=7, X=(7+7)//7=14//7=2, bit_length=2, d=1. Good. For d=1: K = 7*(4-1)+1=7*3+1=22, first of depth2. N=21, X=(21+7)//7=28//7=4, bit_length=3, d=2. Good. So it works.
Thus using X and bit_length yields correct depth.
Now, offset = N - 7*((1<<d)-1). For boundary case K=8 (d=1, N=7), offset = 7 - 7*(2-1)=0, correct. For K=22 (d=2, N=21), offset=21-21=0, correct. For K=49? Actually K=49 is last of depth2? N=48, d=2, offset=48-21=27, correct. For K=50 (first of depth3), d=3, offset=49 - 7*(8-1)=49 - 49 =0, correct.
Thus, we can use this formula.
Now, we need to ensure that when we compute offset, it is non-negative and within the block. Yes.
Thus algorithm:
For each K:
N = K - 1
X = (N + 7) // 7
d = X.bit_length() - 1
offset = N - 7 * ((1 << d) - 1)
block = 1 << d
r = offset // block
output names[r]
Testing on sample:
K=3: N=2, X=(2+7)//7=9//7=1, d=0, offset=2-0=2, block=1, r=2 -> Kohli.
K=9: N=8, X=(8+7)//7=15//7=2, d=1, offset=8-7=1, block=2, r=0 -> Rohit.
Works.
Now, consider potential integer overflow: Not in Python.
Thus solution is robust.
Now, we need to implement in code.
I'll implement solve() accordingly.
Edge Cases:
- K = 10^9: Compute N, X, d, etc. No problem.
Now, note: The names list includes "Sir Jadeja" with a space. Print exactly.
Thus final answer.
We should also include reading input and printing output.
Now, I'll produce final answer with explanation.
But we need to write an explanation for the solution? The problem is to output code only? The instruction says "You are required to output the name of a player as given in the above list who will play a K^th ball." So just code. But in answer we can provide solution code.
But since it's a "text" response, we need to output final code. Usually we provide code with explanation. I'll give a clean Python function.
We can also precompute powers? Not necessary.
Make sure to use fast I/O for large T? T up to 1000, fine. But we can use sys.stdin read.
Now, let's write final code.
I'll produce:
import sys
def solve():
data = sys.stdin.read().split()
if not data:
return
it = iter(data)
t = int(next(it))
names = ["Rohit", "Dhawan", "Kohli", "Yuvraj", "Raina", "Dhoni", "Sir Jadeja"]
out = []
for _ in range(t):
k = int(next(it))
n = k - 1
# compute depth d
x = (n + 7) // 7
d = x.bit_length() - 1
offset = n - 7 * ((1 << d) - 1)
block = 1 << d
r = offset // block
out.append(names[r])
sys.stdout.write("\n".join(out))
if name == "main":
solve()
Test with sample.
Input:
2
3
9
Our code: for K=3: n=2, x=(9)//7=1, d=0, offset=2, block=1, r=2 -> Kohli. For K=9: n=8, x=15//7=2, d=1, offset=1, block=2, r=0 -> Rohit. Output lines: "Kohli\nRohit". matches sample.
Now test K=1: output "Rohit". K=7: "Sir Jadeja". Good.
Thus done.
But wait, check K=8: n=7, x=14//7=2, d=1, offset=0, block=2, r=0 -> Rohit. Good.
Now, check K=14: n=13, x=20//7=2? 13+7=20, //7=2, d=1, offset=13-7=6, block=2, r=3 -> Yuvraj. Good.
So code correct.
Now, think about a potential error: When N+7 is multiple of 7 exactly? That is when N+7 = 7 * X exactly. Then X = (N+7)//7 = X. That's fine. For instance, N+7=14 => X=2, d=1. Works.
Thus final answer.
Now, we could also derive directly using binary representation, but this is fine.
Now I will produce final answer. ```python
import sys
def solve() -> None:
data = sys.stdin.read().split()
if not data:
return
it = iter(data)
t = int(next(it))
names = ["Rohit", "Dhawan", "Kohli", "Yuvraj", "Raina", "Dhoni", "Sir Jadeja"]
out_lines = []
for _ in range(t):
k = int(next(it))
n = k - 1 # zero‑based ball index
# depth d is determined by (n+7)//7
x = (n + 7) // 7
d = x.bit_length() - 1 # floor(log2(x))
offset = n - 7 * ((1 << d) - 1)
block = 1 << d
r = offset // block # root index 0 … 6
out_lines.append(names[r])
sys.stdout.write("\n".join(out_lines))
if name == "main":
solve()