#include<bits/stdc++.h>
using namespace std;
#define int long long 
#define F first
#define S second
int p = 1e9+7;
#define INF 1e18;

void solve(){
    int n,a,b;
    cin>>n>>a>>b;
    if(a==1){
        if((n-1)%b==0)cout<<"Yes\n";
        else cout<<"No\n";
    }
    else{
        int temp=1;
        int flag=0;
        while(temp<=n){
            if((n-temp)%b==0)flag=1;
            temp*=a;
        }
        if(flag)cout<<"Yes\n";
        else cout<<"No\n";
    }
    
}
signed main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t;
    cin>>t;
    while(t--)
    solve();
}