#include <bits/stdc++.h>
using namespace std;
#define io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) 

typedef long long ll;
#define mod (ll)1000000007
int main() {
io;
ll t,n;
cin>>t;
while (t--){ll a,b;
     cin>>n>>a>>b;
     if(b==1){cout<<"Yes"<<endl;}
     else if((n-1)%b==0){cout<<"Yes"<<endl;}
     else if(a==1){cout<<"No"<<endl;}
     else{
         ll q=1; bool p=false;
         while(q<=n && p==false){
             if((n-q)%b==0){p=true;}
             else{q*=a;}
         }
         p? cout<<"Yes"<<endl:cout<<"No"<<endl;
     }
     
     
      
     
    }
     return 0;
}