Problem: There is an infinite set generated as follows:

  * 1 is in this set. 
  * If x is in this set, x ⋅ a and x+b both are in this set. 



For example, when a=3 and b=6, the five smallest elements of the set are:

  * 1, 
  * 3 (1 is in this set, so 1⋅ a=3 is in this set), 
  * 7 (1 is in this set, so 1+b=7 is in this set), 
  * 9 (3 is in this set, so 3⋅ a=9 is in this set), 
  * 13 (7 is in this set, so 7+b=13 is in this set). 



Given positive integers a, b, n, determine if n is in this set.

Input

The input consists of multiple test cases. The first line contains an integer t (1≤ t≤ 10^5) — the number of test cases. The description of the test cases follows.

The only line describing each test case contains three integers n, a, b (1≤ n,a,b≤ 10^9) separated by a single space.

Output

For each test case, print "Yes" if n is in this set, and "No" otherwise. You can print each letter in any case.

Example

Input


5
24 3 5
10 3 6
2345 1 4
19260817 394 485
19260817 233 264


Output


Yes
No
Yes
No
Yes

Note

In the first test case, 24 is generated as follows:

  * 1 is in this set, so 3 and 6 are in this set; 
  * 3 is in this set, so 9 and 8 are in this set; 
  * 8 is in this set, so 24 and 13 are in this set. 



Thus we can see 24 is in this set.

The five smallest elements of the set in the second test case is described in statements. We can see that 10 isn't among them.

Solution (in JAVA):

Solution:
//WHEN IN DOUBT , USE BRUTE FORCE !!!!!!!!!
//https://www.geeksforgeeks.org/count-pairs-with-given-sum/
import java.io.*;
import java.util.*;
//class Main    //AtCoder
//class Solution // Codechef
public class Solution2 //Codeforces
{
    public static void main(String args[])
    {
        try {
            FastReader sc = new FastReader();
            int TT = sc.nextInt();
            for(int hehe=1 ; hehe  <= TT ; hehe++) {
                long N=sc.nextLong();
                long a=sc.nextLong();
                long b=sc.nextLong();
                if(b==1){
                    sopln("YES");
                    continue;
                }
                else if(a==1){
                    if((N-1)%b==0){
                        sopln("YES");
                    }else{
                        sopln("NO");
                    }
                    continue;
                }
                else if(check2(N,a,b)){
                    sopln("YES");
                }
                else
                    sopln("NO");
            }
            out.flush();
        }catch(Exception e){
            sopln(e.getCause());
        }
    }

    private static boolean check2(long n, long a, long b) {
        long aa=1;
        while(aa <= n){
            if((n-aa)%b == 0)
                return true;
            aa=aa*a;
            //sopln("INF");
        }
        return false;
    }

    private static boolean check1(long n, long a, long b) {
        while(n%a == 0){
            n/=a;
            if((n-1)%b == 0)
                return true;
        }
        return (n==1) || (((n-1)%b)==0);
    }

    static long countDigits(long num){
        if(num < 10)return 1l;
        else if(num < 100)return 2l;
        else if(num < 1000)return 3l;
        else if(num < 10000)return 4l;
        else if(num < 100000)return 5l;
        else if(num < 1000000)return 6l;
        else if(num < 10000000)return 7l;
        else if(num < 100000000)return 8l;
        else if(num < 1000000000)return 9l;
        else if(num < 10000000000l)return 10l;
        else if(num < 100000000000l)return 11l;
        else if(num < 1000000000000l)return 12l;
        else if(num < 10000000000000l)return 13l;
        else if(num < 100000000000000l)return 14l;
        else if(num < 1000000000000000l)return 15l;
        else if(num < 10000000000000000l)return 16l;
        else if(num < 100000000000000000l)return 17l;
        else if(num < 1000000000000000000l)return 18l;
        else
            return 19l;
    }

    static void ruffleSort(int[] a) {
        ArrayList<Integer> l=new ArrayList<>();
        for (int i:a) l.add(i);
        Collections.sort(l);
        for (int i=0; i<a.length; i++) a[i]=l.get(i);
    }

