t = int(input())
for _ in range(t):
n = int(input())
a = input().replace(' ', '')
h = -1
if '00' not in a:
h = 1 + (a[0] == '1')
for i in range(1, n):
if a[i] == a[i - 1]:
h += 5
elif a[i] == '1':
h += 1
print(h)
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define pi(x) cout<<x;
#define ps(x) cout<<x<<" ";
#define pnl(x) cout<<x<<"\n";
#define for0(n) for(i=0;i<n;i++)
#define for1(n) for(i=1;i<=n;i++)
#define m(x) memset(x,0,sizeof x);
#define nl cout<<"\n";
#define mp make_pair
#define pb push_back
#define fr first
#define se second
#define Inf 1e16
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll test,h,i,j,xy,flag=0,n,p,u,count,d,o1=0,o2=0,s,e,l,r,x,y,m,z,max1,x1,y1,k,x2,y2,z1,z2,sum,min1;
cin>>test;
while(test--){
cin>>n;
flag=1;
x1=1;
xy=0;
for(i=0;i<n;i++){
cin>>x;
if(x==1){
x1+=flag&&i?5:1;
flag=1;
}else{
if(flag==0){
xy=1;
}
flag=0;
}
}
if(xy){
cout<<"-1\n";
}else{
cout<<x1<<"\n";
}
}
return 0;
}
810A - Straight A | 1433C - Dominant Piranha |
633A - Ebony and Ivory | 1196A - Three Piles of Candies |
299A - Ksusha and Array | 448B - Suffix Structures |
1092B - Teams Forming | 1166C - A Tale of Two Lands |
544B - Sea and Islands | 152B - Steps |
1174D - Ehab and the Expected XOR Problem | 1511A - Review Site |
1316A - Grade Allocation | 838A - Binary Blocks |
1515D - Phoenix and Socks | 1624D - Palindromes Coloring |
1552F - Telepanting | 1692G - 2Sort |
1191A - Tokitsukaze and Enhancement | 903A - Hungry Student Problem |
52B - Right Triangles | 1712A - Wonderful Permutation |
1712D - Empty Graph | 1712B - Woeful Permutation |
1712C - Sort Zero | 1028B - Unnatural Conditions |
735B - Urbanization | 746C - Tram |
1278B - A and B | 1353D - Constructing the Array |