#include <bits/stdc++.h>
using namespace std;
#define int __int128
int x,a,b,cases;
int read(){
int sum = 0;
char ch = getchar();
while(!isdigit(ch)) ch = getchar();
while(isdigit(ch)){
sum = sum * 10 + ch - '0';
ch = getchar();
}
return sum;
}
int sta[41];
void write(int x){
if(x < 0){
putchar('-');
x = -x;
}
int top = 0;
do{
sta[top ++] = x % 10;
x /= 10;
}while(x);
while(top) putchar(sta[-- top] + 48);
puts("");
}
signed main(){
cases = read();
while(cases --){
x = read();
a = read();
b = read();
if(x == 0){
write(-b);
continue;
}
if(a == 1){
if(b == 0) write(x);
else write((x % b) - b);
}
else if(a == 0){
write(-b);
}
else{
int c = b / (a - 1);
if(x < c){
int y = x - c;
int k = c / (- y);
int ans = 1;
while(ans <= k){
ans *= a;
}
if(ans == k) ans *= a;
write(ans * y + c);
}
else write(x);
}
}
return 0;
}