#include<iostream>
#include<cstdio>
using namespace std;
typedef long long ll;
ll re() {
	char c = getchar(); ll all = 0, pd = 1;
	for (; c > '9' || c < '0'; c = getchar()) if (c == '-') pd = -1;
	while (c >= '0' && c <= '9') all = all * 10 + c - '0', c = getchar();
	return all * pd;
}
ll judge(ll a, ll b, ll n,ll t) {
	if(a == 1) return (n-1)%b == 0 ? 1 :0;
	if(n<t) return 0;
	ll ans =0;
	if((n-t)%b == 0) ans = 1;
	return max(judge(a,b,n,t*a),ans);
	
}
int main() {
	ll T = re();
	while (T--) {
		ll n = re(), a = re(), b = re();
		if (judge(a, b, n,1)) printf("Yes\n");
		else printf("No\n");
	}
}
