#include<bits/stdc++.h>
using namespace std;
inline int P_th_power(int N, int M, int P) {
using ivector = vector<int>;
using imatrix = vector<ivector>;
struct dp_tensor: array<imatrix,2> {
struct powers: ivector {
inline powers(const int p) {
long long y, inf = 1e9;
for (int a = 0; y = pow(a,p), y < inf; ++a)
push_back(y); }
inline void generate(const int N, imatrix &dp) const {
const int M = 1e5, null = -1;
dp.resize(N+1,ivector(M+1,null)), dp[0][0] = 0;
for (int n = 0; n <= N; ++n)
for (int m = 0; m <= M; ++m)
if (dp[n][m] != null)
for (int a = 1, b = n+1; b <= N; ++a, ++b) {
const int c = m+at(a);
if (c <= M) {
const int ans = dp[b][c], k = dp[n][m]+1;
if (ans == null or k < ans)
dp[b][c] = k; }
else
break; } } };
inline dp_tensor() {
powers(2).generate(100,at(0));
powers(3).generate( 40,at(1)); } };
static const dp_tensor dp;
return dp[P-2][N][M]; }
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin.exceptions(cin.failbit);
int T; cin >> T;
for(int N, M, P; T--; cout << P_th_power(N,M,P) << '\n')
cin >> N >> M >> P;
return 0; }
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmlubGluZSBpbnQgUF90aF9wb3dlcihpbnQgTiwgaW50IE0sIGludCBQKSB7Cgl1c2luZyBpdmVjdG9yID0gdmVjdG9yPGludD47Cgl1c2luZyBpbWF0cml4ID0gdmVjdG9yPGl2ZWN0b3I+OwoJc3RydWN0IGRwX3RlbnNvcjogYXJyYXk8aW1hdHJpeCwyPiB7CgkJc3RydWN0IHBvd2VyczogaXZlY3RvciB7CiAgICAJCWlubGluZSBwb3dlcnMoY29uc3QgaW50IHApIHsKICAgIAkJCWxvbmcgbG9uZyB5LCBpbmYgPSAxZTk7CiAgICAgICAgCQlmb3IgKGludCBhID0gMDsgeSA9IHBvdyhhLHApLCB5IDwgaW5mOyArK2EpCiAgICAgICAgICAgIAkJcHVzaF9iYWNrKHkpOyB9IAoJC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