#include <bits/stdc++.h>
using namespace std;
#define in ({int x=0;int c=getchar(),n=0;for(;!isdigit(c);c=getchar()) n=(c=='-');for(;isdigit(c);c=getchar()) x=x*10+c-'0';n?-x:x;})
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
long long rnd(long long l,long long r) {return l+rng()%(r-l+1);}
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << " = " << a << endl;
	err(++it, args...);
}
#define debug(x) cerr << #x << " is " << x << "\n";
#define fasty ios_base::sync_with_stdio(0),cin.tie(0);
#define forinc(a,b,c) for(int a=b,_c=c;a<=_c;++a)
#define fordec(a,b,c) for(int a=b,_c=c;a>=_c;--a)
#define forv(a,b) for(auto&a:b)
#define fi first
#define se second
#define pb push_back
#define ii pair<int,int>
#define mt make_tuple
#define all(a) a.begin(),a.end()
#define reset(f, x) memset(f, x, sizeof(f))
#define gg exit(0)
#define int long long
#define yn(x) cout << ((x)?"YES":"NO")
signed main()
{
    int t; cin >> t;
    while(t--)
    {
        int n,a,b; cin >> n >> a >> b;
        if(a==1) yn((n-1)%b==0);
        else
        {
            int k=1; bool ok=false;
            while(k<=n)
            {
                if((n-k)%b==0) ok=true;
                if(n/k<a) break;
                k*=a;
            }
            yn(ok);
        }
        cout << "\n";
    }
}

