//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#include<bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <stack>
#include <queue>
#include <deque>
#include <iterator>
#include <bitset>
#include <assert.h>
#include <new>
#include <sstream>
#include <time.h>

//using    namespace __gnu_pbds;
using namespace std;

/*** Optimization ***/
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")

/*** Typedef ***/
typedef long long ll;
typedef unsigned long long ull;

/*** Input ***/
#define sci1(a) scanf("%d",&a)
#define sci2(a,b) scanf("%d %d",&a,&b)
#define scln1(a) scanf("%lld",&a)
#define scln2(a,b) scanf("%lld %lld",&a,&b)
#define scln3(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)

/*** Output ***/
#define pf1(a) printf("%d\n",a)
#define pf2(a,b) printf("%d %d\n",a,b)
#define pfln1(a) printf("%lld\n",a)
#define pfln2(a,b) printf("%lld %lld\n",a,b)

/*** Loops ***/
#define foR0(num) for(ll i = 0; i < num; i++)
#define foR1(num) for(ll i = 1; i <= num; i++)
#define foRev(num) for(ll i = num - 1; i >= 0; i--)
#define repless(i, x, n) for (ll i = x, _n = (n); i < _n; ++i)
#define repequ(i, x, n) for (ll i = x, _n = (n); i <= _n; ++i)
#define forIn(arr, num) for(ll i = 0; i < num; i++) cin>>arr[i];
#define forIn1(arr, num) for(ll i = 1; i <= num; i++) cin>>arr[i];
#define vpnt(ans) for(ll i = 0; i < ans.size(); i++) cout << ans[i] << (i + 1 < ans.size() ? ' ' : '\n');
#define apnt(arr, num) for(ll i = 0; i < num; i++) cout << arr[i] << (i + 1 < num ? ' ' : '\n');

/*** Define Values ***/

#define     ff                first
#define     ss                second
#define     re                return
#define     MP                make_pair
#define     pb                push_back
#define     SZ(x)             ((ll) (x).size())

#define     EPS               10E-10
#define     mxx               100005
#define     MOD               1000000007
#define     iseq(a,b)         (fabs(a-b)<EPS)
#define     PI                3.141592653589793238462643

#define     gcd(a, b)         __gcd(a,b)
#define     min3(a,b,c)       min(a,min(b,c))
#define     max3(a,b,c)       max(a,max(b,c))
#define     lcm(a, b)         ((a)*(b)/gcd(a,b))
#define     min4(a,b,c,d)     min(d,min(a,min(b,c)))
#define     max4(a,b,c,d)     max(d,max(a,max(b,c)))
#define     input             freopen("input.txt","rt", stdin)
#define     output            freopen("output.txt","wt", stdout)

#define     all(v)            v.begin(),v.end()
#define     mem(nam,val)      memset(nam, val, sizeof(nam))
#define     for2D0(n,m)       for(ll i=0;i<n;i++)for(ll j=0;j<m;j++)
#define     for2D1(n,m)       for(ll i=1;i<=n;i++)for(ll j=1;j<=m;j++)
#define     Unique(X)         (X).resize(unique(all(X))-(X).begin())
#define     get_pos(c,x)      (lower_bound(c.begin(),c.end(),x)-c.begin())
#define     IOS               ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define     for2Dpnt(arr,n,m) for(ll i=0;i<n;i++){for(ll j=0;j<m;j++)cout<<arr[i][j]<<" ";cout<<endl;}

/*** STLs ***/
typedef vector <ll> vll;
typedef set <ll> sll;
typedef multiset <ll> msll;
typedef queue <ll> qll;
typedef stack <ll> stll;
typedef map <ll, ll> mll;
typedef pair <ll, ll> pll;
typedef vector <pair <ll , ll> > vpll;

/*** BitWise Operations
///int Set(int N,int pos){return N=N | (1<<pos);}
///int reset(int N,int pos){return N= N & ~(1<<pos);}
///bool check(int N,int pos){return (bool)(N & (1<<pos));}
***/

/*** Grids
///const int fx[] = {+1,-1,+0,+0};
///const int fy[] = {+0,+0,+1,-1};
///const int fx[] = {+0,+0,+1,-1,-1,+1,-1,+1}; ///King's move
///const int fy[] = {-1,+1,+0,+0,+1,+1,-1,-1}; ///king's Move
///const int fx[] = {-2,-2,-1,-1,+1,+1,+2,+2}; ///knight's move
///const int fy[] = {-1,+1,-2,+2,-2,+2,-1,+1}; ///knight's move
***/

/*** Functions
///typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
///ll toint(string s){ll n=0,k=1;for(int i=s.size()-1; i>=0; i--){n += ((s[i]-'0')*k);k*=10;}return n;}
///string tostring(ll x){string s="";while(x){s += (x%10)+'0';x/=10;}reverse(s.begin(),s.end());return s;}
///bool sortinrev(const pair<ll,ll> &a,const pair<ll,ll> &b)return (a.first > b.first);
///priority_queue< pll ,vector<pll>,greater<pll> >p;
///cout<<*X.find_by_order(0)<<endl;
///cout<<X.order_of_key(-5)<<endl;
***/

/*** Some Prints ***/
#define en cout << '\n';
#define no cout << "NO" << '\n'
#define yes cout << "YES" << '\n'

ll A[1000005];
ll B[1000005];

int main()
{
    IOS;
    ll tst=1;
    // cin>>tst;
    for(ll tt=1; tt<=tst; tt++)
    {
        //code
        map<ll,map<char,ll> >mp;
        foR0(26)cin>>B[i];
        string s;
        cin>>s;
        ll n=SZ(s),val=0,res=0;
        foR0(n)A[i]=B[s[i]-'a'];
        foR0(n)
        {
            val+=A[i];
            if(val==0)
            {
                if(i+1<n&&s[0]==s[i+1])res++;
            }
            if(SZ(mp[val])>0&&i+1<n)res+=mp[val][s[i+1]];
            mp[val][s[i]]++;
        }
        repless(i,0,n)
        {
            ll j=i;
            while(s[i]==s[j])j++;
            res+=(j-i-1);
            i=j-1;
        }
        cout<<res<<endl;

    }

    return 0;
}


/*in the name of Aintah the most beneficent the most merciful.*/
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define MAX 1e18
#define MIN -1e18
// #define MOD 1000000007
//#define mod2 20071027
#define MOD 998244353
// #define base 100001
//#define mod 1000074259
#define endl "\n"
#define base2 31
#define memz(a) memset(a, 0, sizeof(a))
#define memn(a) memset(a, -1, sizeof(a))
#define in1(a) scanf("%lld", &a)
#define in2(a, b) scanf("%lld%lld", &a, &b)
#define TC(c) printf("Case #%lld: ", ++c)
#define out(x) cout << #x << " -> " << x << endl;
#define pii pair<ll, ll>
#define piii pair<ll, pii>
#define FAST ios_base::sync_with_stdio(false); cin.tie(NULL);
#define FILE   freopen("input.txt", "r", stdin);  freopen("out.txt", "w", stdout);
using namespace std;
const int N = 300010;
ll n, a[N], res;
string s;
map<ll, map<ll, ll>>freq;
int solve() {
	for (int i = 0; i < 26; i++) {
		in1(a[i]);
	}
	cin >> s;
	n = s.length();

	ll sum = 0;
	for (int i = 0; i < n; i++) {
		sum += a[s[i] - 'a'];
		freq[sum][s[i] - 'a']++;
		res += freq[sum][s[i + 1] - 'a'];
	}
	printf("%lld\n", res);
	return 0;
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	return solve();
}


/*in the name of Aintah the most beneficent the most merciful.*/
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define MAX 1e18
#define MIN -1e18
// #define MOD 1000000007
//#define mod2 20071027
#define MOD 998244353
// #define base 100001
//#define mod 1000074259
#define endl "\n"
#define base2 31
#define memz(a) memset(a, 0, sizeof(a))
#define memn(a) memset(a, -1, sizeof(a))
#define in1(a) scanf("%lld", &a)
#define in2(a, b) scanf("%lld%lld", &a, &b)
#define TC(c) printf("Case #%lld: ", ++c)
#define out(x) cout << #x << " -> " << x << endl;
#define pii pair<ll, ll>
#define piii pair<ll, pii>
#define FAST ios_base::sync_with_stdio(false); cin.tie(NULL);
#define FILE   freopen("input.txt", "r", stdin);  freopen("out.txt", "w", stdout);
using namespace std;
const int N = 300010;
ll n, a[N], freq[N][30], res;
string s;
int solve() {
	for (int i = 0; i < 26; i++) in1(a[i]);
	cin >> s;
	n = s.length();

	ll sum = 0;
	for (int i = 1; i < n; i++) {
		if (s[i] == s[i - 1]) res++;
	}
	for (int i = 1; i < n; i++) {
		sum += a[s[i] - 'a'];
		res += freq[sum][s[i + 1] - 'a'];
		freq[sum][s[i] - 'a']++;
	}
	printf("%lld\n", res);
	return 0;
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	return solve();
}


/*in the name of Aintah the most beneficent the most merciful.*/
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define MAX 1e18
#define MIN -1e18
// #define MOD 1000000007
//#define mod2 20071027
#define MOD 998244353
// #define base 100001
//#define mod 1000074259
#define endl "\n"
#define base2 31
#define memz(a) memset(a, 0, sizeof(a))
#define memn(a) memset(a, -1, sizeof(a))
#define in1(a) scanf("%lld", &a)
#define in2(a, b) scanf("%lld%lld", &a, &b)
#define TC(c) printf("Case #%lld: ", ++c)
#define out(x) cout << #x << " -> " << x << endl;
#define pii pair<ll, ll>
#define piii pair<ll, pii>
#define FAST ios_base::sync_with_stdio(false); cin.tie(NULL);
#define FILE   freopen("input.txt", "r", stdin);  freopen("out.txt", "w", stdout);
using namespace std;
const int N = 300010;
ll n, a[N], res;
string s;
map<ll, map<ll, ll>>freq;
int solve() {
	for (int i = 0; i < 26; i++) in1(a[i]);
	cin >> s;
	n = s.length();

	ll sum = 0;
	for (int i = 1; i < n; i++) {
		if (s[i] == s[i - 1]) res++;
	}
	for (int i = 1; i < n-1; i++) {
		sum += a[s[i] - 'a'];
		res += freq[sum][s[i + 1] - 'a'];
		freq[sum][s[i] - 'a']++;
	}
	printf("%lld\n", res);
	return 0;
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	return solve();
}


