#include<bits/stdc++.h>
using namespace std; 
int main()
{
int t; cin>>t;  while(t--){
long long n,a,b,x=1,c=0; cin>>n>>a>>b; 
while(x<=n){
if((n-x)%b==0) c=1;
    x*=a;
    if(a==1) break;
}
if(c) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
return 0; 
}
 