#include <bits/stdc++.h>
using namespace std;

#define ll long long 
#define pb push_back

void solve(){
	ll int n , a , b;
	cin >> n >> a >> b;

	//set<int> s;
	//set generate ki ni ki TLE aa jaega so don't use it here
	//even creating a set is of no use as this is a infinite set
	//brute force approach nahi chalega yha 
  
  //n=a^x+y*b
  if(a==1){
    if((n-1)%b==0){
      cout<<"Yes";
    } 
    else cout<<"No";
  }
  else{
    bool found=false;
    ll int term=1;
    while(term<=n){
      if((n-term)%b==0){
        cout<<"Yes";
        found=true;
        break;
      }
      term*=a;
    }
    if(!found)cout<<"No";
  }   
  cout<<endl;
}
int32_t main(){
      ios_base::sync_with_stdio(0);
      cin.tie(0);
      // srand(time(0));
      // fun(100000000);
      // return 0;
      ll int t;
      cin>>t;
      //   t=1;
      while(t--){
        // cout<<"Case #"<<tt<<": ";
            solve();
            // tt++;
      }
      return 0;
}