#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double

int main(void) {
    int t;
    cin >> t;
    while (t-- > 0) {
        ll n, a, b;
        cin >> n >> a >> b;
        ll x = 1;
        int flag = 0;
        if (a == 1) {
            if ((n-1)%b == 0) flag = 1;
        } else {
            while (x <= n) {
                if (x % b == n % b) flag = 1;
                x *= a;
            }
        }
        if (flag) cout << "Yes";
        else cout << "No";
        cout << endl;
    }
}