#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
inline long long round_up_div(long long a,long long b){
return (a-1)/b + 1 ;
}
class CandyAddict{
#define RETURN(x) {\
res[i] = x;\
goto END;\
}
public: vector<long long> solve(vector<int> X, vector<int> Y,vector<int> Z){
int n = X.size();
vector<long long> res,x,y,z;
for(int i=0;i<n;++i){
x.push_back(X[i]);
y.push_back(Y[i]);
z.push_back(Z[i]);
}
res.resize(n);
for (int i=0;i<n;++i){
if (x[i]==y[i]) RETURN(0)
//else if (z[i]==1) res[i] = x[i]%y[i];
else{
int day = 1;
long long rem = x[i] % y[i];
long long cur = x[i] / y[i];
long long diff = cur * (x[i]-y[i]);
while(1){
if (day == z[i]) RETURN(rem)
if (rem + diff < y[i]){
long long period = round_up_div(y[i] - rem, diff);
if (day + period * cur > z[i]) RETURN(rem + (z[i]-day) / cur * diff + (z[i]-day) % cur * x[i])
else{
day += period * cur;
rem = (rem + period * diff) % y[i];
cur++;
}
}
else{
if (day + cur > z[i]) RETURN(rem + (z[i]-day) * x[i])
else{
day += cur;
rem += diff;
cur += (rem / y[i]);
rem %= y[i];
}
}
diff = cur * (x[i]-y[i]);
}
}
END:;
}
return res;
}
};
// local test entry
int main()
{
vector<int> a,b,c;
int n;
cin>>n;
a.resize(n);
b.resize(n);
c.resize(n);
for(int i=0;i<n;++i) cin>>a[i]>>b[i]>>c[i];
vector<long long> result = CandyAddict().solve(a,b,c);
for(int i=0;i<n;++i) cout<<result[i]<<endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8YWxnb3JpdGhtPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmlubGluZSBsb25nIGxvbmcgcm91bmRfdXBfZGl2KGxvbmcgbG9uZyBhLGxvbmcgbG9uZyBiKXsKICAgICAgIHJldHVybiAoYS0xKS9iICsgMSA7Cn0KCmNsYXNzIENhbmR5QWRkaWN0ewogICAgICAjZGVmaW5lIFJFVFVSTih4KSB7XAogICAgICAgICAgICAgIHJlc1tpXSA9IHg7XAogICAgICAgICAgICAgIGdvdG8gRU5EO1wKICAgICAgICAgICAgICB9CiAgICAgIHB1YmxpYzogdmVjdG9yPGxvbmcgbG9uZz4gc29sdmUodmVjdG9yPGludD4gWCwgdmVjdG9yPGludD4gWSx2ZWN0b3I8aW50PiBaKXsKICAgICAgICAgICAgICBpbnQgbiA9IFguc2l6ZSgpOwogICAgICAgICAgICAgIHZlY3Rvcjxsb25nIGxvbmc+IHJlcyx4LHksejsKICAgICAgICAgICAgICBmb3IoaW50IGk9MDtpPG47KytpKXsKICAgICAgICAgICAgICAgICAgICAgIHgucHVzaF9iYWNrKFhbaV0pOwogICAgICAgICAgICAgICAgICAgICAgeS5wdXNoX2JhY2soWVtpXSk7CiAgICAgICAgICAgICAgICAgICAgICB6LnB1c2hfYmFjayhaW2ldKTsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgcmVzLnJlc2l6ZShuKTsKICAgICAgICAgICAgICBmb3IgKGludCBpPTA7aTxuOysraSl7CiAgICAgICAgICAgICAgICAgIGlmICh4W2ldPT15W2ldKSBSRVRVUk4oMCkKICAgICAgICAgICAgICAgICAgLy9lbHNlIGlmICh6W2ldPT0xKSByZXNbaV0gPSB4W2ldJXlbaV07CiAgICAgICAgICAgICAgICAgIGVsc2V7CiAgICAgICAgICAgICAgICAgICAgICAgICAgIGludCBkYXkgPSAxOwogICAgICAgICAgICAgICAgICAgICAgICAgICBsb25nIGxvbmcgcmVtID0geFtpXSAlIHlbaV07CiAgICAgICAgICAgICAgICAgICAgICAgICAgIGxvbmcgbG9uZyBjdXIgPSB4W2ldIC8geVtpXTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgbG9uZyBsb25nIGRpZmYgPSBjdXIgKiAoeFtpXS15W2ldKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgd2hpbGUoMSl7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChkYXkgPT0geltpXSkgUkVUVVJOKHJlbSkgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAocmVtICsgZGlmZiA8IHlbaV0pewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgbG9uZyBsb25nIHBlcmlvZCA9IHJvdW5kX3VwX2Rpdih5W2ldIC0gcmVtLCBkaWZmKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChkYXkgKyBwZXJpb2QgKiBjdXIgPiB6W2ldKSBSRVRVUk4ocmVtICsgKHpbaV0tZGF5KSAvIGN1ciAqIGRpZmYgKyAoeltpXS1kYXkpICUgY3VyICogeFtpXSkKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGVsc2V7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGRheSArPSBwZXJpb2QgKiBjdXI7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHJlbSA9IChyZW0gKyBwZXJpb2QgKiBkaWZmKSAlIHlbaV07CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGN1cisrOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGVsc2V7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKGRheSArIGN1ciA+IHpbaV0pIFJFVFVSTihyZW0gKyAoeltpXS1kYXkpICogeFtpXSkKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBlbHNlewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBkYXkgKz0gY3VyOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICByZW0gKz0gZGlmZjsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgY3VyICs9IChyZW0gLyB5W2ldKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcmVtICU9IHlbaV07CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBkaWZmID0gY3VyICogKHhbaV0teVtpXSk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgRU5EOjsKICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgcmV0dXJuIHJlczsKICAgICAgfQp9OwoKLy8gbG9jYWwgdGVzdCBlbnRyeQppbnQgbWFpbigpCnsKICAgIHZlY3RvcjxpbnQ+IGEsYixjOwogICAgaW50IG47CiAgICBjaW4+Pm47CiAgICBhLnJlc2l6ZShuKTsKICAgIGIucmVzaXplKG4pOwogICAgYy5yZXNpemUobik7CiAgICBmb3IoaW50IGk9MDtpPG47KytpKSBjaW4+PmFbaV0+PmJbaV0+PmNbaV07CiAgICB2ZWN0b3I8bG9uZyBsb25nPiByZXN1bHQgPSBDYW5keUFkZGljdCgpLnNvbHZlKGEsYixjKTsKICAgIGZvcihpbnQgaT0wO2k8bjsrK2kpIGNvdXQ8PHJlc3VsdFtpXTw8ZW5kbDsKICAgIHJldHVybiAwOwp9Cg==
NQoxMDAwMDAwMDAwIDEgMzQyNTY4MzY4CjEwMDAwMDAwMDAgMiA1NjA0OTY3MzAKMTAwMDAwMDAwMCAzIDU4Njk0NzM5NgoxMDAwMDAwMDAwIDk5OTk5OTk5OCAzODY5Mzc1ODMKMTAwMDAwMDAwMCA5OTk5OTk5OTkgNjA5NDgzNzQ1Cg==
5
1000000000 1 342568368
1000000000 2 560496730
1000000000 3 586947396
1000000000 999999998 386937583
1000000000 999999999 609483745