#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;

int main(){
    ios::sync_with_stdio(false);
    cin.tie();
    cout.tie();
    ll t;
    cin>>t;
    while(t-- !=0){
        ll n,a,b;
        cin>>n>>a>>b;
        if(a==1){
            if((n-1)%b==0){
                cout<<"Yes"<<endl;
            }
            else{
                cout<<"No"<<endl;
            }
        }
        else{
            ll z=1;
            ll ans=0;
            while(z<=n){
                if((n-z)%b==0){
                    ans++;
                    break;
                }
                z=z*a;
            }
            if(ans==1){
                cout<<"Yes"<<endl;
            }
            else{
                cout<<"No"<<endl;
            }
        }
    }
}