#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<queue>
#include<fstream>
#include<sstream>
#include<stack>
#include<list>
#include<deque>
#include<bitset>
#include<utility>
#include<climits>
#include<iomanip>
#include<ctime>
#include<complex>
using namespace std;
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#define inf INT_MAX/3
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define SET(a,c) memset(a,c,sizeof a)
#define CLR(a) memset(a,0,sizeof a)
#define pii pair<int,int>
#define pcc pair<char,char>
#define pic pair<int,char>
#define pci pair<char,int>
#define VS vector<string>
#define VI vector<int>
#define debug(x) cout<<#x<<": "<<x<<endl
#define MIN(a,b) (a>b?b:a)
#define MAX(a,b) (a>b?a:b)
#define pi 2*acos(0.0)
#define INFILE() freopen("in0.txt","r",stdin)
#define OUTFILE()freopen("out0.txt","w",stdout)
#define in scanf
#define out printf
#define ll unsigned long long
#define Mx 100015
string str;
ll len;
ll tree[4*Mx];
void insert(int cur , int st , int ed , int pos)
{
if(st==ed && st==pos)
{
tree[cur]++;
return ;
}
int mid,lft,rgt;
mid=(st+ed)/2;
lft=2*cur;
rgt=lft+1;
if(pos<=mid)
{
insert(lft,st,mid,pos);
}
else insert(rgt,mid+1,ed,pos);
tree[cur]=tree[lft]+tree[rgt];
}
int query(int cur , int st , int ed , int x , int y)
{
if(x>y)return 0;
if(st==x && ed==y)
{
return tree[cur];
}
int mid,lft,rgt;
mid=(st+ed)/2;
lft=2*cur;
rgt=lft+1;
if(y<=mid)return query(lft,st,mid,x,y);
else if(x>mid)return query(rgt,mid+1,ed,x,y);
else
{
int a=query(lft,st,mid,x,mid);
int b=query(rgt,mid+1,ed,mid+1,y);
return a+b;
}
}
int main()
{
int i,j,k;
int cas,ks;
cin>>ks;
FOR(cas,1,ks+1)
{
cin>>len;
int nn=(4*len)+5;
//SET(tree,0);
FOR(i,0,nn)tree[i]=0;
cin>>str;
// scanf("%s",str);
int high=0;
ll sz=0;
ll con=0;
ll res=0;
for(i=0; i<len; i++)
{
if(str[i]=='4')
{
con=1;
sz=sz+1;
}
else if(str[i]=='7')
{
if(sz==0)
{
con=1;
}
else
{
ll bad=0;
bad=query(1,1,len,1,con-1 );
res=res+( i - (2*bad) );
insert(1,1,len,con);
con++;
sz=sz-1;
}
}
}
cout<<res<<endl;
}
return 0;
}
I2luY2x1ZGU8aW9zdHJlYW0+CiNpbmNsdWRlPHZlY3Rvcj4KI2luY2x1ZGU8bWFwPgojaW5jbHVkZTxhbGdvcml0aG0+CiNpbmNsdWRlPGNzdGRpbz4KI2luY2x1ZGU8Y21hdGg+CiNpbmNsdWRlPGNzdGRsaWI+CiNpbmNsdWRlPGNzdHJpbmc+CiNpbmNsdWRlPHF1ZXVlPgojaW5jbHVkZTxmc3RyZWFtPgojaW5jbHVkZTxzc3RyZWFtPgojaW5jbHVkZTxzdGFjaz4KI2luY2x1ZGU8bGlzdD4KI2luY2x1ZGU8ZGVxdWU+CiNpbmNsdWRlPGJpdHNldD4KI2luY2x1ZGU8dXRpbGl0eT4KI2luY2x1ZGU8Y2xpbWl0cz4KI2luY2x1ZGU8aW9tYW5pcD4KI2luY2x1ZGU8Y3RpbWU+CiNpbmNsdWRlPGNvbXBsZXg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiAKIAojZGVmaW5lIEZPUihpLGEsYikgZm9yIChpbnQgaT0oYSk7aTwoYik7aSsrKQojZGVmaW5lIFJGT1IoaSxhLGIpIGZvciAoaW50IGk9KGIpLTE7aT49KGEpO2ktLSkKI2RlZmluZSBSRVAoaSxuKSBmb3IgKGludCBpPTA7aTwobik7aSsrKQojZGVmaW5lIFJSRVAoaSxuKSBmb3IgKGludCBpPShuKS0xO2k+PTA7aS0tKQogCiNkZWZpbmUg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WlkPShzdCtlZCkvMjsKICAgIGxmdD0yKmN1cjsKICAgIHJndD1sZnQrMTsKIAogICAgaWYocG9zPD1taWQpCiAgICB7CiAgICAgICAgaW5zZXJ0KGxmdCxzdCxtaWQscG9zKTsKICAgIH0KICAgIGVsc2UgaW5zZXJ0KHJndCxtaWQrMSxlZCxwb3MpOwogCiAgICB0cmVlW2N1cl09dHJlZVtsZnRdK3RyZWVbcmd0XTsKfQogCmludCBxdWVyeShpbnQgY3VyICwgaW50IHN0ICwgaW50IGVkICwgaW50IHggLCBpbnQgeSkKewogICAgaWYoeD55KXJldHVybiAwOwogCiAgICBpZihzdD09eCAmJiBlZD09eSkKICAgIHsKICAgICAgICByZXR1cm4gdHJlZVtjdXJdOwogICAgfQogICAgaW50IG1pZCxsZnQscmd0OwogICAgbWlkPShzdCtlZCkvMjsKICAgIGxmdD0yKmN1cjsKICAgIHJndD1sZnQrMTsKIAogICAgaWYoeTw9bWlkKXJldHVybiBxdWVyeShsZnQsc3QsbWlkLHgseSk7CiAgICBlbHNlIGlmKHg+bWlkKXJldHVybiBxdWVyeShyZ3QsbWlkKzEsZWQseCx5KTsKICAgIGVsc2UKICAgIHsKICAgICAgICBpbnQgYT1xdWVyeShsZnQsc3QsbWlkLHgsbWlkKTsKICAgICAgICBpbnQgYj1xdWVyeShyZ3QsbWlkKzEsZWQsbWlkKzEseSk7CiAgICAgICAgcmV0dXJuIGErYjsKICAgIH0KIAp9CiAKIAppbnQgbWFpbigpCnsKIAogICAgaW50IGksaixrOwogICAgaW50IGNhcyxrczsKICAgIGNpbj4+a3M7CiAgICBGT1IoY2FzLDEsa3MrMSkKICAgIHsKICAgICAgICBjaW4+PmxlbjsKICAgICAgICBpbnQgbm49KDQqbGVuKSs1OwogICAgICAgIC8vU0VUKHRyZWUsMCk7CiAgICAgICAgRk9SKGksMCxubil0cmVlW2ldPTA7CiAgICAgICAgY2luPj5zdHI7CiAKLy8gICAgICAgIHNjYW5mKCIlcyIsc3RyKTsKICAgICAgICBpbnQgaGlnaD0wOwogCiAKICAgICAgICBsbCBzej0wOwogICAgICAgIGxsIGNvbj0wOwogICAgICAgIGxsIHJlcz0wOwogCiAgICAgICAgZm9yKGk9MDsgaTxsZW47IGkrKykKICAgICAgICB7CiAgICAgICAgICAgIGlmKHN0cltpXT09JzQnKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBjb249MTsKICAgICAgICAgICAgICAgIHN6PXN6KzE7CiAKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlIGlmKHN0cltpXT09JzcnKQogICAgICAgICAgICB7CiAKICAgICAgICAgICAgICAgIGlmKHN6PT0wKQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIGNvbj0xOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgZWxzZQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIGxsIGJhZD0wOwogICAgICAgICAgICAgICAgICAgIGJhZD1xdWVyeSgxLDEsbGVuLDEsY29uLTEgKTsKIAogCiAgICAgICAgICAgICAgICAgICAgcmVzPXJlcysoIGkgLSAoMipiYWQpICk7CiAKIAogICAgICAgICAgICAgICAgICAgIGluc2VydCgxLDEsbGVuLGNvbik7CiAgICAgICAgICAgICAgICAgICAgY29uKys7CiAgICAgICAgICAgICAgICAgICAgc3o9c3otMTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBjb3V0PDxyZXM8PGVuZGw7CiAKICAgIH0KIAogICAgcmV0dXJuIDA7Cn0K