#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int n, res;
int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int to_int(string date) {
int res = 0;
for (int i = 0 ; i < date.size() ; ++i) {
res = res * 10 + date[i] - '0';
}
return res;
}
bool is_prime(string date) {
int x = to_int(date.substr(6, 2));
for (int i = 2 ; i <= x / i ; ++ i) {
if (x % i == 0) return false;
}
x = to_int(date.substr(4, 4));
for (int i = 2 ; i <= x / i ; ++ i) {
if (x % i == 0) return false;
}
x = to_int(date);
for (int i = 2 ; i <= x / i ; ++ i) {
if (x % i == 0) return false;
}
return true;
}
bool run_year(int x) {
if ( x % 400 == 0 && x % 100 != 0 || x % 4 == 0 ) return true;
return false;
}
bool check_valid(int year, int month, int day) {
if (month == 0 || month > 12) return false;
if (day == 0) return false;
if (month != 2) {
if (day > days[month]) return false;
} else {
int leap = year % 400 == 0 && year % 100 != 0 || year % 4 == 0;
if (day > 28 + leap) return false;
}
return true;
}
bool judge(int x)
{
if(x < 1 || x > 12) return false;
}
bool judge_1(int x)
{
if(x > 3) return false;
}
void dfs(string date, int idx) {
if(idx > 5 && judge(to_int(date.substr(4 , 2)))) return ;
if(idx > 6 && judge_1(to_int(date.substr(6 , 1)))) return ;
if (idx >= 8) {
int year = to_int(date.substr(0, 4)), month = to_int(date.substr(4, 2)), day = to_int(date.substr(6, 2));
if (check_valid(year, month, day) && is_prime(date)) res++;
return ;
} else if (date[idx] == '-') {
for (int i = 0 ; i <= 9 ; ++i) {
string p = date;
p[idx] = i + '0';
dfs(p, idx + 1);
}
} else {
dfs(date, idx + 1);
}
}
int main() {
cin >> n;
while (n--) {
string date;
cin >> date;
res = 0;
dfs(date, 0);
cout << res << endl;
}
return 0;
}