#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define rep(i,a) for(int i=(int)0;i<(int)a;++i) #define pb push_back #define eb emplace_back using ll=long long; constexpr ll mod = 1e9 + 7; constexpr ll INF = 1LL << 50; template inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } using namespace std; template< typename T > vector< T > convert_base(T x, T b) { vector< T > ret; T t = 1, k = abs(b); while(x) { ret.emplace_back((x * t) % k); if(ret.back() < 0) ret.back() += k; x -= ret.back() * t; x /= k; t *= b / k; } if(ret.empty()) ret.emplace_back(0); reverse(begin(ret), end(ret)); return ret; } void solve(){ int q; cin>>q; while(q--){ ll a,b,c,ans=0; ll x=0,y=0; cin>>a>>b>>c; if(c==1){ cout<<-1<<"\n"; continue; } vectorv=convert_base(a,c); ll n=v.size(); if(n==1)ans=1; else { x=c*v.front()+v[1]; x=(x+c-2)/(c-1); rep(i,2){ if(v[i])y+=(2-i); } for(int i=2;i