#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll mod = 1e9 + 7;
const ll maxn = 1e6 + 10;
ll t, a, b, n;

int main()
{
#ifndef ONLINE_JUDGE
	freopen( "1.in", "r", stdin );
	freopen( "1.out", "w", stdout );
#endif
	cin >> t;
	while (t--) {
		cin >> n >> a >> b;

		if ((n - 1) % b == 0) {
			cout << "Yes" << endl;
		}
		else if (a == 1) cout << "No" << endl;

		else {
			ll te = a;
			bool sy = 0;
			while (te <= 1000000000ll) {
				if (n - te == 0) {
					sy = 1;
					break;
				}
				else if (n - te < 0)
					break;
				if ((n - te) % b == 0)
				{
					sy = 1;
					break;
				}
				te *= a;

			}

			if (sy)
				cout << "Yes" << endl;
			else cout << "No" << endl;
		}
	}


	return 0;
}

