#pragma comment(linker, "/stack:64000000")
#define _CRT_SECURE_NO_DEPRECATE
#define _USE_MATH_DEFINES

#include <algorithm>
#include <iostream>
#include <cassert>
#include <iomanip>
#include <climits>
#include <utility>
#include <cstring>
#include <complex>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <bitset>
#include <string>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <set>
#include <map>

#define forn(i, n) for (int i = 0; i < int(n); i++)
#define ford(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define forl(i, n) for (int i = 1; i <= int(n); i++)
#define for1(i, n) for (int i = 1; i <= int(n); i++)
#define forn1(i, n) for (int i = 1; i <= int(n); i++)
#define fore(i, l, r) for (int i = int(l); i <= int(r); i++)
#define correct(x, y, n, m) (0 <= (x) && (x) < (n) && 0 <= (y) && (y) < (m))
#define debug(x) cerr << #x << " = " << x << endl;
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define sz(a) int((a).size())
#define pb(a) push_back(a)
#define mp(a, b) make_pair((a), (b))
#define x first
#define y second
#define fs first
#define ft first
#define sc second

using namespace std;

template<typename X> inline X abs(const X& a) { return a < 0? -a: a; }
template<typename X> inline X sqr(const X& a) { return a * a; }

typedef long long li;
typedef long double ld;
typedef pair<int, int> pt;
typedef pair<ld, ld> ptd;
typedef unsigned char byte;
typedef vector<vector<int> > matrix;

const int INF = INT_MAX / 2;
const ld EPS = 1e-9;
const ld PI = 3.1415926535897932384626433832795;

const int N = 100 * 1000 + 3;

int x;

inline bool read()
{
    return (scanf("%d", &x) == 1);
}

inline string toStr(int x)
{
    stringstream ss;
    ss << x;
    string ans;
    ss >> ans;

    return ans;
}

int ans = 0;
int a[6][6];
int n;

bool pr[N];

inline bool can(int idx, int cur)
{
    int st = 1;

    int val = 0;

    ford(i, n)
    {
        if (i == idx)
            val += st * cur;
        else
            assert(a[idx][i] != -1), val += st * a[idx][i];

        st *= 10;
    }

    return pr[val];
}

inline int getCnt(int i)
{
    int cnt = 0;

    forn(cur, 10)
        if (can(i, cur))
            cnt++;

    return cnt;
}

void solve(int x, int y)
{
    if (x == n - 1)
    {
        int res = 1;

        forl(i, n - 1)
        {
            int cnt = getCnt(i);

            res *= cnt;
        }

        ans += res;

        return;
    }

    if (getCnt(x - 1) == 0)
        return;

    int nx = x, ny = y + 1;

    if (ny >= n)
        nx = x + 1, ny = nx + 1;

    forn(cur, 10)
    {
        a[x][y] = a[y][x] = cur;

        solve(nx, ny);

        a[x][y] = a[y][x] = -1;
    }
}

inline void solve()
{
    assert(pr[x]);

    string cur = toStr(x);

    n = sz(cur);

    assert(n >= 2 && n <= 5);

    memset(a, -1, sizeof(a));

    forn(i, n)
    {
        a[0][i] = cur[i] - '0';
        a[i][0] = cur[i] - '0';
    }

    ans = 0;

    solve(1, 2);

    printf("%d\n", ans);
}

inline void preCalc()
{
    memset(pr, true, sizeof(pr));

    pr[0] = pr[1] = false;

    for(int i = 2; i < N; i++)
    {
        if (!pr[i])
            continue;

        for(int j = i + i; j < N; j += i)
            pr[j] = false;
    }
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "wt", stdout);
#endif

    preCalc();

    int testCnt;

    assert(scanf("%d", &testCnt) == 1);

    forn(test, testCnt)
    {
        assert(read());
        solve();
    }

    return 0;
}


#pragma comment(linker, "/stack:64000000")
#define _CRT_SECURE_NO_DEPRECATE
#define _USE_MATH_DEFINES

#include <algorithm>
#include <iostream>
#include <cassert>
#include <iomanip>
#include <climits>
#include <utility>
#include <cstring>
#include <complex>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <bitset>
#include <string>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <set>
#include <map>

#define forn(i, n) for (int i = 0; i < int(n); i++)
#define ford(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define forl(i, n) for (int i = 1; i <= int(n); i++)
#define for1(i, n) for (int i = 1; i <= int(n); i++)
#define forn1(i, n) for (int i = 1; i <= int(n); i++)
#define fore(i, l, r) for (int i = int(l); i <= int(r); i++)
#define correct(x, y, n, m) (0 <= (x) && (x) < (n) && 0 <= (y) && (y) < (m))
#define debug(x) cerr << #x << " = " << x << endl;
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define sz(a) int((a).size())
#define pb(a) push_back(a)
#define mp(a, b) make_pair((a), (b))
#define x first
#define y second
#define fs first
#define ft first
#define sc second

using namespace std;

template<typename X> inline X abs(const X& a) { return a < 0? -a: a; }
template<typename X> inline X sqr(const X& a) { return a * a; }

typedef long long li;
typedef long double ld;
typedef pair<int, int> pt;
typedef pair<ld, ld> ptd;
typedef unsigned char byte;
typedef vector<vector<int> > matrix;

const int INF = INT_MAX / 2;
const ld EPS = 1e-9;
const ld PI = 3.1415926535897932384626433832795;

const int N = 100 * 1000 + 3;

int x;

inline bool read()
{
    return (scanf("%d", &x) == 1);
}

inline string toStr(int x)
{
    stringstream ss;
    ss << x;
    string ans;
    ss >> ans;

    return ans;
}

int ans = 0;
int a[6][6];
int n;

bool pr[N];

inline bool can(int idx, int cur)
{
    int st = 1;

    int val = 0;

    ford(i, n)
    {
        if (i == idx)
            val += st * cur;
        else
            val += st * a[idx][i];

        st *= 10;
    }

    return pr[val];
}

inline int getCnt(int i)
{
    int cnt = 0;

    forn(cur, 10)
        if (can(i, cur))
            cnt++;

    return cnt;
}

void solve(int x, int y)
{
    if (x == n - 1)
    {
        int res = 1;

        forl(i, n - 1)
        {
            int cnt = getCnt(i);

            res *= cnt;
        }

        ans += res;

        return;
    }

    if (getCnt(x - 1) == 0)
        return;

    int nx = x, ny = y + 1;

    if (ny >= n)
        nx = x + 1, ny = nx + 1;

    forn(cur, 10)
    {
        a[x][y] = a[y][x] = cur;

        solve(nx, ny);

        a[x][y] = a[y][x] = -1;
    }
}

inline void solve()
{
    assert(pr[x]);

    string cur = toStr(x);

    n = sz(cur);

    assert(n >= 2 && n <= 5);

    memset(a, -1, sizeof(a));

    forn(i, n)
    {
        a[0][i] = cur[i] - '0';
        a[i][0] = cur[i] - '0';
    }

    ans = 0;

    solve(1, 2);

    printf("%d\n", ans);
}

inline void preCalc()
{
    memset(pr, true, sizeof(pr));

    pr[0] = pr[1] = false;

    for(int i = 2; i < N; i++)
    {
        if (!pr[i])
            continue;

        for(int j = i + i; j < N; j += i)
            pr[j] = false;
    }
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "wt", stdout);
#endif

    preCalc();

    int testCnt;

    assert(scanf("%d", &testCnt) == 1);

    forn(test, testCnt)
    {
        assert(read());
        solve();
    }

    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;

int n = 0,a,tc;
int p[100005];
vector<int> cho[6][100005];
int g[6][6],sum[6];
int ret = 0;

void doit(int x,int y,int cur) {
    if (x == n) {
        ret++;
        return;
    }
    //printf("%d %d = %d\n",x,y,cur);
    for (int i=0; i<cho[y+5-n][cur].size(); i++) {
        //printf("%d,%d %d = %d\n",x,y,cur,cho[y+5-n][cur][i]);
        int ti = cho[y+5-n][cur][i];
        for (int j=0; j<n-x-1; j++) {
            g[x][n-1-j] = ti%10;
            sum[n-1-j] = sum[n-1-j]*10+ti%10;
            ti /= 10;
        }
        doit(x+1,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) {
            g[0][n] = ti%10;
            sum[n] = ti%10;
            ti /= 10;
            n++;
        }
        for (int i=0; i<n/2; i++) swap(sum[i],sum[n-1-i]);
        doit(1,1,sum[1]);
        printf("%d\n",ret);
    }
}


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

int n = 0,a,tc;
int p[100005];
int g[6][6];
int ret = 0;

void doit(int x,int y,int cur) {
    if (y == n) {
        if (p[cur] == 1) {
            doit(x+1,0,0);
        }
        return;
    }
    if (x == n) {
        ret++;
        return;
    }
    if (x > y) {
        g[x][y] = g[y][x];
        doit(x,y+1,cur*10+g[x][y]);
        return;
    }
    for (int i=0; i<10; i++) {
        g[x][y] = i;
        doit(x,y+1,cur*10+i);
    }
}

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) {
            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) {
            g[0][n] = ti%10;
            ti /= 10;
            n++;
        }
        for (int i=0; i<n/2; i++) swap(g[0][i],g[0][n-i-1]);
        doit(1,0,0);
        printf("%d\n",ret);
    }
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>
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;
    }
    //printf("%d %d = %d\n",x,y,cur);
    for (int i=0; i<cho[x+5-n][cur].size(); i++) {
        //printf("%d,%d %d = %d\n",x,y,cur,cho[y+5-n][cur][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);
    }
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <math.h>
#include <map>
using namespace std;

const int maxn = 100005;

int sz, sol, row[10];
bool prime[maxn];

map<pair<int,int>, int> M;

void solve(int r, int d, int add){
    if(add == 0){ ++sol; return; }

    if(d == add){
        if( !prime[row[r]] )solve(r+1, 0, add-1);
        return;
    }
    if(d == add-1){
      for(int i = 1; i < 10; i += 2){
         if(d)row[r+d] = row[r+d] * 10 + i;
         row[r]   = row[r] * 10 + i;
         solve(r, d+1, add);
         if(d)row[r+d] = (row[r+d] - i) / 10;
         row[r]   = (row[r] - i) / 10;
      }
      if(row[r] == 0){
         if(d)row[r+d] = row[r+d] * 10 + 2;
         row[r]   = row[r] * 10 + 2;
         solve(r, d+1, add);
         if(d)row[r+d] = (row[r+d] - 2) / 10;
         row[r]   = (row[r] - 2) / 10;
      }
    }else{
      for(int i = 0; i < 10; ++i){
         if(d)row[r+d] = row[r+d] * 10 + i;
         row[r]   = row[r] * 10 + i;
         solve(r, d+1, add);
         if(d)row[r+d] = (row[r+d] - i) / 10;
         row[r]   = (row[r] - i) / 10;
      }
    }
}

