#include<iostream>
using namespace std;
#define ll long long
int main(){
	ll t;
	cin>>t;
	while(t--){
		ll n,a,b;
		cin>>n>>a>>b;
		if(a==1){
			if((n-1)%b==0){
				cout<<"Yes"<<endl;
				continue;
			}
			else{
				cout<<"No"<<endl;
			}
		}
		else{
			ll ans=1,f=0;
			while(ans<=n){
				if((n-ans)%b==0){
					cout<<"Yes"<<endl;
					f=1;
					break;
				}	
				ans*=a;
			}
			if(!f)cout<<"No"<<endl;
		}
	}
	return 0;
} 