#include <iostream> #define ULL unsigned long long
bool Solve() { ULL n, x; std::cin >> n >> x;
if (x == 1)
return (n & (n - 1)) == 0;
ULL S = 0, tmp = n;
while (tmp)
{
S += tmp % x;
tmp /= x;
}
ULL d = x - 1;
for (ULL p = 1; p <= n; p <<= 1)
if (p >= S && (p - S) % d == 0)
return true;
return false;}
int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr);
int T;
std::cin >> T;
while (T--)
std::cout << (Solve() ? "Yes\n" : "No\n");
return 0;}