#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
// cout<<"GfG!";
int t;
cin>>t;
while(t--)
{
ll int k;
cin>>k;
vector<ll int> v1;
for(ll int i=0; i<7; i++)
{
ll int x;
cin>>x;
v1.push_back(x);
}
for(ll int i=0; i<14; i++)
{
v1.push_back(v1[i]);
}
// for(ll int i=0; i<21; i++)
// {
// cout<<v1[i];
// }
ll int b,l,d;
ll int ans,ans1;
ans1=1e9+10;
for(ll int i=0; i<7; i++)
{
d=0;
for(ll int j=0; j<7; j++)
{
if(v1[i+j]==1)
{
d++;
}
}
if(k-d<0)
{
ans=0;
b=0;
}
else
{
ans=((k-d)/d)*7;
b=k-d-k%d;
}
for(ll int j=0; j<14; j++)
{
l=j+1;
if(v1[i+j]==1)
{
b++;
// cout<<b<<" ";
if(b==k)
{
break;
}
}
}
ans=ans+l;
// cout<<ans<<l<<endl;
ans1=min(ans1,ans);
}
cout<<ans1<<endl;
}
return 0;
}
80A - Panoramix's Prediction | 1354B - Ternary String |
122B - Lucky Substring | 266B - Queue at the School |
1490A - Dense Array | 1650B - DIV + MOD |
1549B - Gregor and the Pawn Game | 553A - Kyoya and Colored Balls |
1364A - XXXXX | 1499B - Binary Removals |
1569C - Jury Meeting | 108A - Palindromic Times |
46A - Ball Game | 114A - Cifera |
776A - A Serial Killer | 25B - Phone numbers |
1633C - Kill the Monster | 1611A - Make Even |
1030B - Vasya and Cornfield | 1631A - Min Max Swap |
1296B - Food Buying | 133A - HQ9+ |
1650D - Twist the Permutation | 1209A - Paint the Numbers |
1234A - Equalize Prices Again | 1613A - Long Comparison |
1624B - Make AP | 660B - Seating On Bus |
405A - Gravity Flip | 499B - Lecture |