int main(){

    vector <int> v;
    prime[1] = 1;
    for(int i = 2; i < maxn; ++i)
     for(int j = i+i; j < maxn; j+=i)
        prime[j] = 1;

    int t;
    scanf("%d", &t);

    while(t--){
        int p;
        scanf("%d", &p);
        v.clear();
        while(p){v.push_back(p%10); p /= 10;}

        int n = v.size()-1; sz = n;
        for(int i = n-1; i >= 0; --i)
           row[n-i] = v[i];

        sol = 0;
        solve(1, 0, n);
        printf("%d\n", sol );
    }

    return 0;
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <math.h>
#include <map>
using namespace std;

const int maxn = 100005;

int sz, sol, row[10];
bool prime[maxn];

map<pair<int,int>, int> M;

void solve(int r, int d, int add){
    if(add == 0){
        bool ok = 0;
        for(int i = 1; i <= sz && !ok; ++i) ok |= prime[row[i]];
        if( !ok )++sol;
        return;
    }

    if(d == add){
        solve(r+1, 0, add-1);
        return;
    }
    if(d == add-1){
      for(int i = 1; i < 10; i += 2){
         if(d)row[r+d] = row[r+d] * 10 + i;
         row[r]   = row[r] * 10 + i;
         solve(r, d+1, add);
         if(d)row[r+d] = (row[r+d] - i) / 10;
         row[r]   = (row[r] - i) / 10;
      }
      if(row[r] == 0){
         if(d)row[r+d] = row[r+d] * 10 + 2;
         row[r]   = row[r] * 10 + 2;
         solve(r, d+1, add);
         if(d)row[r+d] = (row[r+d] - 2) / 10;
         row[r]   = (row[r] - 2) / 10;
      }
    }else{
      for(int i = 0; i < 10; ++i){
         if(d)row[r+d] = row[r+d] * 10 + i;
         row[r]   = row[r] * 10 + i;
         solve(r, d+1, add);
         if(d)row[r+d] = (row[r+d] - i) / 10;
         row[r]   = (row[r] - i) / 10;
      }
    }
}

int main(){

    vector <int> v;
    prime[1] = 1;
    for(int i = 2; i < maxn; ++i)
     for(int j = i+i; j < maxn; j+=i)
        prime[j] = 1;

    int t;
    scanf("%d", &t);

    while(t--){
        int p;
        scanf("%d", &p);
        v.clear();
        while(p){v.push_back(p%10); p /= 10;}

        int n = v.size()-1; sz = n;
        for(int i = n-1; i >= 0; --i)
           row[n-i] = v[i];

        sol = 0;
        solve(1, 0, n);
        printf("%d\n", sol );
    }

    return 0;
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <math.h>
#include <map>
using namespace std;

const int maxn = 100005;

int sz, sol, row[10];
bool prime[maxn];

int d[10];
vector <int> pref[maxn][6];

void solve(int r, int add){
    if(add == 0){ ++sol; return; }

    int p = row[r], prev[10];
    for(int i = 1; i < add; ++i)
      prev[i] = row[r+i];

    for(int i = 0; i < pref[p][add].size(); ++i){
       int x = pref[p][add][i];
       while(x < d[add]) x *= 10;

       for(int j = 0; j < add; ++j){
         row[r] = row[r] * 10 + (x%10);
         if(j)row[r+j] = row[r+j] * 10 + (x%10);
         x /= 10;
       }
       solve(r+1, add-1);

       row[r] = p;
       for(int j = 1; j < add; ++j)
         row[r+j] = prev[j];
    }

}

int main(){

    d[1] = 1;
    for(int i = 2; i < 5; ++i)
       d[i] = d[i-1] * 10;

    vector <int> v;
    for(int i = 2; i < maxn; ++i){
     if( !prime[i] ){
         int x = i, rep = 0; v.clear();
         while(x){
             v.push_back(x%10);
             rep = rep*10 + (x%10);  x /= 10;
         }
         int l = v.size(), s = 0;

         for(int j = l; j < 5; ++j)
           pref[0][j].push_back(rep);
         for(int i = l-1; i >= 0; --i){
            s = s * 10 + v[i];
            rep /= 10;
            pref[s][i].push_back(rep);
         }
     }
     for(int j = i+i; j < maxn; j+=i)
        prime[j] = 1;
    }

    int t;
    scanf("%d", &t);

    while(t--){
        int p;
        scanf("%d", &p); v.clear();
        while(p){v.push_back(p%10); p /= 10;}

        int n = v.size()-1; sz = n;
        for(int i = n-1; i >= 0; --i)
           row[n-i] = v[i];

        sol = 0;
        solve(1, n);
        printf("%d\n", sol );
    }

    return 0;
}


#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <math.h>
using namespace std;

const int maxn=100000;
bool flag[maxn];
int p[maxn];
int K;
int len;
int a[10][10];
long long ans;
int s[maxn][6];
int oks[maxn];

int bifind(int x)
{
    int s=0,t=K-1;
    while(s<t)
    {
        int mid=(s+t)>>1;
        if (p[mid]>=x) t=mid;
        else s=mid+1;
    }
    return s;
}

void dfs(int dep)
{
    if (dep==len)
    {
        int now=0;
        for(int i=0;i<len;i++) now=now*10+a[dep-1][i];
        if (!flag[now]) ans++;
        return ;
    }
    int small=0,big=0;
    for(int i=0;i<len;i++)
     if (i<dep) small=small*10+a[dep][i];
     else small=small*10;
    for(int i=0;i<len;i++)
     if (i<dep-1) big=big*10+a[dep][i]; else
     if (i>=dep) big=big*10; else
     big=big*10+a[dep][i]+1;

    //int idx=bifind(small);
    int idx=oks[small];
    for(int i=idx;i<K;i++)
    {
        if (p[i]>big) break;
        for(int j=1;j<len;j++) a[dep][j]=a[j][dep]=s[i][len-1-j];
        dfs(dep+1);
    }
}

int main()
{
    K=0;flag[0]=flag[1]=1;
    for(int i=2;i<maxn;i++)
    {
        if (!flag[i]) p[K++]=i;
        for(int j=0;p[j]*i<maxn&&j<K;j++)
        {
            flag[p[j]*i]=1;
            if (i%p[j]==0) break;
        }
    }
    for(int i=0;i<K;i++)
    {
        int x=p[i];
        for(int j=0;j<5;j++)
        {
            s[i][j]=x%10;
            x/=10;
        }
    }
    int j=0;
    for(int i=0;i<maxn;i++)
    {
        while(j<K&&p[j]<i) j++;
        oks[i]=j;
    }

    int cas;
    char ss[20];
    scanf("%d",&cas);
    while(cas--)
    {
        scanf("%s",ss);
        len=strlen(ss);
        for(int i=0;i<len;i++) a[0][i]=a[i][0]=ss[i]-'0';
        ans=0;
        dfs(1);
        printf("%I64d\n",ans);
    }
    return 0;
}


#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <math.h>
using namespace std;

const int maxn=100000;
bool flag[maxn];
int p[maxn];
int K;
int len;
int a[10][10];
long long ans;
int s[maxn][6];
int oks[maxn];
int quan[10];

int bifind(int x)
{
    int s=0,t=K-1;
    while(s<t)
    {
        int mid=(s+t)>>1;
        if (p[mid]>=x) t=mid;
        else s=mid+1;
    }
    return s;
}

void dfs(int dep)
{
    if (dep==len)
    {
        if (!flag[a[len-1][len-1]]) ans++;
        return ;
    }

//    int small=0,big=0;
//    for(int i=0;i<len;i++)
//     if (i<dep) small=small*10+a[dep][i];
//     else small=small*10;
//    for(int i=0;i<len;i++)
//     if (i<dep-1) big=big*10+a[dep][i]; else
//     if (i>=dep) big=big*10; else
//     big=big*10+a[dep][i]+1;

    //int idx=bifind(small);
    int idx=oks[a[dep][dep-1]];
    int big=a[dep][dep-1]+quan[dep-1];
    for(int i=idx;i<K;i++)
    {
        if (p[i]>=big) break;
        for(int j=1;j<len;j++)
            a[j][dep]=s[i][len-1-j]*quan[dep]+a[j][dep-1];
        dfs(dep+1);
    }
}

int main()
{
    K=0;flag[0]=flag[1]=1;
    for(int i=2;i<maxn;i++)
    {
        if (!flag[i]) p[K++]=i;
        for(int j=0;p[j]*i<maxn&&j<K;j++)
        {
            flag[p[j]*i]=1;
            if (i%p[j]==0) break;
        }
    }
    for(int i=0;i<K;i++)
    {
        int x=p[i];
        for(int j=0;j<5;j++)
        {
            s[i][j]=x%10;
            x/=10;
        }
    }
    int j=0;
    for(int i=0;i<maxn;i++)
    {
        while(j<K&&p[j]<i) j++;
        oks[i]=j;
    }

    int cas;
    char ss[20];
    scanf("%d",&cas);
    while(cas--)
    {
        scanf("%s",ss);
        len=strlen(ss);
        quan[len-1]=1;
        for(int i=len-2;i>=0;i--) quan[i]=quan[i+1]*10;
        for(int i=0;i<len;i++) a[i][0]=(ss[i]-'0')*quan[0];
        ans=0;
        dfs(1);
        printf("%I64d\n",ans);
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
#define maxn 100000
using namespace std;
bool flag[maxn];
int p[maxn],tot,a[10][10],tmp[10],ten[10],cnt;
vector<int>G[maxn];
inline void dfs(int dep,int n)
{
	int cc=0;
	if(dep==n)
	{
		cnt++;
		return ;
	}
	for(int i=dep-1;i>=0;i--)
	{
		cc+=a[dep][dep-1-i]*ten[i];
	}
	int low,up;
	if(cc>=1)
	{
		low=cc*ten[n-dep];
		up=cc*ten[n-dep]+(ten[n-dep]-1);
	}
	else
	{
		low=0;
		up=ten[n-dep]-1;
	}
	for(int i=0;i<(int)G[cc].size();i++)
	{
		int cur=G[cc][i];
		if(cur<low)
			continue;
		if(cur>up)
			break;
		for(int j=dep;j<n;j++)
			a[dep][j]=a[j][dep]=0;
		int now=n-1;
		while(cur&&now>=dep)
		{
			a[dep][now]=a[now][dep]=cur%10;
			now--;
			cur/=10;
		}
		dfs(dep+1,n);
	}
}
int main()
{
	for(int i=2;i<maxn;i++)
	{
		if(!flag[i])
			p[tot++]=i;
		for(int j=0;j<tot&&p[j]*i<maxn;j++)
		{
			flag[i*p[j]]=1;
			if(i%p[j]==0)
				break;
		}
	}
	for(int i=0;i<tot;i++)
	{
		G[p[i]/10].push_back(p[i]);
		G[p[i]/100].push_back(p[i]);
		G[p[i]/1000].push_back(p[i]);
		G[p[i]/10000].push_back(p[i]);
	}
	for(int i=0;i<=10000;i++)
	{
		if((int)G[i].size()==0)
			continue;
		sort(G[i].begin(),G[i].end());
		vector<int>::iterator it=unique(G[i].begin(),G[i].end());
		G[i].erase(it,G[i].end());
	}
	ten[0]=1;
	for(int i=1;i<10;i++)ten[i]=ten[i-1]*10;
	int tt;
	scanf("%d",&tt);
	while(tt--)
	{
		int p;
		scanf("%d",&p);
		int n=0;
		while(p)
		{
			tmp[n++]=p%10;
			p/=10;
		}
		for(int i=0;i<n;i++)
			a[0][i]=tmp[n-1-i],a[i][0]=tmp[n-1-i];
		cnt=0;
		dfs(1,n);
		printf("%d\n",cnt);
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
#define maxn 100000
using namespace std;
bool flag[maxn];
int p[maxn],tot,a[10][10],tmp[10],ten[10],cnt;
vector<int>G[maxn];
vector<int>V[10009][6];
inline void dfs(int dep,int n)
{
	int cc=0,num=0;
	if(dep==n)
	{
		cnt++;
		return ;
	}
	for(int i=dep-1;i>=0;i--)
	{
		cc+=a[dep][dep-1-i]*ten[i];
	}
	for(int i=0;i<dep;i++)
	{
		if(a[dep][i]==0)
			num++;
		else
			break;
	}
	for(int i=0;i<(int)V[cc][n-num].size();i++)
	{
		int cur=V[cc][n-num][i];
		for(int j=dep;j<n;j++)
			a[dep][j]=a[j][dep]=0;
		int now=n-1;
		while(cur&&now>=dep)
		{
			a[dep][now]=a[now][dep]=cur%10;
			now--;
			cur/=10;
		}
		dfs(dep+1,n);
	}
}
int main()
{
	for(int i=2;i<maxn;i++)
	{
		if(!flag[i])
			p[tot++]=i;
		for(int j=0;j<tot&&p[j]*i<maxn;j++)
		{
			flag[i*p[j]]=1;
			if(i%p[j]==0)
				break;
		}
	}
	for(int i=0;i<tot;i++)
	{
		G[p[i]/10].push_back(p[i]);
		G[p[i]/100].push_back(p[i]);
		G[p[i]/1000].push_back(p[i]);
		G[p[i]/10000].push_back(p[i]);
	}
	for(int i=0;i<=10000;i++)
	{
		if((int)G[i].size()==0)
			continue;
		sort(G[i].begin(),G[i].end());
		vector<int>::iterator it=unique(G[i].begin(),G[i].end());
		G[i].erase(it,G[i].end());
	}
	for(int i=1;i<=10000;i++)
	{
		for(int j=0;j<(int)G[i].size();j++)
		{
			int k=G[i][j];
			int cnt=0;
			while(k)
			{
				cnt++;
				k/=10;
			}
			V[i][cnt].push_back(G[i][j]);
		}
	}
	for(int i=0;i<(int)G[0].size();i++)
	{
		int k=G[0][i];
		int cnt=0;
		while(k)
		{
			cnt++;
			k/=10;
		}
		for(int j=cnt;j<6;j++)
			V[0][j].push_back(G[0][i]);
	}
	ten[0]=1;
	for(int i=1;i<10;i++)ten[i]=ten[i-1]*10;
	int tt;
	scanf("%d",&tt);
	while(tt--)
	{
		int p;
		scanf("%d",&p);
		int n=0;
		while(p)
		{
			tmp[n++]=p%10;
			p/=10;
		}
		for(int i=0;i<n;i++)
			a[0][i]=tmp[n-1-i],a[i][0]=tmp[n-1-i];
		cnt=0;
		dfs(1,n);
		printf("%d\n",cnt);
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
#define maxn 100000
using namespace std;
bool flag[maxn];
int p[maxn],tot,a[10][10],tmp[10],ten[10],cnt;
vector<int>G[maxn];
void dfs(int dep,int n)
{
	int cc=0;
	if(dep==n)
	{
		cnt++;
		return ;
	}
	for(int i=dep-1;i>=0;i--)
	{
		cc+=a[dep][dep-1-i]*ten[i];
	}
	int low,up;
	if(cc>=1)
	{
		low=cc*ten[n-dep];
		up=cc*ten[n-dep]+(ten[n-dep]-1);
	}
	else
	{
		low=0;
		up=ten[n-dep]-1;
	}
	for(int i=0;i<(int)G[cc].size();i++)
	{
		int cur=G[cc][i];
		if(cur<low)
			continue;
		if(cur>up)
			break;
		for(int j=dep;j<n;j++)
			a[dep][j]=a[j][dep]=0;
		int now=n-1;
		while(cur&&now>=dep)
		{
			a[dep][now]=a[now][dep]=cur%10;
			now--;
			cur/=10;
		}
		dfs(dep+1,n);
	}
}
int main()
{
	for(int i=2;i<maxn;i++)
	{
		if(!flag[i])
			p[tot++]=i;
		for(int j=0;j<tot&&p[j]*i<maxn;j++)
		{
			flag[i*p[j]]=1;
			if(i%p[j]==0)
				break;
		}
	}
	for(int i=0;i<tot;i++)
	{
		G[p[i]/10].push_back(p[i]);
		G[p[i]/100].push_back(p[i]);
		G[p[i]/1000].push_back(p[i]);
		G[p[i]/10000].push_back(p[i]);
	}
	for(int i=0;i<=10000;i++)
	{
		if((int)G[i].size()==0)
			continue;
		sort(G[i].begin(),G[i].end());
		vector<int>::iterator it=unique(G[i].begin(),G[i].end());
		G[i].erase(it,G[i].end());
	}
	ten[0]=1;
	for(int i=1;i<10;i++)ten[i]=ten[i-1]*10;
	int tt;
	scanf("%d",&tt);
	while(tt--)
	{
		int p;
		scanf("%d",&p);
		int n=0;
		while(p)
		{
			tmp[n++]=p%10;
			p/=10;
		}
		for(int i=0;i<n;i++)
			a[0][i]=tmp[n-1-i],a[i][0]=tmp[n-1-i];
		cnt=0;
		dfs(1,n);
		printf("%d\n",cnt);
	}
	return 0;
}


#include<iostream>
#include<fstream>
#include<sstream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
#include<cstring>
using namespace std;

#define FOR(I,A,B) for (int I=int(A);I<int(B);++I)
#define MEM(A,B) memset(A,B,sizeof(A));
#define CPY(A,B) memcpy(A,B,sizeof(B));
typedef __int64 LL;
const int N(100000);
int ans, m;
bool bo[N];
int a[6], d[6];
vector<int> pr[N][5];
void get_prime()
{
     FOR(i, 2, N)
	    if (!bo[i]) {
		   int j = i, k = 0;
		   for (j = i, k = 0; k <= 4; j /= 10, ++k)
		       if (k) pr[j][k].push_back(i % d[k]);
		   for (j = i + i; j < N; j += i) bo[j] = 1;
		}
}
void search(int x)
{
     if (x == m) {
	    ++ans; return;
	 }
	 int num = 0;
	 FOR(i, 0, x)
	    num = num * 10 + a[i] % d[m - x] / d[m - x - 1];
     FOR(i, 0, pr[num][m - x].size()) {
	     a[x] = num * d[m - x] + pr[num][m - x][i];
		 search(x + 1);
	 }
}
int main()
{
    int i, ca, p;
    //freopen("in.txt", "r", stdin);
	for (i = d[0] = 1; i <= 5; ++i) d[i] = d[i - 1] * 10;
	get_prime();
	for (scanf("%d", &ca); ca; --ca) {
        scanf("%d", &a[0]);
		for (i = a[0], m = 0; i; i /= 10, ++m);
		ans = 0;
		search(1);
		printf("%d\n", ans);
	}
	return 0;
}


#include<iostream>
#include<fstream>
#include<sstream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
#include<cstring>
using namespace std;

#define FOR(I,A,B) for (int I=int(A);I<int(B);++I)
#define MEM(A,B) memset(A,B,sizeof(A));
#define CPY(A,B) memcpy(A,B,sizeof(B));
typedef __int64 LL;
const int N(100000);
int ans, m;
bool bo[N];
int a[6], d[6];
int cnt[6][N];
int cut(int x, int y)
{
    int t = d[y - 1];
    return x - x / t % 10 * t;
}
void get_prime()
{
     FOR(i, 2, N)
	    if (!bo[i]) {
		   FOR(j, 1, 6) ++cnt[j][cut(i, j)];
		   for (int j = i + i; j < N; j += i) bo[j] = 1;
		}
}
void search(int x, int t)
{
     if (x == m + 1) {
	    ans += t;
	    return;
	 }
	 int y = m - x;
	 FOR(i, 0, d[y])
	    if (cnt[y + 1][a[x] * d[y + 1] + i]) {
		   FOR(j, x + 1, m + 1)
		      a[j] = a[j] * 10 + i / d[m - j] % 10;
		   search(x + 1, t * cnt[y + 1][a[x] * d[y + 1] + i]);
		   FOR(j, x + 1, m + 1)
		      a[j] /= 10;
		}
}
int main()
{
    int i, ca, p;
    //freopen("in.txt", "r", stdin);
	for (i = d[0] = 1; i <= 5; ++i) d[i] = d[i - 1] * 10;
	get_prime();
	for (scanf("%d", &ca); ca; --ca) {
        scanf("%d", &a[1]);
		for (i = a[1], m = 0; i; i /= 10, ++m);
        FOR(i, 2, m + 1)
           a[i] = a[1] / d[m - i] % 10;
		ans = 0;
		search(2, 1);
		printf("%d\n", ans);
	}
	return 0;
}


#include<iostream>
#include<fstream>
#include<sstream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
#include<cstring>
using namespace std;

#define FOR(I,A,B) for (int I=int(A);I<int(B);++I)
#define MEM(A,B) memset(A,B,sizeof(A));
#define CPY(A,B) memcpy(A,B,sizeof(B));
typedef __int64 LL;
const int N(100000);
int ans, m;
bool bo[N];
int a[6], d[6];
vector<int> pr[N][5];
void get_prime()
{
     FOR(i, 2, N)
	    if (!bo[i]) {
		   int j = i, k = 0;
		   for (j = i, k = 0; k <= 4; j /= 10, ++k)
		       if (k) pr[j][k].push_back(i % d[k]);
		   for (j = i + i; j < N; j += i) bo[j] = 1;
		}
}
void search(int x)
{
     if (x == m) {
	    ++ans; return;
	 }
	 int num = 0;
	 FOR(i, 0, x)
	    num = num * 10 + a[i] % d[m - x] / d[m - x - 1];
	 for (vector<int>::iterator P = pr[num][m - x].begin(); P != pr[num][m - x].end(); ++P) {
	     a[x] = num * d[m - x] + *P;
		 search(x + 1);
	 }
}
int main()
{
    int i, ca, p;
    //freopen("in.txt", "r", stdin);
	for (i = d[0] = 1; i <= 5; ++i) d[i] = d[i - 1] * 10;
	get_prime();
	for (scanf("%d", &ca); ca; --ca) {
	    scanf("%d", &a[0]);
		for (i = a[0], m = 0; i; i /= 10, ++m);
		ans = 0;
		search(1);
		printf("%d\n", ans);
	}
	return 0;
}


#include<string>
#include<iomanip>
#include<algorithm>
#include<cstring>
#include<vector>
#include<iostream>
using namespace std;

int n,now,p[20000],p_,g[10],ans;
int xx[10];
int tmp[100000][10];
vector<int> yoo[100000][10];

inline int get(int a,int b)
{
	return a/g[b+1]%10;
}

void dfs(int k)
{
	if(k==n+1)
	{
		//for(int i=1;i<=n;++i)cout<<xx[i]<<endl;cout<<endl;
		ans++;
		return;
	}
	int tmp=0;
	for(int i=1;i<k;++i)tmp=tmp*10+get(xx[i],k);
	for(vector<int>::iterator v=yoo[tmp][n-k+1].begin();v!=yoo[tmp][n-k+1].end();v++)
	{
		xx[k]=*v;
		dfs(k+1);
	}
}

int main()
{
	#ifndef ONLINE_JUDGE
	freopen("cf.in","rb",stdin);
	freopen("cf.out","wb",stdout);
	#endif
	int test;
	cin>>test;
	for(int i=2;i<=99999;++i)
	{
		bool can=1;
		for(int j=0;j<p_;++j)if(i%p[j]==0)
		{
			can=0;
			break;
		}
		if(can)p[p_++]=i;
	}
	memset(tmp,-1,sizeof tmp);
	g[0]=100000;g[1]=10000;g[2]=1000;g[3]=100;g[4]=10;g[5]=1;
	for(int i=0;i<p_;++i)
		for(int j=0;j<5;++j)
			yoo[p[i]/g[j+1]][4-j].push_back(p[i]);
	for(;test--;)
	{
		ans=0;
		cin>>now;
		ans=0;
		n=0;
		for(int i=now;i;i/=10)n++;
		g[n+1]=1;
		for(int i=0;i<n;++i)g[n-i]=g[n-i+1]*10;
		xx[1]=now;
		dfs(2);
		cout<<ans<<endl;
	}
	return 0;
}


#include<string>
#include<iomanip>
#include<algorithm>
#include<cstring>
#include<vector>
#include<iostream>
using namespace std;

int n,now,p[20000],p_,g[10],ans;
int xx[10];
int tmp[10000];
vector<int> yoo[10000][10];

inline int get(int a,int b)
{
	return a/g[b+1]%10;
}

void dfs(int k)
{
	int tmp=0;
	for(int i=1;i<k;++i)tmp=tmp*10+get(xx[i],k);
	if(k==n)
	{
		ans+=::tmp[tmp];
		return;
	}
	for(vector<int>::iterator v=yoo[tmp][n-k+1].begin();v!=yoo[tmp][n-k+1].end();v++)
	{
		xx[k]=*v;
		dfs(k+1);
	}
}

int main()
{
	#ifndef ONLINE_JUDGE
	freopen("cf.in","rb",stdin);
	freopen("cf.out","wb",stdout);
	#endif
	int test;
	cin>>test;
	for(int i=2;i<=99999;++i)
	{
		bool can=1;
		for(int j=0;j<p_;++j)if(i%p[j]==0)
		{
			can=0;
			break;
		}
		if(can)p[p_++]=i;
	}
	memset(tmp,0,sizeof tmp);
	g[0]=100000;g[1]=10000;g[2]=1000;g[3]=100;g[4]=10;g[5]=1;
	for(int i=0;i<p_;++i)
		for(int j=0;j<5;++j)
			if(4==j)tmp[p[i]/g[j]]++;else
			yoo[p[i]/g[j+1]][4-j].push_back(p[i]);
	for(;test--;)
	{
		ans=0;
		cin>>now;
		ans=0;
		n=0;
		for(int i=now;i;i/=10)n++;
		g[n+1]=1;
		for(int i=0;i<n;++i)g[n-i]=g[n-i+1]*10;
		xx[1]=now;
		dfs(2);
		cout<<ans<<endl;
	}
	return 0;
}


#include<string>
#include<iomanip>
#include<algorithm>
#include<cstring>
#include<vector>
#include<iostream>
using namespace std;

int n,now,p[20000],p_,g[10],ans;
int xx[10];
int tmp[10000];
int yoo[2][10000][10];

void dfs(int k)
{
	int tmp=0;
	for(int i=1;i<k;++i)tmp=tmp*10+xx[i]/g[k+1]%10;
	if(k==n)
	{
		ans+=::tmp[tmp];
		return;
	}
	for(int v=yoo[0][tmp][n-k+1];v<=yoo[1][tmp][n-k+1];v++)
	{
		xx[k]=p[v];
		dfs(k+1);
	}
}

int main()
{
	#ifndef ONLINE_JUDGE
	freopen("cf.in","rb",stdin);
	freopen("cf.out","wb",stdout);
	#endif
	int test;
	cin>>test;
	for(int i=2;i<=99999;++i)
	{
		bool can=1;
		for(int j=0;j<p_;++j)if(i%p[j]==0)
		{
			can=0;
			break;
		}
		if(can)p[p_++]=i;
	}
	g[0]=100000;g[1]=10000;g[2]=1000;g[3]=100;g[4]=10;g[5]=1;
	memset(yoo[1],-1,sizeof yoo[1]);
	memset(yoo[0],63,sizeof yoo[0]);
	for(int i=0;i<p_;++i)
		for(int j=0;j<5;++j)
			if(4==j)tmp[p[i]/g[j]]++;else
			yoo[0][p[i]/g[j+1]][4-j]=min(yoo[0][p[i]/g[j+1]][4-j],i),
			yoo[1][p[i]/g[j+1]][4-j]=max(yoo[1][p[i]/g[j+1]][4-j],i);
	for(;test--;)
	{
		ans=0;
		cin>>now;
		ans=0;
		n=0;
		for(int i=now;i;i/=10)n++;
		g[n+1]=1;
		for(int i=0;i<n;++i)g[n-i]=g[n-i+1]*10;
		xx[1]=now;
		dfs(2);
		cout<<ans<<endl;
	}
	return 0;
}


#include<string>
#include<iomanip>
#include<algorithm>
#include<cstring>
#include<vector>
#include<iostream>
using namespace std;

int n,now,p[20000],p_,g[10],ans;
int xx[10];
int tmp[10000];
int yoo[2][10000][10],f[1000][1000];

void dfs(int k)
{
	int tmp=0;
	for(int i=1;i<k;++i)tmp=tmp*10+xx[i]/g[k+1]%10;
	if(k==n)
	{
		ans+=::tmp[tmp];
		return;
	}
	for(int v=yoo[0][tmp][n-k+1];v<=yoo[1][tmp][n-k+1];v++)
	{
		xx[k]=p[v];
		dfs(k+1);
	}
}

int dfss(int k)
{
	int tmp=0;
	for(int i=1;i<k;++i)tmp=tmp*10+xx[i]/g[k+1]%10;
	if(k==n-1)
	{
		int ppp=0;
		for(int i=1;i<k;++i)ppp=ppp*10+xx[i]/g[k+2]%10;
		if(f[tmp][ppp]!=-1)
		{
			ans+=f[tmp][ppp];
			return 0;
		}
		f[tmp][ppp]=0;
		for(int v=yoo[0][tmp][n-k+1];v<=yoo[1][tmp][n-k+1];v++)
		{
			xx[k]=p[v];
			f[tmp][ppp]+=dfss(k+1);
		}
		return 0;
	}
	if(k==n)
	{
		ans+=::tmp[tmp];
		return ::tmp[tmp];
	}
	for(int v=yoo[0][tmp][n-k+1];v<=yoo[1][tmp][n-k+1];v++)
	{
		xx[k]=p[v];
		dfss(k+1);
	}
}

int main()
{
	#ifndef ONLINE_JUDGE
	freopen("cf.in","rb",stdin);
	freopen("cf.out","wb",stdout);
	#endif
	int test;
	cin>>test;
	for(int i=2;i<=99999;++i)
	{
		bool can=1;
		for(int j=0;j<p_;++j)if(i%p[j]==0)
		{
			can=0;
			break;
		}
		if(can)p[p_++]=i;
	}
	g[0]=100000;g[1]=10000;g[2]=1000;g[3]=100;g[4]=10;g[5]=1;
	memset(yoo[1],-1,sizeof yoo[1]);
	memset(yoo[0],63,sizeof yoo[0]);
	memset(f,-1,sizeof f);
	for(int i=0;i<p_;++i)
		for(int j=0;j<5;++j)
			if(4==j)tmp[p[i]/g[j]]++;else
			yoo[0][p[i]/g[j+1]][4-j]=min(yoo[0][p[i]/g[j+1]][4-j],i),
			yoo[1][p[i]/g[j+1]][4-j]=max(yoo[1][p[i]/g[j+1]][4-j],i);
	for(;test--;)
	{
		ans=0;
		cin>>now;
		ans=0;
		n=0;
		for(int i=now;i;i/=10)n++;
		g[n+1]=1;
		for(int i=0;i<n;++i)g[n-i]=g[n-i+1]*10;
		xx[1]=now;
		if(n<5)
		dfs(2);
		else dfss(2);
		cout<<ans<<endl;
	}
	return 0;
}


#include<string>
#include<iomanip>
#include<algorithm>
#include<cstring>
#include<vector>
#include<iostream>
using namespace std;

int n,now,p[20000],p_,g[10],ans;
int xx[10];
int tmp[10000];
vector<int> yoo[10000][10];

void dfs(int k)
{
	int tmp=0;
	for(int i=1;i<k;++i)tmp=tmp*10+xx[i]/g[k+1]%10;
	if(k==n)
	{
		ans+=::tmp[tmp];
		return;
	}
	for(vector<int>::iterator v=yoo[tmp][n-k+1].begin();v!=yoo[tmp][n-k+1].end();v++)
	{
		xx[k]=*v;
		dfs(k+1);
	}
}

int main()
{
	#ifndef ONLINE_JUDGE
	freopen("cf.in","rb",stdin);
	freopen("cf.out","wb",stdout);
	#endif
	int test;
	cin>>test;
	for(int i=2;i<=99999;++i)
	{
		bool can=1;
		for(int j=0;j<p_;++j)if(i%p[j]==0)
		{
			can=0;
			break;
		}
		if(can)p[p_++]=i;
	}
	memset(tmp,0,sizeof tmp);
	g[0]=100000;g[1]=10000;g[2]=1000;g[3]=100;g[4]=10;g[5]=1;
	for(int i=0;i<p_;++i)
		for(int j=0;j<5;++j)
			if(4==j)tmp[p[i]/g[j]]++;else
			yoo[p[i]/g[j+1]][4-j].push_back(p[i]);
	for(;test--;)
	{
		ans=0;
		cin>>now;
		ans=0;
		n=0;
		for(int i=now;i;i/=10)n++;
		g[n+1]=1;
		for(int i=0;i<n;++i)g[n-i]=g[n-i+1]*10;
		xx[1]=now;
		dfs(2);
		cout<<ans<<endl;
	}
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>

using namespace std;

/*
 * */
#define MAXN 100010
int N;
vector<int> Q;
int S[10], mx;
int msk[] = {1, 10, 100, 1000, 10000};
int D[10][MAXN];
void cal_prime()
{
    for(int i = 2; i <= 99999; i++)
    {
        bool flag = true;
        for(int j = 0; j < Q.size() && Q[j] <= sqrt(i); j++)
            if(flag && i % Q[j] == 0)
            {
                flag = false;
                break;
            }
        if(flag)
        {
            Q.push_back(i);
            for(int j = 1, k = 0; j <= 5; j++, k++)
            {
                int tt = i - i / msk[k] % 10 * msk[k];
                D[j][tt]++;
            }
        }
    }
}

int dfs(int b)
{
    if(b == mx) return D[1][S[mx] * 10];
    int res = 0, vv;
    for(int i = 0; i < msk[mx - b]; i++)
        if((vv = D[mx - b + 1][S[b] * msk[mx - b + 1] + i]) != 0)
        {
            for(int j = mx, k = i; j >= b + 1; j--, k /= 10)
                S[j] = S[j] * 10 + k % 10;
            res += dfs(b + 1) * vv;
            for(int j = mx; j >= b + 1; j--)
                S[j] /= 10;
        }
    return res;
}

int main()
{
    //freopen("input.txt", "r", stdin);
    scanf("%d", &N);
    memset(D, 0, sizeof(D));
    cal_prime();

    for(int i = 0; i < N; i++)
    {
        int p, pp, tt;
        scanf("%d", &p);
        mx = 0, pp = p;
        memset(S, 0, sizeof(S));
        while(pp) mx++, pp /= 10;
        pp = p, tt = mx;
        while(pp) S[tt--] = pp % 10, pp /= 10;
        int ans = dfs(2);
        printf("%d\n", ans);
    }

    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>

using namespace std;

/*
 * */
#define MAXN 100010
int N, p, pp;
vector<int> Q;
int S[10][10], mx;
int msk[10];
void cal_prime()
{
    Q.push_back(2);
    for(int i = 3; i <= 99999; i++)
    {
        bool flag = true;
        for(int j = 0; j < Q.size() && Q[j] <= sqrt(i); j++)
            if(flag && i % Q[j] == 0)
            {
                flag = false;
                break;
            }
        if(flag) Q.push_back(i);
    }
}

void convert(int x, int S[], int mx)
{
    int bit = 0;
    while(bit < mx) S[++bit] = x % 10, x /= 10;
}

int dfs(int x , int b)
{
    if(b == mx) return 1;
    int bgt = 0, ent = 0, res = 0, i, j, k;
    convert(x, S[b], mx);
    for(i = 1, j = mx - b, k = 1; i <= b; i++, k++)
        bgt += S[i][j] * msk[k];
    ent = bgt + msk[k - 1] - 1;
    int it = lower_bound(Q.begin(), Q.end(), bgt) - Q.begin();
    for(; it < Q.size() && Q[it] <= ent; it++)
        res += dfs(Q[it], b + 1);
    return res;
}

int main()
{
    //freopen("input.txt", "r", stdin);
    scanf("%d", &N);
    cal_prime();
    for(int i = 0; i < N; i++)
    {
        scanf("%d", &p);
        for(int j = 0; j < 10; j++) msk[j] = 0;
        msk[1] = 1, mx = 0, pp = p;
        while(pp) mx++, pp /= 10;
        for(int j = 1; j < mx; j++) msk[1] *= 10;
        for(int j = 2; j <= mx; j++) msk[j] = msk[j - 1] / 10;
        int ans = dfs(p, 1);
        printf("%d\n", ans);
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <cstdio>

using namespace std;

bool pra[100001];
vector <int> prefix[100000][6][6];

int st_cifer(int st){
	int tr = 1,ret=0;
	while(tr<st){
		ret++;
		tr*=10;
	}
	return ret;
}

void eratosten(){
	for(int i=0;i<100000;i++){
		pra[i]=1;
	}
	pra[0]=pra[1]=0;
	for(int i=2;i<100000;i++){
		if(!pra[i])continue;
		for(int j=2*i;j<100000;j+=i){
			pra[j]=0;
		}
		int cifr = st_cifer(i);
		for(int j=cifr;j<=5;j++){
			int tren = i;
			for(int k=j;k>0;k--){
				prefix[tren][k][j].push_back(i);
				tren /=10;
			}
		}
	}
}

int t[6];
int rez;

void st_komb(int dolz, int d){
	if(d<=1){
		rez+= prefix[t[dolz-d]][dolz-d][dolz].size();
		return;
	}
	int tr_vrst = dolz-d;
	int tr_cifra = t[tr_vrst];
	for(int i=0;i<prefix[tr_cifra][tr_vrst][dolz].size();i++){
		int tmp = prefix[tr_cifra][tr_vrst][dolz][i];
		for(int j=dolz-1;j>=tr_vrst;j--){
			t[j] = t[j] * 10;
			t[j]+=(tmp%10);
			tmp/=10;
		}
		st_komb(dolz,d-1);
		for(int j=dolz-1;j>=tr_vrst;j--){
			t[j] /=10;
		}
	}
}

void solve(){
	int cifra;
	scanf("%d",&cifra);
	int st_c = st_cifer(cifra);
	int tr = cifra;
	for(int i=st_c-1;i>=0;i--){
		t[i]=tr%10;
		tr/=10;
	}
	rez = 0;
	st_komb(st_c,st_c-1);
	printf("%d\n",rez);
}

int main(){
	int t;
	scanf("%d",&t);
	eratosten();
	while(t--){
		solve();
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <cstdio>

using namespace std;

bool pra[1000001];
vector <int> plist;
int tabela[6][6];

void eratosten(){
	for(int i=0;i<1000000;i++){
		pra[i]=1;
	}
	pra[0]=pra[1]=0;
	for(int i=2;i<1000000;i++){
		if(!pra[i])continue;
		plist.push_back(i);
		for(int j=2*i;j<1000000;j+=i){
			pra[j]=0;
		}
	}
}

int binar(int cifra){
	int l = 0;
	int d = plist.size()-1;
	while(l<d){
		int s = (l+d)/2;
		if(cifra>plist[s]){
			l=s+1;
		} else {
			d=s;
		}
	}
	return l;
}

void st_to_arr(int c, int l, int poz){
	int kje=0;
	while(c){
		tabela[poz][kje]=c%10;
		c/=10;
		kje++;
	}
	while(kje<l){tabela[poz][kje]=0;kje++;}
	for(int i=0;i<kje/2;i++){
		swap(tabela[poz][i],tabela[poz][kje-1-i]);
	}
}

int arr_to_st(int poz, int l){
	int ret=0;
	for(int i=0;i<l;i++){
		ret+=tabela[poz][i];
		ret*=10;
	}
	return ret/10;
}

long long st_komb(int dolz, int d, int poz){//dolzina besede, koliko manjka, pozicija - smo na komot
	//cout << "klican st_komb d je " << d << endl;
	/*for(int i=0;i<poz;i++){
		for(int j=0;j<dolz;j++){
			cout << tabela[i][j] << ' ';
		}
		cout << endl;
	} cout << endl;*/
	if(poz>=dolz){return 1LL;}
	int rezultat = 0;
	for(int i=0;i<poz;i++){
		tabela[poz][i]=tabela[i][poz];
	}
	for(int i=poz;i<dolz;i++)tabela[poz][i]=0;
	//cout << "levo " << arr_to_st(poz,dolz) << endl;
	int levo = binar(arr_to_st(poz,dolz));
	tabela[poz][poz-1]++;
	//cout << "desno " << arr_to_st(dolz,poz) << endl;
	int desno = binar(arr_to_st(poz,dolz))-1;
	//cout << "iuhgui " << plist[levo] << ' ' << plist[desno] << endl;
	for(int i=levo; i<=desno;i++){
		st_to_arr(plist[i],dolz,poz);
		rezultat+=st_komb(dolz,d-1,poz+1);
	}
	return rezultat;
}

void solve(){
	int cifra;
	scanf("%d",&cifra);
	int st_c = 0;
	int l = 1;
	while(!(l>cifra)){
		l*=10;
		st_c++;
	}
	//cout << "st c " << st_c << endl;
	st_to_arr(cifra,st_c,0);
	cout << st_komb(st_c,st_c-1,1) << "\n";
}

int main(){
	int t;
	scanf("%d",&t);
	eratosten();
	while(t--){
		solve();
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <cstdio>

using namespace std;

bool pra[1000001];
vector <int> plist;

void eratosten(){
	for(int i=0;i<1000000;i++){
		pra[i]=1;
	}
	pra[0]=pra[1]=0;
	for(int i=2;i<1000000;i++){
		if(!pra[i])continue;
		plist.push_back(i);
		for(int j=2*i;j<1000000;j+=i){
			pra[j]=0;
		}
	}
}

int binar(int cifra){
	int l = 0;
	int d = plist.size()-1;
	while(l<d){
		int s = (l+d)/2;
		if(cifra>plist[s]){
			l=s+1;
		} else {
			d=s;
		}
	}
	return l;
}

vector <int> st_to_arr(int c, int l){
	vector <int> ret;
	while(c){
		ret.push_back(c%10);
		c/=10;
	}
	while(ret.size()<l){ret.push_back(0);}
	for(int i=0;i<ret.size()/2;i++){
		swap(ret[i],ret[ret.size()-1-i]);
	}
	return ret;
}

int arr_to_st(vector <int> v){
	int ret=0;
	for(int i=0;i<v.size();i++){
		ret+=v[i];
		ret*=10;
	}
	return ret/10;
}

long long st_komb(vector <vector <int> > stanje, int d){
	/*cout << "klican st_komb d je " << d << endl;
	for(int i=0;i<stanje.size();i++){
		for(int j=0;j<stanje[i].size();j++){
			cout << stanje[i][j] << ' ';
		}
		cout << endl;
	} cout << endl;*/
	if(stanje.size()>=stanje[0].size()){return 1LL;}
	vector <int> tmp;
	int rezultat = 0;
	for(int i=0;i<stanje.size();i++){
		tmp.push_back(stanje[i][stanje.size()]);
	}
	for(int i=0;i<d;i++)tmp.push_back(0);
	//cout << "levo " << arr_to_st(tmp) << endl;
	int levo = binar(arr_to_st(tmp));
	tmp[stanje.size()-1]++;
	//cout << "desno " << arr_to_st(tmp) << endl;
	int desno = binar(arr_to_st(tmp))-1;
	//cout << "iuhgui " << plist[levo] << ' ' << plist[desno] << endl;
	stanje.push_back(tmp);
	for(int i=levo; i<=desno;i++){
		stanje[stanje.size()-1]=st_to_arr(plist[i],stanje[0].size());
		rezultat+=st_komb(stanje,d-1);
	}
	return rezultat;
}

void solve(){
	int cifra;
	scanf("%d",&cifra);
	vector <vector <int> > cifre;
	cifre.push_back(st_to_arr(cifra,0));
	int st_c = 0;
	int l = 1;
	while(!(l>cifra)){
		l*=10;
		st_c++;
	}
	cout << st_komb(cifre,st_c-1) << "\n";
}

int main(){
	int t;
	scanf("%d",&t);
	eratosten();
	while(t--){
		solve();
	}
	return 0;
}


#include <stdio.h>
#include <string.h>
int flag[100010],prime[100010];
int ans[100010],num[10][10];
int get(int num)
{
    int len=0;
    while (num!=0)
    {
        num/=10;
        len++;
    }
    return len;
}
int pcnt;
void dfs(int now,int tnum,int &ttnum)
{
    if (now==tnum)
    {
        for (int i=0;i<tnum+1;i++)
            for (int j=i+1;j<tnum+1;j++)
                if (num[i][j]!=num[j][i]) return;
        ttnum++;
        return;
    }
    int len=tnum+1;
    for (int i=0;i<pcnt;i++)
    {
        int tpp=prime[i];
        if (get(tpp)>len) return;
        for (int i=len-1;i>=0;i--)
        {
            num[now][i]=tpp%10;
            tpp/=10;
        }
        bool ok=true;
        for (int j=0;j<len;j++)
            if (j<=now&&num[now][j]!=num[j][now]) ok=false;
        if (!ok) continue;
        dfs(now+1,tnum,ttnum);
    }
}
int main()
{
    memset(flag,-1,sizeof(flag));
    pcnt=0;
    flag[0]=flag[1]=0;
    for (int i=2; i<=100000; i++)
    {
        if (flag[i]) prime[pcnt++]=i;
        for (int j=0; j<pcnt&&prime[j]<=100000/i; j++)
        {
            flag[i*prime[j]]=0;
            if (i%prime[j]==0) break;
        }
    }
    int T;
    scanf("%d",&T);
    memset(ans,0,sizeof(ans));
    while (T--)
    {
        int pp;
        scanf("%d",&pp);
        int len=get(pp);
        int tp=pp;
        for (int j=len-1; j>=0; j--)
        {
            num[0][j]=tp%10;
            tp/=10;
        }
        for (int j=0; j<pcnt; j++)
        {
            tp=prime[j];
            if (get(tp)>len) continue;
            for (int k=len-1; k>=0; k--)
            {
                num[len-1][k]=tp%10;
                tp/=10;
            }
            if (num[0][len-1]!=num[len-1][0]) continue;
            dfs(1,len-1,ans[pp]);
        }
        printf("%d\n",ans[pp]);
    }
    return 0;
}


#include <stdio.h>
#include <string.h>
int num[10][10];
int ans[10010][6][6],ANS;
int flag[100010],prime[100010];
int get(int x)
{
    int len=0;
    while (x)
    {
        len++;
        x/=10;
    }
    return len;
}
void dfs(int len,int r,int c)
{
    if (r>=len||c>=len)
    {
        for (int i=0; i<len; i++)
            for (int j=i+1; j<len; j++)
                num[j][i]=num[i][j];
        int tans=1;
        for (int i=1; i<len; i++)
        {
            int tnum=0;
            for (int j=0; j<len; j++)
            {
                if (i==j) continue;
                tnum=tnum*10+num[i][j];
            }
            tans*=ans[tnum][len][i];
        }
        ANS+=tans;
        return;
    }
    for (int i=0; i<10; i++)
    {
        num[r][c]=i;
        if (c+1<len) dfs(len,r,c+1);
        else dfs(len,r+1,r+2);
    }
}
int p[100];
int main()
{
    int pcnt=0;
    memset(flag,-1,sizeof(flag));
    for (int i=2; i<=100000; i++)
    {
        if (flag[i]) prime[pcnt++]=i;
        for (int j=0; j<pcnt&&prime[j]<=100000/i; j++)
        {
            flag[i*prime[j]]=0;
            if (i%prime[j]==0) break;
        }
    }
    memset(ans,0,sizeof(ans));
    for (int i=0; i<pcnt; i++)
    {
        int len=get(prime[i]);
        for (int k=len; k<=5; k++)
        {
            int num=prime[i];
            for (int j=k-1; j>=0; j--)
            {
                p[j]=num%10;
                num/=10;
            }
            for (int j=0; j<k; j++)
            {
                int tnum=0;
                for (int kk=0; kk<k; kk++)
                {
                    if (j==kk) continue;
                    tnum=tnum*10+p[kk];
                }
                ans[tnum][k][j]++;
            }
        }
    }
    int T;
    scanf("%d",&T);
    while (T--)
    {
        int n;
        scanf("%d",&n);
        int len=get(n),tn=n;
        for (int i=len-1; i>=0; i--)
        {
            num[0][i]=tn%10;
            tn/=10;
        }
        ANS=0;
        dfs(len,1,2);
        printf("%d\n",ANS);
    }
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <iostream>
#include <set>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef vector<pii> vii;
vector<string> vs;
vi cur;
vi primes;
vi lb(1e6, 0);
int n, res;
int pw10[7];

void rec(int i) {
//    cerr << lb[cur[i]] << " " << cur[i] + pw10[n - i] << endl;
    for (int j = lb[cur[i]]; j < primes.size() && primes[j] < cur[i] + pw10[n - i]; ++j) {
        if (i == n - 1) {
            ++res;
            continue;
        }
        int p = primes[j];
        vi dig(n - i);
        for (int d = 0; d < n - i; ++d) {
            dig[d] = p % 10;
            p /= 10;
            cur[n - d - 1] += pw10[n - i - 1] * dig[d];
        }
        rec(i + 1);
        for (int d = 0; d < n - i; ++d) {
            cur[n - d - 1] -= pw10[n - i - 1] * dig[d];
        }
    }
}

int main() {
    pw10[0] = 1;
    for (int i = 1; i < 7; ++i)
        pw10[i] = 10 * pw10[i - 1];
    vector<int> erat(1e6, 1);
    erat[0] = erat[1] = 0;
    for (int i = 2; i * i < erat.size(); ++i) if (erat[i]) {
        for (int j = i * i; j < erat.size(); j += i)
            erat[j] = 0;
    }
    for (int i = 2; i < erat.size(); ++i) if (erat[i])
        primes.push_back(i);
    for (int i = 1; i < primes.size(); ++i) {
        for (int j = primes[i - 1] + 1; j <= primes[i]; ++j)
            lb[j] = i;
    }
    for (int j = primes.back() + 1; j < lb.size(); ++j)
        lb[j] = primes.size();
    int T;
    cin >> T;
    for (int test = 1; test <= T; ++test) {
        string s;
        cin >> s;
        n = s.size();
        res = 0;
        cur.assign(n, 0);
        for (int i = 1; i < n; ++i)
            cur[i] += pw10[n - 1] * (s[i] - '0');
 //       cerr << cur[1] << endl;
        rec(1);
        cout << res << endl;
    }
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <iostream>
#include <set>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef vector<pii> vii;
vector<string> vs;
vi cur;
vi primes;
int lb[1000000];
int n, res;
int pw10[7];

void rec(int i) {
//    cerr << lb[cur[i]] << " " << cur[i] + pw10[n - i] << endl;
/*    if (i == n - 1) {
        res += lb[cur[i] + pw10[n - i]] - lb[cur[i]];
        return;
    }*/
    for (int j = lb[cur[i]]; j < primes.size() && primes[j] < cur[i] + pw10[n - i]; ++j) {
        int p = primes[j];
        if (i == n - 2) {
            int t = cur[n - 1] + 10 * (p % 10);
            res += lb[t + 10] - lb[t];
            continue;
        }
        vi dig(n - i);
        for (int d = 0; d < n - i; ++d) {
            dig[d] = p % 10;
            p /= 10;
            cur[n - d - 1] += pw10[n - i - 1] * dig[d];
        }
        rec(i + 1);
        for (int d = 0; d < n - i; ++d) {
            cur[n - d - 1] -= pw10[n - i - 1] * dig[d];
        }
    }
}

int main() {
    memset(lb, 0, sizeof(lb));
    pw10[0] = 1;
    for (int i = 1; i < 7; ++i)
        pw10[i] = 10 * pw10[i - 1];
    vector<int> erat(1e6, 1);
    erat[0] = erat[1] = 0;
    for (int i = 2; i * i < erat.size(); ++i) if (erat[i]) {
        for (int j = i * i; j < erat.size(); j += i)
            erat[j] = 0;
    }
    for (int i = 2; i < erat.size(); ++i) if (erat[i])
        primes.push_back(i);
    for (int i = 1; i < primes.size(); ++i) {
        for (int j = primes[i - 1] + 1; j <= primes[i]; ++j)
            lb[j] = i;
    }
    for (int j = primes.back() + 1; j < 1000000; ++j)
        lb[j] = primes.size();
    int T;
    cin >> T;
    for (int test = 1; test <= T; ++test) {
        string s;
        cin >> s;
        n = s.size();
        if (n == 1) {
            cout << 1 << endl;
            continue;
        }
        res = 0;
        cur.assign(n, 0);
        for (int i = 1; i < n; ++i)
            cur[i] += pw10[n - 1] * (s[i] - '0');
 //       cerr << cur[1] << endl;
        rec(1);
        cout << res << endl;
    }
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <iostream>
#include <set>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef vector<pii> vii;
vector<string> vs;
vi cur;
vi primes;
int lb[1000000];
int n, res;
int pw10[7];

void rec(int i) {
//    cerr << lb[cur[i]] << " " << cur[i] + pw10[n - i] << endl;
/*    if (i == n - 1) {
        res += lb[cur[i] + pw10[n - i]] - lb[cur[i]];
        return;
    }*/
    for (int j = lb[cur[i]]; j < primes.size() && primes[j] < cur[i] + pw10[n - i]; ++j) {
        int p = primes[j];
        if (i == n - 2) {
            int t = cur[n - 1] + 10 * (p % 10);
            res += lb[t + 10] - lb[t];
            continue;
        }
        vi dig(n - i);
        for (int d = 0; d < n - i; ++d) {
            dig[d] = p % 10;
            p /= 10;
            cur[n - d - 1] += pw10[n - i - 1] * dig[d];
        }
        rec(i + 1);
        for (int d = 0; d < n - i; ++d) {
            cur[n - d - 1] -= pw10[n - i - 1] * dig[d];
        }
    }
}

int main() {
    memset(lb, 0, sizeof(lb));
    pw10[0] = 1;
    for (int i = 1; i < 7; ++i)
        pw10[i] = 10 * pw10[i - 1];
    vector<int> erat(1e6, 1);
    erat[0] = erat[1] = 0;
    for (int i = 2; i * i < erat.size(); ++i) if (erat[i]) {
        for (int j = i * i; j < erat.size(); j += i)
            erat[j] = 0;
    }
    for (int i = 2; i < erat.size(); ++i) if (erat[i])
        primes.push_back(i);
    for (int i = 1; i < primes.size(); ++i) {
        for (int j = primes[i - 1] + 1; j <= primes[i]; ++j)
            lb[j] = i;
    }
    for (int j = primes.back() + 1; j < 1000000; ++j)
        lb[j] = primes.size();
    int T;
    cin >> T;
    for (int test = 1; test <= T; ++test) {
        string s;
        cin >> s;
        n = s.size();
        if (n == 1) {
            cout << 1 << endl;
            continue;
        }
        res = 0;
        cur.assign(n, 0);
        for (int i = 1; i < n; ++i)
            cur[i] += pw10[n - 1] * (s[i] - '0');
        if (n == 2) {
            cout << lb[cur[1] + 10] - lb[cur[1]] << endl;
            continue;
        }
 //       cerr << cur[1] << endl;
        rec(1);
        cout << res << endl;
    }
    return 0;
}


#include <iostream>
#include <ctime>
#include <cassert>
#include <cmath>
#include <string>
#include <cstring>
#include <vector>
#include <fstream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <set>
#include <iomanip>
#define nextLine() { for (int c = getchar(); c != '\n' && c != EOF; c = getchar()); }
#define sqr(a) ((a)*(a))
#define has(mask,i) (((mask) & (1<<(i))) == 0 ? false : true)
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
using namespace std;

#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define fi first
#define se second

typedef long long LL;
typedef long double ldb;

const int inf = (1 << 30) - 1;
const ldb eps = 1e-9;
const ldb pi = fabs(atan2(0.0, -1.0));

bool isPrime(int a)
{
    for (int i = 2; sqr(i) <= a; i++)
        if (a % i == 0) return false;
    return true;
}

string toString(int a, int nlen = -1)
{
    string res = "";
    do
    {
        res += char('0' + (a % 10));
        a /= 10;
    } while (a > 0);
    while ((int)res.size() < nlen)
        res += "0";
    reverse(res.begin(), res.end());
    return res;
}

int length(int a)
{
    int res = 0;
    do
    {
        res++;
        a /= 10;
    } while (a > 0);
    return res;
}

int toInt(const string &s)
{
    int res = 0;
    for (int i = 0; i < (int)s.size(); i++)
        res = res * 10 + s[i] - '0';
    return res;
}

vector <int> starts[1000005][7];
vector <int> primes[7];

void precalcPrimes()
{
    for (int i = 2; i <= 99999; i++)
    {
        if (!isPrime(i)) continue;
        string s = toString(i);
        primes[s.size()].pb(i);
        for (int j = 1; j < (int)s.size(); j++)
        {
            starts[toInt(s.substr(0, j))][s.size()].pb(i);
        }
    }
}

int a;
inline void load()
{
    scanf("%d", &a);
}

int pw[7];

inline int get(int a, int pos)
{
    return (a / pw[pos]) % 10;
}

inline int erase(int a, int pos)
{
    int cur = get(a, pos);
    return a - pw[pos] * cur;
}

inline int set(int a, int b, int pos)
{
    a = erase(a, pos);
    return a + pw[pos] * b;
}

int ans;
int size;
int board[7];

void gen(int r)
{
    if (r == size)
    {
        return;
    }

    int cur = 0;
    bool nzero = false;
    int len = size;
    for (int i = 0; i < r; i++)
    {
        int c = get(board[i], size - r - 1);
        if (!nzero)
        {
            nzero |= c != 0;
            if (!nzero) len--;
        }
        cur = cur * 10 + c;
    }

    if (cur == 0)
    {
        for (int len = size - r; len > 0; len--)
        {
            if (r == size - 1)
            {
                ans += primes[len].size();
            }
            else
            {
                for (int i = 0; i < (int)primes[len].size(); i++)
                {
                    board[r] = primes[len][i];
                    gen(r + 1);
                    board[r] = 0;
                }
            }
        }
    }
    else
    {
        if (r == size - 1)
        {
            ans += starts[cur][len].size();
            return;
        }
        for (int i = 0; i < (int)starts[cur][len].size(); i++)
        {
            board[r] = starts[cur][len][i];
            gen(r + 1);
            board[r] = 0;
        }
    }
}

inline void solve()
{
    size = length(a);
    board[0] = a;
    ans = 0;
    gen(1);
    printf("%d\n", ans);
}

int main()
{
    clock_t start = clock();
#ifndef ONLINE_JUDGE
    freopen("e.in","r",stdin);
    freopen("e.out","w",stdout);
#endif

    pw[0] = 1;
    for (int i = 1; i < 7; i++)
        pw[i] = pw[i - 1] * 10;

    precalcPrimes();

    int t;
    scanf("%d", &t);
    for (int tt = 0; tt < t; tt++)
    {
        //clock_t cur = clock();
        load();
        solve();
        //eprintf("test %d elapsed time: %.6lf\n", tt, (double)(clock() - cur) / CLOCKS_PER_SEC);
    }
    eprintf("elapsed time: %.6lf\n", (double)(clock() - start) / CLOCKS_PER_SEC);
    return 0;
}


#include <iostream>
#include <ctime>
#include <cassert>
#include <cmath>
#include <string>
#include <cstring>
#include <vector>
#include <fstream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <iomanip>
#define nextLine() { for (int c = getchar(); c != '\n' && c != EOF; c = getchar()); }
#define sqr(a) ((a)*(a))
#define has(mask,i) (((mask) & (1<<(i))) == 0 ? false : true)
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
using namespace std;

#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define fi first
#define se second

typedef long long LL;
typedef long double ldb;

const int inf = (1 << 30) - 1;
const ldb eps = 1e-9;
const ldb pi = fabs(atan2(0.0, -1.0));

inline bool isPrime(int a)
{
    for (int i = 2; sqr(i) <= a; i++)
        if (a % i == 0) return false;
    return true;
}

inline int length(int a)
{
    int res = 0;
    do
    {
        res++;
        a /= 10;
    } while (a > 0);
    return res;
}

bool prime[1000005];
inline void precalcPrimes()
{
    memset(prime, false, sizeof(prime));
    for (int i = 2; i <= 99999; i++)
        prime[i] = isPrime(i);
}

int a;
inline void load()
{
    scanf("%d", &a);
}

int pw[7];

inline int get(int a, int pos)
{
    return (a / pw[pos]) % 10;
}

inline int erase(int a, int pos)
{
    int cur = get(a, pos);
    return a - pw[pos] * cur;
}

inline int set(int a, int b, int pos)
{
    a = erase(a, pos);
    return a + pw[pos] * b;
}

int ans;
int size;
int board[7];
int prec[7][7][1000005];

void calcPrec()
{
    clock_t start = clock();
    for (int size = 1; size < 6; size++)
    {
        for (int a = 0; a <= 99999; a++)
        {
            for (int i = 0; i < size; i++)
            {
                prec[size][i][a] = 0;
                for (int j = 0; j <= 9; j++)
                {
                    int cur = set(a, j, i);
                    if (prime[cur]) prec[size][i][a]++;
                }
            }
        }
    }
    eprintf("precalcing: %.6lf\n", (double)(clock() - start) / CLOCKS_PER_SEC);
}

void gen(int r, int c)
{
    if (r == size)
    {
        int cans = 1;
        for (int i = 1; i < size; i++)
            cans *= prec[size][size - i - 1][board[i]];
        ans += cans;
        return;
    }

    if (c == size)
    {
        gen(r + 1, r + 2);
        return;
    }

    for (int i = 0; i <= 9; i++)
    {
        if (c == size - 1 && (i == 0 || i == 4 || i == 6 || i == 8))
            continue;
        board[r] = set(board[r], i, size - c - 1);
        board[c] = set(board[c], i, size - r - 1);

        gen(r, c + 1);

        board[r] = erase(board[r], size - c - 1);
        board[c] = erase(board[c], size - r - 1);
    }
}

inline void solve()
{
    memset(board, 0, sizeof(board));
    size = length(a);

    board[0] = a;
    for (int i = 1; i < size; i++)
        board[i] = set(board[i], get(board[0], size - i - 1), size - 1);

    ans = 0;
    gen(1, 2);
    printf("%d\n", ans);
}

int main()
{
    clock_t start = clock();
#ifndef ONLINE_JUDGE
    freopen("e.in","r",stdin);
    freopen("e.out","w",stdout);
#endif

    pw[0] = 1;
    for (int i = 1; i < 7; i++)
        pw[i] = pw[i - 1] * 10;

    precalcPrimes();
    calcPrec();

    int t;
    scanf("%d", &t);
    for (int tt = 0; tt < t; tt++)
    {
        clock_t cur = clock();
        load();
        solve();
        eprintf("test %d elapsed time: %.6lf\n", tt, (double)(clock() - cur) / CLOCKS_PER_SEC);
    }
    eprintf("elapsed time: %.6lf\n", (double)(clock() - start) / CLOCKS_PER_SEC);
    return 0;
}


#include <iostream>
#include <cassert>
#include <cmath>
#include <string>
#include <cstring>
#include <vector>
#include <fstream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <set>
#include <iomanip>
#define nextLine() { for (int c = getchar(); c != '\n' && c != EOF; c = getchar()); }
#define sqr(a) ((a)*(a))
#define has(mask,i) (((mask) & (1<<(i))) == 0 ? false : true)
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
using namespace std;

#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define fi first
#define se second

typedef long long LL;
typedef long double ldb;

const int inf = (1 << 30) - 1;
const ldb eps = 1e-9;
const ldb pi = fabs(atan2(0.0, -1.0));

bool isPrime(int a)
{
	for (int i = 2; sqr(i) <= a; i++)
		if (a % i == 0) return false;
	return true;
}

string toString(int a)
{
	string res = "";
	do
	{
		res += char('0' + (a % 10));
		a /= 10;
	} while (a > 0);
	reverse(res.begin(), res.end());
	return res;
}

int toInt(const string &s)
{
	int res = 0;
	for (int i = 0; i < (int)s.size(); i++)
		res = res * 10 + s[i] - '0';
	return res;
}

vector <int> starts[100005][6];
vector <int> primes[6];

void precalcPrimes()
{
	for (int i = 2; i <= 99999; i++)
	{
		if (!isPrime(i)) continue;
		string s = toString(i);
		primes[s.size()].pb(i);
		for (int j = 1; j < (int)s.size(); j++)
		{
			starts[toInt(s.substr(0, j))][s.size()].pb(i);
		}
	}
}

int a;
inline void load()
{
	scanf("%d", &a);
}

int ans;
string b[10];

void gen(int i)
{
	if (i == (int)b[0].size())
	{
		ans++;
		return;
	}
	int s = toInt(b[i].substr(0, i));
	int len = b[0].size();
	for (int j = 0; j < (int)b[i].size() && b[i][j] == '0'; j++)
		len--;

	//eprintf("i = %d s = %d\n", i, s);
	if (s == 0)
	{
		for (int len = b[0].size() - i; len > 0; len--)
		{
			for (int k = 0; k < (int)primes[len].size(); k++)
			{
				string t = toString(primes[len][k]);
				while (t.size() < b[0].size())
					t = "0" + t;
				//cerr << t << endl;
				for (int j = i; j < (int)t.size(); j++)
				{
					b[i][j] = t[j];
					b[j][i] = t[j];
				}

				gen(i + 1);
				//for (int j = 0; j < (int)t.size(); j++)
				//	b[i + j][i] = ' ';
			}
		}
	}
	else
	{
		for (int k = 0; k < (int)starts[s][len].size(); k++)
		{
			int cur = starts[s][len][k];
			string t = toString(cur);
			while (t.size() < b[0].size())
				t = "0" + t;
			//cerr << "t = " << t << endl;
			for (int j = i; j < (int)t.size(); j++)
			{
				b[i][j] = t[j];
				b[j][i] = t[j];
			}
			gen(i + 1);
			//for (int j = i + 1; j < (int)t.size(); j++)
			//	b[j][i] = ' ';
		}
	}
}

void solve()
{
	b[0] = toString(a);
	for (int i = 1; i < (int)b[0].size(); i++)
	{
		b[i] = "";
		for (int j = 0; j < (int)b[0].size(); j++)
			b[i] += ".";
	}

	for (int i = 1; i < (int)b[0].size(); i++)
		b[i][0] = b[0][i];
	ans = 0;
	gen(1);
	printf("%d\n", ans);
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("e.in","r",stdin);
	freopen("e.out","w",stdout);
#endif
	precalcPrimes();
	int t;
	scanf("%d", &t);
	for (int tt = 0; tt < t; tt++)
	{
		load();
		solve();
	}
	return 0;
}


#include <iostream>
#include <cassert>
#include <cmath>
#include <string>
#include <cstring>
#include <vector>
#include <fstream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <set>
#include <iomanip>
#define nextLine() { for (int c = getchar(); c != '\n' && c != EOF; c = getchar()); }
#define sqr(a) ((a)*(a))
#define has(mask,i) (((mask) & (1<<(i))) == 0 ? false : true)
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
using namespace std;

#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define fi first
#define se second

typedef long long LL;
typedef long double ldb;

const int inf = (1 << 30) - 1;
const ldb eps = 1e-9;
const ldb pi = fabs(atan2(0.0, -1.0));

bool isPrime(int a)
{
    for (int i = 2; sqr(i) <= a; i++)
        if (a % i == 0) return false;
    return true;
}

string toString(int a, int nlen = -1)
{
    string res = "";
    do
    {
        res += char('0' + (a % 10));
        a /= 10;
    } while (a > 0);
    while ((int)res.size() < nlen)
        res += "0";
    reverse(res.begin(), res.end());
    return res;
}

int length(int a)
{
    int res = 0;
    do
    {
        res++;
        a /= 10;
    } while (a > 0);
    return res;
}

int toInt(const string &s)
{
    int res = 0;
    for (int i = 0; i < (int)s.size(); i++)
        res = res * 10 + s[i] - '0';
    return res;
}

vector <int> starts[1000005][7];
vector <int> primes[7];

void precalcPrimes()
{
    for (int i = 2; i <= 99999; i++)
    {
        if (!isPrime(i)) continue;
        string s = toString(i);
        primes[s.size()].pb(i);
        for (int j = 1; j < (int)s.size(); j++)
        {
            starts[toInt(s.substr(0, j))][s.size()].pb(i);
        }
    }
}

int a;
inline void load()
{
    scanf("%d", &a);
}

int pw[7];

inline int get(int a, int pos)
{
    return (a / pw[pos]) % 10;
}

inline int erase(int a, int pos)
{
    int cur = get(a, pos);
    return a - pw[pos] * cur;
}

inline int set(int a, int b, int pos)
{
    a = erase(a, pos);
    return a + pw[pos] * b;
}

int ans;
int size;
int board[7];

void gen(int r)
{
    if (r == size)
    {
        ans++;
        return;
    }

    int cur = 0;
    bool nzero = false;
    int len = size;
    for (int i = 0; i < r; i++)
    {
        int c = get(board[i], size - r - 1);
        if (!nzero)
        {
            if (c != 0)
                nzero = true;
            else
                len--;
        }
        cur = cur * 10 + c;
    }

    if (cur == 0)
    {
        for (int len = size - r; len > 0; len--)
        {
            for (int i = 0; i < (int)primes[len].size(); i++)
            {
                board[r] = primes[len][i];
                gen(r + 1);
                board[r] = 0;
            }
        }
    }
    else
    {
        for (int i = 0; i < (int)starts[cur][len].size(); i++)
        {
            board[r] = starts[cur][len][i];
            gen(r + 1);
            board[r] = 0;
        }
    }
}

void solve()
{
    size = length(a);
    board[0] = a;
    ans = 0;
    gen(1);
    printf("%d\n", ans);
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("e.in","r",stdin);
    freopen("e.out","w",stdout);
#endif
    pw[0] = 1;
    for (int i = 1; i < 7; i++)
        pw[i] = pw[i - 1] * 10;
    precalcPrimes();

    int t;
    scanf("%d", &t);
    for (int tt = 0; tt < t; tt++)
    {
        load();
        solve();
    }
    return 0;
}


#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <sstream>
//#include <ctime>
using namespace std;
#pragma comment(linker, "/STACK:255000000")

typedef long long ll;

#define rep(i, a, b) for(i = (a); i < (b); ++i)
#define repb(i, a, b) for(i = (a) - 1; i >= (b); --i)
#define repd(i, a, b, d) for(i = (a); i < (b); i += (d))
#define repbd(i, a, b, d) for(i = (a) - 1; i >= (b); i -= (d))
#define reps(i, s) for(i = 0; (s)[i]; ++i)
#define repl(i, l) for(i = l.begin(); i != l.end(); ++i)

#define in(f, a) scanf("%"#f, &(a))

bool firstout = 1;

#define out(f, a) printf("%"#f, (a))
#define outf(f, a) printf((firstout) ? "%"#f : " %"#f, (a)), firstout = 0
#define nl printf("\n"), firstout = 1

#define all(x) (x).begin(),(x).end()
#define sqr(x) ((x) * (x))
#define mp make_pair

template<class T>
void minn(T &a, T b)
{
	if(b < a) a = b;
}

template<class T>
void maxx(T &a, T b)
{
	if(a < b) a = b;
}

#define inf 1012345678
#define eps 1e-9

#ifdef XDEBUG
#define mod 23
#else
#define mod 1000000009
#endif

int madd(int &a, int b)
{
	a += b;
	if(a >= mod) a -= mod;
	return a;
}

int msub(int &a, int b)
{
	a -= b;
	if(a < 0) a += mod;
	return a;
}

int mmult(int &a, int b)
{
	return a = (ll)a * b % mod;
}

int mdiv(ll a, ll b, ll m)
{
	a = (a % m + m) % m;
	b = (b % m + m) % m;
	if(a % b == 0) return a / b;
	return (a + m * mdiv(-a, m, b)) / b;
}

#define N 101234
#define M 1012

int n, m, np;
int PP[N];
int P[N];
int A[5];
int D[5];

void st(int i, int a)
{
	int j;
	//A[i] = a;
	rep(j, i + 1, 5) A[j] += a / D[j] % 10 * D[i];
}

int calc(int i)
{
	if(i == 5) return 1;
	int j, k, a;
	a = A[i];
	//rep(j, 0, i) a += A[j] / D[i] % 10 * D[j];
	/*int l = 0, r = np, mid;
	for(; l + 1 < r;)
	{
		mid = (l + r) / 2;
		if(P[mid] <= a) l = mid;
		else r = mid;
	}*/
	int *p = P + PP[a];
	//if(P[j] < a) ++j;
	int res = 0, d = a + 10 * D[i];
	for(; *p < d; ++p)
	{
		//A[i] = a;
		st(i, *p);
		res += calc(i + 1);
		st(i, -*p);
	}
	//st(i, a);
	return res;
}

int main()
{
	//ll start = clock();
#ifdef XDEBUG
	freopen("in.txt", "rt", stdin);
#else
#endif

	int i, j, k;
	char c;
	int a, d, b;

	PP[0] = PP[1] = 0;
	rep(i, 2, N) PP[i] = 1;
	for(i = 2; i * i < N; ++i) if(PP[i]) for(j = i * i; j < N; j += i) PP[j] = 0;
	np = 0;
	rep(i, 0, N) if(PP[i]) P[np++] = i;

	j = np - 1;
	repb(i, N, 0)
	{
		for(; j && P[j - 1] >= i; --j);
		PP[i] = j;
	}

	D[4] = 1;
	repb(i, 4, 0) D[i] = D[i + 1] * 10;

	in(d, n);
	int ts;
#if 0
	int tss;
	in(d, tss);
	rep(ts, 1, tss + 1)
#else
	for(ts = 1; in(d, a) > 0; ++ts)
#endif
	{
		rep(i, 0, 5) A[i] = 0;
		for(b = a, n = 0; b; b /= 10, ++n);
		n = 5 - n;
		//A[n] = a;
		st(n, a);
		int res = calc(n + 1);
		out(d, res); nl;
	}

	return 0;
}


#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <sstream>
using namespace std;
#pragma comment(linker, "/STACK:255000000")

typedef long long ll;

#define rep(i, a, b) for(i = (a); i < (b); ++i)
#define repb(i, a, b) for(i = (a) - 1; i >= (b); --i)
#define repd(i, a, b, d) for(i = (a); i < (b); i += (d))
#define repbd(i, a, b, d) for(i = (a) - 1; i >= (b); i -= (d))
#define reps(i, s) for(i = 0; (s)[i]; ++i)
#define repl(i, l) for(i = l.begin(); i != l.end(); ++i)

#define in(f, a) scanf("%"#f, &(a))

bool firstout = 1;

#define out(f, a) printf("%"#f, (a))
#define outf(f, a) printf((firstout) ? "%"#f : " %"#f, (a)), firstout = 0
#define nl printf("\n"), firstout = 1

#define all(x) (x).begin(),(x).end()
#define sqr(x) ((x) * (x))
#define mp make_pair

template<class T>
void minn(T &a, T b)
{
	if(b < a) a = b;
}

template<class T>
void maxx(T &a, T b)
{
	if(a < b) a = b;
}

#define inf 1012345678
#define eps 1e-9

#ifdef XDEBUG
#define mod 23
#else
#define mod 1000000009
#endif

int madd(int &a, int b)
{
	a += b;
	if(a >= mod) a -= mod;
	return a;
}

int msub(int &a, int b)
{
	a -= b;
	if(a < 0) a += mod;
	return a;
}

int mmult(int &a, int b)
{
	return a = (ll)a * b % mod;
}

int mdiv(ll a, ll b, ll m)
{
	a = (a % m + m) % m;
	b = (b % m + m) % m;
	if(a % b == 0) return a / b;
	return (a + m * mdiv(-a, m, b)) / b;
}

#define N 101234
#define M 1012

int n, m;
bool P[N];
int D[N][5];
int A[5];
int H[5];

void st(int i, int a)
{
	A[i] += a;
	int j;
	rep(j, i + 1, 5) A[j] += a / H[j] % 10 * H[i];
}

int calc(int i)
{
	if(i == 5) return 1;
	int j;
	int res = 0, r;
	rep(j, 0, H[i])
	{
		st(i, j);
		r = D[A[i]][5 - i - 1];
		if(r) res += r * calc(i + 1);
		st(i, -j);
	}
	return res;
}

int main()
{
#ifdef XDEBUG
	freopen("in.txt", "rt", stdin);
#else
#endif

	int i, j, k;
	char c;
	int a, d;

	P[0] = P[1] = 0;
	rep(i, 2, N) P[i] = 1;
	for(i = 2; i * i < N; ++i) if(P[i]) for(j = i * i; j < N; j += i) P[j] = 0;
	rep(i, 0, N) rep(j, 0, 5)
	{
		d = 1;
		rep(k, 0, j) d *= 10;
		D[i][j] = 0;
		rep(k, 0, 10) if(i + k * d < N) D[i][j] += P[i + k * d];
	}

	H[4] = 1;
	repb(i, 4, 0) H[i] = H[i + 1] * 10;

	in(d, n);

	int ts;
#if 0
	int tss;
	in(d, tss);
	rep(ts, 1, tss + 1)
#else
	for(ts = 1; in(d, a) > 0; ++ts)
#endif
	{
		rep(i, 0, 5) A[i] = 0;
		d = 1;
		n = 0;
		for(; d <= a; d *= 10) ++n;
		n = 5 - n;
		st(n, a);
		int res = calc(n + 1);
		out(d, res); nl;
	}

	return 0;
}


#include<vector>
#include<cmath>
#include<map>
#include<cstdlib>
#include<iostream>
#include<sstream>
#include<string>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<set>
#include<stack>
#include<bitset>
#include<functional>
#include<cstdlib>
#include<ctime>
#include<queue>
#include<deque>
using namespace std;
#define pb push_back
#define pf push_front
typedef long long lint;
#define mp make_pair
#define fi first
#define se second
typedef pair<int,int> pint;
#define All(s) s.begin(),s.end()
#define rAll(s) s.rbegin(),s.rend()
#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
int zyo[10];int aru[110000][10];
bool so[110000];
int hyo[6][4][4]={{{}},{{}},{{7}},{{7,0},{0,7}},{{7,2,1},{2,7,0},{1,0,7}},{{7,5,4,2},{5,7,3,1},{4,3,7,0},{2,1,0,7}}};
int cal(int a,int b){return (a%zyo[b+1])/zyo[b];}
int main()
{
	int i,j,k,l,n,t,x;string st;
	memset(aru,0,sizeof(aru));memset(so,false,sizeof(so));
	so[0]=so[1]=true;
	zyo[0]=1;rep(i,9) zyo[i+1]=zyo[i]*10;
	REP(i,2,100000){
		if(so[i]) continue;
		rep(j,5) aru[i/zyo[j+1]*zyo[j+1]+i%zyo[j]][j]++;
		for(j=i+i;j<100000;j+=i) so[j]=true;
	}
//	cout<<aru[10][0]<<endl;
	cin>>t;
	rep(i,t){
		cin>>st;istringstream sin(st);sin>>x;
		int out=0,n=st.size();
		rep(j,zyo[(n-2)*(n-1)/2]){
			int ret=1;
			rep(k,n-1){
				int s=cal(x,k)*zyo[n-1];
				rep(l,n-1){
					if(k==l) continue;s+=zyo[l]*cal(j,hyo[n][k][l]);
				}
				ret*=aru[s][k];
			}
			out+=ret;
		}
		cout<<out<<endl;
	}
	return 0;
}


#include<vector>
#include<cmath>
#include<map>
#include<cstdlib>
#include<iostream>
#include<sstream>
#include<string>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<set>
#include<stack>
#include<bitset>
#include<functional>
#include<cstdlib>
#include<ctime>
#include<queue>
#include<deque>
using namespace std;
#define pb push_back
#define pf push_front
typedef long long lint;
#define mp make_pair
#define fi first
#define se second
typedef pair<int,int> pint;
#define All(s) s.begin(),s.end()
#define rAll(s) s.rbegin(),s.rend()
#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
int zyo[10];int aru[110000][10];
bool so[110000];
int hyo[6][4][4]={{{}},{{}},{{7}},{{7,0},{0,7}},{{7,2,1},{2,7,0},{1,0,7}},{{7,5,4,2},{5,7,3,1},{4,3,7,0},{2,1,0,7}}};
int cal(int a,int b){return (a%zyo[b+1])/zyo[b];}
int main()
{
	int i,j,k,l,n,t,x;string st;
	memset(aru,0,sizeof(aru));memset(so,false,sizeof(so));
	so[0]=so[1]=true;
	zyo[0]=1;rep(i,9) zyo[i+1]=zyo[i]*10;
	REP(i,2,100000){
		if(so[i]) continue;
		rep(j,5) aru[i/zyo[j+1]*zyo[j+1]+i%zyo[j]][j]++;
		for(j=i+i;j<100000;j+=i) so[j]=true;
	}
//	cout<<aru[10][0]<<endl;
	cin>>t;
	rep(i,t){
		cin>>st;istringstream sin(st);sin>>x;
		int out=0,n=st.size();
		rep(j,zyo[n*(n-1)/2]){
			int ret=1;
			rep(k,n-1){
				int s=cal(x,k)*zyo[n-1];
				rep(l,n-1){
					if(k==l) continue;s+=zyo[l]*cal(j,hyo[n][k][l]);
				}
				ret*=aru[s][k];
			}
			out+=ret;
		}
		cout<<out/zyo[n-1]<<endl;
	}
	return 0;
}


#include<vector>
#include<cmath>
#include<map>
#include<cstdlib>
#include<iostream>
#include<sstream>
#include<string>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<set>
#include<stack>
#include<bitset>
#include<functional>
#include<cstdlib>
#include<ctime>
#include<queue>
#include<deque>
using namespace std;
#define pb push_back
#define pf push_front
typedef long long lint;
#define mp make_pair
#define fi first
#define se second
typedef pair<int,int> pint;
#define All(s) s.begin(),s.end()
#define rAll(s) s.rbegin(),s.rend()
#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
int zyo[10];int aru[110000][10];
bool so[110000];
int hyo[6][4][4]={{{}},{{}},{{7}},{{7,0},{0,7}},{{7,2,1},{2,7,0},{1,0,7}},{{7,5,4,2},{5,7,3,1},{4,3,7,0},{2,1,0,7}}};
int cal(int a,int b){return (a%zyo[b+1])/zyo[b];}
int main()
{
	int i,j,k,l,n,t,x;string st;
	memset(aru,0,sizeof(aru));memset(so,false,sizeof(so));
	so[0]=so[1]=true;
	zyo[0]=1;rep(i,9) zyo[i+1]=zyo[i]*10;
	REP(i,2,100000){
		if(so[i]) continue;
		rep(j,5) aru[i/zyo[j+1]*zyo[j+1]+i%zyo[j]][j]++;
		for(j=i+i;j<100000;j+=i) so[j]=true;
	}
//	cout<<aru[10][0]<<endl;
	cin>>t;
	rep(i,t){
		cin>>st;istringstream sin(st);sin>>x;
		int out=0,n=st.size();
		rep(j,zyo[(n-2)*(n-1)/2]){
			int ret=1;
			for(k=n-2;k>=0 && ret>0;k--){
				int s=cal(x,k)*zyo[n-1];
				rep(l,n-1){
					if(k==l) continue;s+=zyo[l]*cal(j,hyo[n][k][l]);
				}
				ret*=aru[s][k];
			}
			out+=ret;
		}
		cout<<out<<endl;
	}
	return 0;
}


/*
 * File:   main.cpp
 * Author: liuyubo
 *
 * Created on March 16, 2012, 2:38 AM
 */

#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>

#include <utility>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <queue>
#include <map>

using namespace std;

void loadData( void );
int proc( void );
void printRes( int );

vector<int> primes;
int primeDigits[10000][5];
int pow10[6];
int dp[10000];

int n;
int matrix[5][5];

void generatePrimeNumber( vector<int> &v , int range );
void initPow10( void );
void initPrimeDigits( void );

int dfs( int );

bool lyb_debug = true;

void printCurMatrix( void );

int main(int argc, char** argv) {

    loadData();

    return 0;
}

void printCurMatrix( void ){

    for( int i = 0 ; i < n ; i ++ ){
        for( int j = 0 ; j < n ; j ++ )
            printf("%d",matrix[i][j]);
        printf("\n");
    }
    return;
}

void initPow10( void ){

    pow10[0] = 1;
    for( int i = 1 ; i < 6 ; i ++ )
        pow10[i] = pow10[i-1]*10;
    return;
}

void generatePrimeNumber( vector<int> &v , int range ){

    memset( dp , 0 , sizeof( dp ) );
    v.clear();
    v.push_back( 2 );
    v.push_back( 3 );
    dp[0] += 2;

    int j,test_len;
    for( int i = 5 ; i <= range ; i += 2 ){

        //printf("i:%d\n",i);
        test_len = (int)sqrt( (double)i );
        for( j = 1 ; v[j] <= test_len ; j ++ )
            if( i%v[j] == 0 )
                break;

        if( v[j] > test_len ){
            v.push_back(i);
            dp[i/10]++;
        }
    }

    //if( lyb_debug ){
    //    printf("== %d prime numbers ==\n",v.size());
        //for( int i = 0 ; i < v.size() ; i ++ )
        //    printf("%d ",v[i]);
    //    printf("\n");
    //}
    return;
}

void initPrimeDigits( void ){

    memset( primeDigits , 0 , sizeof( primeDigits ) );
    int i,num,index;
    for( i = 0 ; i < primes.size() ; i ++ ){

        num = primes[i];
        index = 0;
        while( num ){
            primeDigits[i][index++] = num%10;
            num /= 10;
        }
    }
    return;
}

int dfs( int level ){

    int low_bound = 0,i;
    for( i = 0 ; i < level ; i ++ )
        low_bound += matrix[level][i]*pow10[n-i-1];

    //if( lyb_debug ){
    //    printf("*in level %d: low_bound=%d up_bound=%d startPrime=%d\n\n",level,low_bound,up_bound,*iter);
    //}
    int res = 0,next_lower=0;
    if( level == n - 2 ){
        for( i = 0 ; i < level ; i ++ )
            next_lower += matrix[level+1][i]*pow10[n-i-2];
        for( ; iter != primes.end() && *iter < up_bound; iter ++ ){
            res += dp[next_lower+ primeDigits[ iter-primes.begin() ][0] ];
            //if( lyb_debug ){
            //    matrix[level][level] = primeDigits[ iter-primes.begin() ][ 0 ];
            //    printf("==a result:==\n");
            //    printCurMatrix();
            //    printf("\n");
            //}
        }
    }
    else if( level == n - 1 ){
        res = dp[ matrix[1][0]*10 ];
    }
    else{

        int up_bound = low_bound + pow10[n-level];
        vector<int>::iterator iter = upper_bound( primes.begin() , primes.end() , low_bound );
        for( ; iter != primes.end() && *iter < up_bound ; iter ++ ){

            for( i = level ;i < n ; i ++ )
                matrix[level][i] = matrix[i][level] = primeDigits[ iter-primes.begin() ][ n-1-i ];

            res += dfs( level + 1 );
        }
    }

    return res;
}

int proc( void ){

    return 0;
}

void loadData( void ){

    generatePrimeNumber( primes , 99999 );
    initPrimeDigits();
    initPow10();

    int T;
    char input[6],enter;
    scanf("%d%c",&T,&enter);

    int i;
    while( T -- ){
        scanf("%s%c",input,&enter);
        n = strlen( input );

        //memset( matrix , 0 , sizeof( matrix ) );
        for( i = 0 ; i < n ; i ++ )
            matrix[0][i] = matrix[i][0] = input[i]-'0';

        //if( lyb_debug ){
        //    printf("case %d:\n",T);
        //}
        printRes( dfs( 1 ) );
    }

    return;
}

void printRes( int res ){

    printf("%d\n",res);
    return;
}


/*
 * File:   main.cpp
 * Author: liuyubo
 *
 * Created on March 16, 2012, 2:38 AM
 */

#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>

#include <utility>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <queue>
#include <map>

using namespace std;

void loadData( void );
int proc( void );
void printRes( int );

vector<int> primes;
int primeDigits[10000][5];
int pow10[6];
int dp[10000];

int n;
int matrix[5][5];

void generatePrimeNumber( vector<int> &v , int range );
void initPow10( void );
void initPrimeDigits( void );

int dfs( int );

bool lyb_debug = true;

void printCurMatrix( void );

int main(int argc, char** argv) {

    loadData();

    return 0;
}

void printCurMatrix( void ){

    for( int i = 0 ; i < n ; i ++ ){
        for( int j = 0 ; j < n ; j ++ )
            printf("%d",matrix[i][j]);
        printf("\n");
    }
    return;
}

void initPow10( void ){

    pow10[0] = 1;
    for( int i = 1 ; i < 6 ; i ++ )
        pow10[i] = pow10[i-1]*10;
    return;
}

void generatePrimeNumber( vector<int> &v , int range ){

    memset( dp , 0 , sizeof( dp ) );
    v.clear();
    v.push_back( 2 );
    v.push_back( 3 );
    dp[0] += 2;

    int j,test_len;
    for( int i = 5 ; i <= range ; i += 2 ){

        //printf("i:%d\n",i);
        test_len = (int)sqrt( (double)i );
        for( j = 1 ; v[j] <= test_len ; j ++ )
            if( i%v[j] == 0 )
                break;

        if( v[j] > test_len ){
            v.push_back(i);
            dp[i/10]++;
        }
    }

    //if( lyb_debug ){
    //    printf("== %d prime numbers ==\n",v.size());
        //for( int i = 0 ; i < v.size() ; i ++ )
        //    printf("%d ",v[i]);
    //    printf("\n");
    //}
    return;
}

void initPrimeDigits( void ){

    memset( primeDigits , 0 , sizeof( primeDigits ) );
    int i,num,index;
    for( i = 0 ; i < primes.size() ; i ++ ){

        num = primes[i];
        index = 0;
        while( num ){
            primeDigits[i][index++] = num%10;
            num /= 10;
        }
    }
    return;
}

int dfs( int level ){

    int low_bound = 0,i;
    for( i = 0 ; i < level ; i ++ )
        low_bound += matrix[level][i]*pow10[n-i-1];

    int up_bound = low_bound + pow10[n-level];

    vector<int>::iterator iter = upper_bound( primes.begin() , primes.end() , low_bound );

    //if( lyb_debug ){
    //    printf("*in level %d: low_bound=%d up_bound=%d startPrime=%d\n\n",level,low_bound,up_bound,*iter);
    //}
    int res = 0,next_lower=0;
    if( level == n - 2 ){
        for( i = 0 ; i < level ; i ++ )
            next_lower += matrix[level+1][i]*pow10[n-i-2];
        for( ; iter != primes.end() && *iter < up_bound; iter ++ ){
            res += dp[next_lower+ primeDigits[ iter-primes.begin() ][0] ];
            //if( lyb_debug ){
            //    matrix[level][level] = primeDigits[ iter-primes.begin() ][ 0 ];
            //    printf("==a result:==\n");
            //    printCurMatrix();
            //    printf("\n");
            //}
        }
    }
    else if( level == n - 1 ){
        res = dp[ matrix[1][0]*10 ];
    }
    else{
        for( ; iter != primes.end() && *iter < up_bound ; iter ++ ){

            for( i = level ;i < n ; i ++ )
                matrix[level][i] = matrix[i][level] = primeDigits[ iter-primes.begin() ][ n-1-i ];

            res += dfs( level + 1 );
        }
    }

    return res;
}

int proc( void ){

    return 0;
}

void loadData( void ){

    generatePrimeNumber( primes , 99999 );
    initPrimeDigits();
    initPow10();

    int T;
    char input[6],enter;
    scanf("%d%c",&T,&enter);

    int i;
    while( T -- ){
        scanf("%s%c",input,&enter);
        n = strlen( input );

        //memset( matrix , 0 , sizeof( matrix ) );
        for( i = 0 ; i < n ; i ++ )
            matrix[0][i] = matrix[i][0] = input[i]-'0';

        //if( lyb_debug ){
        //    printf("case %d:\n",T);
        //}
        printRes( dfs( 1 ) );
    }

    return;
}

void printRes( int res ){

    printf("%d\n",res);
    return;
}


/*
 * File:   main.cpp
 * Author: liuyubo
 *
 * Created on March 16, 2012, 2:38 AM
 */

#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>

#include <utility>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <queue>
#include <map>

using namespace std;

void loadData( void );
int proc( void );
void printRes( int );

vector<int> primes;
int primeDigits[10000][5];
int pow10[6];
int dp[10000];

int n;
int matrix[5][5];

void generatePrimeNumber( vector<int> &v , int range );
void initPow10( void );
void initPrimeDigits( void );

int dfs( int );

bool lyb_debug = true;

void printCurMatrix( void );

int main(int argc, char** argv) {

    loadData();

    return 0;
}

void printCurMatrix( void ){

    for( int i = 0 ; i < n ; i ++ ){
        for( int j = 0 ; j < n ; j ++ )
            printf("%d",matrix[i][j]);
        printf("\n");
    }
    return;
}

void initPow10( void ){

    pow10[0] = 1;
    for( int i = 1 ; i < 6 ; i ++ )
        pow10[i] = pow10[i-1]*10;
    return;
}

void generatePrimeNumber( vector<int> &v , int range ){

    memset( dp , 0 , sizeof( dp ) );
    v.clear();
    v.push_back( 2 );
    v.push_back( 3 );
    dp[0] += 2;

    int j,test_len;
    for( int i = 5 ; i <= range ; i += 2 ){

        //printf("i:%d\n",i);
        test_len = (int)sqrt( (double)i );
        for( j = 1 ; v[j] <= test_len ; j ++ )
            if( i%v[j] == 0 )
                break;

        if( v[j] > test_len ){
            v.push_back(i);
            dp[i/10]++;
        }
    }

    //if( lyb_debug ){
    //    printf("== %d prime numbers ==\n",v.size());
        //for( int i = 0 ; i < v.size() ; i ++ )
        //    printf("%d ",v[i]);
    //    printf("\n");
    //}
    return;
}

void initPrimeDigits( void ){

    memset( primeDigits , 0 , sizeof( primeDigits ) );
    int i,num,index;
    for( i = 0 ; i < primes.size() ; i ++ ){

        num = primes[i];
        index = 0;
        while( num ){
            primeDigits[i][index++] = num%10;
            num /= 10;
        }
    }
    return;
}

int dfs( int level ){

    int low_bound = 0,i;
    for( i = 0 ; i < level ; i ++ )
        low_bound += matrix[level][i]*pow10[n-i-1];

    int up_bound = low_bound + pow10[n-level];

    vector<int>::iterator iter = upper_bound( primes.begin() , primes.end() , low_bound );

    //if( lyb_debug ){
    //    printf("*in level %d: low_bound=%d up_bound=%d startPrime=%d\n\n",level,low_bound,up_bound,*iter);
    //}
    int res = 0,next_lower=0;
    if( level == n - 2 ){
        for( i = 0 ; i < level ; i ++ )
            next_lower += matrix[level+1][i]*pow10[n-i-2];
        for( ; iter != primes.end() && *iter < up_bound; iter ++ ){
            res += dp[next_lower+ primeDigits[ iter-primes.begin() ][0] ];
            //if( lyb_debug ){
            //    matrix[level][level] = primeDigits[ iter-primes.begin() ][ 0 ];
            //    printf("==a result:==\n");
            //    printCurMatrix();
            //    printf("\n");
            //}
        }
    }
    else if( level == n - 1 ){
        res = dp[ matrix[1][0] ];
    }
    else{
        for( ; iter != primes.end() && *iter < up_bound ; iter ++ ){

            for( i = level ;i < n ; i ++ )
                matrix[level][i] = matrix[i][level] = primeDigits[ iter-primes.begin() ][ n-1-i ];

            res += dfs( level + 1 );
        }
    }

    return res;
}

int proc( void ){

    return 0;
}

void loadData( void ){

    generatePrimeNumber( primes , 99999 );
    initPrimeDigits();
    initPow10();

    int T;
    char input[6],enter;
    scanf("%d%c",&T,&enter);

    int i;
    while( T -- ){
        scanf("%s%c",input,&enter);
        n = strlen( input );

        //memset( matrix , 0 , sizeof( matrix ) );
        for( i = 0 ; i < n ; i ++ )
            matrix[0][i] = matrix[i][0] = input[i]-'0';

        //if( lyb_debug ){
        //    printf("case %d:\n",T);
        //}
        printRes( dfs( 1 ) );
    }

    return;
}

void printRes( int res ){

    printf("%d\n",res);
    return;
}


/*
 * File:   main.cpp
 * Author: liuyubo
 *
 * Created on March 16, 2012, 2:38 AM
 */

#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>

#include <utility>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <queue>
#include <map>

using namespace std;

void loadData( void );
int proc( void );
void printRes( int );

vector<int> primes;
int primeDigits[10000][5];
int pow10[6];

int n;
int matrix[5][5];

void generatePrimeNumber( vector<int> &v , int range );
void initPow10( void );
void initPrimeDigits( void );

int dfs( int );

bool lyb_debug = false;

void printCurMatrix( void );

int main(int argc, char** argv) {

    loadData();
    printRes( proc() );

    return 0;
}

void printCurMatrix( void ){

    for( int i = 0 ; i < n ; i ++ ){
        for( int j = 0 ; j < n ; j ++ )
            printf("%d",matrix[i][j]);
        printf("\n");
    }
    return;
}

void initPow10( void ){

    pow10[0] = 1;
    for( int i = 1 ; i < 6 ; i ++ )
        pow10[i] = pow10[i-1]*10;
    return;
}

void generatePrimeNumber( vector<int> &v , int range ){

    v.clear();
    v.push_back( 2 );
    v.push_back( 3 );

    int j;
    int test_len;
    for( int i = 5 ; i <= range ; i += 2 ){

        //printf("i:%d\n",i);
        test_len = (int)sqrt( (double)i );
        for( j = 1 ; v[j] <= test_len ; j ++ )
            if( i%v[j] == 0 )
                break;

        if( v[j] > test_len )
            v.push_back(i);
    }

    if( lyb_debug ){
        printf("== %d prime numbers ==\n",v.size());
        //for( int i = 0 ; i < v.size() ; i ++ )
        //    printf("%d ",v[i]);
        printf("\n");
    }
    return;
}

void initPrimeDigits( void ){

    memset( primeDigits , 0 , sizeof( primeDigits ) );
    for( int i = 0 ; i < primes.size() ; i ++ ){

        int num = primes[i];
        int index = 0;
        while( num ){
            primeDigits[i][index] = num%10;
            num /= 10;
            index ++;
        }
    }
    return;
}

int dfs( int level ){

    if( level == n ){
        if( lyb_debug ){
            printf("==a result:==\n");
            printCurMatrix();
            printf("\n");
        }
        return 1;
    }

    int low_bound = 0;
    for( int i = 0 ; i < level ; i ++ )
        low_bound += matrix[level][i]*pow10[n-i-1];

    int up_bound = low_bound + pow10[n-level];

    vector<int>::iterator iter = upper_bound( primes.begin() , primes.end() , low_bound );

    if( lyb_debug ){
        printf("*in level %d: low_bound=%d up_bound=%d startPrime=%d\n\n",level,low_bound,up_bound,*iter);
    }
    int res = 0;
    for( ; iter != primes.end() ; iter ++ ){

        if( *iter > up_bound )
            break;

        for( int i = level ;i < n ; i ++ )
            matrix[level][i] = matrix[i][level] = primeDigits[ iter-primes.begin() ][ n-1-i ];

        res += dfs( level + 1 );
    }

    return res;
}

int proc( void ){

    return dfs( 1 );;
}

void loadData( void ){

    generatePrimeNumber( primes , 99999 );
    initPrimeDigits();
    initPow10();

    int T;
    char input[6],enter;
    scanf("%d%c",&T,&enter);
    while( T -- ){
        scanf("%s%c",input,&enter);
        n = strlen( input );

        memset( matrix , 0 , sizeof( matrix ) );
        for( int i = 0 ; i < n ; i ++ )
            matrix[0][i] = matrix[i][0] = input[i]-'0';

        printRes( proc() );
    }

    return;
}

void printRes( int res ){

    printf("%d\n",res);
    return;
}


/*
 * File:   main.cpp
 * Author: liuyubo
 *
 * Created on March 16, 2012, 2:38 AM
 */

#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>

#include <utility>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <queue>
#include <map>

using namespace std;

void loadData( void );
int proc( void );
void printRes( int );

vector<int> primes;
int primeDigits[10000][5];
int pow10[6];

int n;
int matrix[5][5];

void generatePrimeNumber( vector<int> &v , int range );
void initPow10( void );
void initPrimeDigits( void );

int dfs( int );

bool lyb_debug = false;

void printCurMatrix( void );

int main(int argc, char** argv) {

    loadData();

    return 0;
}

void printCurMatrix( void ){

    for( int i = 0 ; i < n ; i ++ ){
        for( int j = 0 ; j < n ; j ++ )
            printf("%d",matrix[i][j]);
        printf("\n");
    }
    return;
}

void initPow10( void ){

    pow10[0] = 1;
    for( int i = 1 ; i < 6 ; i ++ )
        pow10[i] = pow10[i-1]*10;
    return;
}

void generatePrimeNumber( vector<int> &v , int range ){

    v.clear();
    v.push_back( 2 );
    v.push_back( 3 );

    int j,test_len;
    for( int i = 5 ; i <= range ; i += 2 ){

        //printf("i:%d\n",i);
        test_len = (int)sqrt( (double)i );
        for( j = 1 ; v[j] <= test_len ; j ++ )
            if( i%v[j] == 0 )
                break;

        if( v[j] > test_len )
            v.push_back(i);
    }

    //if( lyb_debug ){
    //    printf("== %d prime numbers ==\n",v.size());
        //for( int i = 0 ; i < v.size() ; i ++ )
        //    printf("%d ",v[i]);
    //    printf("\n");
    //}
    return;
}

void initPrimeDigits( void ){

    memset( primeDigits , 0 , sizeof( primeDigits ) );
    int i,num,index;
    for( i = 0 ; i < primes.size() ; i ++ ){

        num = primes[i];
        index = 0;
        while( num ){
            primeDigits[i][index++] = num%10;
            num /= 10;
        }
    }
    return;
}

int dfs( int level ){

    int low_bound = 0,i;
    for( i = 0 ; i < level ; i ++ )
        low_bound += matrix[level][i]*pow10[n-i-1];

    int up_bound = low_bound + pow10[n-level];

    vector<int>::iterator iter = upper_bound( primes.begin() , primes.end() , low_bound );

    //if( lyb_debug ){
    //    printf("*in level %d: low_bound=%d up_bound=%d startPrime=%d\n\n",level,low_bound,up_bound,*iter);
    //}
    int res = 0;
    if( level == n - 1 ){
        for( ; iter != primes.end() && *iter < up_bound; iter ++ )
            res ++;
    }
    else{
        for( ; iter != primes.end() && *iter < up_bound ; iter ++ ){

            for( i = level ;i < n ; i ++ )
                matrix[level][i] = matrix[i][level] = primeDigits[ iter-primes.begin() ][ n-1-i ];

            res += dfs( level + 1 );
        }
    }

    return res;
}

int proc( void ){

    return dfs( 1 );
}

void loadData( void ){

    generatePrimeNumber( primes , 99999 );
    initPrimeDigits();
    initPow10();

    int T;
    char input[6],enter;
    scanf("%d%c",&T,&enter);

    int i;
    while( T -- ){
        scanf("%s%c",input,&enter);
        n = strlen( input );

        //memset( matrix , 0 , sizeof( matrix ) );
        for( i = 0 ; i < n ; i ++ )
            matrix[0][i] = matrix[i][0] = input[i]-'0';

        //if( lyb_debug ){
        //    printf("case %d:\n",T);
        //}
        printRes( proc() );
    }

    return;
}

void printRes( int res ){

    printf("%d\n",res);
    return;
}