We are given a subtraction game: starting with n, players alternately subtract a number between 1 and k inclusive. The new n becomes the result. The game ends when n <= 0. The player who makes the move that makes n <= 0 loses. So it's a misère version? Actually it says: "The person to play the last move loses the game." So it's a misère condition: the player who makes the move that results in zero or negative loses. Usually in subtraction games, the player who cannot move loses (normal play). Here, moves are always possible as long as n > 0, because you can subtract at least 1. The game ends when n becomes zero or negative after a move. That move is the last move, and the player who made it loses. So it's like a "poisoned chocolate" game: you don't want to take the last chip. Actually it's like players subtract from n, and if after your move n <= 0, you lose. So it's a misère subtraction game. But careful: The game continues until the value of n becomes zero or negative. The person to play the last move loses. That's exactly misère version.
We need to determine winner given n, k, both optimal.
Let's analyze.
We can think of positions with n positive. The player whose turn it is wants to avoid moving to a state where after his move n <= 0, because then he loses immediately. Also, he wants to force opponent into a losing position.
Define a position n (positive integer) as winning (W) if the player about to move can force a win (i.e., eventually opponent loses) assuming both play optimally. But careful: The win condition is: the player who makes the move that makes n <= 0 loses. So a player wins if he does NOT make the losing move, i.e., the opponent eventually makes the losing move. So from perspective of player to move, if he can force opponent to be the one who eventually makes the move that results in n <= 0, then he wins. So standard combinatorial game: position is losing (P-position) if the player to move will lose with optimal play (i.e., all moves lead to winning positions for opponent), winning (N-position) if there exists a move that leads to a losing position for opponent. But careful: The terminal positions? Usually terminal positions are those where no moves are possible, and the player to move loses. Here, the terminal condition is not "no moves" but rather if you make a move that reduces n to <= 0, you lose. So there is no move from n <= 0 because game ends before next turn. So the positions with n > 0 are non-terminal. But we can treat "after a move, if n <= 0, the mover loses". So from a state n > 0, the player has moves: subtract x in [1, k], resulting in new value n' = n - x. If n' <= 0, then the player loses immediately. So such a move is a losing move for the player; they would avoid it unless forced. So we need to consider safe moves: those with n' > 0. Among those, the player can move to another state n' > 0, and then it's opponent's turn. If from that state, opponent is in a losing position (i.e., opponent will eventually lose), then this move is winning. If all safe moves lead to winning positions for opponent, and also if there are no safe moves? But is it possible to have no safe moves? Since k >= 1, if n > 0, can we always subtract 1? Subtracting 1 gives n-1 which could be zero if n=1. So if n=1, then any subtraction (1 to k) results in n' <= 0 because n=1, subtract 1 gives 0, subtract >1 gives negative. So from n=1, all moves are losing immediately (since they make n <= 0). Thus the player whose turn it is when n=1 is forced to lose (if they must move). So n=1 is a losing position (P-position). Actually, if n=1, any move results in loss, so it's losing for player to move.
If n=2, moves: subtract 1 -> n'=1 > 0, safe. subtract 2 -> n'=0, loses immediately, so that's a bad move unless no alternative. So the player can subtract 1 to go to n'=1, which is losing for opponent. So n=2 is winning.
If n=3 and k=2: moves: subtract 1 -> n'=2, safe. subtract 2 -> n'=1, safe? Actually n'=1 > 0, safe. Both safe. n=2 and n=1: we need to know statuses. n=2 is winning (for player to move? Actually at n=2, the player to move (which would be opponent) can win? Let's compute: from n=2, as we determined, the player (call it player A) can subtract 1 to go to n=1, which is losing for opponent. So n=2 is winning for player to move. n=1 is losing for player to move. So from n=3, if Rani subtracts 1 to n=2, she gives Nandu a winning position, which is bad. If she subtracts 2 to n=1, she gives Nandu a losing position, which is good. So she chooses subtract 2, so she wins. So n=3 is winning. That matches sample: Rani wins.
General analysis: This is a misère subtraction game where the losing condition is to be the player who makes n <= 0. Equivalent to: players take turns subtracting from n, and the player who is forced to take the last positive number (i.e., leaves <=0) loses. But that is like normal play in a subtraction game where you want to avoid taking the last? Actually typical normal play subtraction game: you subtract from n, and the player who makes n=0 wins (i.e., takes the last). Here, the player who makes n <= 0 loses. So it's the misère version of the take-away game. Usually the misère Nim analysis is tricky, but for subtraction games with a fixed subtraction set, there's a known pattern. For subtraction games where you can subtract 1..k, normal play: positions are winning if n mod (k+1) != 0, losing if n mod (k+1) == 0 (the classic take-away game where last move wins). For misère version (last move loses), the pattern is: For the game where you can take 1..k objects and the player who takes the last loses, the strategy is: if n mod (k+1) == 1 then? Actually, we need to derive.
Classic take-away (normal): players remove 1..k objects, player who takes last wins. Winning positions: n % (k+1) != 0; losing: n % (k+1) == 0.
Misère version (player who takes last loses) for subtraction games with move set {1,...,k} and no other restrictions, is known: For all n > 0, if n % (k+1) == 1 then it's a losing position? Let's test: n=1, losing; n=2, winning; n=3 with k=2: 3 % 3 = 0, but we found winning. Actually if k=2, k+1=3. n=1 mod3=1 -> losing (yes). n=2 mod3=2 -> winning (yes). n=3 mod3=0 -> according to normal play, that would be losing, but we found winning. So maybe misère is opposite for some n? Check n=4,k=2: possible moves subtract 1,2. If subtract 1 -> n=3 (winning for opponent as we determined? Actually we said n=3 is winning for player to move? Let's compute n=3: we saw from n=3, player can force win by subtracting 2 to n=1. So n=3 is winning. So if Rani from n=4 subtracts 1 gives n=3 which is winning for Nandu -> bad. Subtract 2 -> n=2 (winning for opponent? n=2 is winning for player to move). So both moves lead to winning positions for opponent, so n=4 should be losing? Check manually: Rani at n=4, k=2. Options:
- Sub 2 -> n=2. Then Nandu at n=2: he can sub 1 -> n=1. Then Rani at n=1 forced to sub 1 or 2, makes n<=0 and loses. So Nandu wins.
- Sub 1 -> n=3. Nandu at n=3: he can sub 2 -> n=1. Then Rani at n=1 loses. So Nandu wins. So n=4 is losing. Now n mod (k+1) = 4 mod 3 = 1. So losing when mod = 1? For n=4 mod3=1 -> losing (yes). n=5 mod3=2 -> what? Let's test n=5,k=2: Rani can sub? Option: sub 1 -> n=4 (losing for opponent? n=4 losing for player to move, so good). So n=5 is winning. n mod3=2 -> winning. n=6 mod3=0 -> let's test. Rani at n=6: moves sub 1->5 (winning for opponent), sub 2->4 (losing for opponent). So she can sub 2 to n=4 which is losing for Nandu, so n=6 is winning. So n=6 mod3=0 is winning. n=7 mod3=1 -> presumably losing. Check: n=7,k=2: moves: sub1->6 (winning for opponent), sub2->5 (winning). So both winning, so losing. So pattern: losing positions when n mod (k+1) == 1, winning otherwise. For n=1 mod3=1 lose; n=4 mod3=1 lose; n=7 mod3=1 lose. All else win. That seems to be the pattern.
Let's test other k. For k=1 (only subtract 1). Then game: each move subtract 1, player who makes n<=0 loses. That's basically the players alternate subtracting 1 from n. So if n=1, first player loses (since they must subtract 1 making n=0). n=2: first subtracts 1 -> n=1, second forced to subtract 1 -> loses, so first wins. n=3: first subtracts 1 -> n=2, then second subtracts 1 -> n=1, first subtracts 1 -> loses? Actually careful: after first subtract 1 to n=2, second's turn: n=2, second subtracts 1 -> n=1, then first's turn: n=1, first must subtract 1 -> n=0, first loses. So with n=3, first loses. So pattern: losing when n is odd? Actually n=1 lose, n=2 win, n=3 lose, n=4 win? Let's check n=4: first subtract 1 -> n=3 (second's turn at 3, which we determined losing for player to move? At n=3, player to move loses as we saw. So if first moves to n=3, second loses? Actually at n=3, player to move loses (since we saw first loses when starting at n=3). So if second is at n=3, second loses. So first wins. So pattern: n odd -> first loses? n=1,3 lose; n=2,4 win. So losing when n mod 2 = 1. For k=1, k+1=2. According to our hypothesized rule: losing when n mod (k+1) == 1, i.e., n mod 2 == 1. That matches. So rule seems to hold.
Now check k=3. Let's test small n. k=3, moves 1..3. Determine losing positions manually? The rule predicts losing when n mod 4 == 1. So n=1 lose, n=2 win, n=3 win? n=3 mod4=3 -> win, n=4 mod4=0 -> win? Actually rule says only mod 1 is losing, so n=5 lose, n=6 win, n=7 win, n=8 win? Wait n=8 mod4=0 -> win. Let's test n=2: moves: subtract 1 -> n=1 (safe? Actually n=1>0? subtract 1 gives n=0? Wait n=2 subtract 1 gives 1 >0 safe; subtract 2 gives 0 immediate loss; subtract 3 gives -1 loss. So best move: subtract 1 to n=1, which is losing for opponent (since n=1 lose). So n=2 win. Good.
n=3: moves: subtract 1 -> n=2 (winning for opponent, so bad), subtract 2 -> n=1 (losing for opponent, good), subtract 3 -> 0 lose immediate. So there is a winning move (subtract 2). So n=3 win. Good.
n=4: moves: subtract 1 -> n=3 (winning for opponent? n=3 win, so opponent gets winning, bad), subtract 2 -> n=2 (winning for opponent, bad), subtract 3 -> n=1 (losing for opponent, good). So n=4 win. So indeed n=4 win.
n=5: moves: subtract 1 -> n=4 (winning for opponent), subtract 2 -> n=3 (winning), subtract 3 -> n=2 (winning). All safe moves lead to winning positions, so n=5 lose. Check if any move leads to immediate loss? subtract >5? no. subtract exactly 5? cannot since max k=3, so all moves safe. So n=5 lose. So rule holds.
Thus general solution: For given n,k, if n % (k+1) == 1 then the first player (Rani) loses, i.e., Nandu wins? Actually we need to output "Rani" if winner is Rani, else "Nandu". Since Rani starts, if the starting position is winning for the first player, then Rani wins; if losing, then Nandu wins. According to our analysis, losing positions are when n mod (k+1) == 1. So if n % (k+1) == 1, then first player loses -> Nandu wins. Else Rani wins.
But is this always correct? Let's test some edge cases: n = k. Then k+1 > n. n % (k+1) = n. So condition is n == 1? If n>1, then n% (k+1) != 1, so first player wins. Check: n=k > 1. For example n=5,k=5. First player can subtract any number 1..5. If she subtracts 5, she makes n=0 and loses immediately, so that's a bad move. She can subtract less, like 4, making n=1. Then opponent at n=1 loses (since any move makes n<=0). So first player wins by subtracting 4. So indeed winning. If n=1,k=1, then n%2=1 -> first loses, correct. If n=1,k=100, then n% (101)=1 -> first loses, correct because forced to subtract at least 1 resulting in 0 and lose.
What about n=2,k=2? k+1=3, 2%3=2 !=1 => first wins. Check: Rani can subtract 1 -> n=1, then Nandu loses (since any move loses). So win. That's correct. n=3,k=3? 3%4=3 -> win. Rani can subtract 2 -> n=1? Actually subtract 2 -> n=1, then opponent loses. So win. n=4,k=3? 4%4=0 -> win. Rani can subtract 3 -> n=1, opponent loses. So win. n=5,k=3? 5%4=1 -> lose, as earlier.
Thus pattern seems consistent.
But is there any nuance when k = 1? Already matches.
Thus solution is straightforward: For each test case, compute if n % (k+1) == 1 => Nandu wins, else Rani wins.
But wait: constraint: 1 ≤ k ≤ n. So k+1 may be > n, but that's fine. Mod works.
Now the problem mentions "Large Input/Output Data. Use fast I/O." So t up to 1e6, and n up to 1e6. We need to read many test cases quickly and output. In C++ we can use scanf/printf or fast I/O. In Python, we can use sys.stdin.buffer.read and parse ints.
But need to consider that t can be 1e6, reading line by line might be slow, but using sys.stdin.buffer.read and splitting is okay. But Python might be borderline? However, 1e6 test cases is a lot for Python, but each test case only two ints, total 2e6 ints, which is fine with fast parsing. Output also 1e6 lines, can use write to buffer.
But we need to ensure not to exceed time. Usually HackerEarth Python solutions for 1e6 test cases with simple logic can pass if using fast I/O.
Let's implement:
Read all data: data = sys.stdin.buffer.read().split()
First element is t, then pairs n,k.
Loop through index.
For each test case, compute (n % (k+1) == 1). If true, print "Nandu\n" else "Rani\n".
But note: n and k are up to 1e6, k+1 may overflow? Not in Python.
Edge Cases: t up to 1e6, need to be efficient.
We'll produce output as list of strings and then join.
Alternatively, we can compute directly.
But we must be careful: In Python, using modulo for each test case is cheap.
Let's test with sample: (2,1) -> (2%2==1? Actually k+1=2, 2%2=0 !=1 -> Rani. (3,2) -> k+1=3, 3%3=0 -> Rani. matches.
But wait sample output says both Rani. Yes.
Let's test with n=1,k=1: should output Nandu? Actually starting n=1, Rani loses, so winner is Nandu. Our condition n%2==1 true, so output Nandu.
Now, we need to confirm that the misère subtraction game with subtraction set {1,...,k} indeed has that simple rule. I recall that for subtraction games where the move set is all numbers from 1 to m, the misère version is: if n % (m+1) == 1, then previous player wins? Actually let's derive formally.
We can think of it as: The losing positions are those where n ≡ 1 mod (k+1). Because from any other n, you can subtract appropriate amount to bring n' to 1 mod (k+1). And from n ≡ 1 mod (k+1) (and n>0), any move will change it to something not ≡ 1 mod (k+1) and also not to 0? Let's check: If n = 1 + t(k+1), with t>=0. If you subtract x (1<=x<=k), then n' = 1 + t(k+1) - x = (t(k+1) - (x-1)) . Mod (k+1), n' ≡ 1 - x (mod k+1) ≡ (k+2 - x) mod (k+1). Since x from 1 to k, 1-x mod (k+1) ranges from 0 to -k+1? Actually compute: if x=1, n' ≡0; if x=k, n' ≡1 - k ≡ (k+1) + 1 - k = 2? Actually 1 - k mod (k+1) = (1 - k + k+1) = 2, so yields 2. So possible residues are 0, k, k-1, ..., 2? That is, everything except 1. Also, could n' become <=0? If n=1 (t=0), subtract 1 gives 0, which is <=0, but that move is losing because immediate loss. So from n=1, there is no safe move, so it's terminal losing. For n>1 of form 1 mod (k+1) (i.e., n=1+(k+1)*t, t>=1), subtracting any x up to k yields n' >= 1+(k+1)t - k >= 1+(k+1)t - k = 1 + kt + t - k = 1 + k(t-1) + t? Actually simpler: Since t>=1, n >= k+2? For t=1, n = k+2? Wait 1+(k+1)*1 = k+2. So n = k+2 at minimum. Then subtract x (max k) yields n' >= (k+2 - k) = 2 > 0. So safe moves all lead to n' >0. So the player cannot force a win by forcing opponent to lose immediately; they must consider subsequent. And from those n' (which are not ≡1 mod (k+1)), opponent can then subtract an appropriate amount to bring it back to ≡1 mod (k+1) because from any n' not ≡1 mod (k+1), you can subtract ( (n' - 1) mod (k+1) )? Actually to get to 1 mod (k+1), you want to subtract y such that n' - y ≡ 1 (mod k+1). That requires y ≡ n' - 1 (mod k+1). Since n' mod (k+1) can be 0,2,3,...,k, then n'-1 mod (k+1) can be -1,1,2,...,k-1? But let's do systematically: For any n not ≡ 1 mod (k+1), there exists y in [1,k] such that (n - y) ≡ 1 mod (k+1). Because the residues of n mod (k+1) are from 0 to k, excluding 1. If residue r ≠ 1, then we want r - y ≡ 1 mod (k+1) => y ≡ r-1 (mod k+1). Since r ∈ {0,2,3,...,k}, r-1 mod (k+1) ranges from -1 mod (k+1)=k, to k-1? Actually if r=0, then r-1 = -1 ≡ k (mod k+1), and k is in [1,k] (since k>=1). If r=2, then r-1=1, y=1 is allowed. If r=3, r-1=2, etc. If r=k, r-1=k-1, also in range. So there is always a y in 1..k that achieves the target, and also resulting n' = n-y >0? Need to ensure that after subtracting, n' is still >0. Could it become 0 or negative? Possibly if n is small? For n=2 and k=100, n mod (101) = 2 ≠1, we can subtract y = r-1 = 1 to get n' = 1, which is >0. For n=2, r=2, subtract 1 =>1 safe. For n=3, r=3, subtract 2 =>1 safe. For n=0 mod (k+1) but small? n could be exactly k+1? But n mod (k+1) = 0, not 1. For n = k+1, residue 0, we can subtract y = k (since r-1 = -1 ≡ k) to get n' = (k+1) - k = 1 >0. So safe. So indeed from any n not ≡1, you can move to a state that is ≡1 mod (k+1) and positive. Also, from any state ≡1, all moves go to states not ≡1. Thus the positions with n ≡ 1 mod (k+1) are losing positions, because any move leads to a winning position for opponent (since opponent can always move back to ≡1). And from any other position, there exists a move to a losing position (≡1). So this is correct.
But we also have to consider that if from a non-1 position, the move to a ≡1 position might cause n' <= 0? Could that happen? If n is small, say n=2 and k=1? k+1=2, n=2 mod2=0, not 1. To move to ≡1, we need to subtract y such that n-y ≡ 1 mod 2 => 2-y ≡ 1 mod2 => y ≡ 1 mod2 => y=1. That yields n' = 1 >0. Safe. If n=2 and k=2? k+1=3, n mod3=2, not 1. To get to 1 mod3, we need y ≡ n-1 mod3 = 1 mod3 => y=1 yields n'=1. Safe. If n=3 and k=2, mod3=0, need y ≡ -1 mod3 = 2, yields n'=1. Safe. If n=1 and k=anything, it's already losing, and there is no safe move. So it's consistent.
Therefore the solution is indeed: if (n % (k+1) == 1) output "Nandu" else "Rani".
Now we need to implement. t up to 1e6, so reading line by line with input() is too slow. Use sys.stdin.buffer.read().
Potential pitfalls: The problem says "Large Input/Output Data. Use fast I/O." So we need to be careful in Python. Also the output should be exactly "Rani" or "Nandu" with newline.
We'll parse all integers at once:
import sys
def main():
data = sys.stdin.buffer.read().split()
if not data: return
t = int(data[0])
out_lines = []
idx = 1
for _ in range(t):
n = int(data[idx]); k = int(data[idx+1]); idx += 2
if n % (k+1) == 1:
out_lines.append("Nandu")
else:
out_lines.append("Rani")
sys.stdout.write("\n".join(out_lines))
But note: This will hold up to 1e6 strings in a list, which is okay memory wise (maybe ~10-20 MB). Alternatively we could write directly to sys.stdout.buffer with a write for each, but that might be slower due to many syscalls. Joining at end is faster. However building a list of strings is fine.
But we need to ensure we don't run into recursion or anything.
Edge Cases: t can be 0? But constraints say t>=1, so fine.
Now is there any scenario where k is so large relative to n that k+1 > n, and n % (k+1) = n. Condition n==1 would be losing. That's correct: only n=1 is losing when k >= n? Actually if k >= n and n>1, first player can always subtract n-1 to leave 1, forcing opponent to lose, so winning. That matches rule: n % (k+1) = n, which equals 1 only if n=1.
Thus solution stands.
But wait: Check scenario n=2, k=2: rule says win, correct. n=2,k=100: rule says win (2%101=2 !=1). Indeed first player subtract 1 -> n=1, opponent loses.
Now double-check with larger values: n=10,k=3: k+1=4, 10%4=2 !=1 => win. Can we find a strategy? Subtract to get to 1 mod4: from 10, we need to get to 9? Actually 9%4=1? 9%4=1 yes. So subtract 1 yields 9, which is losing for opponent. So win.
All good.
Thus answer.
But let's double-check the sample explanation: They gave n=2,k=1 => Rani wins. 2%2=0 -> Rani. n=3,k=2 => 3%3=0 -> Rani. Both Rani.
Now consider n=4,k=3: 4%4=0 -> Rani, we earlier thought win (subtract 3 to 1). Good.
So implement.
Complexity O(t).
Potential issue: Python's modulo with large numbers? k+1 up to 1e6+1, fine.
Now maybe we should consider that t can be as large as 1e6, and each test case we compute modulo, which is fine.
But note: The problem statement says "Note : Large Input/Output Data. Use fast I/O." So we must use fast I/O.
Also check that we need to output exactly 'Rani' or 'Nandu' case-sensitive.
Thus final code.
But we must be careful about reading the input: first line is t, then t lines each with two ints. However, there could be extra whitespace, but using split is fine.
Now, is there any trick? Could there be a situation where the misère condition leads to a different pattern because of the possibility to subtract exactly k and cause immediate loss? But we already accounted because such moves are losing and would be avoided unless forced. In losing positions, all safe moves lead to winning positions; but if there is an unsafe move that leads to immediate loss, that's even worse, so still losing. So the analysis holds.
But wait: In the case where n <= k? If n <= k, then there is a move that subtracts n exactly which makes n=0 and loses immediately. But also there might be a safe move that leaves positive n. So if there exists any safe move to a losing position, the player will take it. So the pattern remains.
Thus solution is correct.
Now let's think about possible pitfalls: When n is exactly 1, the player must subtract at least 1 and at most k, which forces n' <=0, so immediate loss. So losing.
When n is in the range 2 to k+1? Let's test n = k+1? Then n mod (k+1) = 0. According to rule, winning. Indeed from n = k+1, player can subtract k to get to 1, which is losing for opponent. So winning.
When n = k+2? That's 1 mod (k+1) => losing? Let's test n=6,k=4: k+1=5, 6%5=1 -> losing? Check: n=6,k=4. Moves: subtract 1->5 (which mod5=0, not 1), subtract2->4 (mod5=4), subtract3->3(mod5=3), subtract4->2(mod5=2). All are not 1. Are any of these positions losing? Need to know if those positions are winning for the opponent. According to rule, they are all winning because they are not 1 mod5. So from n=6, all safe moves lead to winning positions. But also there is possibility of subtracting 5? No max is 4. So all moves lead to positions where opponent can force a win. So n=6 should be losing. Let's manually test: Rani at 6, k=4. She can subtract 1->5. Nandu at 5 (mod5=0, winning for player to move). Nandu can subtract? To force win, Nandu should move to a losing position for Rani, i.e., to a position ≡1 mod5. From 5, mod5=0, to get to 1 mod5, need subtract 4? Because 0-4 ≡1? Actually (5 - y) mod5 = 1 => y ≡ 4 mod5, and y=4 is allowed, yields 1. So Nandu subtracts 4 -> n=1, Rani loses. So indeed Rani loses. If Rani subtracts 2->4, Nandu at 4 (mod5=4) can subtract 3 to get 1 (since 4-3=1). If subtract 3->3, Nandu subtract 2 to get 1. If subtract 4->2, Nandu subtract 1 to get 1. So indeed losing. So rule holds.
Thus final answer.
Now we should provide code.
But wait, problem statement: "Note : Large Input/Output Data. Use fast I/O." So we must handle t up to 1e6. In Python, reading with sys.stdin.buffer.read() and splitting is fine, but converting all strings to ints may take some time. However 2e6 ints conversion is doable within typical time limits (maybe ~0.5-1 sec). But we need to be careful. Could we use a faster integer parsing? Probably not needed.
We can also use map(int, sys.stdin.buffer.read().split()) but that will create a list of all ints, which is 2e6+1 ints, that's memory ~16 MB (since each int object is ~28 bytes, 2e6*28 ≈ 56 MB, which might be high). Better to iterate over the splitted bytes and convert as needed, but that also creates a list of strings of length ~2e6+1, each string object overhead. That's also memory heavy. But t up to 1e6, each test case two numbers, total tokens ~2e6+1. Splitting into list of bytes objects will allocate many small objects, memory maybe a few hundred MB? Actually each bytes object might be a reference to the underlying memory? In Python, when we split a bytes object, it creates new bytes objects for each token. This can be memory intensive for 2 million tokens. However, typical HackerEarth memory limit? Not specified but likely 1-2 GB? Possibly 512 MB. Let's estimate: Each bytes object contains the digits. For numbers up to 1e6, up to 7 digits plus maybe newline. So average maybe 6 bytes. Overhead for bytes object is something like 33 bytes? So per token maybe ~40 bytes. For 2 million tokens => 80 MB. That's acceptable. Plus list overhead ~16 MB. So total maybe ~100 MB. Might be okay, but borderline. However we can process stream without storing all tokens using a generator that reads and yields ints. For example, we can use a custom fast integer reader that reads from sys.stdin.buffer.read() and yields ints using a pointer to the raw bytes. That would be more efficient and avoid creating many objects. But for simplicity, we can risk using split. Many Python solutions on HackerEarth for 1e6 ints pass with split. I've seen solutions reading 2e6 ints with split and passing. But it's better to be safe.
Alternative: Use sys.stdin.buffer.read() and then map to ints, but that also creates list. Better: Use sys.stdin.buffer.readline in a loop? That would be slower due to Python overhead for each line but for 1e6 lines it might be okay? 1e6 lines reading with readline might be borderline but maybe still passes if optimized? Usually they recommend using sys.stdin.buffer.read and split for large input.
But we can implement a fast int parser using memoryview? Not necessary.
Given time constraints, I'll implement a fast generator using sys.stdin.buffer.read and manual parsing.
But to keep it simple and reliable, I'll propose using sys.stdin.buffer.read().split() because the problem is simple.
But note: t can be up to 1e6, we need to output 1e6 lines, building a list of 1e6 strings and then join is okay, memory ~10-20 MB for strings? Actually each string "Rani" or "Nandu" plus newline if we store with newline? We'll store without newline and join with newline. Storing 1e6 strings of length 4-5 characters as Python objects overhead is maybe ~50 bytes each, so 50 MB. Combined with input tokens ~100 MB, total ~150 MB, still maybe okay if memory limit is high. But to be safe, we could write output incrementally using sys.stdout.write in a loop, but that may be slower due to many syscalls. However we can accumulate in a list of strings and then join, which is typical.
Alternatively, we can use io.StringIO to accumulate.
Given that Python solutions on HackerEarth sometimes face TLE for 1e6 I/O, we should optimize.
Better approach: Use sys.stdin.buffer.read into data, parse ints, and then compute results and write to sys.stdout using a single write with join. That's fastest.
I'll go with that.
But we must be careful about memory: The list of output strings: we can store as bytes in a list and then use b'\n'.join(). Or just strings.
Simplify: out = [] then append. At end, sys.stdout.write('\n'.join(out))
Now, we should also consider that t is up to 1e6, and we need to process quickly. The modulo operation is cheap.
Thus code is straightforward.
Double-check: In some languages, the answer might be reversed? Let's test with n=1,k=1: According to game: Rani moves, must subtract 1, n becomes 0, she loses, so winner is Nandu. Our condition says n%(k+1)==1 => true => output Nandu. Good.
With n=2,k=1: Rani wins => Rani. Condition false => Rani.
With n=3,k=2: Rani wins => Rani. condition false => Rani.
With n=4,k=2: we earlier determined Nandu wins? Actually we said n=4,k=2 is losing for first player? Let's recalc: n=4,k=2. Rani can subtract 1->3 (winning for opponent?), we earlier said n=3 is winning for player to move. So if Rani goes to 3, Nandu can win. Rani subtract 2->2 (n=2 is winning for opponent). So both moves give opponent winning position. So Rani loses, Nandu wins. n=4%3=1 => true => Nandu. Good.
Thus correct.
Now, think about n and k such that k is huge but n is 1, losing. That's fine.
Thus answer.
Now we need to produce final answer.
Potential edge: t can be 0? But constraint says 1 ≤ t.
Thus final code:
import sys
def main():
data = sys.stdin.buffer.read().split()
if not data:
return
t = int(data[0])
out = []
idx = 1
for _ in range(t):
n = int(data[idx]); k = int(data[idx+1]); idx += 2
if n % (k+1) == 1:
out.append("Nandu")
else:
out.append("Rani")
sys.stdout.write("\n".join(out))
if name == "main":
main()
But we need to ensure that we use fast I/O: this uses split which is okay.
Alternatively, we can read line by line with sys.stdin.buffer.readline for t lines, but might be slower. But with t=1e6, reading line by line might be okay if each line is small? It involves 1e6 calls, which may be slower. So the bulk read is better.
Now, note that the input may have trailing spaces/newline, split handles.
Thus final solution.
But maybe there is a nuance: The problem states "both play alternately, Rani playing the first move." And "the person to play the last move loses." That's exactly misère.
Thus answer.
Now I'll produce final answer. ```python
import sys
def solve() -> None:
data = sys.stdin.buffer.read().split()
if not data:
return
t = int(data[0])
out = []
idx = 1
for _ in range(t):
n = int(data[idx])
k = int(data[idx + 1])
idx += 2
# losing positions are exactly those where n % (k+1) == 1
if n % (k + 1) == 1:
out.append("Nandu")
else:
out.append("Rani")
sys.stdout.write("\n".join(out))
if name == "main":
solve()