#include <bits/stdc++.h>
using namespace std;
int n = 0, a, tc;
int p[100005];
vector<int> cho[6][100005];
int sum[6];
int ret = 0;
void doit(int x, int cur) {
  if (x == n - 1) {
    ret += cho[x + 5 - n][cur].size();
    return;
  }
  for (int i = 0; i < cho[x + 5 - n][cur].size(); i++) {
    int ti = cho[x + 5 - n][cur][i];
    for (int j = 0; j < n - x - 1; j++) {
      sum[n - 1 - j] = sum[n - 1 - j] * 10 + ti % 10;
      ti /= 10;
    }
    doit(x + 1, sum[x + 1]);
    for (int j = 0; j < n - x - 1; j++) {
      sum[n - 1 - j] /= 10;
    }
  }
}
int main() {
  scanf("%d", &tc);
  for (int i = 0; i <= 100000; i++) p[i] = 1;
  p[0] = p[1] = 0;
  for (int i = 2; i <= 100000; i++)
    if (p[i] == 1) {
      int ti = i;
      for (int j = 0; j < 5; j++) {
        cho[5 - j][ti].push_back(i);
        ti /= 10;
      }
      for (int j = i + i; j <= 100000; j += i) p[j] = 0;
    }
  while (tc--) {
    scanf("%d", &a);
    int ti = a;
    ret = 0;
    n = 0;
    while (ti > 0) {
      sum[n] = ti % 10;
      ti /= 10;
      n++;
    }
    for (int i = 0; i < n / 2; i++) swap(sum[i], sum[n - 1 - i]);
    doit(1, sum[1]);
    printf("%d\n", ret);
  }
}
