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:


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
import java.util.TreeMap;




public class CodeForcesB {
	static HashMap<String,Integer> hm;
	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()); 
        } 
  
        double nextDouble() 
        { 
            return Double.parseDouble(next()); 
        } 
  
        String nextLine() 
        { 
            String str = ""; 
            try
            { 
                str = br.readLine(); 
            } 
            catch (IOException e) 
            { 
                e.printStackTrace(); 
            } 
            return str; 
        }
        int[] nextArray(int n)
        {
        	int arr[]=new int[n];
        	for(int i=0;i<n;i++)
        		arr[i]=nextInt();
        	return arr;
        }
    }
	static boolean palin(char[] arr)
	{
		if(arr.length==1)
			return false;
		for(int i=0;i<arr.length/2;i++)
			if(arr[i]!=arr[arr.length-1-i])
				return false;
		return true;
	}
	static long gcd(long a,long b)
    {
        // Everything divides 0 
        if (a == 0)
          return b;
        if (b == 0)
          return a;
      
        // base case
        if (a == b)
            return a;
      
        // a is greater
        if (a > b)
            return gcd(a-b, b);
        return gcd(a, b-a);
    }
	static boolean palin(String all)
	{
		for(int i=0;i<all.length()-1;i++)
		{
			for(int j=0;j<=i;j++)
			{
				if(palin(all.substring(j,all.length()-i+j).toCharArray()))
					return true;
			}
		}
		return false;
	}
	static void helper(int n,int[] arr,StringBuilder sb)
	{
		int max=0;
		for(int i=1;i<n;i++)
		{
			if(arr[i]-arr[max]>0)
				max=i;
		}
		for(int i=max;i<n;i++)
			sb.append(arr[i]+" ");
		if(max>0)
		helper(max, arr, sb);
		
	}
	static boolean helper(String text,String ans,HashSet<String> hs)
	{
		if(text.equals(ans))
			return true;
		if(hs.contains(text))
			return false;
		hs.add(text);
		int c=0,c2=0;
		char[] arr=text.toCharArray();
		StringBuilder sb=new StringBuilder();
		for(int i=0;i<arr.length;i++)
			
		{
			char ch=arr[i];
			if(ch=='1')
			{
				c++;
				sb.append(0);
			}
			else {
				c--;
				sb.append(1);
			}
			if(arr[i]!=ans.charAt(i))
				c2++;
			if(c==0&&c2%2==0)
			{
				if(i==arr.length-1)
				{
					if(helper(sb.toString(), ans, hs))
						return true;
				}
				else {
					if(helper(sb.toString()+text.substring(i+1), ans, hs))
						return true;
				}
			
			}
				
		}
		return false;
	}
	static boolean helper(String text,String ans,int i,HashSet<String> hs,PriorityQueue<Integer> pq)
	{
		if(ans.equals(text))
			return true;
		if(hs.contains(text)||i==-1)
			return false;
		int curr=pq.remove();
		String change=text.substring(0,curr+1);
		int num=Integer.parseInt(change,2);
		String changed=(~num)+"";
		int prev=-1;
		if(!pq.isEmpty())
			prev=pq.peek();
		return helper(changed+text.substring(i+1), ans,prev, hs,pq)||helper(text, ans,prev, hs,pq);
	}
	public static <K, V extends Comparable<V> > TreeMap<K, V>
    valueSort( Map<K, V> map)
    {
        // Static Method with return type Map and
        // extending comparator class which compares values
        // associated with two keys
        Comparator<K> valueComparator = new Comparator<K>()
        {
              
            public int compare(K k1, K k2)
            {
  
                int comp = map.get(k1).compareTo(map.get(k2));
  
                if (comp >= 0)
                     return 0;
  
                else
                     return comp;
            }
        };
        System.out.println(map);
        TreeMap<K, V> sorted = new TreeMap<K, V>(valueComparator);
        
        sorted.putAll(map);
  
        return sorted;
    }
	public static HashMap<Integer, Integer> sortByValue(HashMap<Integer, Integer> hm)
    {
        // Create a list from elements of HashMap
        List<Entry<Integer, Integer>> list =
               new LinkedList<Map.Entry<Integer, Integer> >(hm.entrySet());
  
        // Sort the list
        Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() {
        	public int compare(Map.Entry<Integer, Integer> o1, 
                               Map.Entry<Integer, Integer> o2)
            {
                if (o1.getValue()==o2.getValue())
                	return o1.getKey()-o2.getKey();
                return -o1.getValue()+o2.getValue();
                
            }
        });
          
        // put data from sorted list to hashmap 
        HashMap<Integer, Integer> temp = new LinkedHashMap<Integer, Integer>();
        for (Map.Entry<Integer, Integer> aa : list) {
            temp.put(aa.getKey(), aa.getValue());
        }
        return temp;
    }
	static int helper(int[] arr,int i,long curr)
	{
		if(curr<0)
			return -1;
		if(i==arr.length)
			return 0;
//		System.out.println(i+","+curr);
		if(arr[i]>=0)
			return 1+helper(arr, i+1, arr[i]+curr);
		else {
//			System.out.println(i+","+Math.max(helper(arr, i+1, curr),1+helper(arr, i+1,arr[i]+ curr)));
			return Math.max(helper(arr, i+1, curr),1+helper(arr, i+1,arr[i]+ curr));
		}
	}
	static boolean helper(int a,int b,int n,HashSet<Integer> hs)
	{
		if(n<=0)
			return false;
		if(n==1||n==a)
			return true;
		if(hs.contains(n))
			return false;
		double check=Math.log10(n)/Math.log10(a);
		if(Math.ceil(check)==check)
			return true;
		check=Math.log10(n)/Math.log10(b+1);
		if(Math.ceil(check)==check)
			return true;
		while(n>0)
		{
			hs.add(n);
			double ans=Math.log10(n)/Math.log10(a);
			if(Math.ceil(ans)==ans)
				return true;
			n-=b;
		}
		return false;
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
			FastReader fs=new FastReader();
			PrintWriter out=new PrintWriter(System.out);
			//out.print("111");
			int T=fs.nextInt();
			StringBuilder sb=new StringBuilder();
			while(T-->0)
			{
				int n=fs.nextInt(),a=fs.nextInt(),b=fs.nextInt();
//				boolean[] dp=new boolean[n+1];
				HashSet<Integer> hs=new HashSet<>();
				boolean res=false;
				double ans=Math.log10(n)/Math.log10(a);
				if(a==1)
				{
					if((n-1)%b==0)
						sb.append("YES\n");
					else {
						sb.append("NO\n");
					}
					continue;
				}
				if(Math.ceil(ans)==ans)
					res=true;
				else {
					long st=1;
					while(st<=n)
					{
						long rem=n-st;
						if(rem%b==0)
						{
							res=true;
							break;
						}
						st*=a;
					}
				}
//				System.out.println(ans);
				if(res)
					sb.append("YES");
				else {
					sb.append("NO");
				}
				sb.append("\n");
			}
			System.out.println(sb);
			
	}

}