import java.util.Scanner;
import java.util.HashSet;

public class D {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        boolean[] notGood = new boolean[350];
        boolean[] investigated = new boolean[350];
        int K = sc.nextInt();
        investigated[1] = true;
        l: for (int i = 2; i < 350; i++) {
            if (investigated[i]) {
                continue l;
            }
            int t = i;
            HashSet<Integer> h = new HashSet<>();
            //HashSet<Integer> hh = new HashSet<>();
            while (!investigated[t]) {
                //System.out.println(t);
                investigated[t] = true;
                h.add(t);
                t = update(t);
            }
            if (notGood[t] || h.contains(t)) {
                for (Integer s: h) {
                    notGood[s] = true;
                }
            }
        }
        /*
        for (int i = 2; i < 350; i++) {
            System.out.println(i + " " + notGood[i]);
        }
        */
        loop: while ( K > 0) {
            K--;
            int count = sc.nextInt();
            int p = sc.nextInt();
            if (p == 1) {
                System.out.println(count + " " + p + " " + "NO");
                    continue loop;
            }
            for (int i = 2; i < (int)Math.pow(p, 0.5) + 1; i++) {
                if (p % i == 0) {
                    System.out.println(count + " " + p + " " + "NO");
                    continue loop;
                }
            }
            int cur = update(p);
            if (notGood[cur]) {
                System.out.println(count + " " + p + " " + "NO");
                    continue loop;
            } else {
                System.out.println(count + " " + p + " " + "YES");
                    continue loop;
            }

        }
    }

    public static int update(int cur) {
        int sum = 0;
        while (cur != 0) {
            sum += (cur % 10) * (cur % 10);
            cur = cur / 10;
        }
        return sum;
    }
}