/*@author piyush0120*/
/*check whether it has test_cases*/
/*'a'-96 gives 1, similarly '1'-0 gives 1*/
#define pb push_back
#define F first
#define S second
#define lb lower_bound
#define ub upper_bound
#define MP make_pair
#define tos to_string
#define ll long long
#define ull unsigned long long
#define len(a) a.size()
#define PI pair<int,int>
#define MM(a , x) memset(a , x , sizeof(a))
#define ign ignore(numeric_limits<streamsize>::max(), '\n')
#define forl(i,b,n) for(int i=b;i<n;i++)
#define testc int t;cin>>t;while(t--)
#define maxN 1000001
#define MOD 1000000007
#include <bits/stdc++.h>
using namespace std;
using namespace chrono;
int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a);}
bool sortByVal(const pair<int, int> &a, const pair<int, int> &b) {return (a.second < b.second);/*sort(v.begin(), v.end(), sortByVal);*/}
/*****************/
void solve()
{
	ll n, a, b; cin >> n >> a >> b;
	if ((n - 1) % b == 0)
	{
		cout << "Yes\n"; return;
	}
	if (a == 1)
	{
		cout << "No\n"; return;
	}
	ll x = 1;
	while (x <= n)
	{
		if ((n - x) % b == 0)
		{
			//cout << x << "," << n - x << "\n";
			cout << "Yes\n"; return;
		}
		x *= a;
	}
	cout << "No\n";
	return;
}

int main()
{

#ifndef ONLINE_JUDGE

	freopen("input2.txt", "r", stdin);
	freopen("output2.txt", "w", stdout);
	freopen("error.txt", "w", stderr);
#endif

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cout << fixed;
	cout << setprecision(10);
	auto start = high_resolution_clock::now();
	testc
	solve();
	auto stop = high_resolution_clock::now();
	auto duration = duration_cast<chrono::microseconds>(stop - start);

#ifndef ONLINE_JUDGE
	cerr << "Time: " << duration.count() / 1000 << endl;
#endif
}
/*!-Solve A & B quickly with less submissions-!*/
/*!--Reach -->Pupil--!*/