#include <bits/stdc++.h>
using namespace std;
int n, t;
long long ansAll;
long long pows[13]; //pows[i] denote i^n
int cnt[10]; //0-9 cnt(composition)
int test[10]; //0-9 cnt(number)
long long fpm(long long a, long long b);
void generatePow(int n);
int check(long long pow);
void dfs(int c, long long res = 0, long long minRes = 0, long long digit = 1);
int main(){
ios::sync_with_stdio(false); cin.tie(0);
cin >> t;
while(t--){
cin >> n;
generatePow(n);
dfs(9);
cout << ansAll << endl;
ansAll = 0;
memset(cnt, 0, sizeof(int));
}
return 0;
}
long long fpm(long long a, long long b) {
long long ans = 1;
for ( ; b; b >>= 1, a = a * a)
if (b & 1) ans = ans * a;
return ans;
}
void generatePow(int n){ //generate an n times sequence of 0-9
for(int i = 1; i < 10; i++) {
pows[i] = fpm(i, n);
}
}
int check(long long pow){
for(int i = 0; i < 10; i++){
test[i] = 0;
}
while(pow > 0){
int i = pow % 10;
test[i]++;
//check each digit of pow, if some number appears more than its composition's: false
if(test[i] > cnt[i]){
return 0;
}
pow /= 10;
}
for(int i = 0; i < 10; i++) {
if (test[i] != cnt[i]) {
return 0;
}
}
return 1;
}
void dfs(int c, long long res , long long minRes, long long digit){
if(minRes > res){
return;
}
ansAll += check(res) * res;
for(int i = 0; i <= c; i++){
cnt[i]++;
dfs(i, res + pows[i], minRes + max(digit / 10, digit * i), digit * 10);
cnt[i]--;
}
}