#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i,a,b) for(ll i=a;i<b;i++)
#define frev(i,n,b)  for(ll i=n-1;i>=b;i--)
#define fi first
#define sec second
#define pb(x) push_back(x);
#define mod 1000000007
#define PI 3.141592653
typedef vector<ll> vi;
typedef pair<ll,ll> pii;
typedef map<ll,ll> mii;
typedef unordered_map<ll,ll> umii;
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif    

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

        ll m=1;
        bool key=0;
        while(m<=n){
            if((n-m)%b==0) key=1;
                m*=a;
                if(a==1) break;
        }
        key? cout<<"Yes\n":cout<<"No\n";
    }
    
    return 0;
}