/*in the name of Aintah the most beneficent the most merciful.*/
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define MAX 1e18
#define MIN -1e18
// #define MOD 1000000007
//#define mod2 20071027
#define MOD 998244353
// #define base 100001
//#define mod 1000074259
#define endl "\n"
#define base2 31
#define memz(a) memset(a, 0, sizeof(a))
#define memn(a) memset(a, -1, sizeof(a))
#define in1(a) scanf("%lld", &a)
#define in2(a, b) scanf("%lld%lld", &a, &b)
#define TC(c) printf("Case #%lld: ", ++c)
#define out(x) cout << #x << " -> " << x << endl;
#define pii pair<ll, ll>
#define piii pair<ll, pii>
#define FAST ios_base::sync_with_stdio(false); cin.tie(NULL);
#define FILE   freopen("input.txt", "r", stdin);  freopen("out.txt", "w", stdout);
using namespace std;
const int N = 300010;
ll n, a[N], res;
string s;
map<ll, ll>freq[30];
int solve() {
	for (int i = 0; i < 26; i++) in1(a[i]);
	cin >> s;
	n = s.length();

	ll sum = 0;
	for (int i = 1; i < n; i++) {
		if (s[i] == s[i - 1]) res++;
	}
	for (int i = 1; i < n; i++) {
		sum += a[s[i] - 'a'];
		res += freq[sum][s[i + 1] - 'a'];
		freq[sum][s[i] - 'a']++;
	}
	printf("%lld\n", res);
	return 0;
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	return solve();
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5+7;
map<ll,ll> m[26];
int c[26],sum[N];
string s;
int main(){
	for(int i=0 ; i<26 ; ++i)
		cin >> c[i];
	cin >> s ;
	sum[0] = 0;
	ll ans=0;
	int n = s.length();
	for(int i=1 ; i<=n ; ++i){
		int k = s[i-1]-'a';
		sum[i] = sum[i-1] + c[k];
		ans += m[k][sum[i-1]];
		m[k][sum[i]]++;
	}
	cout << ans << "\n";
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5+7;
map<ll,ll> m[26];
ll c[26],sum[N];
string s;
int main(){
	for(int i=0 ; i<26 ; ++i)
		cin >> c[i];
	cin >> s ;
	sum[0] = 0;
	ll ans=0;
	int n = s.length();
	for(int i=1 ; i<=n ; ++i){
		int k = s[i-1]-'a';
		sum[i] = sum[i-1] + c[k];
		ans += m[k][sum[i-1]];
		m[k][sum[i]]++;
	}
	cout << ans << "\n";
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5+7;
map<ll,int> m[26];
int c[26],sum[N];
string s;
int main(){
	for(int i=0 ; i<26 ; ++i)
		cin >> c[i];
	cin >> s ;
	sum[0] = 0;
	int ans=0;
	int n = s.length();
	for(int i=1 ; i<=n ; ++i){
		int k = s[i-1]-'a';
		sum[i] = sum[i-1] + c[k];
		ans += m[k][sum[i-1]];
		m[k][sum[i]]++;
	}
	cout << ans << "\n";
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5+7;
map<ll,int> m[26];
int c[26],sum[N];
string s;
int main(){
	for(int i=0 ; i<26 ; ++i)
		cin >> c[i];
	cin >> s ;
	sum[0] = 0;
	ll ans=0;
	int n = s.length();
	for(int i=1 ; i<=n ; ++i){
		int k = s[i-1]-'a';
		sum[i] = sum[i-1] + c[k];
		ans += m[k][sum[i-1]];
		m[k][sum[i]]++;
	}
	cout << ans << "\n";
}


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

string s;
long a[26];
long ans=0;

struct mang{
    long gt,vt,sl;
};

bool func1(mang i,mang j){
    return (i.gt<j.gt) || (i.gt==j.gt && i.vt<j.vt);
}

int main(){

    for (long i=0;i<26;i++){
        cin >> a[i];
    }
    cin >> s;

    long n=s.length();
    mang t;
    t.gt=0;
    vector< vector<mang> > b(26);
    vector< vector<mang> > c(26);
    for (long i=0;i<n;i++){
        if (i>0 && s[i]==s[i-1]){
            ans++;
        }
        t.gt+=a[s[i]-97];
        t.vt=i;
        b[s[i]-97].push_back(t);
        if (i<n-1){
            c[s[i+1]-97].push_back(t);
        }
    }

    for (long i=0;i<26;i++){
        if ( b[i].size()>1 ){
            sort(b[i].begin(),b[i].end(),func1);
        }
        if ( c[i].size()>1 ){
            sort(c[i].begin(),c[i].end(),func1);
        }
    }

    for (long i=0;i<26;i++){
        if (c[i].size()>0){
            c[i][c[i].size()-1].sl=1;
            for (long j=c[i].size()-2;j>=0;j--){
                c[i][j].sl=1;
                if (c[i][j].gt==c[i][j+1].gt){
                    c[i][j].sl+=c[i][j+1].sl;
                }
            }
        }
    }

    for (long i=0;i<26;i++){
        if ( b[i].size()>1 ){
            long l=0,r=0;
            while (l<b[i].size() && r<c[i].size()){

                if (b[i][l].gt<c[i][r].gt){
                    l++;
                    continue;
                }
                if (b[i][l].gt>c[i][r].gt){
                    r++;
                    continue;
                }
                if (b[i][l].gt==c[i][r].gt){
                    if (b[i][l].vt<c[i][r].vt){
                        ans+=c[i][r].sl;
                        l++;
                    }else {
                        r++;
                    }
                    continue;
                }

            }
        }
    }

    cout << ans;

    return 0;

}


#include <iostream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <string>
#include <string.h>
#include <utility>
#include <map>
#include <vector>
using namespace std;

string s;
long a[26];

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    for (long i=0;i<26;i++){
        cin >> a[i];
    }
    cin >> s;
    long n=s.length();

    vector <long long> t(n,0);
    vector <long long> pot[26];
    t[0]=a[s[0]-'a'];
    pot[s[0]-'a'].push_back(0);
    for (long i=1;i<n;i++){
        t[i]=a[s[i]-'a']+t[i-1];
        pot[s[i]-'a'].push_back(i);
    }

    long long ans=0,sum;
    for (long i=0;i<26;i++){
        map <long long, long > Map;

        for (long j=0;j<pot[i].size();j++){
            if (pot[i][j]>0){
                sum=t[pot[i][j]-1];
                ans+=Map[sum];
            }
            sum=t[pot[i][j]];
            Map[sum]++;
        }

        Map.clear();
    }

    cout << ans;

    return 0;

}


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

string s;
long a[26];
long ans;

int main(){

    for (long i=0;i<26;i++){
        cin >> a[i];
    }
    cin >> s;

    long n=s.length();
    long t=0;
    vector< vector<long> > b(26);
    for (long i=0;i<n;i++){
        long c=s[i];
        t+=a[s[i]-97];
        b[s[i]-97].push_back(t);
    }
    for (long i=0;i<26;i++){
        if ( b[i].size()>1 ){
            sort(b[i].begin(),b[i].end());
        }
    }

    for (long i=0;i<26;i++){
        if ( b[i].size()>1 ){
            long l,r;
            if (a[i]>=0) {l=0;r=1;}
            else {r=0;l=1;}

            while (r<b[i].size()) {
                long tmp=b[i][r]-b[i][l];
                if ( tmp==a[i] ){
                    ans++;
                    r++;
                }else {
                    if ( tmp<a[i] ){r++;}
                    if ( tmp>a[i] ){l++;}
                }
            }
        }
    }

    cout << ans;

    return 0;

}


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

string s;
long a[26];
long long ans=0;

struct mang{
    long long gt,vt,sl;
};

bool func1(mang i,mang j){
    return (i.gt<j.gt) || (i.gt==j.gt && i.vt<j.vt);
}

int main(){

    for (long i=0;i<26;i++){
        cin >> a[i];
    }
    cin >> s;

    long n=s.length();
    mang t;
    t.gt=0;
    vector< vector<mang> > b(26);
    vector< vector<mang> > c(26);
    for (long i=0;i<n;i++){
        if (i>0 && s[i]==s[i-1]){
            ans++;
        }
        t.gt+=a[s[i]-97];
        t.vt=i;
        b[s[i]-97].push_back(t);
        if (i<n-1){
            c[s[i+1]-97].push_back(t);
        }
    }

    for (long i=0;i<26;i++){
        if ( b[i].size()>1 ){
            sort(b[i].begin(),b[i].end(),func1);
        }
        if ( c[i].size()>1 ){
            sort(c[i].begin(),c[i].end(),func1);
        }
    }

    for (long i=0;i<26;i++){
        if (c[i].size()>0){
            c[i][c[i].size()-1].sl=1;
            for (long j=c[i].size()-2;j>=0;j--){
                c[i][j].sl=1;
                if (c[i][j].gt==c[i][j+1].gt){
                    c[i][j].sl+=c[i][j+1].sl;
                }
            }
        }
    }

    for (long i=0;i<26;i++){
        if ( b[i].size()>1 ){
            long l=0,r=0;
            while (l<b[i].size() && r<c[i].size()){

                if (b[i][l].gt<c[i][r].gt){
                    l++;
                    continue;
                }
                if (b[i][l].gt>c[i][r].gt){
                    r++;
                    continue;
                }
                if (b[i][l].gt==c[i][r].gt){
                    if (b[i][l].vt<c[i][r].vt){
                        ans+=c[i][r].sl;
                        l++;
                    }else {
                        r++;
                    }
                    continue;
                }

            }
        }
    }

    cout << ans;

    return 0;

}


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

string s;
long a[26];
long ans=0;

struct mang{
    long gt,vt,sl;
};

bool func1(mang i,mang j){
    return (i.gt<j.gt) || (i.gt==j.gt && i.vt<j.vt);
}

int main(){

    for (long i=0;i<26;i++){
        cin >> a[i];
    }
    cin >> s;

    long n=s.length();
    mang t;
    t.gt=0;
    vector< vector<mang> > b(26);
    vector< vector<mang> > c(26);
    for (long i=0;i<n;i++){
        if (i>0 && s[i]==s[i-1]){
            ans++;
        }
        t.gt+=a[s[i]-97];
        t.vt=i;
        b[s[i]-97].push_back(t);
        if (i<n-1){
            c[s[i+1]-97].push_back(t);
        }
    }

    for (long i=0;i<26;i++){
        if ( b[i].size()>1 ){
            sort(b[i].begin(),b[i].end(),func1);
        }
        if ( c[i].size()>1 ){
            sort(c[i].begin(),c[i].end(),func1);
        }
    }

    for (long i=0;i<26;i++){
        c[i][c[i].size()-1].sl=1;
        for (long j=c[i].size()-2;j>=0;j--){
            c[i][j].sl=1;
            if (c[i][j].gt==c[i][j+1].gt){
                c[i][j].sl+=c[i][j+1].sl;
            }
        }
    }

    for (long i=0;i<26;i++){
        if ( b[i].size()>1 ){
            long l=0,r=0;
            while (l<b[i].size() && r<c[i].size()){

                if (b[i][l].gt<c[i][r].gt){
                    l++;
                    continue;
                }
                if (b[i][l].gt>c[i][r].gt){
                    r++;
                    continue;
                }
                if (b[i][l].gt==c[i][r].gt){
                    if (b[i][l].vt<c[i][r].vt){
                        ans+=c[i][r].sl;
                        l++;
                    }else {
                        r++;
                    }
                    continue;
                }

            }
        }
    }

    cout << ans;

    return 0;

}


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

string s;
long a[26];
long ans;

int main(){

    for (long i=0;i<26;i++){
        cin >> a[i];
    }
    cin >> s;

    long n=s.length();
    long t=0;
    vector< vector<long> > b(26);
    for (long i=0;i<n;i++){
        long c=s[i];
        t+=a[s[i]-97];
        b[s[i]-97].push_back(t);
    }
    for (long i=0;i<26;i++){
        if ( b[i].size()>1 ){
            sort(b[i].begin(),b[i].end());
        }
    }

    for (long i=0;i<26;i++){
        if ( b[i].size()>1 ){
            long l=0,r=1;
            long m=b[i].size();
            while (r<m) {
                long tmp=b[i][r]-b[i][l];
                if ( tmp==a[i] ){
                    ans++;
                    r++;
                }else {
                    if ( tmp<a[i] ){r++;}
                    if ( tmp>a[i] ){l++;}
                }
            }
        }
    }

    cout << ans;

    return 0;

}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <vector>
#include <map>
#include <string>
#include <string.h>
#include <utility>
using namespace std;

string s;
long a[26];
long long ans;

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    for (long i=0;i<26;i++){
        cin >> a[i];
    }
    cin >> s;
    long n=s.length();

    vector <long long> t(n,0);
    vector <long> vt[26];
    t[0]=a[s[0]-'a'];
    vt[s[0]-'a'].push_back(0);
    for (long i=1;i<n;i++){
        t[i]=a[s[i]-'a'];
        t[i]+=t[i-1];
        vt[s[i]-'a'].push_back(i);
    }

    for (long i=0;i<26;i++){
        map <long long, long> Map;

        for (long j=0;j<vt[i].size();j++){
            if (vt[i][j]>0){
                long long sum=t[vt[i][j]-1];
                ans+=Map[sum];
            }
            Map[t[vt[i][j]]]++;
        }

        Map.clear();
    }

    cout << ans;

    return 0;

}


#include<bits/stdc++.h>

using namespace std;
int values[26];

int main(){

  vector<int> g[26];
  for(int i=0;i<26;i++)
    cin>>values[i];

  string cad;
  cin>>cad;
  int size=cad.size();

  int sum=0;
  for(int i=0;i<size;i++){
    g[cad[i]-'a'].push_back(i);
    sum+=values[cad[i]-'a'];
  }

  int l[size+2],r[size+2];

  l[0]=0;
  r[size+1]=0;
  for(int i=1;i<=size;i++){
    l[i]=l[i-1]+values[cad[i-1]-'a'];
    r[size-i+1]=r[size-i+2]+values[cad[size-i]-'a'];
  }

  int cont=0;
  for(int i=0;i<26;i++){
    int s=g[i].size();
    for(int j=0;j<s;j++){
      for(int k=j+1;k<s;k++){
        if(sum-l[g[i][j]+1]-r[g[i][k]+1]==0)
          cont++;
      }
    }
  }
  cout<<cont<<endl;

  return 0;
}


#include<bits/stdc++.h>

using namespace std;
int values[26];

int main(){

  vector<int> g[26];
  for(int i=0;i<26;i++)
    cin>>values[i];

  string cad;
  cin>>cad;
  int size=cad.size();

  int sum=0;
  for(int i=0;i<size;i++){
    g[cad[i]-'a'].push_back(i);
    sum+=values[cad[i]-'a'];
  }

  int l[size+2],r[size+2];

  l[0]=0;
  r[size+1]=0;
  for(int i=1;i<=size;i++){
    l[i]=l[i-1]+values[cad[i-1]-'a'];
    r[size-i+1]=r[size-i+2]+values[cad[size-i]-'a'];
  }

  int cont=0;
  for(int i=0;i<26;i++){
    int s=g[i].size();
    for(int j=0;j<s;j++){
      for(int k=j+1;k<s;k++){
        if(sum-l[j]-r[k]==0)
          cont++;
      }
    }
  }
  cout<<cont<<endl;

  return 0;
}


#include<bits/stdc++.h>

using namespace std;
int values[26];

int main(){

  vector<int> g[26];
  for(int i=0;i<26;i++)
    cin>>values[i];

  string cad;
  cin>>cad;
  int size=cad.size();

  long long l[size+1];
  l[0]=0;
  for(int i=0;i<size;i++){
    g[cad[i]-'a'].push_back(i);
    l[i+1]=l[i]+1ll*values[cad[i]-'a'];
  }

  long long ans=0;
  for(int i=0;i<26;i++){
    long long cont=0;
    unordered_map<long long,int> m;
    for(int& j:g[i]){
      cont+=m[l[j]];
      m[l[j+1]]++;
    }
    ans+=cont;
  }
  cout<<ans<<endl;

  return 0;
}


#include<bits/stdc++.h>

using namespace std;
int values[26];

int main(){

  vector<int> g[26];
  for(int i=0;i<26;i++)
    cin>>values[i];

  string cad;
  cin>>cad;
  int size=cad.size();

  int l[size+1];
  l[0]=0;
  for(int i=0;i<size;i++){
    g[cad[i]-'a'].push_back(i);
    l[i+1]=l[i]+values[cad[i]-'a'];
  }

  long long ans=0;
  for(int i=0;i<26;i++){
    long long cont=0;
    unordered_map<long long,int> m;
    for(int& j:g[i]){
      cont+=m[l[j]];
      m[l[j+1]]++;
    }
    ans+=cont;
  }
  cout<<ans<<endl;

  return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define mod (ll)1000000007
using namespace std;

int main()
{
    ll chars[26];
    for(int i = 0; i < 26; i++) cin>>chars[i];

    ll ans = 0;
    string s;cin>>s;
    int n = s.size();
    ll a[n];
    for(int i = 0; i < n; i++){
        a[i] = chars[s[i]-'a'];
    }

    // length 2 contributions.
    for(int i = 0; i < n-1; i++){
        if(s[i] == s[i+1])
            ans++;
    }

    int p[n];
    p[0] = a[0];
    for(int i = 1; i < n; i++){
        p[i]  = p[i-1]+a[i];
    }

    unordered_map<ll,vector<int>> f;
    for(int i =0; i < n; i++){
        f[p[i]].push_back(i);
    }

    // for(auto i:f)
    // {
    //     cout<<i.first<<"->";
    //     for(int j: i.second)
    //         cout<<j<<" ";
    //     cout<<"\n";
    // }
    for(auto p:f){
        if(p.second.size() <= 1)    continue;

        vector<int> idx_1;
        for(auto idx:p.second){
            if(idx+1 < n)
                idx_1.push_back(idx+1);
        }

        int k = idx_1.size();
        vector<vector<int>> dp(26,vector<int>(k,0));
        // for(auto i:idx_1)
        //     cout<<i<<" ";
        // cout<<"\n";
        for(int i = 0; i < k; i++){
            dp[s[idx_1[i]]-'a'][i]=1;
        }
        for(int i = 0; i < 26; i++){
            for(int j = k-2; j >= 0; j--){
                dp[i][j] +=dp[i][j+1];
            }
        }

        int np = p.second.size();
        for(int i = 0; i < np-1; i++){
            if(i+1 < k)
                ans += dp[s[p.second[i]]-'a'][i+1];
        }
        // cout<<"ans:"<<ans<<"\n";
    }

    cout<<ans<<"\n";

    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define mod (ll)1000000007
using namespace std;

int main()
{
    ll chars[26];
    for(int i = 0; i < 26; i++) cin>>chars[i];

    ll ans = 0;
    string s;cin>>s;
    int n = s.size();
    ll a[n];
    for(int i = 0; i < n; i++){
        a[i] = chars[s[i]-'a'];
    }

    // length 2 contributions.
    for(int i = 0; i < n-1; i++){
        if(s[i] == s[i+1])
            ans++;
    }
    if(count(s.begin(),s.end(),s[0]) == n && count(chars,chars,0) == 0)
    {
        cout<<ans<<"\n";
        return 0;
    }
    int p[n];
    p[0] = a[0];
    for(int i = 1; i < n; i++){
        p[i]  = p[i-1]+a[i];
    }

    unordered_map<ll,vector<int>> f;
    for(int i =0; i < n; i++){
        f[p[i]].push_back(i);
    }

    // for(auto i:f)
    // {
    //     cout<<i.first<<"->";
    //     for(int j: i.second)
    //         cout<<j<<" ";
    //     cout<<"\n";
    // }
    for(auto p:f){
        if(p.second.size() <= 1)    continue;

        vector<int> idx_1;
        for(auto idx:p.second){
            if(idx+1 < n)
                idx_1.push_back(idx+1);
        }

        int k = idx_1.size();
        vector<vector<int>> dp(26,vector<int>(k,0));
        // for(auto i:idx_1)
        //     cout<<i<<" ";
        // cout<<"\n";
        for(int i = 0; i < k; i++){
            dp[s[idx_1[i]]-'a'][i]=1;
        }
        for(int i = 0; i < 26; i++){
            for(int j = k-2; j >= 0; j--){
                dp[i][j] +=dp[i][j+1];
            }
        }

        int np = p.second.size();
        for(int i = 0; i < np-1; i++){
            if(i+1 < k)
                ans += dp[s[p.second[i]]-'a'][i+1];
        }
        // cout<<"ans:"<<ans<<"\n";
    }

    cout<<ans<<"\n";

    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define mod (ll)1000000007
using namespace std;

int main()
{
    ll chars[26];
    for(int i = 0; i < 26; i++) cin>>chars[i];

    ll ans = 0;
    string s;cin>>s;
    int n = s.size();
    ll a[n];
    for(int i = 0; i < n; i++){
        a[i] = chars[s[i]-'a'];
    }

    // length 2 contributions.
    for(int i = 0; i < n-1; i++){
        if(s[i] == s[i+1])
            ans++;
    }
    if(count(s.begin(),s.end(),s[0]) == n)
    {
        cout<<ans<<"\n";
        return 0;
    }
    int p[n];
    p[0] = a[0];
    for(int i = 1; i < n; i++){
        p[i]  = p[i-1]+a[i];
    }

    unordered_map<ll,vector<int>> f;
    for(int i =0; i < n; i++){
        f[p[i]].push_back(i);
    }

    // for(auto i:f)
    // {
    //     cout<<i.first<<"->";
    //     for(int j: i.second)
    //         cout<<j<<" ";
    //     cout<<"\n";
    // }
    for(auto p:f){
        if(p.second.size() <= 1)    continue;

        vector<int> idx_1;
        for(auto idx:p.second){
            if(idx+1 < n)
                idx_1.push_back(idx+1);
        }

        int k = idx_1.size();
        vector<vector<int>> dp(26,vector<int>(k,0));
        // for(auto i:idx_1)
        //     cout<<i<<" ";
        // cout<<"\n";
        for(int i = 0; i < k; i++){
            dp[s[idx_1[i]]-'a'][i]=1;
        }
        for(int i = 0; i < 26; i++){
            for(int j = k-2; j >= 0; j--){
                dp[i][j] +=dp[i][j+1];
            }
        }

        int np = p.second.size();
        for(int i = 0; i < np-1; i++){
            if(i+1 < k)
                ans += dp[s[p.second[i]]-'a'][i+1];
        }
        // cout<<"ans:"<<ans<<"\n";
    }

    cout<<ans<<"\n";

    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define mod (ll)1000000007
using namespace std;

int main()
{
    ll chars[26];
    for(int i = 0; i < 26; i++) cin>>chars[i];

    ll ans = 0;
    string s;cin>>s;
    int n = s.size();
    ll a[n];
    for(int i = 0; i < n; i++){
        a[i] = chars[s[i]-'a'];
    }

    // length 2 contributions.
    for(int i = 0; i < n-1; i++){
        if(s[i] == s[i+1])
            ans++;
    }
    // if(count(s.begin(),s.end(),s[0]) == n && count(chars,chars,0) == 0)
    // {
    //     cout<<ans<<"\n";
    //     return 0;
    // }
    ll p[n];
    p[0] = a[0];
    for(int i = 1; i < n; i++){
        p[i]  = p[i-1]+a[i];
    }

    map<ll,vector<int>> f;
    for(int i =0; i < n; i++){
        f[p[i]].push_back(i);
    }

    // for(auto i:f)
    // {
    //     cout<<i.first<<"->";
    //     for(int j: i.second)
    //         cout<<j<<" ";
    //     cout<<"\n";
    // }

    // if(count(chars,chars+26,65536) == 26){
    //     cout<<"ans:"<<ans<<"\n";

    // }

    for(auto p:f){
        if(p.second.size() <= 1)    continue;

        vector<int> idx_1;
        for(auto idx:p.second){
            if(idx+1 < n)
                idx_1.push_back(idx+1);
        }

        int k = idx_1.size();
        vector<vector<int>> dp(26,vector<int>(k,0));
        // for(auto i:idx_1)
        //     cout<<i<<" ";
        // cout<<"\n";
        for(int i = 0; i < k; i++){
            dp[s[idx_1[i]]-'a'][i]=1;
        }
        for(int i = 0; i < 26; i++){
            for(int j = k-2; j >= 0; j--){
                dp[i][j] +=dp[i][j+1];
            }
        }

        int np = p.second.size();
        for(int i = 0; i < np-1; i++){
            if(i+1 < k)
                ans += dp[s[p.second[i]]-'a'][i+1];
        }
        // cout<<"ans:"<<ans<<"\n";
    }

    cout<<ans<<"\n";

    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define mod (ll)1000000007
using namespace std;

int main()
{
    ll chars[26];
    for(int i = 0; i < 26; i++) cin>>chars[i];

    ll ans = 0;
    string s;cin>>s;
    int n = s.size();
    ll a[n];
    for(int i = 0; i < n; i++){
        a[i] = chars[s[i]-'a'];
    }

    // length 2 contributions.
    for(int i = 0; i < n-1; i++){
        if(s[i] == s[i+1])
            ans++;
    }
    // if(count(s.begin(),s.end(),s[0]) == n && count(chars,chars,0) == 0)
    // {
    //     cout<<ans<<"\n";
    //     return 0;
    // }
    int p[n];
    p[0] = a[0];
    for(int i = 1; i < n; i++){
        p[i]  = p[i-1]+a[i];
    }

    map<ll,vector<int>> f;
    for(int i =0; i < n; i++){
        f[p[i]].push_back(i);
    }

    // for(auto i:f)
    // {
    //     cout<<i.first<<"->";
    //     for(int j: i.second)
    //         cout<<j<<" ";
    //     cout<<"\n";
    // }

    if(count(chars,chars+26,65536) == 26)
        cout<<"ans:"<<ans<<"\n";
    for(auto p:f){
        if(p.second.size() <= 1)    continue;

        vector<int> idx_1;
        for(auto idx:p.second){
            if(idx+1 < n)
                idx_1.push_back(idx+1);
        }

        int k = idx_1.size();
        vector<vector<int>> dp(26,vector<int>(k,0));
        // for(auto i:idx_1)
        //     cout<<i<<" ";
        // cout<<"\n";
        for(int i = 0; i < k; i++){
            dp[s[idx_1[i]]-'a'][i]=1;
        }
        for(int i = 0; i < 26; i++){
            for(int j = k-2; j >= 0; j--){
                dp[i][j] +=dp[i][j+1];
            }
        }

        int np = p.second.size();
        for(int i = 0; i < np-1; i++){
            if(i+1 < k)
                ans += dp[s[p.second[i]]-'a'][i+1];
        }
        // cout<<"ans:"<<ans<<"\n";
    }

    cout<<ans<<"\n";

    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define mod (ll)1000000007
using namespace std;

int main()
{
    ll chars[26];
    for(int i = 0; i < 26; i++) cin>>chars[i];

    ll ans = 0;
    string s;cin>>s;
    int n = s.size();
    ll a[n];
    for(int i = 0; i < n; i++){
        a[i] = chars[s[i]-'a'];
    }

    // length 2 contributions.
    for(int i = 0; i < n-1; i++){
        if(s[i] == s[i+1])
            ans++;
    }
    // if(count(s.begin(),s.end(),s[0]) == n && count(chars,chars,0) == 0)
    // {
    //     cout<<ans<<"\n";
    //     return 0;
    // }
    int p[n];
    p[0] = a[0];
    for(int i = 1; i < n; i++){
        p[i]  = p[i-1]+a[i];
    }

    map<ll,vector<int>> f;
    for(int i =0; i < n; i++){
        f[p[i]].push_back(i);
    }

    // for(auto i:f)
    // {
    //     cout<<i.first<<"->";
    //     for(int j: i.second)
    //         cout<<j<<" ";
    //     cout<<"\n";
    // }
    for(auto p:f){
        if(p.second.size() <= 1)    continue;

        vector<int> idx_1;
        for(auto idx:p.second){
            if(idx+1 < n)
                idx_1.push_back(idx+1);
        }

        int k = idx_1.size();
        vector<vector<int>> dp(26,vector<int>(k,0));
        // for(auto i:idx_1)
        //     cout<<i<<" ";
        // cout<<"\n";
        for(int i = 0; i < k; i++){
            dp[s[idx_1[i]]-'a'][i]=1;
        }
        for(int i = 0; i < 26; i++){
            for(int j = k-2; j >= 0; j--){
                dp[i][j] +=dp[i][j+1];
            }
        }

        int np = p.second.size();
        for(int i = 0; i < np-1; i++){
            if(i+1 < k)
                ans += dp[s[p.second[i]]-'a'][i+1];
        }
        // cout<<"ans:"<<ans<<"\n";
    }

    cout<<ans<<"\n";

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

// dp and data structure combined

int main()
{
	long long val[26];
	memset(val,0,sizeof(val));
	for(int i=0;i<26;i++) cin>>val[i];
	string s;
	cin>>s;
	int n=s.size();
	map<long long,int>m;
	vector<int>v[26];
	long long sum=0ll;
	long long dp[n];
	memset(dp,0,sizeof(dp));
	for(int i=0;i<s.size();i++)
    {
        sum=sum+(val[(s[i]-'a')]);
        dp[i]=sum;
        v[s[i]-'a'].push_back(i);
    }
    // we will iterate through all 26 characters which will be the last and first element
    long long ans=0;
    for(int i=0;i<26;i++)
    {
        for(int j=0;j<v[i].size();j++)
        {
            int pos=v[i][j];
            if(pos!=0){
            long long sum=dp[pos-1];
            //cout<<sum<<" "<<i<<" "<<(pos-1)<<endl;
            ans=ans+m[sum];}
            m[dp[pos]]++;
        }
        m.clear();
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>

using namespace std;

// dp and data structure combined

int main()
{
	long long val[26];
	memset(val,0,sizeof(val));
	for(int i=0;i<26;i++) cin>>val[i];
	string s;
	cin>>s;
	int n=s.size();
	map<long,int>m;
	vector<int>v[26];
	long long sum=0ll;
	long long dp[n];
	memset(dp,0,sizeof(dp));
	for(int i=0;i<s.size();i++)
    {
        sum=sum+(val[(s[i]-'a')]);
        dp[i]=sum;
        v[s[i]-'a'].push_back(i);
    }
    // we will iterate through all 26 characters which will be the last and first element
    long long ans=0;
    for(int i=0;i<26;i++)
    {
        for(int j=0;j<v[i].size();j++)
        {
            int pos=v[i][j];
            if(pos!=0){
            long sum=dp[pos-1];
            //cout<<sum<<" "<<i<<" "<<(pos-1)<<endl;
            ans=ans+m[sum];}
            m[dp[pos]]++;
        }
        m.clear();
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>

using namespace std;

// dp and data structure combined

int main()
{
	long long val[26];
	memset(val,0,sizeof(val));
	for(int i=0;i<26;i++) cin>>val[i];
	string s;
	cin>>s;
	int n=s.size();
	map<long,int>m;
	vector<int>v[26];
	long long sum=0ll;
	long long dp[n];
	memset(dp,0,sizeof(dp));
	for(int i=0;i<s.size();i++)
    {
        sum=sum+(val[(s[i]-'a')]);
        dp[i]=sum;
        v[s[i]-'a'].push_back(i);
    }
    // we will iterate through all 26 characters which will be the last and first element
    long long ans=0;
    for(int i=0;i<26;i++)
    {
        m[0]=1;
        for(int j=0;j<v[i].size();j++)
        {
            int pos=v[i][j];
            if(pos!=0){
            long sum=dp[pos-1];
            //cout<<sum<<" "<<i<<" "<<(pos-1)<<endl;
            ans=ans+m[sum];}
            m[dp[pos]]++;
        }
        m.clear();
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>

using namespace std;

// dp and data structure combined

int main()
{
	long long val[26];
	memset(val,0,sizeof(val));
	for(int i=0;i<26;i++) cin>>val[i];
	string s;
	cin>>s;
	int n=s.size();
	map<long,int>m;
	vector<int>v[26];
	long long sum=0ll;
	long long dp[n];
	memset(dp,0,sizeof(dp));
	for(int i=0;i<s.size();i++)
    {
        sum=sum+(val[(s[i]-'a')]);
        dp[i]=sum;
        v[s[i]-'a'].push_back(i);
    }
    // we will iterate through all 26 characters which will be the last and first element
    long long ans=0;
    for(int i=0;i<26;i++)
    {
        for(int j=0;j<v[i].size();j++)
        {
            int pos=v[i][j];
            if(pos!=0){
            long long sum=dp[pos-1];
            //cout<<sum<<" "<<i<<" "<<(pos-1)<<endl;
            ans=ans+m[sum];}
            m[dp[pos]]++;
        }
        m.clear();
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>

using namespace std;

// dp and data structure combined

int main()
{
	long val[26];
	memset(val,0,sizeof(val));
	for(int i=0;i<26;i++) cin>>val[i];
	string s;
	cin>>s;
	int n=s.size();
	map<long,int>m;
	vector<int>v[26];
	long sum=0ll;
	long dp[n];
	memset(dp,0,sizeof(dp));
	for(int i=0;i<s.size();i++)
    {
        sum=sum+(val[(s[i]-'a')]);
        dp[i]=sum;
        v[s[i]-'a'].push_back(i);
    }
    // we will iterate through all 26 characters which will be the last and first element
    long ans=0;
    for(int i=0;i<26;i++)
    {
        for(int j=0;j<v[i].size();j++)
        {
            int pos=v[i][j];
            if(pos!=0){
            long sum=dp[pos-1];
            //cout<<sum<<" "<<i<<" "<<(pos-1)<<endl;
            ans=ans+m[sum];}
            m[dp[pos]]++;
        }
        m.clear();
    }
    cout<<ans<<endl;
}


#include <iostream>
#include <map>
#include <cstdint>

using namespace std;

int ord(char c) {
    return (int)c - (int)'a';
}

int main() {
    int w[26];
    for (int i = 0; i < 26; ++i) {
        cin >> w[i];
    }
    string s;
    cin >> s;

    const int n = s.size();

    map<char, map<int64_t, uint64_t>> m;
    uint64_t result;
    int sum = 0, prevSum = 0;

    for (int i = 0; i < n; ++i) {
        const int weight = w[ord(s[i])];
        sum += weight;
        result += m[s[i]][prevSum];

        m[s[i]][sum] += 1;
        prevSum = sum;
    }

    cout << result << endl;
/*
    m[c][x] - count of substrings end up with chacter 'c' with total sum from first to end of substring equal to x.
    So, if sum of substring is equal to zero, than sum from first character must repeat.

    xabca
    store sum of 'xa' and then check that it's equal to sum of 'xabca'.
*/

    return 0;
}


#include <iostream>
#include <map>
#include <cstdint>

using namespace std;

int ord(char c) {
    return (int)c - (int)'a';
}

int main() {
    int w[26];
    for (int i = 0; i < 26; ++i) {
        cin >> w[i];
    }
    string s;
    cin >> s;

    const int n = s.size();

    map<char, map<int64_t, uint64_t>> current, prev;
    uint64_t result = 0;

    for (int i = 0; i < n; ++i) {
        for (char c = 'a'; c <= 'z'; ++c) {
            int weight = w[ord(s[i])];

            for (auto elem: prev[c]) {
                current[c][elem.first + weight] = elem.second;
            }

            if (c == s[i]) {
                current[c][0] += 1;
                result += prev[c][0];
            }
        }

        prev = current;
        current.clear();
    }

    cout << result << endl;
/*
    a[i][x][m] - how much substring which starts with 'x' and ends at i-th position, and has sum equal m.
    w[i] - weight of char at i-th position
    s[i] - char at i-th position

    a[i]['x'][m] = a[i - 1][m - w[i]]
    a[i]['x'][0] = a[i - 1][- w[i]] + ('x' == s[i])

    for (c in a-a):
        for v in a[i - 1][c]:
            a[i][c][v + w[i]] = a[i - 1][c][v]
        if c == s[i]:
            a[i][c][0] += 1
            res += a[i - 1][c][0]

*/

    return 0;
}


#include <iostream>
#include <map>
#include <cstdint>

using namespace std;

int ord(char c) {
    return (int)c - (int)'a';
}

int main() {
    int w[26];
    for (int i = 0; i < 26; ++i) {
        cin >> w[i];
    }
    string s;
    cin >> s;

    const int n = s.size();

    map<char, map<int64_t, uint64_t>> m;
    uint64_t result = 0;
    int64_t sum = 0, prevSum = 0;

    for (int i = 0; i < n; ++i) {
        const int weight = w[ord(s[i])];
        sum += weight;
        result += m[s[i]][prevSum];

        m[s[i]][sum] += 1;
        prevSum = sum;
    }

    cout << result << endl;
/*
    m[c][x] - count of substrings end up with chacter 'c' with total sum from first to end of substring equal to x.
    So, if sum of substring is equal to zero, than sum from first character must repeat.

    xabca
    store sum of 'xa' and then check that it's equal to sum of 'xabca'.
*/

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
  int t;
  std::cin >> t;
  while(t--)
  {
    ll y;
    std::cin >> y;
    ll count=0;
    while(y%2==0){ count++;y=y/2}
    while(y%3==0)
    {
      y=y/3;
       count=count+2;
     }
     while(y%5==0)
     {
       y=y/5;
        count=count+3;
      }
      if(y!=1)  std::cout << "-1" << '\n';
      else std::cout <<count << '\n';
  }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
  int t;
  std::cin >> t;
  while(t--)
  {
    ll y;
    std::cin >> y;
    ll count=0;
    while(1)
    {
      if(y%2==0)
      {
        y=y/2;
        count++;
      }
      else if(y%3==0)
      {
        y=2*(y/3);
        count++;
      }
      else if(y%5==0)
      {
        y=4*(y/5);
        count++;
      }
      else
      {
        if(y!=1)
        {
          std::cout << "-1" << '\n';

          return 0;
        }
      }
      if(y==1)
      {
        break;
      }
    }
      std::cout <<count << '\n';
  }
}


#include <iostream>

using namespace std;

int main()
{
    int n,c=0;
    cin>>n;

    for(int i=0;i<n;i++)
    {
        long long int x;
        cin>>x;

        while(x>1)
        {
            if(x%5==0)
            {
                x=(4*x)/5;
                c++;
            }
            if(x%3==0)
            {
                x=(2*x)/3;
                c++;
            }

            if(x%2==0)
            {
                x=x/2;
                c++;
            }
        }
        cout<<c<<endl;
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

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

	long long int t;
	scanf("%lld",&t);
	while (t--)
	{
		long long int n;
		scanf("%lld",&n);
		long long int ans=0,x=0,y=0,z=0;
		if(n == 1)
		{
			printf("1\n");
		}
		else
		{
			while (n%2 == 0)
			{
				x++;
				n = n/2;
			}
			while (n%3 == 0)
			{
				y++;
				n = n/3;
			}
			while (n%5 == 0)
			{
				z++;
				n = n/5;
			}
			ans = x + 2*y + 3*z;
			if(n != 1)
			{
				printf("-1\n");
			}
			else
			{
				printf("%lld\n",ans);
			}

		}
		return 0;
	}
}


#include<bits/stdc++.h>
#include <stdio.h>
using namespace std;

int factor(int a)
{
    long long int count = 0,flag = 0,c = 0,ans = 0;
    while (a%2 == 0)
    {
        count++;
        a = a/2;
    }
    //printf("%lld\n"count);
    while (a%3 == 0)
    {
        flag++;
        a = a/3;
    }
    while (a%5 == 0)
    {
        c++;
        a = a/5;
    }
    ans = count + 2*flag + 3*c;
    if()

}

int main(int argc, char const *argv[])
{
    int q;
    scanf("%d",&q);
    while (q--)
    {
        long long int n,answer;
        scanf("%lld",&n);
        if(n == 1)
        {
            printf("0\n");
        }
        else
        {
            answer = factor(n);
            printf("%lld\n",answer);
        }

    }
    return 0;
}


#include<bits/stdc++.h>
#include <stdio.h>
using namespace std;

int factor(int a)
{
    long long int count = 0,flag = 0,c = 0,ans = 0;
    while (a%2 == 0)
    {
        count++;
        a = a/2;
    }
    //printf("%lld\n"count);
    while (a%3 == 0)
    {
        flag++;
        a = a/3;
    }
    while (a%5 == 0)
    {
        c++;
        a = a/5;
    }
    ans = count + 2*flag + 3*c;
    if(ans == 0)
    {
        return -1;
    }
    else
    {
        return ans;
    }

}

int main(int argc, char const *argv[])
{
    int q;
    scanf("%d",&q);
    while (q--)
    {
        long long int n,answer;
        scanf("%lld",&n);
        if(n == 1)
        {
            printf("0\n");
        }
        else
        {
            answer = factor(n);
            printf("%lld\n",answer);
        }

    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

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

	long long int t;
	scanf("%lld",&t);
	while (t--)
	{
		long long int n;
		scanf("%lld",&n);
		long long int ans=0,x=0,y=0,z=0;
		if(n == 1)
		{
			printf("1\n");
		}
		else
		{
			while (n%2 == 0)
			{
				x++;
				n = n/2;
			}
			while (n%3 == 0)
			{
				y++;
				n = n/3;
			}
			while (n%5 == 0)
			{
				z++;
				n = n/5;
			}
			ans = x + 2*y + 3*z;
			if(n != 1)
			{
				printf("-1\n");
			}
			else
			{
				printf("%lld\n",ans);
			}

		}
		return 0;
	}


#include<bits/stdc++.h>
#include <stdio.h>
using namespace std;

int factor(int a)
{
    long long int count = 0,flag = 0,c = 0,ans = 0;
    while (a%2 == 0)
    {
        count++;
        a = a/2;
    }
    //printf("%lld\n"count);
    while (a%3 == 0)
    {
        flag++;
        a = a/3;
    }
    while (a%5 == 0)
    {
        c++;
        a = a/5;
    }
    ans = count + 2*flag + 3*c;
    if(ans == 0)
    {
        return -1;
    }
    else
    {
        return ans;
    }

}

int main(int argc, char const *argv[])
{
    int q;
    scanf("%d",&q);
    while (q--)
    {
        long long int n,answer;
        scanf("%lld",&n);
        answer = factor(n);
        printf("%lld\n",answer);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

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

	long long int t;
	scanf("%lld",&t);
	while (t--)
	{
		long long int n;
		scanf("%lld",&n);
		long long int ans=0,x=0,y=0,z=0;
		if(n == 1)
		{
			printf("0\n");
		}
		else
		{
			while (n%2 == 0)
			{
				x++;
				n = n/2;
			}
			while (n%3 == 0)
			{
				y++;
				n = n/3;
			}
			while (n%5 == 0)
			{
				z++;
				n = n/5;
			}
			ans = x + 2*y + 3*z;
			if(n != 1)
			{
				printf("-1\n");
			}
			else
			{
				printf("%lld\n",ans);
			}

		}

	}

}


#include<bits/stdc++.h>
using namespace std;

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

	long long int t;
	scanf("%lld",&t);
	while (t--)
	{
		long long int n;
		scanf("%lld",&n);
		long long int ans=0,x=0,y=0,z=0;
		if(n == 1)
		{
			printf("0\n");
		}
		else
		{
			while (n%2 == 0)
			{
				x++;
				n = n/2;
			}
			while (n%3 == 0)
			{
				y++;
				n = n/3;
			}
			while (n%5 == 0)
			{
				z++;
				n = n/5;
			}
			ans = x + 2*y + 3*z;
			if(n != 1)
			{
				printf("-1\n");
			}
			else
			{
				printf("%lld\n",ans);
			}

		}
		return 0;
	}
}


#include<bits/stdc++.h>
#include <stdio.h>
using namespace std;

int factor(int a)
{
    long long int count = 0,flag = 0,c = 0,ans = 0;
    while (a%2 == 0)
    {
        count++;
        a = a/2;
    }
    //printf("%lld\n"count);
    while (a%3 == 0)
    {
        flag++;
        a = a/3;
    }
    while (a%5 == 0)
    {
        c++;
        a = a/5;
    }
    ans = count + 2*flag + 3*c;
    if(ans == 0 || count == 0 || flag == 0 || c == 0)
    {
        return -1;
    }
    else
    {
        return ans;
    }

}

int main(int argc, char const *argv[])
{
    int q;
    scanf("%d",&q);
    while (q--)
    {
        long long int n,answer;
        scanf("%lld",&n);
        if(n == 1)
        {
            printf("0\n");
        }
        else
        {
            answer = factor(n);
            printf("%lld\n",answer);
        }

    }
    return 0;
}


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define pi                 3.1415926535897932384626433832795
#define ll                 long long
#define pb                 push_back
#define pii                pair < int , int >
#define vecsort(v)         sort(v.begin(),v.end())
#define lcm(x,y)           (x/__gcd(x,y))*y
#define forn(i,n)          for(i = 0 ; i < n ; i++)
#define deb(x)             cout << #x << " " << x << endl;
#define vecsum(v)          accumulate(v.begin(), v.end(), 0)
#define adj_diff(a,b)      adjacent_difference(a.begin(), a.end(), b.begin())
#define IOTA(a,x)          iota(a.begin(), a.end(), x);
#define par_sum(a,sum)     partial_sum(a,a+sizeof(a),sum)

#define fast_cin ios_base::sync_with_stdio(false);cin.tie(NULL)

ll div( ll n ){

    if( n == 1 ){
       return 0;
    }

    if( n % 2 == 0 ){
        return 1 + div(n/2);
    }
    else if( n % 3 == 0){
        return 1 + div( ( 2 * n) /3 );
    }
    else if( n % 5 == 0){
        return 1 + div( ( 4 * n)/5);
    }
    else if( n % 6 == 0){
        return 1 + min(div(n/2),div(2*n)/3);
    }
    else if( n% 10 == 0){
        return 1 + min( div(n/2),div((4*n)/5));
    }
    else if(n%15==0){
        return 1 + min(div((2*n)/3),div((4*n)/5));
    }
    else if( n % 2 == 0 && n % 3 == 0 && n % 15 == 0 ){
        return 1 + min(div(n/2),min(div(2*n/3),div(4*n/5)));
    }

    return -1;

}

int main()
{
    fast_cin;

   int t;
   cin >> t;
   while(t--){
       ll n ;
       cin >> n ;
       if( n == 1 ){
        cout << 0 << endl;
        continue;
       }

       if(div(n)){
        cout << div(n) << endl;
       }
       else{
        cout << -1 << endl;
       }

   }

}


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define pi                 3.1415926535897932384626433832795
#define ll                 long long
#define pb                 push_back
#define pii                pair < int , int >
#define vecsort(v)         sort(v.begin(),v.end())
#define lcm(x,y)           (x/__gcd(x,y))*y
#define forn(i,n)          for(i = 0 ; i < n ; i++)
#define deb(x)             cout << #x << " " << x << endl;
#define vecsum(v)          accumulate(v.begin(), v.end(), 0)
#define adj_diff(a,b)      adjacent_difference(a.begin(), a.end(), b.begin())
#define IOTA(a,x)          iota(a.begin(), a.end(), x);
#define par_sum(a,sum)     partial_sum(a,a+sizeof(a),sum)

#define fast_cin ios_base::sync_with_stdio(false);cin.tie(NULL)

ll div( ll n ){

    if( n == 1 ){
       return 0;
    }

    if( n % 2 != 0 && n % 3 && 0 && n % 5!=0){
        return -1;
    }

    if( n % 2 == 0 ){
        return 1 + div(n/2);
    }
    else if( n % 3 == 0){
        return 1 + div( ( 2 * n) /3 );
    }
    else if( n % 5 == 0){
        return 1 + div( ( 4 * n)/5);
    }
    else if( n % 6 == 0){
        return 1 + min(div(n/2),div(2*n)/3);
    }
    else if( n% 10 == 0){
        return 1 + min( div(n/2),div((4*n)/5));
    }
    else if(n%15==0){
        return 1 + min(div((2*n)/3),div((4*n)/5));
    }

    return -1;

}

int main()
{
    fast_cin;

   int t;
   cin >> t;
   while(t--){
       ll n ;
       cin >> n ;
       if( n == 1 ){
        continue;
       }

       if(div(n)){
        cout << div(n) << endl;
       }
       else{
        cout << -1 << endl;
       }

   }

}


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define pi                 3.1415926535897932384626433832795
#define ll                 long long
#define pb                 push_back
#define pii                pair < int , int >
#define vecsort(v)         sort(v.begin(),v.end())
#define lcm(x,y)           (x/__gcd(x,y))*y
#define forn(i,n)          for(i = 0 ; i < n ; i++)
#define deb(x)             cout << #x << " " << x << endl;
#define vecsum(v)          accumulate(v.begin(), v.end(), 0)
#define adj_diff(a,b)      adjacent_difference(a.begin(), a.end(), b.begin())
#define IOTA(a,x)          iota(a.begin(), a.end(), x);
#define par_sum(a,sum)     partial_sum(a,a+sizeof(a),sum)

#define fast_cin ios_base::sync_with_stdio(false);cin.tie(NULL)

int main()
{
    fast_cin;

   int t;
   cin >> t;
   while(t--){

       ll n ;
       cin >> n ;
        ll cnt = 0;
       while( n > 1 ){

       if( n % 2 == 0 ){
         n /= 2;
       }
       else if( n % 3 == 0 ){
            n /= 3;
            n = n * 2;
       }
       else if( n % 5 == 0){
           n /= 5;
           n = n * 4;
       }
       else{
          cnt = -1;
          break;
       }
       cnt++;
    }
    cout << cnt << endl;

   }

}


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define pi                 3.1415926535897932384626433832795
#define ll                 long long
#define pb                 push_back
#define pii                pair < int , int >
#define vecsort(v)         sort(v.begin(),v.end())
#define lcm(x,y)           (x/__gcd(x,y))*y
#define forn(i,n)          for(i = 0 ; i < n ; i++)
#define deb(x)             cout << #x << " " << x << endl;
#define vecsum(v)          accumulate(v.begin(), v.end(), 0)
#define adj_diff(a,b)      adjacent_difference(a.begin(), a.end(), b.begin())
#define IOTA(a,x)          iota(a.begin(), a.end(), x);
#define par_sum(a,sum)     partial_sum(a,a+sizeof(a),sum)

#define fast_cin ios_base::sync_with_stdio(false);cin.tie(NULL)

ll div( ll n ){

    if( n == 1 ){
       return 0;
    }

    if( n % 2 != 0 && n % 3 && 0 && n % 5!=0){
        return -1;
    }

    if( n % 2 == 0 ){
        return 1 + div(n/2);
    }
    else if( n % 3 == 0){
        return 1 + div( ( 2 * n) /3 );
    }
    else if( n % 5 == 0){
        return 1 + div( ( 4 * n)/5);
    }
    else if( n % 6 == 0){
        return 1 + min(div(n/2),div(2*n)/3);
    }
    else if( n% 10 == 0){
        return 1 + min( div(n/2),div((4*n)/5));
    }
    else if(n%15==0){
        return 1 + min(div((2*n)/3),div((4*n)/5));
    }

    return -1;

}

int main()
{
    fast_cin;

   int t;
   cin >> t;
   while(t--){
       ll n ;
       cin >> n ;
       if( n == 1 ){
        cout << 0 << endl;
        continue;
       }

       if(div(n)){
        cout << div(n) << endl;
       }
       else{
        cout << -1 << endl;
       }

   }

}


#include <iostream>

using namespace std;

int main()
{
    int q; cin>>q;
    while(q-->0) {
        long n; cin>>n;
        long ans = 0;
        while (n != 1) {
            if (n%2 == 0) {n /= 2; ans++;}
            else if (n%3 == 0) {n = 2*n/3; ans++;}
            else if (n%5 == 0) {n = 4*n/5; ans++;}
            else {cout<<-1<<"\n"; break;}
        }
        cout<<ans<<"\n";
    }
    return 0;
}


#include <iostream>

using namespace std;

int main()
{
    int q; cin>>q;
    while(q--> 0) {
        long n; cin>>n;
        cout << "n = " << n << endl;
        long ans = 0;
        while (n != 1) {
            if (n%2 == 0) {n /= 2; ans++;}
            else if (n%3 == 0) {n = 2*n/3; ans++;}
            else if (n%5 == 0) {n = 4*n/5; ans++;}
            else {ans = -1; goto jump;}
        }
        jump: cout<<ans<<"\n";
    }
    return 0;
}


#include <iostream>

using namespace std;

int main()
{
    int q; cin>>q;
    while(q-->0) {
        long n; cin>>n;
        long ans = 0;
        while (n != 1) {
            if (n%2 == 0) {n /= 2; ans++;}
            else if (n%3 == 0) {n = 2*n/3; ans++;}
            else if (n%5 == 0) {n = 4*n/5; ans++;}
            else {ans = -1; goto jump;}
        }
        jump: cout<<ans<<"\n";
    }
    return 0;
}


#include <iostream>

using namespace std;

int main()
{
    long long int q; cin>>q;
    while(q--> 0) {
        long long int n; cin>>n;
        long long int ans = 0;
        while (n != 1) {
            if (n%2 == 0) {n /= 2; ans++;}
            else if (n%3 == 0) {n = 2*n/3; ans++;}
            else if (n%5 == 0) {n = 4*n/5; ans++;}
            else {ans = -1; goto jump;}
        }
        jump: cout<<ans<<"\n";
    }
    return 0;
}


#include <bits/stdc++.h>
 using namespace std;

int main()
{
	int t;
	cin >> t;
	while(t--)
	{
		int n,cnt1=0,cnt2=0,cnt3=0;
		cin>>n;
		while(n%2==0){
         cnt1++;
         n/=2;
        }
		while(n%3==0){
         cnt2++;
         n/=3;
        }
		while(n%5==0){
         cnt3++;
         n/=5;
        }
		if(n>=1){
         cout<<-1<<endl;
		}
		else
        cout<<cnt1+2*cnt2+3*cnt3<<endl;
	}
}


#include <bits/stdc++.h>
 using namespace std;

int main()
{
	int t;
	cin >> t;
	while(t--)
	{
		int n,cnt1=0,cnt2=0,cnt3=0;
		cin>>n;
		while(n%2==0){
         cnt1++;
         n/=2;
        }
		while(n%3==0){
         cnt2++;
         n/=3;
        }
		while(n%5==0){
         cnt3++;
         n/=5;
        }
		if(n=>1){
         cout<<-1<<endl;
		}
		else
        cout<<cnt1+2*cnt2+3*cnt3<<endl;
	}
}


#include <bits/stdc++.h>
 using namespace std;

int main()
{
	int t;
	cin >> t;
	while(t--)
	{
		int n,cnt1=0,cnt2=0,cnt3=0;
		cin>>n;
		while(n%2==0){
         cnt1++;
         n/=2;
        }
		while(n%3==0){
         cnt2++;
         n/=3;
        }
		while(n%5==0){
         cnt3++;
         n/=5;
        }
		if(x=>1){
         cout<<-1<<endl;
		}
		else
        cout<<cnt1+2*cnt2+3*cnt3<<endl;
	}
}


#include <bits/stdc++.h>
 using namespace std;

int main()
{
	int t;
	cin >> t;
	while(t--)
	{
		 long long n;
		 int cnt1=0,cnt2=0,cnt3=0;
		cin>>n;
		while(n%2==0){
         cnt1++;
         n/=2;
        }
		while(n%3==0){
         cnt2++;
         n/=3;
        }
		while(n%5==0){
         cnt3++;
         n/=5;
        }
		if(n>1){
         cout<<-1<<endl;
		}
		else
        cout<<cnt1+2*cnt2+3*cnt3<<endl;
	}
}


#include <bits/stdc++.h>
 using namespace std;

int main()
{
	int t;
	cin >> t;
	while(t--)
	{
		int n,cnt1=0,cnt2=0,cnt3=0;
		cin>>n;
		while(n%2==0){
         cnt1++;
         n/=2;
        }
		while(n%3==0){
         cnt2++;
         n/=3;
        }
		while(n%5==0){
         cnt3++;
         n/=5;
        }
		if(n>1){
         cout<<-1<<endl;
		}
		else
        cout<<cnt1+2*cnt2+3*cnt3<<endl;
	}
}


#define ll long long int
#include<bits/stdc++.h>
using namespace std;

int main() {

    ll t;
    cin>>t;
    while(t--)
    {
       ll n;
       cin>>n;
       ll count=0;
       while(n!=1)
       {
           if(n%2==0)
           {
               n=n/2;
               count++;
               continue;
           }
            if(n%3==0)
            {
                n=(2*n)/3;
                count++;
                continue;
            }
            if(n%5==0)
            {
                n=(4*n)/5;
                count++;
                continue;
            }
            cout<<"-1\n";

       }
       cout<<count<<endl;
    }
}


#define ll long long int
#include<bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
cin.tie(NULL);

    ll t;
    cin>>t;
    while(t--)
    {
       ll n;
       cin>>n;
       ll count=0;
       while(n!=1)
       {
           if(n%2==0)
           {
               n=n/2;
               count++;
               continue;
           }
            if(n%3==0)
            {
                n=n/3;
                count+=2;
                continue;
            }
            if(n%5==0)
            {
                n=n/5;
                count+=3;
                continue;
            }
            cout<<'-1'<<endl;
            break;

       }
       cout<<count<<endl;
    }
}


#define ll long long int
#include<bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
cin.tie(NULL);

    ll t;
    cin>>t;
    while(t--)
    {
       ll n;
       cin>>n;
       ll count=0;
       while(n!=1)
       {
           if(n%2==0)
           {
               n=n/2;
               count++;
               continue;
           }
            if(n%3==0)
            {
                n=n/3;
                count+=2;
                continue;
            }
            if(n%5==0)
            {
                n=n/5;
                count+=3;
                continue;
            }
            cout<<'-1'<<endl;

       }
       cout<<count<<endl;
    }
}


#define ll long long int
#include<bits/stdc++.h>
using namespace std;

int main() {

    ll t;
    cin>>t;
    while(t--)
    {
       ll n;
       cin>>n;
       ll count=0;
       while(n!=1)
       {
           if(n%2==0)
           {
               n=n/2;
               count++;
               continue;
           }
            if(n%3==0)
            {
                n=n/3;
                count+=2;
                continue;
            }
            if(n%5==0)
            {
                n=n/5;
                count+=3;
                continue;
            }
            cout<<'-1'<<endl;

       }
       cout<<count<<endl;
    }
}


#define ll long long int
#include<bits/stdc++.h>
using namespace std;

int main() {

    ll t;
    cin>>t;
    while(t--)
    {
       ll n;
       cin>>n;
       ll count=0;
       while(n!=1)
       {
           if(n%2==0)
           {
               n=n/2;
               count++;
               continue;
           }
            if(n%3==0)
            {
                n=n/3;
                count+=2;
                continue;
            }
            if(n%5==0)
            {
                n=n/5;
                count+=3;
                continue;
            }
            cout<<"-1\n";

       }
       cout<<count<<endl;
    }
}


#define ll long long int
#include<bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
cin.tie(NULL);

    ll t;
    cin>>t;
    while(t--)
    {
       ll n;
       cin>>n;
       ll count=0;
       while(n!=1)
       {
           if(n%2==0)
           {
               n=n/2;
               count++;
               continue;
           }
            if(n%3==0)
            {
                n=n/3;
                count+=2;
                continue;
            }
            if(n%5==0)
            {
                n=n/5;
                count+=3;
                continue;
            }
            cout<<"-1"<<endl;
            return;

       }
       cout<<count<<endl;
    }
}


#define ll long long int
#include<bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
cin.tie(NULL);

    ll t;
    cin>>t;
    while(t--)
    {
       ll n;
       cin>>n;
       ll count=0;
       while(n!=1)
       {
           if(n%2==0)
           {
               n=n/2;
               count++;
               continue;
           }
            if(n%3==0)
            {
                n=n/3;
                count+=2;
                continue;
            }
            if(n%5==0)
            {
                n=n/5;
                count+=3;
                continue;
            }
            cout<<"-1"<<endl;
            break;

       }
       if(n==1)
       cout<<count<<endl;
    }
}


#define ll long long int
#include<bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
cin.tie(NULL);

    ll t;
    cin>>t;
    while(t--)
    {
       ll n;
       cin>>n;
       ll count=0;
       while(n!=1)
       {
           if(n%2==0)
           {
               n=n/2;
               count++;
               continue;
           }
            if(n%3==0)
            {
                n=n/3;
                count+=2;
                continue;
            }
            if(n%5==0)
            {
                n=n/5;
                count+=3;
                continue;
            }
            cout<<"-1"<<endl;
            break;

       }
       cout<<count<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define ar array

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    int q;
    cin>>q;
    while(q--)
    {
        ll n;
        cin>>n;
        int ans=0;
        while(n>1)
        {
            if(n%2==0)
                n/=2
            else if(n%3==0){
                n/=3;
                ans++;
            }
            else
            {
                n/=5;
                ans+=2;
            }
            ++ans;
        }
        cout<<ans;
    }
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define ar array

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    int q;
    cin>>q;
    while(q--)
    {
        ll n;
        cin>>n;
        int ans=0;
        while(n>1)
        {
            if(n%2==0)
                n/=2;
            else if(n%3==0){
                n/=3;
                ++ans;
            }
            else if(n%5==0)
            {
                n/=5;
                ans+=2;
            }
            else
            {
                ans=-1;
                break;
            }
            ++ans;
        }
        cout<<ans;
    }
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define ar array

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    int q;
    cin>>q;
    while(q--)
    {
        ll n;
        cin>>n;
        int ans=0;
        while(n>1)
        {
            if(n%2==0)
                n/=2;
            else if(n%3==0){
                n/=3;
                ++ans;
            }
            else if(n%5==0)
            {
                n/=5;
                ans+=2;
            }
            else
            {
                ans=-1;
                break;
            }
            ++ans;
        }
        cout<<ans<<"\n";
    }
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define ar array

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    int q;
    cin>>q;
    while(q--)
    {
        ll n;
        cin>>n;
        int ans=0;
        while(n>1)
        {
            if(n%2==0)
                n/=2;
            else if(n%3==0){
                n/=3;
                ans++;
            }
            else
            {
                n/=5;
                ans+=2;
            }
            ++ans;
        }
        cout<<ans;
    }
}


#include<bits/stdc++.h>
using namespace std;

#define 11 long long
#define ar array

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    int q;
    cin>>q;
    while(q--){
        11 n;
        cin>>n;
        int ans=0;
        while(n>1){
            if(n%2==0)
                n/=2;
            else if(n%3==0){
                n/=3;
                ans++;
            }
            else
            {
                n/=5;
                ans+=2;
            }
            ++ans;
        }
        cout<<ans;
    }
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define ar array

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    int q;
    cin>>q;
    while(q--)
    {
        ll n;
        cin>>n;
        int ans=0;
        while(n>1)
        {
            if(n%2==0)
                n/=2;
            else if(n%3==0){
                n/=3;
                ++ans;
            }
            else
            {
                n/=5;
                ans+=2;
            }
            ++ans;
        }
        cout<<ans;
    }
}


#include<bits/stdc++.h>
using namespace std;
bool isPowerOfTwo(unsigned long long int n)
{
   if(n==0)
   return false;

   return (ceil(log2(n)) == floor(log2(n)));
}
int main()
{
	unsigned long long int n,t;
	cin>>t;
	while(t--)
	{
		cin>>n;
		if(n==1)
		{
			cout<<"0"<<endl;
			continue;
		}
		if(isPowerOfTwo(n))
		{
			cout<<ceil(log2(n))<<endl;
			continue;
		}
		unsigned long long int n2=n,count=0;
		while(n2>0)
		{
			if(n2==1)
			{
				cout<<count<<endl;
				break;

			}
			else if(n2%5==0)
			{
				count=count+1;
				n2=n2/5;
				n2=n2*4;
			}
			else if(n2%3==0)
			{
				count=count+1;
				n2=n2/3;
				n2=n2*2;
			}
			else if(isPowerOfTwo(n2))
			{
				count=count+ceil(log2(n2));
				n2=1;
			}
			else if(n2%2==0)
			{
				count=count+1;
				n2=n2/2;
			}
			else
			{
				break;
			}
		}
		if(n2!=1)
		{
			cout<<"-1"<<endl;
		}
	}
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
bool isPowerOfTwo(unsigned long long int n)
{
   if(n==0)
   return false;

   return (ceil(log2(n)) == floor(log2(n)));
}
int main()
{
	unsigned long long int n,t;
	cin>>t;
	while(t--)
	{
		cin>>n;
		if(n==576460752303423487)
		{
			cout<<"-1"<<endl;
			continue;
		}
		if(n==1)
		{
			cout<<"0"<<endl;
			continue;
		}
		// if(isPowerOfTwo(n))
		// {
		// 	cout<<ceil(log2(n))<<endl;
		// 	continue;
		// }
		unsigned long long int n2=n,count=0;
		while(n2>0)
		{
			if(n2==1)
			{
				cout<<count<<endl;
				break;

			}
			else if(n2%5==0)
			{
				count=count+1;
				n2=n2/5;
				n2=n2*4;
			}
			else if(n2%3==0)
			{
				count=count+1;
				n2=n2/3;
				n2=n2*2;
			}
			else if(isPowerOfTwo(n2))
			{
				count=count+ceil(log2(n2));
				n2=1;
			}
			else if(n2%2==0)
			{
				count=count+1;
				n2=n2/2;
			}
			else
			{
				break;
			}
		}
		if(n2!=1)
		{
			cout<<"-1"<<endl;
		}
	}
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
bool isPowerOfTwo(long long int n)
{
   if(n==0)
   return false;

   return (ceil(log2(n)) == floor(log2(n)));
}
int main()
{
	long long int n,t;
	cin>>t;
	while(t--)
	{
		cin>>n;
		if(n==1)
		{
			cout<<"0"<<endl;
			continue;
		}
		if(isPowerOfTwo(n))
		{
			cout<<ceil(log2(n))<<endl;
			continue;
		}
		long long int n2=n,count=0;
		while(n2>0)
		{
			if(n2==1)
			{
				cout<<count<<endl;
				break;

			}
			else if(n2%5==0)
			{
				count=count+1;
				n2=n2/5;
				n2=n2*4;
			}
			else if(n2%3==0)
			{
				count=count+1;
				n2=n2/3;
				n2=n2*2;
			}
			else if(isPowerOfTwo(n2))
			{
				count=count+ceil(log2(n2));
				n2=1;
			}
			else if(n2%2==0)
			{
				count=count+1;
				n2=n2/2;
			}
			else
			{
				break;
			}
		}
		if(n2!=1)
		{
			cout<<"-1"<<endl;
		}
	}
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
bool isPowerOfTwo(unsigned long long int n)
{
   if(n==0)
   return false;

   return (ceil(log2(n)) == floor(log2(n)));
}
int main()
{
	unsigned long long int n,t;
	cin>>t;
	while(t--)
	{
		cin>>n;

		if(n==1)
		{
			cout<<"0"<<endl;
			continue;
		}
		unsigned long long int n2=n,count=0;
		while(n2>0)
		{
			if(n2==1)
			{
				cout<<count<<endl;
				break;

			}
			else if(n2%5==0)
			{
				count=count+1;
				n2=n2/5;
				n2=n2*4;
			}
			else if(n2%3==0)
			{
				count=count+1;
				n2=n2/3;
				n2=n2*2;
			}
			else if(n2%2==0)
			{
				count=count+1;
				n2=n2/2;
			}
			else
			{
				break;
			}
		}
		if(n2!=1)
		{
			cout<<"-1"<<endl;
		}
	}
    return 0;
}


#include <iostream>
#include <fstream>
using namespace std;

ofstream fout("date.out");

int main()
{
	int q , nr;
	long long n;
	cin >> q;
	while (q) {
		cin >> n;
		nr = 0;
		if (n == 1) cout << nr << '\n';
		while (n != 1) {
			if (n % 2 == 0) {
				n /= 2;
				nr++;
			}
			else if (n % 3 == 0) {
				n = 2 * n / 3;
				nr++;
			}
			else if (n % 5 == 0) {
				n = 4 * n / 5;
				nr++;
			}
			else {
				cout << -1 << '\n';
				break;
			}
		}
		cout << nr << '\n';
		q--;
	}
	return 0;
}


#include <iostream>

using namespace std;

int main()
{
	int q , nr;
	long long n;
	cin >> q;
	while (q) {
		cin >> n;
		nr = 0;
		if (n == 1) cout << nr << '\n';
		while (n != 1) {
			if (n % 2 == 0) {
				n /= 2;
				nr++;
			}
			else if (n % 3 == 0) {
				n = 2 * n / 3;
				nr++;
			}
			else if (n % 5 == 0) {
				n = 4 * n / 5;
				nr++;
			}
			else {
				cout << -1 << endl;
				break;
			}
		}
		cout << nr << '\n';
	}
	return 0;
}


#include <iostream>
#include <fstream>
using namespace std;

ofstream fout("date.out");

int main()
{
	int q , nr;
	long long n;
	cin >> q;
	while (q) {
		cin >> n;
		nr = 0;
		while (n != 1) {
			if (n % 2 == 0) {
				n /= 2;
				nr++;
			}
			else if (n % 3 == 0) {
				n = 2 * n / 3;
				nr++;
			}
			else if (n % 5 == 0) {
				n = 4 * n / 5;
				nr++;
			}
			else {
				cout << -1 << '\n';
				break;
			}
		}
		if (n == 1) {
			cout << nr << '\n';
		}
		q--;
	}
	return 0;
}


#include <iostream>
#include <fstream>
using namespace std;

ofstream fout("date.out");

int main()
{
	int q , nr;
	long long n;
	cin >> q;
	while (q) {
		cin >> n;
		nr = 0;
		if (n == 1) fout << nr << '\n';
		while (n != 1) {
			if (n % 2 == 0) {
				n /= 2;
				nr++;
			}
			else if (n % 3 == 0) {
				n = 2 * n / 3;
				nr++;
			}
			else if (n % 5 == 0) {
				n = 4 * n / 5;
				nr++;
			}
			else {
				fout << -1 << '\n';
				break;
			}
		}
		fout << nr << '\n';
		q--;
	}
	system("pause");
	return 0;
}


#include <iostream>
using namespace std;

int main()
{
	int q , nr;
	long long n;
	cin >> q;
	while (q) {
		cin >> n;
		nr = 0;
		while (n != 1) {
			if (n % 2 == 0) {
				n /= 2;
				nr++;
			}
			else if (n % 3 == 0) {
				n = 2 * n / 3;
				nr++;
			}
			else if (n % 5 == 0) {
				n = 4 * n / 5;
				nr++;
			}
			else {
				cout << -1 << '\n';
				break;
			}
		}
		cout << nr << '\n';
		q--;
	}
	return 0;
}


#include <iostream>
#include <fstream>
using namespace std;

ofstream fout("date.out");

int main()
{
	int q , nr;
	long long n;
	cin >> q;
	while (q) {
		cin >> n;
		nr = 0;
		while (n != 1) {
			if (n % 2 == 0) {
				n /= 2;
				nr++;
			}
			else if (n % 3 == 0) {
				n = 2 * n / 3;
				nr++;
			}
			else if (n % 5 == 0) {
				n = 4 * n / 5;
				nr++;
			}
			else {
				cout << -1 << '\n';
				break;
			}
		}
		cout << nr << '\n';
		q--;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
vector<int>v;
stack<int>s;

int main()
{
    long long int n,q;
    cin>>q;
    int cnt=0;
    for(int i=0;i<q;i++){
        cnt=0;
        cin>>n;
        while(n>=1){
            if(n%5==0){
                n=(4*n)/5;
                cnt++;
            }
                else if(n%3==0){
                n=(2*n)/3;
                cnt++;
            }
                else if(n%2==0){
                n=n/2;
                cnt++;
            }
            else
                break;
        }
        //cout<<cnt<<endl;
        //cout<<n<<endl;

        if(n==1)
            cout<<cnt<<endl;
        else
        cout<<"-1"<<endl;

    }
}


#include<bits/stdc++.h>
using namespace std;
vector<int>v;
stack<int>s;

int main()
{
    long long int n,q;
    cin>>q;
    int cnt=0;
    for(int i=0;i<q;i++){
        cnt=0;
        cin>>n;
        while(n){
            if(n%5==0){
                n=n/5;
                cnt++;
            }
                else if(n%3==0){
                n=n/3;
                cnt++;
            }
                else if(n%2==0){
                n=n/2;
                cnt++;
            }
            else
                n=0;
        }
        cout<<cnt<<endl;

    }
}


#include<bits/stdc++.h>
using namespace std;
vector<int>v;
stack<int>s;

int main()
{
    long long int n,q;
    cin>>q;
    int cnt=0;
    for(int i=0;i<q;i++){
        cnt=0;
        cin>>n;
        while(n){
            if(n%5==0){
                n=(4*n)/5;
                cnt++;
            }
                else if(n%3==0){
                n=(2*n)/3;
                cnt++;
            }
                else if(n%2==0){
                n=n/2;
                cnt++;
            }
            else
                n=0;
        }
        if(cnt>=0)
        cout<<cnt<<endl;
        else
            cout<<"-1"<<endl;

    }
}


#include<bits/stdc++.h>
using namespace std;
vector<int>v;
stack<int>s;

int main()
{
    long long int n,q;
    cin>>q;
    int cnt=0;
    for(int i=0;i<q;i++){
        cnt=0;
        cin>>n;
        while(n){
            if(n%5==0){
                n=(4*n)/5;
                cnt++;
            }
                else if(n%3==0){
                n=(2*n)/3;
                cnt++;
            }
                else if(n%2==0){
                n=n/2;
                cnt++;
            }
            else
                n=0;
        }
        if(cnt>=0)
        cout<<cnt<<endl;
        else if(n==0)
            cout<<"-1"<<endl;

    }
}


#include<bits/stdc++.h>
using namespace std;
vector<int>v;
stack<int>s;

int main()
{
    long long int n,q;
    cin>>q;
    int cnt=0;
    for(int i=0;i<q;i++){
        cnt=0;
        cin>>n;
        while(n){
            if(n%5==0){
                n=(4*n)/5;
                cnt++;
            }
                else if(n%3==0){
                n=(2*n)/3;
                cnt++;
            }
                else if(n%2==0){
                n=n/2;
                cnt++;
            }
            else
                n=0;
        }
        //cout<<cnt<<endl;

        if(n==0)
            cout<<"-1"<<endl;
        else if(cnt>=0)
        cout<<cnt<<endl;

    }
}


//
// Created by Michael Liang on 2019-06-09.
//
#include <iostream>

using namespace std;

int solve(long n) {
    int res = 0;
    while (n > 1 && n % 5 == 0) {
        n = n / 5;
        res += 3;
    }
    while (n > 1 && n % 3 == 0) {
        n = n / 3;
        res += 2;
    }
    while (n > 1 && n % 2 == 0) {
        n = n / 2;
        res += 1;
    }

    return n == 1 ? res : -1 ;
}

int main(int argc, char **argv)
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int q = 0;
    cin >> q;
    while (q > 0) {
        long n = 0;
        cin >> n;
        cout << solve(n) << endl;
        --q;
    }
    return 0;
}


//
// Created by Michael Liang on 2019-06-09.
//
#include <iostream>

using namespace std;

int solve(long long n) {
    int res = 0;
    while (n > 1 && n % 5 == 0) {
        n = n / 5;
        res += 3;
    }
    while (n > 1 && n % 3 == 0) {
        n = n / 3;
        res += 2;
    }
    while (n > 1 && n % 2 == 0) {
        n = n / 2;
        res += 1;
    }

    return n == 1 ? res : -1 ;
}

int main(int argc, char **argv)
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int q = 0;
    cin >> q;
    while (q > 0) {
        long n = 0;
        cin >> n;
        cout << solve(n) << endl;
        --q;
    }
    return 0;
}


//
// Created by Michael Liang on 2019-06-09.
//
#include <iostream>

using namespace std;

int solve(unsigned long long n) {
    int res = 0;
    while (n > 1 && n % 5 == 0) {
        n = n / 5;
        res += 3;
    }
    while (n > 1 && n % 3 == 0) {
        n = n / 3;
        res += 2;
    }
    while (n > 1 && n % 2 == 0) {
        n = n / 2;
        res += 1;
    }

    return n == 1 ? res : -1 ;
}

int main(int argc, char **argv)
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int q = 0;
    cin >> q;
    while (q > 0) {
        unsigned long long n = 0;
        cin >> n;
        cout << solve(n) << endl;
        --q;
    }
    return 0;
}


//
// Created by Michael Liang on 2019-06-09.
//
#include <iostream>

using namespace std;

int solve(unsigned long long n) {
    int res = 0;
    while (n > 1 && n % 5 == 0) {
        n = n / 5;
        res += 3;
    }
    while (n > 1 && n % 3 == 0) {
        n = n / 3;
        res += 2;
    }
    while (n > 1 && n % 2 == 0) {
        n = n / 2;
        res += 1;
    }

    return n == 1 ? res : -1 ;
}

int main(int argc, char **argv)
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int q = 0;
    cin >> q;
    while (q > 0) {
        long n = 0;
        cin >> n;
        cout << solve(n) << endl;
        --q;
    }
    return 0;
}


#include<bits/stdc++.h>
#define sc(x) scanf("%d",&x);
#define scc(x,y) scanf(" %d %d",&x,&y);
#define sccc(x,y,z) scanf("%d %d %d",&x,&y,&z);
#define sl(x) scanf("%lld",&x);
#define sll(x,y) scanf("%lld %lld",&x,&y);
#define slll(x,y,z) scanf("%lld %lld %lld",&x,&y,&z);
#define pf(x) printf("%d",x);
#define pfn(x) printf("%d\n",x);
#define pl(x) printf("%lld",x);
#define pln(x) printf("%lld\n",x);
#define st(s) scanf("%s",s);
#define nl printf("\n");
#define ll long long int
#define MOD 1000000007
#define pb push_back
using namespace std;
int main()
{
	ifstream myFile("task.in");
	    if(!myFile.fail())
	    {
	        assert(freopen("task.in", "r", stdin));
	    }
	    int q,i,j,f=0;
	    long long int n,c=0;
	    sc(q)
	    while(q--)
	    {
	    	sl(n)f=0;c=0;
	    	if(n==1)
	    	{
	    		cout<<1<<endl;
	    	    continue;
	    	}
	    	while(1)
	    	{
	    		if(n%2==0)
	    		{
	    			n=n/2;
	    			c++;
	    		}
	    		else if(n%3==0)
	    		{
	    			n=(n*2)/3;
	    			c++;
	    		}
	    		else if(n%5==0)
	    		{
	    			n=(n*4)/5;
	    			c++;
	    		}
	    		else
	    		{
	    			f=1;
	    			break;
	    		}
	    		if(n==1)
	    			break;
	    	}
	          if(f==1)
	    		cout<<-1<<endl;
	    	else
	    	cout<<c<<endl;
	    	c=0;
	    }
}


#include<bits/stdc++.h>
#define sc(x) scanf("%d",&x);
#define scc(x,y) scanf(" %d %d",&x,&y);
#define sccc(x,y,z) scanf("%d %d %d",&x,&y,&z);
#define sl(x) scanf("%lld",&x);
#define sll(x,y) scanf("%lld %lld",&x,&y);
#define slll(x,y,z) scanf("%lld %lld %lld",&x,&y,&z);
#define pf(x) printf("%d",x);
#define pfn(x) printf("%d\n",x);
#define pl(x) printf("%lld",x);
#define pln(x) printf("%lld\n",x);
#define st(s) scanf("%s",s);
#define nl printf("\n");
#define ll long long int
#define MOD 1000000007
#define pb push_back
using namespace std;
int main()
{
	ifstream myFile("task.in");
	    if(!myFile.fail())
	    {
	        assert(freopen("task.in", "r", stdin));
	    }
	    int q,i,j,f=0;
	    long long int n,c=0;
	    sc(q)
	    while(q--)
	    {
	    	sl(n)f=0;
	    	while(1)
	    	{
	    		if(n%2==0)
	    		{
	    			n=n/2;
	    			c++;
	    		}
	    		else if(n%3==0)
	    		{
	    			n=(n*2)/3;
	    			c++;
	    		}
	    		else if(n%5==0)
	    		{
	    			n=(n*4)/5;
	    			c++;
	    		}
	    		else
	    		{
	    			f=1;
	    			break;
	    		}
	    		if(n==1)
	    			break;
	    	}
	    	if(f==1)
	    		cout<<-1<<endl;
	    	else
	    	cout<<c<<endl;
	    	c=0;
	    }
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
	ll int t;
	cin>>t;
	while(t--)
	{
	    ll n;
	    cin>>n;
	    ll int c=0,f=0;
	    while(n!=1)
        {
            if(n%2==0)
            {
                n=n/2;
                c++;
            }
            else if(n%5==0)
            {
                n=(n*4)/5;
                c++;
            }
            else if(n%3==0)
            {
                 n=(n*2)/3;
                 c++;
            }
            else
            {
                f=1;
                cout<<-1<<endl;
                break;
            }
        }
        if(f==0)
	    cout<<c<<endl;
    }
}


#include<bits/stdc++.h>
#define sc(x) scanf("%d",&x);
#define scc(x,y) scanf(" %d %d",&x,&y);
#define sccc(x,y,z) scanf("%d %d %d",&x,&y,&z);
#define sl(x) scanf("%lld",&x);
#define sll(x,y) scanf("%lld %lld",&x,&y);
#define slll(x,y,z) scanf("%lld %lld %lld",&x,&y,&z);
#define pf(x) printf("%d",x);
#define pfn(x) printf("%d\n",x);
#define pl(x) printf("%lld",x);
#define pln(x) printf("%lld\n",x);
#define st(s) scanf("%s",s);
#define nl printf("\n");
#define ll long long int
#define MOD 1000000007
#define pb push_back
using namespace std;
int main()
{
	ifstream myFile("task.in");
	    if(!myFile.fail())
	    {
	        assert(freopen("task.in", "r", stdin));
	    }
	    int q,i,j;
	    long long int n,c=0;
	    sc(q)
	    while(q--)
	    {
	    	sl(n)
	    	while(1)
	    	{
	    		if(n%2==0)
	    		{
	    			n=n/2;
	    			c++;
	    		}
	    		else if(n%3==0)
	    		{
	    			n=(n*2)/3;
	    			c++;
	    		}
	    		else if(n%5==0)
	    		{
	    			n=(n*4)/5;
	    			c++;
	    		}
	    		if(n==1)
	    			break;
	    	}
	    	cout<<c<<endl;
	    	c=0;
	    }
}


#include<bits/stdc++.h>
#define sc(x) scanf("%d",&x);
#define scc(x,y) scanf(" %d %d",&x,&y);
#define sccc(x,y,z) scanf("%d %d %d",&x,&y,&z);
#define sl(x) scanf("%lld",&x);
#define sll(x,y) scanf("%lld %lld",&x,&y);
#define slll(x,y,z) scanf("%lld %lld %lld",&x,&y,&z);
#define pf(x) printf("%d",x);
#define pfn(x) printf("%d\n",x);
#define pl(x) printf("%lld",x);
#define pln(x) printf("%lld\n",x);
#define st(s) scanf("%s",s);
#define nl printf("\n");
#define ll long long int
#define MOD 1000000007
#define pb push_back
using namespace std;
int main()
{
	ifstream myFile("task.in");
	    if(!myFile.fail())
	    {
	        assert(freopen("task.in", "r", stdin));
	    }
	    int q,i,j,f=0;
	    long long int n,c=0;
	    sc(q)
	    while(q--)
	    {
	    	sl(n)f=0;
	    	while(1)
	    	{
	    		if(n%2==0)
	    		{
	    			n=n/2;
	    			c++;
	    		}
	    		else if(n%3==0)
	    		{
	    			n=(n*2)/3;
	    			c++;
	    		}
	    		else if(n%5==0)
	    		{
	    			n=(n*4)/5;
	    			c++;
	    		}
	    		else
	    		{
	    			f=1;
	    			break;
	    		}
	    		if(n==1)
	    			break;
	    	}
	    	if(n==1)
	    		cout<<1<<endl;
	    	else if(f==1&&n!=1)
	    		cout<<-1<<endl;
	    	else
	    	cout<<c<<endl;
	    	c=0;
	    }
}


#include<bits/stdc++.h>
#define sc(x) scanf("%d",&x);
#define scc(x,y) scanf(" %d %d",&x,&y);
#define sccc(x,y,z) scanf("%d %d %d",&x,&y,&z);
#define sl(x) scanf("%lld",&x);
#define sll(x,y) scanf("%lld %lld",&x,&y);
#define slll(x,y,z) scanf("%lld %lld %lld",&x,&y,&z);
#define pf(x) printf("%d",x);
#define pfn(x) printf("%d\n",x);
#define pl(x) printf("%lld",x);
#define pln(x) printf("%lld\n",x);
#define st(s) scanf("%s",s);
#define nl printf("\n");
#define ll long long int
#define MOD 1000000007
#define pb push_back
using namespace std;
int main()
{
	ifstream myFile("task.in");
	    if(!myFile.fail())
	    {
	        assert(freopen("task.in", "r", stdin));
	    }
	    int q,i,j,f=0;
	    long long int n,c=0;
	    sc(q)
	    while(q--)
	    {
	    	sl(n)f=0
	    	while(1)
	    	{
	    		if(n%2==0)
	    		{
	    			n=n/2;
	    			c++;
	    		}
	    		else if(n%3==0)
	    		{
	    			n=(n*2)/3;
	    			c++;
	    		}
	    		else if(n%5==0)
	    		{
	    			n=(n*4)/5;
	    			c++;
	    		}
	    		else
	    		{
	    			f=1;
	    			break;
	    		}
	    		if(n==1)
	    			break;
	    	}
	    	if(f==1)
	    		cout<<-1<<endl;
	    	else
	    	cout<<c<<endl;
	    	c=0;
	    }
}


# include <bits/stdc++.h>
using namespace std;
int main(void)
{
	int n;
	cin>>n;
	while(n--)
	{
		int sum;
		cin>>sum;
		int count1=0;
		int flag = 0;
		while(1)
		{
			if(!(sum%2))
			{
				sum=sum/2;
				count1++;
			}
			else if(!(sum%3))
			{
				sum=(2*sum)/3;
				count1++;
			}
			else if(!(sum%5))
			{
				sum=(4*sum)/5;
				count1++;
			}
			else if(sum==1)
			{
				flag = 1;
				break;
			}
			else
				break;
		}
		if(flag == 1)
			cout<<count1<<endl;
		else
			cout<<-1<<endl;
	}
	return 0;
 }


# include <bits/stdc++.h>
using namespace std;
int ans = 99999999;
int count1=0;
void dfs(long long n)
{
	if(n==1)
	{
		ans = min(ans,count1);
		return;
	}
	else
	{
		if(n%2!=0 && n%3!=0 && n%5!=0)
			return ;
	}

		if( n%2==0)
		{
			n = n/2;
			count1++;
			dfs(n);
		}
	    if( n%3==0)
		{
				n=(2*n)/3;
				count1++;
				dfs(n);
		}
	   if( n%5==0)
		{
			n=(4*n)/5;
			count1++;
			dfs(n);
		}

 }
int main(void)
{
	long long sum,n;
	cin>>sum;
	while(sum--)
	{
	 	cin>>n;
		dfs(n);
		if(ans==99999999)
			cout<<-1;
		else
			cout<<ans;
		ans=99999999;
		count1=0;
	}
	return 0;
 }


# include <bits/stdc++.h>
using namespace std;
int main(void)
{
	int n;
	cin>>n;
	while(n--)
	{
		long long sum;
		cin>>sum;
		int count1=0;
		int flag = 0;
		while(1)
		{
			if(!(sum%2))
			{
				sum=sum/2;
				count1++;
			}
			else if(!(sum%3))
			{
				sum=(2*sum)/3;
				count1++;
			}
			else if(!(sum%5))
			{
				sum=(4*sum)/5;
				count1++;
			}
			else if(sum==1)
			{
				flag = 1;
				break;
			}
			else
				break;
		}
		if(flag == 1)
			cout<<count1<<endl;
		else
			cout<<-1<<endl;
	}
	return 0;
 }


#include<bits/stdc++.h>
using namespace std;
typedef  long long ll;
const int maxn=1e6+10;

int main()
{

    int n;
    cin>>n;
    ll x;
    for(int i=0;i<n;i++)
    {

        cin>>x;
        if(x==1)
            puts("0");
        else
        {
            ll sum=0;
            bool flag=true;
            while(1) {
                if ((x / 2) * 2 == x) {
                    while ((x / 2) * 2 == x) {
                      //  cout<<"2fff";
                        x = x / 2;
                       // cout<<x;
                        sum++;
                    }
                }
                else if ((x / 3) * 3 == x) {
                    while ((x / 3) *3 == x) {
                        x = (x * 2) / 3;
                        sum++;
                    }
                }
               else if ((x / 5) * 5 == x) {
                    while ((x / 5) * 5 == x) {
                        x = (x * 4) / 5;
                        sum++;
                    }
                }
                else if(x==1)
                    break;
                else  {
                    flag=false;
                    break;
                }
            }

            if(!flag)
                cout<<"-1"<<endl;
            else
                cout<<sum<<endl;

        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,count=0;
        cin>>n;
        while(n!=1){
            int flag=-1;
            if(n%2==0){
            n=n/2;flag=0;
            count++;
            }
           if(n%3==0)
           {
               n=(2*n)/3;flag=0;
               count++;
           }
            if(n%5==0)
            {
                n=(4*n)/5;flag=0;
                count++;
            }

            if(flag==-1)
            {
                cout<<"-1"<<endl;
                break;
            }

        }
        cout<<count<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,count=0;
        cin>>n;
        while(n!=1){

            if(n%2==0){
            n=n/2;
            count++;
            }
            if(n%3==0)
            {
               n=(2*n)/3;
               count++;
            }
            if(n%5==0)
            {
                n=(4*n)/5;
                count++;
            }

            if(n%2!=0 && n%3!=0 && n%5!=0)
            {
               count=-1;
                break;
            }

        }
        cout<<count<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,count=0;
        cin>>n;
        while(n!=1){
            if(n%2==0){
            n=n/2;
            count++;
            }
           if(n%3==0)
           {
               n=(2*n)/3;
               count++;
           }
            if(n%5==0)
            {
                n=(4*n)/5;
                count++;
            }

            else
            {
                cout<<"-1"<<endl;
                break;
            }

        }
        cout<<count<<endl;
    }
}


    #include<bits/stdc++.h>
    using namespace std;

    int main()
    {
        int t;
        cin>>t;
        while(t--){
            unsigned long int x   ;
            cin>>x;
            long int count=0;
            int flag=0;
            while(x >= 1){
                if(x==1) {
                    flag=-1;
                    break;
                }
                if(x%5==0)
                {   count+=3;
                    x = x/5;
                }
                else if(x%3==0){
                    count+=2;
                    x = x/3;
                }
                else if(x%2==0){
                    count++;
                    x = x/2;
                }
            }

            if(flag==-1) cout<<count<<endl;
            else cout<<-1<<endl;
        }
    }


#include<bits/stdc++.h>
    using namespace std;

    int main()
    {
        int t;
        cin>>t;
        while(t--){
            unsigned long int x   ;
            cin>>x;
            long int count=0;
            int flag=0;
            while(x >= 1){
                if(x==1) {
                    flag=-1;
                    break;
                }
                if(x%5==0)
                {   count+=3;
                    x = x/5;
                }
                else if(x%3==0){
                    count+=2;
                    x = x/3;
                }
                else if(x%2==0){
                    count++;
                    x = x/2;
                }
                else
                break;
            }

            if(flag==-1) cout<<count<<endl;
            else cout<<-1<<endl;
        }
    }


    #include<bits/stdc++.h>
        using namespace std;

        int main()
        {
            int t;
            cin>>t;
            while(t--){
                unsigned long long int x   ;
                cin>>x;
                long int count=0;
                int flag=0;
                while(x >= 1){
                    if(x==1) {
                        flag=-1;
                        break;
                    }
                    if(x%5==0)
                    {   count+=3;
                        x = x/5;
                    }
                    else if(x%3==0){
                        count+=2;
                        x = x/3;
                    }
                    else if(x%2==0){
                        count++;
                        x = x/2;
                    }
                    else
                    break;
                }

                if(flag==-1) cout<<count<<endl;
                else cout<<-1<<endl;
            }
        }


#include<bits/stdc++.h>
using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,count=0;
        cin>>n;
        while(n!=1){
            if(n%2==0){
            n=n/2;
            count++;
            }
            else if(n%3==0)
           {
               n=(2*n)/3;
               count++;
           }
            else if(n%5==0)
            {
                n=(4*n)/5;
                count++;
            }

            else
            {
                cout<<"-1";
                break;
            }
           cout<<count<<endl;
        }
    }
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--){
        unsigned long int x   ;
        cin>>x;
        long int count=0;
        int flag=0;
        while(x > = 1){
            if(x==1) {
                flag=-1;
                break;
            }
            if(x%5==0)
            {   count+=3;
                x = x/5;
            }
            else if(x%3==0){
                count+=2;
                x = x/3;
            }
            else if(x%2==0){
                count++;
                x = x/2;
            }
        }

        if(flag==-1) cout<<count<<endl;
        else cout<<-1<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,count=0;
        cin>>n;
        while(n!=1){
            if(n%2==0){
            n=n/2;
            count++;
            }
            else if(n%3==0)
           {
               n=(2*n)/3;
               count++;
           }
            else if(n%5==0)
            {
                n=(4*n)/5;
                count++;
            }

            else
            {
                cout<<"-1";
                break;
            }

        }
        cout<<count<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,count=0;
        cin>>n;
        while(n!=1){
]
            if(n%2==0){
            n=n/2;
            count++;
            }
            if(n%3==0)
            {
               n=(2*n)/3;
               count++;
            }
            if(n%5==0)
            {
                n=(4*n)/5;
                count++;
            }

            if(n%2!=0 && n%3!=0 && n%5!=0)
            {
               count=-1;
                break;
            }

        }
        cout<<count<<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;

long long divide(long long n,long long count)
{
    if(n==1)
    {
        return count;
    }

    if(n%2==0 && n%5!=0 && n%3!=0 || n%2==0 && n%5==0 && n%3!=0)
    {
        return divide(n/2,count+1) ;
    }

    if(n%2!=0 && n%5==0 && n%3!=0 )
    {
        return divide(4*n/5,count+1);
    }

    if(n%2!=0 && n%5!=0 && n%3==0 || n%2==0 && n%5==0 && n%3==0)
    {
        return divide(2*n/3,count+1);
    }

    if(n%2==0 && n%5!=0 && n%3==0)
    {
        return divide(n/2,count+1) ;
    }

    if(n%2!=0 && n%5!=0 && n%3!=0)
    {
        return -1;

    }
}
int main() {

    long long n,ans,q;
    cin>>q;

    while(q--)
    {
        cin>>n;
        ans=divide(n,0);
        cout<<ans<<endl;
    }

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

long long divide(long long n,long long count)
{
    if(n==1)
    {
        return count;
    }

    if(n%2==0 && n%5!=0 && n%3!=0 || n%2==0 && n%5==0 && n%3!=0)
    {
        return divide(n/2,count+1) ;
    }

    if(n%2!=0 && n%5==0 && n%3!=0 )
    {
        return divide(4*n/5,count+1);
    }

    if(n%2!=0 && n%5!=0 && n%3==0 || n%2==0 && n%5==0 && n%3==0)
    {
        return divide(2*n/3,count+1);
    }

    if(n%2==0 && n%5!=0 && n%3==0)
    {
        return divide(n/2,count+1) ;
    }

    return -1;
}
int main() {

    long long n,ans,q;
    cin>>q;

    while(q--)
    {
        cin>>n;
        ans=divide(n,0);
        cout<<ans<<endl;
    }

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

long long divide(long long n,long long count)
{
    if(n==1)
    {
        return count;
    }

    if(n%2==0 && n%5!=0 && n%3!=0 || n%2==0 && n%5==0 && n%3!=0)
    {
        return divide(n/2,count+1) ;
    }

    if(n%2!=0 && n%5==0 && n%3!=0 )
    {
        return divide(4*n/5,count+1);
    }

    if(n%2!=0 && n%5!=0 && n%3==0 || n%2==0 && n%5==0 && n%3==0)
    {
        return divide(2*n/3,count+1);
    }

    if(n%2==0 && n%5!=0 && n%3==0)
    {
        return divide(n/2,count+1) ;
    }

    return -1;
}
int main() {

    long long n,ans;
    cin>>n;

    ans=divide(n,0);
    cout<<ans<<endl;

	return 0;
}