/* package codechef; // don’t place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be “Main” only if the class is public. */
class Code
{
public static void getOverLuckyNumber(int input){
int[] container = new int[input];
int containerCount = 0;
boolean special = false;
double sqrt = Math.sqrt((double)input);
int half = (int)sqrt + 1;
if (input % sqrt == 0.0) {
special = true;
half = (int)sqrt;
}
int overLuckyNumber = 0;
for (int i = 1, length = half; i < length; i ++) {
if (input % i == 0) {
int multiply = input / i;
container[containerCount] = i;
containerCount++;
container[containerCount] = multiply;
containerCount++;
}
}
if (special) {
container[containerCount] = (int)sqrt;
}
for(int k = 0; k < input; k ++) {
int multiplyCouple = container[k];
while(multiplyCouple != 0) {
int check = multiplyCouple % 10;
if (check == 4 || check == 7) {
overLuckyNumber ++;
}
multiplyCouple = multiplyCouple / 10;
}
}
System.out.println(overLuckyNumber);
}
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String content = br.readLine();
int testcase = Integer.parseInt(content);
for (int i = 0; i < testcase; i ++) {
String input = br.readLine();
getOverLuckyNumber(Integer.parseInt(input));
}
}
}