#include<bits/stdc++.h>
using namespace std;
#define ll long long 
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    int t;
    cin>>t;
    for(int i=0;i<t;i++)
    {
        ll n,a,b;
        cin>>n>>a>>b;
        if(n==1)
        {
            cout<<"Yes\n";
            continue;
        }
        if(a==1)
        {
            if((n-1)%b==0)
            {
                cout<<"Yes\n";
            }
            else
            {
                cout<<"No\n";
            }
        }
        else
        {
            if((n-1)%b==0)
            {
                cout<<"Yes\n";
                continue;
            }
            bool x = 0;
            ll z = 1;
            while(z*a<=n)
            {
                ll y = n-z*a;
                if(y%b==0)
                {
                    x = 1;
                    break;
                }
                z = z*a;
            }
            if(x)
            {
                cout<<"Yes\n";
            }
            else
            {
                cout<<"No\n";
            }
        }
    }
    return 0;
}