#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

int P;
bool chk[10000];

void work() {
	int T, n;
	scanf("%d", &T);
	scanf("%d", &n);

	if (n == 1) {
		printf("%d %d NO\n", T, n);
		return;
	}
	for (int i = 2; i < sqrt(n) + 1; ++i)
		if (n % i == 0) {
			printf("%d %d NO\n", T, n);
			return;
		}

	bool flag = false;
	memset(chk, false, sizeof(chk));
	chk[n] = true;
	int cur = n;
	while (1) {
		int sum = 0;
		int tmp = cur;
		while (tmp > 0) {
			sum += (tmp % 10) * (tmp % 10);
			tmp /= 10;
		}
		cur = sum;
		if (cur == 1) {
			printf("%d %d YES\n", T, n);
			return;
		}
		if (chk[cur]) {
			printf("%d %d NO\n", T, n);
			return;
		}
		chk[cur] = true;
	}
}

int main() {
	scanf("%d", &P);
	for (int i = 0; i < P; ++i) {
		work();
	}
	return 0;
}