#include <bits/stdc++.h>
using namespace std;
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define PI 3.1415926535897932384626433832795
#define mod 1000000007
#define F first
#define S second
#define MOD 998244353
#define aLL(str) (str).begin(), (str).end()
#define IOS                  \
	ios::sync_with_stdio(0); \
	cin.tie(0);              \
	cout.tie(0)
#define ll long long int
typedef vector<ll> vi;
typedef vector<pair<ll,ll>> vii;
typedef vector<string> vs;
typedef vector<vector<ll>> vvi;
bool comp(pair<pair<ll,ll>,ll> a,pair<pair<ll,ll>,ll> b)
{
	if(a.S==b.S)
		return a.F.F<b.F.F;
	else
		return a.S<b.S;
}

int main()
{
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	IOS;
	int t=1;
	cin>>t;
	while(t--)
	{ 
	ll n,a,b;
	cin>>n>>a>>b;
	if(b==1)
	{
		cout<<"Yes"<<endl;
		continue;
	}
	else if(a==1)
	{
		if((n-1)%b==0)
			cout<<"Yes"<<endl;
		else
			cout<<"No"<<endl;
		continue;
	}
	ll m=1,f=0;
	while(m<n)
	{
		if(m%b==n%b)
		{
			f=1;
			break;
		}
		m=m*a;
	}
	if(f==1 || m==n)
		cout<<"Yes"<<endl;
	else
		cout<<"No"<<endl;
}
}