#pragma GCC optimize("Ofast") #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using ll=long long int; using pii=pair; using pil=pair; using pli=pair; using pll=pair; using psi=pair; using pis=pair; using psl=pair; using pls=pair; using pss=pair; template using vc=vector; template using vvc=vector>; template using vvvc=vector>>; template using vvvvc=vector>; template using vvvvvc=vector>; templateinline constexpr bool chmin(T&a,const U b){if(a<=b)return false;a=b;return true;} templateinline constexpr bool chmax(T&a,const U b){if(a>=b)return false;a=b;return true;} #define bit(n,k) ( (n>>k)&1 ) inline void bin101(){ ios::sync_with_stdio(false); cin.tie(nullptr); cout< inline void Yes(T flag){ if(flag) cout<<"Yes"< inline void YES(T flag){ if(flag) cout<<"YES"< inline void vin1(vector &v){ for(int i=1;i>v[i]; } //0-indexed vector cin template inline void vin0(vector &v){ for(int i=0;i>v[i]; } //1-indexed vector cin template inline void vin1(vector> &v){ for(int i=1;i>v[i][j]; } } //0-indexed vector cin template inline void vin0(vector> &v){ for(int i=0;i>v[i][j]; } } //デバッグ template inline void vout(const vector &v){ cout<<"\nstart\n"; const int sz=v.size(); for(int i=0;i inline void vout(const vvc &v){ cout<<"\nstart\n"; const int sz=v.size(); for(int i=0;i inline void gvout(const vector &v){ cout<<"\nstart\n"; const int sz=v.size(); for(int i=0;i inline void gvout(const vvc &v){ cout<<"\nstart\n"; const int sz=v.size(); for(int i=0;i inline void vout(const vvvc &v){ cout<<"\nstart\n"; const int sz=v.size(); for(int i=0;i inline ostream &operator<<(ostream &os,const pair &p) { os< inline istream &operator>>(istream &is,pair &p) { is>>p.first>>p.second; return is; } //ソート template inline void vsort(vector &v){ sort(v.begin(),v.end()); } //逆順ソート template inline void rvsort(vector &v){ sort(v.rbegin(),v.rend()); } //要素数a 初期値x template inline vector vmake(int a,T x){ return vector(a,x); } //data[a][b] 初期値x template inline vector> vmake(int a,int b,T x){ return vector>(a, vector(b,x)); } //data[a][b][c] 初期値x template inline vector>> vmake(int a,int b,int c,T x){ return vector>> (a, vector>(b, vector(c, x))); } //data[a][b][c][d] 初期値x template inline vector>>> vmake(int a,int b,int c,int d,T x){ return vector>>> (a,vvvc(b,vvc(c,vc(d,x)))); } //data[a][b][c][d][e] 初期値x template inline vvvvvc vmake(int a,int b,int c,int d,int e,T x){ return vvvvvc (a,vvvvc(b,vvvc(c,vvc(d,vc(e,x))))); } //1ビットの数を返す inline int popcount(int x){ return __builtin_popcount(x); } //1ビットの数を返す inline int popcount(ll x){ return __builtin_popcountll(x); } //queのfront() pop() template inline T pop(queue &que){ T x=que.front(); que.pop(); return x; } //priority_que top() pop() template inline T pop(priority_queue &que){ T x=que.top(); que.pop(); return x; } //stack top() pop() template inline T pop(stack &st){ T x=st.top(); st.pop(); return x; } #define SZ(x) ( (int)x.size() ) #define pb push_back /* 満たすものの個数を返す mode:0 x未満 mode:1 x以下の数 mode:2 x以上の数 mode:3 x超 mode:4 x */ template inline int count_bound(vector &v,T x,int mode){ switch(mode){ case 0: return lower_bound(v.begin(),v.end(),x)-v.begin(); case 1: return upper_bound(v.begin(),v.end(),x)-v.begin(); case 2: return v.end()-lower_bound(v.begin(),v.end(),x); case 3: return v.end()-upper_bound(v.begin(),v.end(),x); case 4: return upper_bound(v.begin(),v.end(),x)-lower_bound(v.begin(),v.end(),x); } } /* mode:0 xより小さい数で最大の数 mode:1 x以下の数で最大の数 mode:2 x以上の数で最小の数 mode:3 xより大きい数で最小の数 */ template inline T value_bound(vector &v,T x,int mode){ switch(mode){ case 0: return *(--lower_bound(v.begin(),v.end(),x)); case 1: return *(--upper_bound(v.begin(),v.end(),x)); case 2: return *lower_bound(v.begin(),v.end(),x); case 3: return *upper_bound(v.begin(),v.end(),x); } } constexpr int MAX=1<<30; constexpr ll INF=1LL<<62; constexpr ll MOD=1e9+7; int dx[]={1,-1,0,0},dy[]={0,0,-1,1}; //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ signed main(){ bin101(); int Q; cin>>Q; while(Q--){ ll A,B,C; cin>>A>>B>>C; if(C==1){ cout<<-1< V; while(A!=0){ V.push_back(A%C); A/=C; } reverse(V.begin(),V.end()); ll ret=0; if(V.size()==1){ cout<