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

#define fl(i,a,b) for(int i=a; i<b; i++)
#define v vector<int>
#define ss string
#define p pair
#define pi p<int,int>
#define ll long long int


int main(){
    int t;
    cin>>t;
    while(t--){
        ll n,a,b;
        cin>>n>>a>>b;

        if(b==1){
            cout<<"Yes\n";
        }
        else if(a==1){
            if((n-1)%b==0) cout<<"Yes\n";
            else cout<<"No\n";
        }
        else{
            vector<ll> vec;
            vec.push_back(1);
            for(ll i=0; i<vec.size(); i++){
                if(vec[i]*a<= 1e9){
                    vec.push_back(vec[i]*a);
                }
            }
            bool flag=0;
            for(auto val : vec){
                if(n>= val && (n- val)%b==0){
                    flag=1;
                    cout<<"Yes\n";
                    break;
                }
            }
            if(!flag){
                cout<<"No\n";
            }

        }
    }

    return 0;
}