#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    long long int t;
    cin>>t;
    while(t--)
    {
        long long int n,a,b;
        cin>>n>>a>>b;
        int temp=0;
        if(n==1)
        cout<<"YES"<<"\n";
        else if(a==1)
        {
            if((n-1)%b==0)
            cout<<"Yes"<<"\n";
            else
            cout<<"No"<<"\n";
        }
        else
        {
            long long int cnt=1;
            while(n-cnt>=0)
            {
                
                if((n-cnt)%b==0)
                {
                cout<<"Yes"<<"\n";
                temp=1;
                break;
                }
                cnt=cnt*a;
            }
            if(temp==0)
            cout<<"No"<<"\n";
        }
    }
    return 0;
}