#include <iostream>
using namespace std;
int main()
{
int e, i , n , f, g, sodas = 0;
cin >> n ;
for (int i = 0; i < n ; i++)
{
cin >> e >> f >> g;
e += f;
sodas=0;
while (e >= g)
{
int temp = e;
e /= g;
sodas += e;
e += temp % g;
}
cout << sodas << endl;
}
return 0 ;
}