#include <bits/stdc++.h>
using namespace std;
#define int long long int
const int mod = 1000000007;
const int N = 100005;
const int M = 200005;

void run() 
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}

void solve()
{
	int n, a, b;

	cin>>n>>a>>b;

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

		return;
	}

	if((n-1)%b == 0 || b == 1)
	{
		cout<<"Yes\n";
		return;
	}

	int m = 1;

	while(m <= n)
	{
		if(m%b == n%b)
		{
			cout<<"Yes\n";
			return;
		}
		m = m*a;
	}

	cout<<"No\n";
}

signed main()
{
	run();

	int q;
	cin>>q;

	while(q--)
	{
		solve();
	}

	return 0;
}