#include<bits/stdc++.h>
using namespace std;
int P_th_power (int n, int m, int p) {
// Write your code here
vector<vector<int> > dp(n + 1, vector<int>(n + 1, -1));
vector<int> power(n + 1);
power[1] = 1;
for(int i = 2; i <= n; ++i) {
power[i] = 1;
for(int j = 1; j <= p; ++j) {
power[i] *= i;
}
}
auto func = [&](int cur, int n, int m, auto& func) -> int {
if(n == 0 and m == 0) {
return 0;
}
if((cur > n) or (n > m) or (power[cur] > m)) {
return 1e9;
}
int& val = dp[n][cur];
if(val != -1) {
return val;
}
// not include
val = func(cur + 1, n, m, func);
int cur_n = n, cur_m = m;
auto isValid = [&](int n, int m, int val) -> bool {
int term_a = val;
int term_b = power[val];
return term_a <= n and term_b <= m;
};
// number of times to include 'i' in sequence
int times = 0;
while(isValid(cur_n, cur_m, cur)) {
++times;
cur_n -= cur;
cur_m -= power[cur];
val = min(val, times + func(cur + 1, cur_n, cur_m, func));
}
return val;
};
int ans = func(1, n, m, func);
if(ans == 1e9) ans = -1;
return ans;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T;
for(int t_i = 0; t_i < T; t_i++)
{
int N;
cin >> N;
int M;
cin >> M;
int P;
cin >> P;
int out_;
out_ = P_th_power(N, M, P);
cout << out_;
cout << "\n";
}
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmludCBQX3RoX3Bvd2VyIChpbnQgbiwgaW50IG0sIGludCBwKSB7CiAgIC8vIFdyaXRlIHlvdXIgY29kZSBoZXJlCiAgIHZlY3Rvcjx2ZWN0b3I8aW50PiA+IGRwKG4gKyAxLCB2ZWN0b3I8aW50PihuICsgMSwgLTEpKTsKICAgdmVjdG9yPGludD4gcG93ZXIobiArIDEpOwogICBwb3dlclsxXSA9IDE7CiAgIGZvcihpbnQgaSA9IDI7IGkgPD0gbjsgKytpKSB7CiAgICAgICBwb3dlcltpXSA9IDE7CiAgICAgICBmb3IoaW50IGogPSAxOyBqIDw9IHA7ICsraikgewogICAgICAgICAgIHBvd2VyW2ldICo9IGk7CiAgICAgICB9CiAgIH0KCiAgIGF1dG8gZnVuYyA9IFsmXShpbnQgY3VyLCBpbnQgbiwgaW50IG0sIGF1dG8mIGZ1bmMpIC0+IGludCB7CiAgICAgICBpZihuID09IDAgYW5kIG0gPT0gMCkgewogICAgICAgICAgIHJldHVybiAwOwogICAgICAgfQoKICAgICAgIGlmKChjdXIgPiBuKSBvciAobiA+IG0pIG9yIChwb3dlcltjdXJdID4gbSkpIHsKICAgICAgICAgICByZXR1cm4gMWU5OwogICAgICAgfQogICAgICAgaW50JiB2YWwgPSBkcFtuXVtjdXJdOwogICAgICAgaWYodmFsICE9IC0xKSB7CiAgICAgICAgICAgcmV0dXJuIHZhbDsKICAgICAgIH0KICAgICAgIAogICAgICAgLy8gbm90IGluY2x1ZGUKICAgICAgIHZhbCA9IGZ1bmMoY3VyICsgMSwgbiwgbSwgZnVuYyk7CgogICAgICAgaW50IGN1cl9uID0gbiwgY3VyX20gPSBtOwogICAgICAgYXV0byBpc1ZhbGlkID0gWyZdKGludCBuLCBpbnQgbSwgaW50IHZhbCkgLT4gYm9vbCB7CiAgICAgICAgICAgaW50IHRlcm1fYSA9IHZhbDsKICAgICAgICAgICBpbnQgdGVybV9iID0gcG93ZXJbdmFsXTsKICAgICAgICAgICByZXR1cm4gdGVybV9hIDw9IG4gYW5kIHRlcm1fYiA8PSBtOwogICAgICAgfTsKICAgICAgIAogICAgICAvLyBudW1iZXIgb2YgdGltZXMgdG8gaW5jbHVkZSAnaScgaW4gc2VxdWVuY2UKICAgICAgIGludCB0aW1lcyA9IDA7CiAgICAgICB3aGlsZShpc1ZhbGlkKGN1cl9uLCBjdXJfbSwgY3VyKSkgewogICAgICAgICAgICsrdGltZXM7CiAgICAgICAgICAgY3VyX24gLT0gY3VyOwogICAgICAgICAgIGN1cl9tIC09IHBvd2VyW2N1cl07CiAgICAgICAgICAgdmFsID0gbWluKHZhbCwgdGltZXMgKyBmdW5jKGN1ciArIDEsIGN1cl9uLCBjdXJfbSwgZnVuYykpOwogICAgICAgfQoKICAgICAgIHJldHVybiB2YWw7CiAgIH07CiAgIGludCBhbnMgPSBmdW5jKDEsIG4sIG0sIGZ1bmMpOwogICBpZihhbnMgPT0gMWU5KSBhbnMgPSAtMTsKICAgcmV0dXJuIGFuczsKfQoKaW50IG1haW4oKSB7CgogICAgaW9zOjpzeW5jX3dpdGhfc3RkaW8oMCk7CiAgICBjaW4udGllKDApOwogICAgaW50IFQ7CiAgICBjaW4gPj4gVDsKICAgIGZvcihpbnQgdF9pID0gMDsgdF9pIDwgVDsgdF9pKyspCiAgICB7CiAgICAgICAgaW50IE47CiAgICAgICAgY2luID4+IE47CiAgICAgICAgaW50IE07CiAgICAgICAgY2luID4+IE07CiAgICAgICAgaW50IFA7CiAgICAgICAgY2luID4+IFA7CgogICAgICAgIGludCBvdXRfOwogICAgICAgIG91dF8gPSBQX3RoX3Bvd2VyKE4sIE0sIFApOwogICAgICAgIGNvdXQgPDwgb3V0XzsKICAgICAgICBjb3V0IDw8ICJcbiI7CiAgICB9Cn0=