// Arup Guha
// 3/18/2016
// Solution to 2014 NY Regional Problem D: Happy Happy Prime Prime

import java.util.*;

public class d {

	final public static int MAX = 10001;

	public static void main(String[] args) {

		// Run a regular prime sieve.
		boolean[] sieve = new boolean[MAX];
		Arrays.fill(sieve, true);
		sieve[0] = sieve[1] = false;
		for (int i=2; i<MAX; i++)
			for (int j=2*i; j<MAX; j+=i)
				sieve[j] = false;

		// Now, screen out primes that aren't happy.
		for (int i=2; i<MAX; i++)
			if (sieve[i] && !isHappy(i))
				sieve[i] = false;


		Scanner stdin = new Scanner(System.in);
		int numCases = stdin.nextInt();

		// Process each case.
		for (int loop=1; loop<=numCases; loop++) {

			// Read in data.
			int caseNum = stdin.nextInt();
			int n = stdin.nextInt();

			// Output appropriate result.
			if (sieve[n])
				System.out.println(loop+" "+n+" YES");
			else
				System.out.println(loop+" "+n+" NO");
		}
	}

	// Returns true iff n is happy.
	public static boolean isHappy(int n) {

		// Simple case.
		if (n == 1) return true;

		// Keep track of which numbers we've visited in used.
		boolean[] used = new boolean[MAX];
		used[n] = true;

		// Run until we get to 1 or a repeat.
		while (true) {
			n = sumSqDigits(n);
			if (n == 1) return true;
			if (used[n]) return false;
			used[n] = true;
		}
	}

	// Returns the sum of the square of the digits of n.
	public static int sumSqDigits(int n) {
		int res = 0;
		while (n > 0) {
			int d = n%10;
			res += d*d;
			n /= 10;
		}
		return res;
	}
}