    static void ruffleSortRev(long[] a) {
        ArrayList<Long> l=new ArrayList<>();
        for (long i:a) l.add(i);
        Collections.sort(l);Collections.reverse(l);
        for (int i=0; i<a.length; i++) a[i]=l.get(i);
    }

    static int power(int x, int y, int p)
    {

        // Initialize result
        int res = 1;

        // Update x if it is more than or
        // equal to p
        x = x % p;

        while (y > 0) {

            // If y is odd, multiply x
            // with result
            if (y % 2 == 1)
                res = (res * x) % p;

            // y must be even now
            y = y >> 1; // y = y/2
            x = (x * x) % p;
        }

        return res;
    }

    // Returns n^(-1) mod p
    static int modInverse(int n, int p)
    {
        return power(n, p - 2, p);
    }

    // Returns nCr % p using Fermat's
    // little theorem.
    static int nCrModPFermat(int n, int r,
                             int p)
    {

        if (n<r)
            return 0;
        // Base case
        if (r == 0)
            return 1;

        // Fill factorial array so that we
        // can find all factorial of r, n
        // and n-r
        int[] fac = new int[n + 1];
        fac[0] = 1;

        for (int i = 1; i <= n; i++)
            fac[i] = fac[i - 1] * i % p;

        return (fac[n] * modInverse(fac[r], p)
                % p * modInverse(fac[n - r], p)
                % p)
                % p;
    }


    static boolean isPrime(long n)
    {

        // Check if number is less than
        // equal to 1
        if (n <= 1)
            return false;

            // Check if number is 2
        else if (n == 2)
            return true;

            // Check if n is a multiple of 2
        else if (n % 2 == 0)
            return false;

        // If not, then just check the odds
        for (int i = 3; i <= Math.sqrt(n); i += 2)
        {
            if (n % i == 0)
                return false;
        }
        return true;
    }

    static long gcd(long a, long b)
    {
        if (b == 0)
            return a;
        return gcd(b, a % b);
    }

    static long countDig(long N){
        if(N < 10)
            return 1;
        else
            return 1+countDig(N/10);
    }


    public final static int d = 256;
    static int MOD = 1000000007;
    static final double PI = Math.PI;
    private static BufferedReader in = new BufferedReader (new InputStreamReader (System.in));
    private static PrintWriter out = new PrintWriter (new OutputStreamWriter (System.out));
    static class FastReader{
        BufferedReader br;
        StringTokenizer st;
        public FastReader()
        {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        char nextChar()
        {
            try {
                return (char) (br.read());
            }catch (IOException e){
                return '~';
            }
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
    static void sop(Object o){out.print(o);}
    static double ceil(double d){
        return Math.ceil(d);
    }
    static double floor(double d){
        return Math.floor(d);
    }
    static double round(double d){
        return Math.round(d);
    }

    static void sopln(Object o){out.println(o);}
    static void printArray(boolean[] arr){
        for(int i=0;i<arr.length;i++){
            sop(arr[i]+" ");
        }
        sopln("");
    }
    static void printArray(long[] arr){
        for(int i=0;i<arr.length;i++){
            sop(arr[i]+" ");
        }
        sopln("");
    }
    static long power(long x, long y, long p)
    {

        // Initialize result
        long res = 1;

        // Update x if it is more than or
        // equal to p
        x = x % p;

        while (y > 0) {

            // If y is odd, multiply x
            // with result
            if (y % 2 == 1)
                res = (res * x) % p;

            // y must be even now
            y = y >> 1; // y = y/2
            x = (x * x) % p;
        }

        return res;
    }

    // Returns n^(-1) mod p
    static long modInverse(long n, int p)
    {
        return power(n, p - 2, p);
    }

    // Returns nCr % p using Fermat's
    // little theorem.

    ///

    // Function to find modular
// inverse of a under modulo p
// using Fermat's method.
// Assumption: p is prime
    static long modFact(int n,
                        int p)
    {
        if (n >= p)
            return 0;

        long result = 1;
        for (int i = 1; i <= n; i++)
            result = (result * i) % p;

        return result;
    }

}