CF1982B
#include <bits/stdc++.h>
#define int long long
using namespace std;
int t;
int x, y, k;
signed main()
{
cin >> t;
while(t--)
{
cin >> x >> y >> k;
while(k > 0 && x != 1)
{
int cnt = (x + y - 1) / y * y;
k -= cnt - x;
x = cnt;
while (x % y == 0) x /= y;
}
cout << x << '\n';
}
return 0;
}