#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<utility>
#include<set>
#include<stack>
#include<list>
#include<deque>
#include<bitset>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<cstdlib>
#include<climits>
#include<cmath>
#include<cctype>
#define pb push_back
#define mp make_pair
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define ren(i,a,b) for(int i=a;i>=b;i--)
#define ff first
#define ss second
#define pll pair<long long int,long long int>
#define pii pair<int,int>
#define vll vector<long long int>
#define vii vector<int>
#define gi(n) scanf("%d",&n)
#define gll(n) scanf("%lld",&n)
#define gstr(n) scanf("%s",n)
#define gl(n) cin >> n
#define oi(n) printf("%d",n)
#define oll(n) printf("%lld",n)
#define ostr(n) printf("%s",n)
#define ol(n) cout << n
#define os cout<<" "
#define on cout<<"\n"
#define o2(a,b) cout<<a<<" "<<b
#define all(n) n.begin(),n.end()
#define present(s,x) (s.find(x) != s.end())
#define cpresent(s,x) (find(all(s),x) != s.end())
#define tr(container, it) for(__typeof(container.begin()) it = container.begin(); it != container.end(); it++)
using namespace std;
typedef unsigned long long int ll;
//typedef long long int ll;
typedef vector<vector<ll> > mat;
bool of(ll a,ll b)
{
if(((a*b)/b)!=a)return true;
return false;
}
vector<ll> v[15],v1;
int main()
{ios_base::sync_with_stdio(false);
//freopen("output.txt","w",stdout);
int t;
gl(t);
ll x=2;
while(1)
{
v[2].pb(x);
if(of(x,2))
break;
x*=2;
}
tr(v[2],it)v1.pb(*it);
x=3;
while(1)
{
v[3].pb(x);
tr(v1,it)
{
ll y=*it;
if(of(y,x))break;
v[3].pb(y*x);
}
if(of(x,3))break;
x*=3;
}
tr(v[3],it)v1.pb(*it);
sort(all(v1));
x=5;
while(1)
{
v[5].pb(x);
tr(v1,it)
{
ll y=*it;
if(of(y,x))break;
v[5].pb(y*x);
}
if(of(x,5))break;
x*=5;
}
tr(v[5],it)v1.pb(*it);
sort(all(v1));
x=7;
while(1)
{
v[7].pb(x);
tr(v1,it)
{
ll y=*it;
if(of(y,x))break;
v[7].pb(y*x);
}
if(of(x,7))break;
x*=7;
}
tr(v[7],it)v1.pb(*it);
sort(all(v1));
x=11;
while(1)
{
v[11].pb(x);
tr(v1,it)
{
ll y=*it;
if(of(y,x))break;
v[11].pb(y*x);
}
if(of(x,11))break;
x*=11;
}
tr(v[11],it)v1.pb(*it);
sort(all(v1));
sort(all(v[2]));
sort(all(v[3]));
sort(all(v[5]));
sort(all(v[7]));
sort(all(v[11]));
//ol(v[2].size());on;
//ol(v[3].size());on;
//ol(v[5].size());on;
//ol(v[7].size());on;
//ol(v[11].size());on;
while(t--)
{
ll a,k,n;
cin>>a>>k>>n;
if(n==0)
{
ol(a);on;
}
else
{
int idx=lower_bound(all(v[k]),a)-v[k].begin();
//ol(v[k][idx+1]);on;
ol(v[k][idx+n-1]);on;
}
}
return 0;
}
I2luY2x1ZGU8aW9zdHJlYW0+CiNpbmNsdWRlPGFsZ29yaXRobT4KI2luY2x1ZGU8dmVjdG9yPgojaW5jbHVkZTxxdWV1ZT4KI2luY2x1ZGU8bWFwPgojaW5jbHVkZTx1dGlsaXR5PgojaW5jbHVkZTxzZXQ+CiNpbmNsdWRlPHN0YWNrPgojaW5jbHVkZTxsaXN0PgojaW5jbHVkZTxkZXF1ZT4KI2luY2x1ZGU8Yml0c2V0PgojaW5jbHVkZTxpb21hbmlwPgojaW5jbHVkZTxjc3RyaW5nPgojaW5jbHVkZTxzc3RyZWFtPgojaW5jbHVkZTxjc3RkaW8+CiNpbmNsdWRlPGNzdGRsaWI+CiNpbmNsdWRlPGNsaW1pdHM+CiNpbmNsdWRlPGNtYXRoPgojaW5jbHVkZTxjY3R5cGU+CgoKI2RlZmluZSBwYiBwdXNoX2JhY2sKI2RlZmluZSBtcCBtYWtlX3BhaXIKI2RlZmluZSByZXAoaSxhLGIpIGZvcihpbnQgaT1hO2k8PWI7aSsrKQojZGVmaW5lIHJlbihpLGEsYikgZm9yKGludCBpPWE7aT49YjtpLS0pCi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