#include <bits/stdc++.h>
using namespace std;
const int MaxN = 100000;
int p[MaxN];
vector<int> a[6][6][MaxN];
void init() {
  p[0] = p[1] = 1;
  for (int i = 2; i < MaxN; i++)
    if (!p[i]) {
      int len = 0, t = i;
      while (t) len++, t /= 10;
      for (int k = len; k <= 5; k++) {
        t = i;
        for (int j = 0; j < k; j++) {
          a[k][k - j][t].push_back(i);
          t /= 10;
        }
      }
      for (int j = i + i; j < MaxN; j += i) p[j] = 1;
    }
}
int g[6][6], n;
char s[20];
int sol(int pos) {
  if (pos == n) return 1;
  int t = 0;
  for (int i = 0; i < pos; i++) t = t * 10 + g[pos][i];
  int ret = 0;
  for (int i = 0, m = a[n][pos][t].size(); i < m; i++) {
    int x = a[n][pos][t][i];
    for (int j = n - 1; j > pos; j--) g[pos][j] = g[j][pos] = x % 10, x /= 10;
    ret += sol(pos + 1);
  }
  return ret;
}
int main() {
  init();
  int T;
  scanf("%d", &T);
  while (T--) {
    scanf("%s", s);
    n = strlen(s);
    for (int i = 0; i < n; i++) g[0][i] = g[i][0] = s[i] - '0';
    printf("%d\n", sol(1));
  }
  return 0;
}
