---
{"category_name":"easy","problem_code":"CHFINVNT","problem_name":"Chef and Bulb Invention","problemComponents":{"constraints":"- $1 \\leq T \\leq 10^5$\n- $1 \\leq N, K \\leq 10^9$\n- $0 \\leq p \u003C N$","constraintsState":true,"subtasks":"**Subtask #1 (100 points):** Original constraints","subtasksState":true,"inputFormat":"- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.\n- The first and only line of each test case contains three space-separated integers $N$, $p$, and $K$.","inputFormatState":true,"outputFormat":"For each test case, print a single line containing one integer — after how much time Chef will be able to give Chefland a new invention assuming that testing $1$ gas takes $1$ day.","outputFormatState":true,"sampleTestCases":{"0":{"id":1,"input":"4\n10 5 5\n10 6 5\n10 4 5\n10 8 5","output":"2\n4\n9\n8","explanation":"**Test case $1$:** On the day $1$ Chef will test gas numbered $0$ and on the day $2$ Chef will test gas numbered $5$.\n\n**Test case $2$:** On the day $1$ Chef will test gas numbered $0$, on the day $2$ Chef will test gas numbered $5$, on the day $3$ Chef will test gas numbered $1$, and on the day $4$ Chef will test gas numbered $6$.\n\n**Test case $3$:** On the day $1$ Chef will test gas numbered $0$, on the day $2$ Chef will test gas numbered $5$, on the day $3$ Chef will test gas numbered $1$, on the day $4$ Chef will test gas numbered $6$, on the day $5$ Chef will test gas numbered $2$, on the day $6$ Chef will test gas numbered $7$, on the day $7$ Chef will test gas numbered $3$, on the day $8$ Chef will test gas numbered $8$, and on the day $9$ Chef will test gas numbered $4$.\n\n**Test case $4$:** On the day $1$ Chef will test gas numbered $0$, on the day $2$ Chef will test gas numbered $5$, on the day $3$ Chef will test gas numbered $1$, on the day $4$ Chef will test gas numbered $6$, on the day $5$ Chef will test gas numbered $2$, on the day $6$ Chef will test gas numbered $7$, on the day $7$ Chef will test gas numbered $3$, and on the day $8$ Chef will test gas numbered $8$.","isDeleted":false}}},"video_editorial_url":"https://youtu.be/w_ZrOrviTY0","languages_supported":{"0":"CPP14","1":"C","2":"JAVA","3":"PYTH 3.6","4":"CPP17","5":"PYTH","6":"PYP3","7":"CS2","8":"ADA","9":"PYPY","10":"TEXT","11":"PAS fpc","12":"NODEJS","13":"RUBY","14":"PHP","15":"GO","16":"HASK","17":"TCL","18":"PERL","19":"SCALA","20":"LUA","21":"kotlin","22":"BASH","23":"JS","24":"LISP sbcl","25":"rust","26":"PAS gpc","27":"BF","28":"CLOJ","29":"R","30":"D","31":"CAML","32":"FORT","33":"ASM","34":"swift","35":"FS","36":"WSPC","37":"LISP clisp","38":"SQL","39":"SCM guile","40":"PERL6","41":"ERL","42":"CLPS","43":"ICK","44":"NICE","45":"PRLG","46":"ICON","47":"COB","48":"SCM chicken","49":"PIKE","50":"SCM qobi","51":"ST","52":"SQLQ","53":"NEM"},"max_timelimit":1,"source_sizelimit":50000,"problem_author":"souradeep_adm","problem_tester":"","date_added":"18-07-2021","tags":{"0":"aug21","1":"math","2":"simple","3":"souradeep_adm"},"problem_difficulty_level":"Unavailable","best_tag":"","editorial_url":"https://discuss.codechef.com/problems/CHFINVNT","time":{"view_start_date":1629117000,"submit_start_date":1629117000,"visible_start_date":1629117000,"end_date":1735669800},"is_direct_submittable":false,"problemDiscussURL":"https://discuss.codechef.com/search?q=CHFINVNT","is_proctored":false,"visitedContests":{},"layout":"problem"}
---
### Read problem statements in [Bengali](https://www.codechef.com/download/translated/AUG21/bengali/CHFINVNT.pdf), [Mandarin Chinese](https://www.codechef.com/download/translated/AUG21/mandarin/CHFINVNT.pdf), [Russian](https://www.codechef.com/download/translated/AUG21/russian/CHFINVNT.pdf), and [Vietnamese](https://www.codechef.com/download/translated/AUG21/vietnamese/CHFINVNT.pdf) as well.
Chef is trying to invent the light bulb that can run at room temperature without electricity. So he has $N$ gases numbered from $0$ to $N - 1$ that he can use and he doesn't know which one of the $N$ gases will work but we do know it.
Now Chef has worked on multiple search algorithms to optimize search. For this project, he uses a modulo-based search algorithm that he invented himself. So first he chooses an integer $K$ and selects all indices $i$ in increasing order such that $i \bmod K = 0$ and test the gases on such indices, then all indices $i$ in increasing order such that $i \bmod K = 1$, and test the gases on such indices, and so on.
Given $N$, the index of the gas $p$ that will work, and $K$, find after how much time will he be able to give Chefland a new invention assuming that testing $1$ gas takes $1$ day.
For example, consider $N = 5, p = 2$ and $K = 3$.
- On the $1^{st}$ day, Chef tests gas numbered $0$ because $0 \bmod 3 = 0$.
- On the $2^{nd}$ day, Chef tests gas numbered $3$ because $3 \bmod 3 = 0$.
- On the $3^{rd}$ day, Chef tests gas numbered $1$ because $1 \bmod 3 = 1$.
- On the $4^{th}$ day, Chef tests gas numbered $4$ because $4 \bmod 3 = 1$.
- On the $5^{th}$ day, Chef tests gas numbered $2$ because $2 \bmod 3 = 2$.
So after $5$ days, Chef will be able to give Chefland a new invention
<aside style='background: #f8f8f8;padding: 10px 15px;'><div>All submissions for this problem are available.</div></aside>