#include <bits/stdc++.h>
using namespace std;
#define int long long
int n,a,b;
signed main()
{
	int t; cin >> t;
	while( t-- )
	{
		cin >> n >> a >> b;
		if( a==1 )	
		{
			if( (n-1)%b==0 )	cout << "YES\n";
			else	cout << "No\n";
			continue;
		}
		int az = 1, flag = 0;
		for(int i=1;;i++)
		{
			if( az>n )	break;
			if( (n-az)%b==0 )	flag = 1;
			az = az*a;
		}
		if( flag )	cout << "Yes\n";
		else	cout << "No\n";
	}
}
