#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<queue>
#include<cstdio>
#include<deque>
//#include<map>
#include<unordered_map>
#include<set>  //multiset
#include<stack>
#include<algorithm>
#include<sstream>
#include<bitset>
//#include<regex>
using namespace std;
typedef long long ll;
typedef unsigned long long ULL;
#define int long long
typedef pair<int, int> PII;
const double PI = 3.141592653589793;
const int INF = 1e18;
const int mod = 1e9 + 7;
const int N = 1e6 + 100, M = 110;

signed main()
{
	int t; cin >> t;
	int n, a, b;
	while (t--) {
		cin >> n >> a >> b;
		int f = 1;
		if (a == 1) {
			if ((n - 1) % b == 0) cout << "Yes" << endl;
			else cout << "No" << endl;
			continue;
		}
		for (int i = 1; i <= n; i *= a) {
			if ((n - i) % b == 0) {
				f = 0;
				cout << "Yes" << endl;
				break;
			}
		}
		if (f) cout << "No" << endl;
	}
	return 0;
}