Taming a T-Rex
Difficulty: Hard

Although evolution theory suggests that mammals started to dominate this world only after the mass extinction of the dinosaurs, there are some people who say man and dinosaurs may have co-existed for some time. Some argue that man even tamed and used some of these animals like the Flintstones. Shankar is such a believer and Sunil is a skeptic.  
One day Sunil asked Shankar, "If your argument is right how would you tame a T-REX and what would you use it for?". Shankar replied, "We can use it for cow transportation from one village to another and we can keep it calm by feeding it at constant intervals". Sunil argued that the T-REX would have a maximum capacity C to carry. Let us say the distance is d km. If it is long, the T-REX would eat all the cows before it reaches the other village. Shankar argued that he knew a method that takes the maximum possible number of cows M to the destination.  
Sunil replied, "Oh really? I will give a few conditions. The T-REX will eat 1 cow every km until it reaches the destination. It may do so at any time during a 1 km stretch. So, there can not be a situation where the TREX has no cow to eat. If you drop cows in the middle, you can do so only at distances which are a multiple of 1 km. And, finally all the cows at the destination need to be alive i.e you can not cut a cow (fractions are not allowed)".  
Shankar was stunned. He needs your help. Given I (the number of cows at the starting village) , d and C find M, the maximum number of cows that can be taken to the destination subject to the mentioned constraints.   

**Input Format:**
There will be multiple test cases in the input. The input begins with a line containing a single integer n,n ≤ 300, which gives the number of test cases. Then n lines follow each containing the three integers I, 1 ≤ I ≤ 106, d, 1 ≤ d ≤ 105, and C, 1 ≤ I ≤ 106, in that order separated by spaces. d is in kilometers.

**Output Format:**
For each test case print on a separate line the maximum number of cows that can be transported to the destination village under the given conditions.

**Example:**  

*Input:*  
2  
3000 1000 1000  
30 10 10  

*Output:*  
533  
5  

Input Format:

Output Format:

Sample Input:

Sample Output:

Python 3 starter code:


Reference solution (C++):
	#include <vector>
	#include <list>
	#include <map>
    #include <set>
	#include <deque>
	#include <queue>
	#include <stack>
	#include <bitset>
	#include <algorithm>
	#include <functional>
	#include <numeric>
	#include <utility>
	#include <sstream>
	#include <iostream>
	#include <iomanip>
	#include <cstdio>
	#include <cmath>
	#include <cstdlib>
	#include <cctype>
	#include <string>
	#include <cstring>
	#include <cstdio>
	#include <cmath>
	#include <cstdlib>
	#include <ctime>

	using namespace std;

	int solve(int n,int D,int C)
	{
	if (C==1) return 0;
	while (1)
	{
		if (n<=D) return 0;
		if (D==0) return n;
		if (n<=C) return max(0,n-D);
		int M=n%C;
		int S=n/C;
		if (M>=2)
		{
			int E=2*S+1;
			int G=min(D,(M-2)/E+1);
			D-=G;
			n-=G*E;
		}
		else
			{
			D--;
			n-=(M+2*S-1);
			}
		}
	}

	int main()
	{
	//	freopen("input.txt","r",stdin);
	int testcase;
	for (scanf("%d",&testcase);testcase>0;testcase--)
	{
		int n,D,C;
		scanf("%d%d%d",&n,&D,&C);
		int ret=solve(n,D,C);
		printf("%d\n",ret);
	}
	return 0;
	}