#include <bits/stdc++.h>
using namespace std;
// #define ll long long
#define int long long int
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define fo(i, n) for (int i = 0; i < n; i++)
#define Fo(i, k, n) for (int i = k; k < n ? i < n : i > n; k < n ? i += 1 : i -= 1)
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<int, pair<int, int>> pipii;
typedef priority_queue<int, vector<int>, greater<int>> minpq;
typedef priority_queue<int> maxpq;
//const double PI = acos(-1);
const int mod = 1000000007;
const int inf = 1e15;
#define endl "\n";

// ----------------------DEBUG TEMPLATE--------------------------
void __print(int x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V>
void __print(const pair<T, V> &x)
{
    cerr << '{';
    __print(x.first);
    cerr << ',';
    __print(x.second);
    cerr << '}';
}
template <typename T>
void __print(const T &x)
{
    int f = 0;
    cerr << '{';
    for (auto &i : x)
        cerr << (f++ ? "," : ""), __print(i);
    cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v)
{
    __print(t);
    if (sizeof...(v))
        cerr << ", ";
    _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)               \
    cerr << "[" << #x << "] = ["; \
    _print(x)
#else
#define debug(x...)
#endif
// =========================DEBUG TEMPLATE============================

void tc()
{
    int n,a,b;
    cin>>n>>a>>b;
    if(b==1){
        cout<<"Yes"<<endl;
        return;
    }

    if(a==1){
        if((n-1)%b){
            cout<<"NO"<<endl;
        }
        else{
            cout<<"YES"<<endl;
        }
        return;
    }
    vi powers;
    int temp = 1;
    while(temp <= n){
        powers.pb(temp);
        temp*=a;
    }

    fo(i,powers.size()){
        int val = n - powers[i];
        if(val%b){

        }
        else{
            cout<<"YES"<<endl;
            return;
        }
    }
    cout<<"NO"<<endl;

}
int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int t = 1;
    cin >> t;
    while (t--)
    {
        tc();
    }
}
