#pragma GCC optimize("Ofast")
/*#pragma GCC target("avx,avx2,fma")
#pragma GCC target ("sse4")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")*/
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld double
#define rep(i,a,b) for(ll i=a; i<b; ++i)
#define BOOST std::ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb push_back
#define all(v) v.begin(),v.end()
#define sl(a) a.size()
#define mod 1000000007
#define lcm(a,b) (a*b)/__gcd(a,b)
#define MAX1 100006
#define f first
#define s second
#define pi 2*acos(0.0)
bool isPowerOfTwo (ll x) {  return x && (!(x&(x-1)));  }
ll Modular_Exponentiation(ll x, ll n, ll M)
{
    if(n==0) return 1;
    if(n%2) return (x*Modular_Exponentiation((x*x)%M,n/2,M))%M;
    else return (Modular_Exponentiation((x*x)%M,n/2,M))%M;
}
bool isPrime(ll n)
{
    if (n<=1)  return false;
    if (n<=3)  return true;
    if (n%2==0||n%3==0) return false;
    for(ll i=5; i*i<=n; i+=6)
        if (n%i==0||n%(i+2)==0) return false;

    return true;
}
/********************************************************************************************/
ll n, arr[200005];
ll dp[200005][2];
ll cal(ll indx, int turn)
{
    if(indx>n) return 0;
    if(dp[indx][turn]!=-1) return dp[indx][turn];
    if(turn)
    {
        ll a1=1e18,a2=1e18;
        if(indx<n) a2=(arr[indx]+arr[indx+1]+cal(indx+2,0));
        else a1=(arr[indx]+cal(indx+1,0));
        return dp[indx][turn]=min(a1,a2);
    }
    else
    {
        ll a1=1e18,a2=1e18;
        if(indx<n) a1=cal(indx+2,1);
        else return a2=cal(indx+1,1);
        return dp[indx][turn]=min(a1,a2);
    }
}
void solve()
{
    cin>>n;
    rep(i,1,n+1) cin>>arr[i];
    memset(dp,-1,sizeof(dp));
    cout<<cal(1,1)<<"\n";
}
int32_t main()
{
    #ifndef ONLINE_JUDGE
    // for getting input from input.txt
    freopen("input.txt","r",stdin);
    //for getting error from error.txt
    freopen("error.txt", "w", stderr);
    // for writing output to output.txt
    freopen("output.txt","w",stdout);
    #endif
    BOOST
    int T=1; cin>>T;
    while(T--) solve();
    cerr<<"Time taken : "<<fixed<<setprecision(5)<<((float)clock()/CLOCKS_PER_SEC)*1000<<" ms"<<"\n";
    cerr<<"My CLOCKS_PER_SEC= "<<CLOCKS_PER_SEC<<"\n";
    return 0;
}


#include <cstdio>
#include <vector>
using namespace std;
vector<int> v;
int n;

void fri(int &t, int &sum)
{
    if (t == t - 1)
    {
        if (v[t])
        {
            sum++;
        }
        else
        {
            /* code */
        }
    }
    else
    {
        if (v[t])
        {
            if (v[t + 1])
            {
                sum++;
                t++;
            }
            else
            {
                sum++;
                t += 2;
            }
        }
        else
        {
            if (v[t + 1])
            {
                t++;
            }
            else
            {
                t += 2;
            }
        }
    }
}

void me(int &t)
{
    if (t == t - 1)
    {
        t++;
    }
    else
    {
        if (v[t])
        {
            if (v[t + 1])
            {
                t += 2;
            }
            else
            {
                t++;
            }
        }
        else
        {
            if (v[t + 1])
            {
                t += 2;
            }
            else
            {
                t++;
            }
        }
    }
}

int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        int temp;
        scanf("%d", &n);
        for (int i = 0; i < n; i++)
        {
            scanf("%d", &temp);
            v.push_back(temp);
        }
        int sum = 0;
        for (int i = 0; i < n;)
        {
            fri(i, sum);
            me(i);
        }
        printf("%d\n", sum);
        v.clear();
    }
}


#include <cstdio>
#include <vector>
using namespace std;
vector<int> v;
int n;

void fri(int &t, int &sum)
{
    if (t == n - 1)
    {
        if (v[t])
        {
            t++
            sum++;
        }
        else
        {
            t++
        }
    }
    else
    {
        if (v[t])
        {
            if (v[t + 1])
            {
                sum++;
                t++;
            }
            else
            {
                sum++;
                t += 2;
            }
        }
        else
        {
            if (v[t + 1])
            {
                t++;
            }
            else
            {
                t += 2;
            }
        }
    }
}

void me(int &t)
{
    if (t == n - 1)
    {
        t++;
    }
    else
    {
        if (v[t])
        {
            if (v[t + 1])
            {
                t += 2;
            }
            else
            {
                t++;
            }
        }
        else
        {
            if (v[t + 1])
            {
                t += 2;
            }
            else
            {
                t++;
            }
        }
    }
}

int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        int temp;
        scanf("%d", &n);
        for (int i = 0; i < n; i++)
        {
            scanf("%d", &temp);
            v.push_back(temp);
        }
        int sum = 0;
        for (int i = 0; i < n;)
        {
            fri(i, sum);
            me(i);
        }
        printf("%d\n", sum);
        v.clear();
    }
}


#include <cstdio>
#include <vector>
using namespace std;
vector<int> v;
int n;

void fri(int &t, int &sum)
{
    if (t == n - 1)
    {
        if (v[t])
        {
            sum++;
            t++;
        }
        else
        {
            t++;
        }
    }
    else
    {
        if (v[t])
        {
            if (v[t + 1])
            {
                sum++;
                t++;
            }
            else
            {
                sum++;
                t += 2;
            }
        }
        else
        {
            if (v[t + 1])
            {
                t++;
            }
            else
            {
                t += 2;
            }
        }
    }
}

void me(int &t)
{
    if (t == n - 1)
    {
        t++;
    }
    else
    {
        if (v[t])
        {
            if (v[t + 1])
            {
                t += 2;
            }
            else
            {
                t++;
            }
        }
        else
        {
            if (v[t + 1])
            {
                t += 2;
            }
            else
            {
                t++;
            }
        }
    }
}

int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        int temp;
        scanf("%d", &n);
        for (int i = 0; i < n; i++)
        {
            scanf("%d", &temp);
            v.push_back(temp);
        }
        int sum = 0;
        for (int i = 0; i < n;)
        {
            fri(i, sum);
            me(i);
        }
        printf("%d\n", sum);
        v.clear();
    }
}


#include <cstdio>
#include <vector>
using namespace std;
vector<int> v;
int n;

void fri(int &t, int &sum)
{
    if (t == n - 1)
    {
        if (v[t])
        {
            sum++;
        }
        else
        {
            /* code */
        }
    }
    else
    {
        if (v[t])
        {
            if (v[t + 1])
            {
                sum++;
                t++;
            }
            else
            {
                sum++;
                t += 2;
            }
        }
        else
        {
            if (v[t + 1])
            {
                t++;
            }
            else
            {
                t += 2;
            }
        }
    }
}

void me(int &t)
{
    if (t == n - 1)
    {
        t++;
    }
    else
    {
        if (v[t])
        {
            if (v[t + 1])
            {
                t += 2;
            }
            else
            {
                t++;
            }
        }
        else
        {
            if (v[t + 1])
            {
                t += 2;
            }
            else
            {
                t++;
            }
        }
    }
}

int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        int temp;
        scanf("%d", &n);
        for (int i = 0; i < n; i++)
        {
            scanf("%d", &temp);
            v.push_back(temp);
        }
        int sum = 0;
        for (int i = 0; i < n;)
        {
            fri(i, sum);
            me(i);
        }
        printf("%d\n", sum);
        v.clear();
    }
}


#include <cstdio>
#include <vector>
using namespace std;
vector<int> v;
int n;

void fri(int &t, int &sum)
{
    if (t == n - 1)
    {
        if (v[t])
        {
            sum++;
            t++;
        }
        else
        {
            t++;
        }
    }
    else
    {
        if (v[t])
        {
            if (v[t + 1])
            {
                sum++;
                t++;
            }
            else
            {
                sum++;
                t += 2;
            }
        }
        else
        {
            if (v[t + 1])
            {
                t++;
            }
            else
            {
                t += 2;
            }
        }
    }
}

void me(int &t)
{
    if (t == n - 1)
    {
        t++;
    }
    else
    {
        if (v[t])
        {
            if (v[t + 1])
            {
                t += 2;
            }
            else
            {
                t++;
            }
        }
        else
        {
            if (v[t + 1])
            {
                t += 2;
            }
            else
            {
                t++;
            }
        }
    }
}

int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        int temp;
        scanf("%d", &n);
        for (int i = 0; i < n; i++)
        {
            scanf("%d", &temp);
            v.push_back(temp);
        }
        int sum = 0;
        for (int i = 0; i < n;)
        {
            fri(i, sum);
            if (i < n)
            {
                me(i);
            }
        }
        printf("%d\n", sum);
        v.clear();
    }
}


#include<iostream>
using namespace std;
int main()
{
	 int n;
	 cin>>n;
	 int a[n+1];
     long long ans=0;

	 for( int i=1;i<=n;i++)
	 {
	 	cin>>a[i];
	 	if( i==1 && a[i]==1) ans++;
	 }

	  for( int i=2;i<=n;i++)
	 {

	 	if(a[i]+a[i+1]+a[i+2]==3) ans++,i +=2;
	 }
	 cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int a[n];
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
        }
        int count=0;
        for(int i=2;i<n-2;i++)
        {
            if(a[i]+a[i+1]+a[i+2]==3)
            {
                count++;
                i+=2;
            }
        }
        if(a[1]==1) count++;
        cout<<count<<'\n';
    }
}


#include<iostream>
using namespace std;
int main()
{
	 int n;
	 cin>>n;
	 int a[n+1];
     ll ans=0;

	 for( int i=1;i<=n;i++)
	 {
	 	cin>>a[i];
	 	if( i==1 && a[i]==1) ans++;
	 }

	  for( int i=2;i<=n;i++)
	 {

	 	if(a[i]+a[i+1]+a[i+2]==3) ans++,i +=2;
	 }
	 cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int a[n];
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
            if(i==1 && a[1]==1) count++;
        }
        int count=0;
        for(int i=2;i<n-2;i++)
        {
            if(a[i]+a[i+1]+a[i+2]==3)
            {
                count++;
                i+=2;
            }
        }
        // if(a[0]==1) count++;
        cout<<count<<'\n';
    }
}


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

int main()
{

int t;
cin>>t;
while(t--)
{
	 int n;
	 cin>>n;
	 int a[n+1];
     ll ans=0;

	 for( int i=1;i<=n;i++)
	 {
	 	cin>>a[i];
	 	if( i==1 && a[i]==1) ans++;
	 }

	  for( int i=2;i<=n;i++)
	 {

	 	if(a[i]+a[i+1]+a[i+2]==3) ans++,i +=2;
	 }
	 cout<<ans<<endl;

    }

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int a[n];
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
        }
        int count=0;
        for(int i=2;i<n-2;i++)
        {
            if(a[i]+a[i+1]+a[i+2]==3)
            {
                count++;
                i+=2;
            }
        }
        if(a[0]==1) count++;
        cout<<count<<'\n';
    }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int a[n];
                int count=0;

        for(int i=0;i<n;i++)
        {
            cin>>a[i];
            if(i==1 && a[1]==1) count++;
        }
        for(int i=2;i<n-2;i++)
        {
            if(a[i]+a[i+1]+a[i+2]==3)
            {
                count++;
                i+=2;
            }
        }
        // if(a[0]==1) count++;
        cout<<count<<'\n';
    }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define llu unsigned long long
#define pb push_back
#define eb emplace_back
#define ll long long
#define f(i,a,b) for(ll i=a;i<b;i++)
#define fs(i, s) for(ll i=0;s[i]!='\0';i++)
#define fr(i,a,b) for(ll i=a;i>=b;i--)
#define gcd(a,b) __gcd(a,b)
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define c1(x) __builtin_popcountll(x)
#define setprs(x) cout << fixed << setprecision(x);
#define nl '\n'
const ll mod=1e9 + 7;
const ll sz=2e5 + 100;
const double PI =3.14159265;
ll power(ll a, ll b, ll m = mod){if(b==0)return 1;else if(b==1)return a%m;else{ll t=power(a,b>>1,m);t=(t * t)%m;if(b&1)return (t * a)%m;else return t;}}

//--------------------------------------------------------

#define int long long

int dp[sz][2];

int find(int a[], int n, int i, int f){
    if(i > n - 1){
        return 0;
    }
    if(dp[i][f] != -1){
        return dp[i][f];
    }
    if(f == 1){
        return dp[i][f] = min(a[i] + find(a, n, i + 1, !f), (i + 1 < n) ? (a[i] + a[i+1] + find(a, n, i + 2, !f)) : (mod));
    }else{
        return dp[i][f] = min(find(a, n, i + 1, !f), find(a, n, i + 2, !f));
    }
}

void solve(){
    int n;
    cin >> n;
    int a[sz];
    f(i, 0, n){
        cin >> a[i];
    }
    f(i, 0, sz){
        dp[i][0] = dp[i][1] = -1;
    }
    cout << find(a, n, 0, 1);
}

int32_t main()
{
    fastio
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    int t=1;
    cin>>t;
    while(t--){
        solve();
        cout << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define llu unsigned long long
#define pb push_back
#define eb emplace_back
#define ll long long
#define f(i,a,b) for(ll i=a;i<b;i++)
#define fs(i, s) for(ll i=0;s[i]!='\0';i++)
#define fr(i,a,b) for(ll i=a;i>=b;i--)
#define gcd(a,b) __gcd(a,b)
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define c1(x) __builtin_popcountll(x)
#define setprs(x) cout << fixed << setprecision(x);
#define nl '\n'
const ll mod=1e9 + 7;
const ll sz=2e5 + 100;
const double PI =3.14159265;
ll power(ll a, ll b, ll m = mod){if(b==0)return 1;else if(b==1)return a%m;else{ll t=power(a,b>>1,m);t=(t * t)%m;if(b&1)return (t * a)%m;else return t;}}

//--------------------------------------------------------

#define int long long

int dp[sz][2];

int find(int a[], int n, int i, int f){
    if(i > n - 1){
        return 0;
    }
    if(dp[i][f] != -1){
        return dp[i][f];
    }
    if(f == 1){
        dp[i][f] = a[i] + find(a, n, i + 1, 0);
        if(i + 1 < n){
            dp[i][f] = min(dp[i][f], a[i] + a[i+1] + find(a, n, i + 2, 0));
        }
        return dp[i][f];
    }

    return dp[i][f] = min(find(a, n, i + 1, 1), find(a, n, i + 2, 1));

}

void solve(){
    int n;
    cin >> n;
    int a[sz];
    f(i, 0, n){
        cin >> a[i];
    }
    f(i, 0, sz){
        dp[i][0] = dp[i][1] = -1;
    }
    cout << find(a, n, 0, 1);
}

int32_t main()
{
    fastio
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    int t=1;
    cin>>t;
    while(t--){
        solve();
        cout << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define llu unsigned long long
#define pb push_back
#define eb emplace_back
#define ll long long
#define f(i,a,b) for(ll i=a;i<b;i++)
#define fs(i, s) for(ll i=0;s[i]!='\0';i++)
#define fr(i,a,b) for(ll i=a;i>=b;i--)
#define gcd(a,b) __gcd(a,b)
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define c1(x) __builtin_popcountll(x)
#define setprs(x) cout << fixed << setprecision(x);
#define nl '\n'
const ll mod=1e9 + 7;
const ll sz=2e5 + 100;
const double PI =3.14159265;
ll power(ll a, ll b, ll m = mod){if(b==0)return 1;else if(b==1)return a%m;else{ll t=power(a,b>>1,m);t=(t * t)%m;if(b&1)return (t * a)%m;else return t;}}

//--------------------------------------------------------

#define int long long

int dp[sz][2];

int find(int a[], int n, int i, int f){
    if(i > n - 1){
        return 0;
    }
    if(dp[i][f] != -1){
        return dp[i][f];
    }
    if(f == 1){
        dp[i][f] = a[i] + find(a, n, i + 1, 0);
        if(i + 1 < n){
            dp[i][f] = min(dp[i][f], a[i] + a[i+1] + find(a, n, i + 2, 0));
        }
        return dp[i][f];
    }

    return dp[i][f] = min(find(a, n, i + 1, 1), find(a, n, i + 2, 1));

}

void solve(){
    int n;
    cin >> n;
    int a[sz];
    f(i, 0, n){
        cin >> a[i];
    }
    f(i, 0, n){
        dp[i][0] = dp[i][1] = -1;
    }
    cout << find(a, n, 0, 1);
}

int32_t main()
{
    fastio
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    int t=1;
    cin>>t;
    while(t--){
        solve();
        cout << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define llu unsigned long long
#define pb push_back
#define eb emplace_back
#define ll long long
#define f(i,a,b) for(ll i=a;i<b;i++)
#define fs(i, s) for(ll i=0;s[i]!='\0';i++)
#define fr(i,a,b) for(ll i=a;i>=b;i--)
#define gcd(a,b) __gcd(a,b)
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define c1(x) __builtin_popcountll(x)
#define setprs(x) cout << fixed << setprecision(x);
#define nl '\n'
const ll mod=1e9 + 7;
const ll sz=2e5 + 100;
const double PI =3.14159265;
ll power(ll a, ll b, ll m = mod){if(b==0)return 1;else if(b==1)return a%m;else{ll t=power(a,b>>1,m);t=(t * t)%m;if(b&1)return (t * a)%m;else return t;}}

//--------------------------------------------------------

#define int long long

void solve(){
    int n;
    cin >> n;
    int a[sz];
    f(i, 0, n){
        cin >> a[i];
    }
    int sum = 0;
    int f = 1;
    int i;
    for(i = 0; i < n - 1; ){
        if(f){
            if(a[i] == 1 && a[i + 1] == 0){
                sum += 1;
                i += 2;
            }else if(a[i] == 0 && a[i + 1] == 0){
                i += 2;
            }else if(a[i] == 1 && a[i + 1] == 1){
                sum += 1;
                i += 1;
            }else{
                i += 1;
            }
            f = 0;
        }else{
            if(a[i + 1] == 0){
                i += 1;
            }else{
                i += 2;
            }
            f = 1;
        }
    }
    if(i == n - 1 && f == 1 && a[n - 1] == 1){
        sum += 1;
    }
    cout << sum;
}

int32_t main()
{
    fastio
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    int t=1;
    cin>>t;
    while(t--){
        solve();
        cout << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define llu unsigned long long
#define pb push_back
#define eb emplace_back
#define ll long long
#define f(i,a,b) for(ll i=a;i<b;i++)
#define fs(i, s) for(ll i=0;s[i]!='\0';i++)
#define fr(i,a,b) for(ll i=a;i>=b;i--)
#define gcd(a,b) __gcd(a,b)
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define c1(x) __builtin_popcountll(x)
#define setprs(x) cout << fixed << setprecision(x);
#define nl '\n'
const ll mod=1e9 + 7;
const ll sz=2e5 + 100;
const double PI =3.14159265;
ll power(ll a, ll b, ll m = mod){if(b==0)return 1;else if(b==1)return a%m;else{ll t=power(a,b>>1,m);t=(t * t)%m;if(b&1)return (t * a)%m;else return t;}}

//--------------------------------------------------------

#define int long long

void solve(){
    int n;
    cin >> n;
    int a[sz];
    f(i, 0, n){
        cin >> a[i];
    }
    int sum = 0;
    int f = 1;
    int i;
    for(i = 0; i < n - 1; ){
        if(f){
            if(a[i] == 1 && a[i + 1] == 0){
                sum += 1;
                i += 2;
            }else if(a[i] == 0 && a[i + 1] == 0){
                i += 2;
            }else if(a[i] == 1 && a[i + 1] == 1){
                sum += 1;
                i += 1;
            }else{
                i += 1;
            }
            f = 0;
        }else{
            if(a[i + 1] == 0){
                i += 1;
            }else{
                i += 2;
            }
            f = 1;
        }
    }
    if(i < n){
        if( f == 1 && a[i] == 1 ){
            sum += 1;
        }
    }
    cout << sum;
}

int32_t main()
{
    fastio
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    int t=1;
    cin>>t;
    while(t--){
        solve();
        cout << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define llu unsigned long long
#define pb push_back
#define eb emplace_back
#define ll long long
#define f(i,a,b) for(ll i=a;i<b;i++)
#define fs(i, s) for(ll i=0;s[i]!='\0';i++)
#define fr(i,a,b) for(ll i=a;i>=b;i--)
#define gcd(a,b) __gcd(a,b)
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define c1(x) __builtin_popcountll(x)
#define setprs(x) cout << fixed << setprecision(x);
#define nl '\n'
const ll mod=1e9 + 7;
const ll sz=2e5 + 100;
const double PI =3.14159265;
ll power(ll a, ll b, ll m = mod){if(b==0)return 1;else if(b==1)return a%m;else{ll t=power(a,b>>1,m);t=(t * t)%m;if(b&1)return (t * a)%m;else return t;}}

//--------------------------------------------------------

#define int long long

int dp[sz][2];

int find(int a[], int n, int i, int f){
    if(i > n - 1){
        return 0;
    }
    if(dp[i][f] != -1){
        return dp[i][f];
    }
    if(f == 1){
        return dp[i][f] = min(a[i] + find(a, n, i + 1, !f), a[i] + a[i+1] + find(a, n, i + 2, !f));
    }else{
        return dp[i][f] = min(find(a, n, i + 1, !f), find(a, n, i + 2, !f));
    }
}

void solve(){
    int n;
    cin >> n;
    int a[sz];
    f(i, 0, n){
        cin >> a[i];
    }
    f(i, 0, sz){
        dp[i][0] = dp[i][1] = -1;
    }
    cout << find(a, n, 0, 1);
}

int32_t main()
{
    fastio
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    int t=1;
    cin>>t;
    while(t--){
        solve();
        cout << endl;
    }
    return 0;
}


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

const sz = 2e5 + 1;
int dp[sz][2];

int find(int a[], int n, int i, int f){
    if(i > n - 1){
        return 0;
    }

    if(dp[i][f] != -1){
        return dp[i][f];
    }

    if(f == 1){
        dp[i][f] = a[i] + find(a, n, i + 1, 0);
        if(i + 1 < n){
            dp[i][f] = min(dp[i][f], a[i] + a[i+1] + find(a, n, i + 2, 0));
        }
        return dp[i][f];
    }

    return dp[i][f] = min(find(a, n, i + 1, 1), find(a, n, i + 2, 1));

}

void solve() {
    int n;
    cin >> n;
    int a[sz];
    for(int i = 0; i < n; i ++){
        cin >> a[i];
    }
    for(int i = 0; i < n; i ++){
        dp[i][0] = dp[i][1] = -1;
    }
    cout << find(a, n, 0, 1);
}

int32_t main() {
    int t = 1;
    cin >> t;
    while(t --){
        solve();
        cout << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define llu unsigned long long
#define pb push_back
#define eb emplace_back
#define ll long long
#define f(i,a,b) for(ll i=a;i<b;i++)
#define fs(i, s) for(ll i=0;s[i]!='\0';i++)
#define fr(i,a,b) for(ll i=a;i>=b;i--)
#define gcd(a,b) __gcd(a,b)
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define c1(x) __builtin_popcountll(x)
#define setprs(x) cout << fixed << setprecision(x);
#define nl '\n'
const ll mod=1e9 + 7;
const ll sz=2e5 + 100;
const double PI =3.14159265;
ll power(ll a, ll b, ll m = mod){if(b==0)return 1;else if(b==1)return a%m;else{ll t=power(a,b>>1,m);t=(t * t)%m;if(b&1)return (t * a)%m;else return t;}}

//--------------------------------------------------------

#define int long long

void solve(){
    int n;
    cin >> n;
    int a[sz];
    f(i, 0, n){
        cin >> a[i];
    }
    int sum = 0;
    int f = 1;
    int i;
    for(i = 0; i < n - 1; ){
        if(f){
            if(a[i] == 1 && a[i + 1] == 0){
                sum += 1;
                i += 2;
            }else if(a[i] == 0 && a[i + 1] == 0){
                i += 2;
            }else if(a[i] == 1 && a[i + 1] == 1){
                sum += 1;
                i += 1;
            }else{
                i += 1;
            }
            f = 0;
        }else{
            if(a[i + 1] == 0){
                i += 1;
            }else{
                i += 2;
            }
            f = 1;
        }
    }
    cout << sum;
}

int32_t main()
{
    fastio
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    int t=1;
    cin>>t;
    while(t--){
        solve();
        cout << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<int>dp(1000000, -1);
int solve(vector<int>v, int i)
{
  if (dp[i] != -1)
    return dp[i];

  if (i >= v.size())
    dp[i] =  0;
  else {
    int sum = 0;
    int a, b, c = INT_MAX, d = INT_MAX;
    a = sum + v[i] + solve(v, i + 2);
    c  = sum + v[i] + solve(v, i + 3);
    if (i + 1 < v.size())
    { b = sum + v[i] + v[i + 1] + solve(v, i + 3);
      d = sum + v[i] + v[i + 1] + solve(v, i + 4);
    }
    dp[i] = min({a, b, c, d});
  }

  return dp[i];
}

int main()
{
#ifndef ONLINE_JUDGE
  freopen("ip.txt", "r", stdin);
  freopen("op.txt", "w", stdout);
#endif
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--)
  {
    int n;
    cin >> n;
    vector<int>a(n);
    for (int i = 0; i < n; i++)
      cin >> a[i];

    cout << solve(a, 0) << endl;

    for (int i = 0; i < 1000000; i++)
      dp[i] = -1;

  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<int>dp(2 * 100000 + 2, -1);
int solve(vector<int>&v, int i, int n)
{
  if (dp[i] != -1)
    return dp[i];

  if (i >= n)
    dp[i] =  0;
  else {
    int sum = 0;
    int a, b=INT_MAX, c , d = INT_MAX;
    a = sum + v[i] + solve(v, i + 2,n);
    c  = sum + v[i] + solve(v, i + 3,n);
    if (i + 1 < n)
    { b = sum + v[i] + v[i + 1] + solve(v, i + 3,n);
      d = sum + v[i] + v[i + 1] + solve(v, i + 4,n);
    }
    dp[i] = min({a, b, c, d});
  }

  return dp[i];
}

int main()
{
#ifndef ONLINE_JUDGE
  freopen("ip.txt", "r", stdin);
  freopen("op.txt", "w", stdout);
#endif
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--)
  {
    int n;
    cin >> n;
    vector<int>a(n);
    for (int i = 0; i < n; i++)
      cin >> a[i];

    cout << solve(a, 0,n) << endl;

    for (int i = 0; i < 2 * 100000 + 2; i++)
      dp[i] = -1;

  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<int>dp(2 * 100000 + 2, -1);
int solve(vector<int>&v, int i)
{
  if (dp[i] != -1)
    return dp[i];

  if (i >= v.size())
    dp[i] =  0;
  else {
    int sum = 0;
    int a, b, c = INT_MAX, d = INT_MAX;
    a = sum + v[i] + solve(v, i + 2);
    c  = sum + v[i] + solve(v, i + 3);
    if (i + 1 < v.size())
    { b = sum + v[i] + v[i + 1] + solve(v, i + 3);
      d = sum + v[i] + v[i + 1] + solve(v, i + 4);
    }
    dp[i] = min({a, b, c, d});
  }

  return dp[i];
}

int main()
{
#ifndef ONLINE_JUDGE
  freopen("ip.txt", "r", stdin);
  freopen("op.txt", "w", stdout);
#endif
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--)
  {
    int n;
    cin >> n;
    vector<int>a(n);
    for (int i = 0; i < n; i++)
      cin >> a[i];

    cout << solve(a, 0) << endl;

    for (int i = 0; i < 2 * 100000 + 2; i++)
      dp[i] = -1;

  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<int> solve(vector<int>&a, vector<int>b, vector<int>&c)
{
  if (c.size() == 0 || c.size() == 1)
    return a;
  int j = c.size() - 1;
  for (int i = 0; i < a.size(); i++)
  {
    if (b[i] == 0)
    {
      a[i] = c[j];
      j--;
    }
  }
  return a;
}
int main()
{
#ifndef ONLINE_JUDGE
  freopen("ip.txt", "r", stdin);
  freopen("op.txt", "w", stdout);
#endif
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--)
  {
    int n;
    cin >> n;
    vector<int>a(n);
    for (int i = 0; i < n; i++)
      cin >> a[i];

    int cnt = 0, p1 = 1, p2 = 0;

    if (a[0] == 1)
      cnt++;
    for (int i = 1; i < n; i++)
    {
      if (a[i] == 1)
      {
        if (p2 < 2)
        {
          p2++;
          p1 = 0;
        }
        else if (p2 == 2)
        {
          cnt++;
          p1++;
          p2 = 0;
        }
      }
      else if (a[i] == 0)
      {
        if (p1 < 2)
        {
          p1++;
          p2 = 0;
        }
        else if (p1 == 2)
        {
          p2++;
          p1 = 0;
        }
      }

    }
    cout << cnt << endl;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<int>dp(2 * 100000 + 2, -1);
int solve(vector<int>&v, int i, int n)
{
  if (dp[i] != -1)
    return dp[i];

  if (i >= n)
    dp[i] =  0;
  else {
    int sum = 0;
    int a, b=INT_MAX, c , d = INT_MAX;
    a = sum + v[i] + solve(v, i + 2);
    c  = sum + v[i] + solve(v, i + 3);
    if (i + 1 < n)
    { b = sum + v[i] + v[i + 1] + solve(v, i + 3);
      d = sum + v[i] + v[i + 1] + solve(v, i + 4);
    }
    dp[i] = min({a, b, c, d});
  }

  return dp[i];
}

int main()
{
#ifndef ONLINE_JUDGE
  freopen("ip.txt", "r", stdin);
  freopen("op.txt", "w", stdout);
#endif
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--)
  {
    int n;
    cin >> n;
    vector<int>a(n);
    for (int i = 0; i < n; i++)
      cin >> a[i];

    cout << solve(a, 0,n) << endl;

    for (int i = 0; i < 2 * 100000 + 2; i++)
      dp[i] = -1;

  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<int>dp(2 * 100000 + 2, -1);
int solve(vector<int>v, int i)
{
  if (dp[i] != -1)
    return dp[i];

  if (i >= v.size())
    dp[i] =  0;
  else {
    int sum = 0;
    int a, b, c = INT_MAX, d = INT_MAX;
    a = sum + v[i] + solve(v, i + 2);
    c  = sum + v[i] + solve(v, i + 3);
    if (i + 1 < v.size())
    { b = sum + v[i] + v[i + 1] + solve(v, i + 3);
      d = sum + v[i] + v[i + 1] + solve(v, i + 4);
    }
    dp[i] = min({a, b, c, d});
  }

  return dp[i];
}

int main()
{
#ifndef ONLINE_JUDGE
  freopen("ip.txt", "r", stdin);
  freopen("op.txt", "w", stdout);
#endif
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--)
  {
    int n;
    cin >> n;
    vector<int>a(n);
    for (int i = 0; i < n; i++)
      cin >> a[i];

    cout << solve(a, 0) << endl;

    for (int i = 0; i < 2 * 100000 + 2; i++)
      dp[i] = -1;

  }
  return 0;
}


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

int solve(vector<int>&v, int i, int n, vector<int>&dp)
{
  if (dp[i] != -1)
    return dp[i];

  if (i >= n)
    dp[i] =  0;
  else {
    int sum = 0;
    int a, b = INT_MAX, c , d = INT_MAX;
    a = sum + v[i] + solve(v, i + 2, n, dp);
    c  = sum + v[i] + solve(v, i + 3, n, dp);
    if (i + 1 < n)
    { b = sum + v[i] + v[i + 1] + solve(v, i + 3, n, dp);
      d = sum + v[i] + v[i + 1] + solve(v, i + 4, n, dp);
    }
    dp[i] = min({a, b, c, d});
  }

  return dp[i];
}

int main()
{
#ifndef ONLINE_JUDGE
  freopen("ip.txt", "r", stdin);
  freopen("op.txt", "w", stdout);
#endif
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--)
  {
    int n;
    cin >> n;
    vector<int>a(n), dp(n + 5, -1);
    for (int i = 0; i < n; i++)
      cin >> a[i];

    cout << solve(a, 0, n, dp) << endl;

  }
  return 0;
}


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

#define          FaRaBi                 ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define          ll                     long long int
#define          output                 freopen("output.txt","wt", stdout)
#define          ld                     long double
#define          pii                    pair < int, int>
#define          pll                    pair < ll, ll>
#define          MOD                    1000000007
#define          ff                     first
#define          ss                     second
#define          pb                     push_back
#define          pf                     printf
#define          mp                     make_pair
#define          gcd(a, b)              __gcd(a,b)
#define          lcm(a, b)              ((a)*(b)/gcd(a,b))
#define          PI                     acos(-1.0)
#define          zero(a)                memset(a,0,sizeof a)
#define          all(v)                 v.begin(),v.end()
#define          Max(v)                 *max_element(all(v))
#define          Min(v)                 *min_element(all(v))
#define          Upper(c,x)             (upper_bound(c.begin(),c.end(),x)-c.begin())
#define          Lower(c,x)             (lower_bound(c.begin(),c.end(),x)-c.begin())
#define          Unique(X)              (X).erase(unique(all(X)),(X).end())
#define          no                     cout << "NO" << endl ;
#define          yes                    cout << "YES" << endl ;
#define          segment_tree           int Lnode = node << 1 , Rnode = Lnode + 1 , mid = ( b + e ) >> 1 ;
#define          siz                    10005

///--------------------**********----------------------------------

vector < ll > v, v1, v2, v3, v4 ;
vector < pll > vec ;
vector < ll > adj[ 100005 ] ;
map < ll, ll > Mp, Mp1, Mp2 ;
set < ll > st, st1, st2 ;
stack < ll > Stk ;
multiset < ll > S ;
deque < ll > A ;

///---------------------**********--------------------------------

ll n, a, b, c, cnt ;
ll dp[ 200005 ][ 5 ] ;

void Reset()
{
    v.clear() ;
    for( int i = 0 ; i <= n ; i ++ )
    {
        for( int j = 0 ; j <= 2 ; j ++ )
        {
            dp[ i ][ j ] = -1 ;
        }
    }
}

void Input()
{
    cin >> n ;
    Reset() ;
    for( int i = 0 ; i < n ; i ++ )
    {
        cin >> a ;
        v.pb( a ) ;
    }
}

ll Fun( int pos, int state )
{
    if( pos >= n ) return 0 ;

    if( dp[ pos ][ state ] != -1 ) return dp[ pos ][ state ] ;

    ll res = 0 ;
    if( state == 0 )
    {
        if( v[ pos ] == 1 )
        {
            res = min( 1 + Fun( pos + 1, 0 ) , 1 + Fun( pos + 1, 1 ) ) ;
        }
        else
        {
            if( pos + 1 < n )
            {
                if( v[ pos + 1 ] == 0 ) res = Fun( pos + 2, 1 ) ;
                else res = Fun( pos + 1, 1 ) ;
            }
            else
            {
                res = min( Fun( pos + 1, 1 ), Fun( pos + 1, 0 ) ) ;
            }
        }
    }
    else
    {
        res = min( Fun( pos + 1, 0 ) , Fun( pos + 2, 0 ) ) ;
    }
    return dp[ pos ][ state ] = res ;
}

void Calculation()
{
    cout << Fun( 0, 0 ) << endl ;
}

void solve()
{
    //Reset() ;
    Input() ;
    Calculation() ;
}

int main()
{
    FaRaBi ;
    int t ;
    cin >> t ;
    //t = 1 ;
    while( t-- ) solve() ;
    return 0 ;
}


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

#define          FaRaBi                 ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define          ll                     long long int
#define          output                 freopen("output.txt","wt", stdout)
#define          ld                     long double
#define          pii                    pair < int, int>
#define          pll                    pair < ll, ll>
#define          MOD                    1000000007
#define          ff                     first
#define          ss                     second
#define          pb                     push_back
#define          pf                     printf
#define          mp                     make_pair
#define          gcd(a, b)              __gcd(a,b)
#define          lcm(a, b)              ((a)*(b)/gcd(a,b))
#define          PI                     acos(-1.0)
#define          zero(a)                memset(a,0,sizeof a)
#define          all(v)                 v.begin(),v.end()
#define          Max(v)                 *max_element(all(v))
#define          Min(v)                 *min_element(all(v))
#define          Upper(c,x)             (upper_bound(c.begin(),c.end(),x)-c.begin())
#define          Lower(c,x)             (lower_bound(c.begin(),c.end(),x)-c.begin())
#define          Unique(X)              (X).erase(unique(all(X)),(X).end())
#define          no                     cout << "NO" << endl ;
#define          yes                    cout << "YES" << endl ;
#define          segment_tree           int Lnode = node << 1 , Rnode = Lnode + 1 , mid = ( b + e ) >> 1 ;
#define          siz                    10005

///--------------------**********----------------------------------

vector < ll > v, v1, v2, v3, v4 ;
vector < pll > vec ;
vector < ll > adj[ 100005 ] ;
map < ll, ll > Mp, Mp1, Mp2 ;
set < ll > st, st1, st2 ;
stack < ll > Stk ;
multiset < ll > S ;
deque < ll > A ;

///---------------------**********--------------------------------

ll n, a, b, c, cnt ;

void Reset()
{
    v.clear() ;
}

void Input()
{
    cin >> n ;
    for( int i = 0 ; i < n ; i ++ )
    {
        cin >> a ;
        v.pb( a ) ;
    }
}

void Calculation()
{
    int ck = 0, cnt = 0 ;
    for( int i = 0 ; i < n ; i ++ )
    {
        if( ck == 0 )
        {
            if( v[ i ] == 1 )
            {
                cnt ++ ;
                if( i + 1 < n && v[ i + 1 ] == 1 )
                {
                    ck = 1 ;
                }
                else if( i + 1 < n && v[ i + 1 ] == 0 )
                {
                    if( i + 2 < n && v[ i + 2 ] == 0 )
                    {
                        i += 2 ;
                        ck = 1 ;
                    }
                    else
                    {
                        i ++ ;
                        ck = 1 ;
                    }
                }
            }
            else
            {
                if( i + 1 < n && v[ i + 1 ] == 1 )
                {
                    ck = 1 ;
                }
                else if( i + 1 < n && v[ i + 1 ] == 0 )
                {
                    i ++ ;
                    ck = 1 ;
                }
            }
        }
        else
        {
            if( v[ i ] == 0 )
            {
                cnt ++ ;
                if( i + 1 < n && v[ i + 1 ] == 0 )
                {
                    ck = 0 ;
                }
                else if( i + 1 < n && v[ i + 1 ] == 1 )
                {
                    if( i + 2 < n && v[ i + 2 ] == 1 )
                    {
                        i += 2 ;
                        ck = 0 ;
                    }
                    else
                    {
                        i ++ ;
                        ck = 0 ;
                    }
                }
            }
            else
            {
                if( i + 1 < n && v[ i + 1 ] == 0 )
                {
                    ck = 0 ;
                }
                else if( i + 1 < n && v[ i + 1 ] == 1 )
                {
                    i ++ ;
                    ck = 0 ;
                }
            }
        }
    }
    cout << cnt << endl ;
}

void solve()
{
    Reset() ;
    Input() ;
    Calculation() ;
}

int main()
{
    FaRaBi ;
    int t ;
    cin >> t ;
    //t = 1 ;
    while( t-- ) solve() ;
    return 0 ;
}


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

#define          FaRaBi                 ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define          ll                     long long int
#define          output                 freopen("output.txt","wt", stdout)
#define          ld                     long double
#define          pii                    pair < int, int>
#define          pll                    pair < ll, ll>
#define          MOD                    1000000007
#define          ff                     first
#define          ss                     second
#define          pb                     push_back
#define          pf                     printf
#define          mp                     make_pair
#define          gcd(a, b)              __gcd(a,b)
#define          lcm(a, b)              ((a)*(b)/gcd(a,b))
#define          PI                     acos(-1.0)
#define          zero(a)                memset(a,0,sizeof a)
#define          all(v)                 v.begin(),v.end()
#define          Max(v)                 *max_element(all(v))
#define          Min(v)                 *min_element(all(v))
#define          Upper(c,x)             (upper_bound(c.begin(),c.end(),x)-c.begin())
#define          Lower(c,x)             (lower_bound(c.begin(),c.end(),x)-c.begin())
#define          Unique(X)              (X).erase(unique(all(X)),(X).end())
#define          no                     cout << "NO" << endl ;
#define          yes                    cout << "YES" << endl ;
#define          segment_tree           int Lnode = node << 1 , Rnode = Lnode + 1 , mid = ( b + e ) >> 1 ;
#define          siz                    10005

///--------------------**********----------------------------------

vector < ll > v, v1, v2, v3, v4 ;
vector < pll > vec ;
vector < ll > adj[ 100005 ] ;
map < ll, ll > Mp, Mp1, Mp2 ;
set < ll > st, st1, st2 ;
stack < ll > Stk ;
multiset < ll > S ;
deque < ll > A ;

///---------------------**********--------------------------------

ll n, a, b, c, cnt ;
ll dp[ 200005 ][ 5 ] ;

void Reset()
{
    v.clear() ;
    for( int i = 0 ; i <= n ; i ++ )
    {
        for( int j = 0 ; j <= 2 ; j ++ )
        {
            dp[ i ][ j ] = -1 ;
        }
    }
}

void Input()
{
    cin >> n ;
    Reset() ;
    for( int i = 0 ; i < n ; i ++ )
    {
        cin >> a ;
        v.pb( a ) ;
    }
}

ll Fun( int pos, int state, int cnt )
{
    if( pos >= n ) return 0 ;

    if( dp[ pos ][ state ] != -1 ) return dp[ pos ][ state ] ;

    ll res = 0 ;

    if( cnt == 1 )
    {
        if( state == 0 )
        {
            if( v[ pos ] == 1 )
            {
                res = 1 + Fun( pos + 1, 1, 0 ) ;
            }
            else
            {
                res = Fun( pos + 1 , 1, 0 ) ;
            }
        }
        else
        {
            res = Fun( pos + 1, 0, 0 ) ;
        }
    }
    else
    {
        if( state == 0 )
        {
            if( v[ pos ] == 1 )
            {
                res = min( 1 + Fun( pos + 1, 0, cnt + 1 ), 1 + Fun( pos + 1, 1, 0 ) ) ;
            }
            else
            {
                if( pos + 1 < n )
                {
                    if( v[ pos + 1 ] == 0 ) res = Fun( pos + 2, 1, 0 ) ;
                    else res = Fun( pos + 1, 1, 0 ) ;
                }
                else
                {
                    res = min( Fun( pos + 1, 1, 0 ), Fun( pos + 1, 0, cnt + 1 ) ) ;
                }
            }
        }
        else
        {
            res = min( Fun( pos + 1, 0, 0 ), Fun( pos + 2, 0, 0 ) ) ;
        }
    }

    return dp[ pos ][ state ] = res ;
}

void Calculation()
{
    cout << Fun( 0, 0, 0 ) << endl ;
}

void solve()
{
    //Reset() ;
    Input() ;
    Calculation() ;
}

int main()
{
    FaRaBi ;
    int t ;
    cin >> t ;
    //t = 1 ;
    while( t-- ) solve() ;
    return 0 ;
}


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

#define          FaRaBi                 ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define          ll                     long long int
#define          output                 freopen("output.txt","wt", stdout)
#define          ld                     long double
#define          pii                    pair < int, int>
#define          pll                    pair < ll, ll>
#define          MOD                    1000000007
#define          ff                     first
#define          ss                     second
#define          pb                     push_back
#define          pf                     printf
#define          mp                     make_pair
#define          gcd(a, b)              __gcd(a,b)
#define          lcm(a, b)              ((a)*(b)/gcd(a,b))
#define          PI                     acos(-1.0)
#define          zero(a)                memset(a,0,sizeof a)
#define          all(v)                 v.begin(),v.end()
#define          Max(v)                 *max_element(all(v))
#define          Min(v)                 *min_element(all(v))
#define          Upper(c,x)             (upper_bound(c.begin(),c.end(),x)-c.begin())
#define          Lower(c,x)             (lower_bound(c.begin(),c.end(),x)-c.begin())
#define          Unique(X)              (X).erase(unique(all(X)),(X).end())
#define          no                     cout << "NO" << endl ;
#define          yes                    cout << "YES" << endl ;
#define          segment_tree           int Lnode = node << 1 , Rnode = Lnode + 1 , mid = ( b + e ) >> 1 ;
#define          siz                    10005

///--------------------**********----------------------------------

vector < ll > v, v1, v2, v3, v4 ;
vector < pll > vec ;
vector < ll > adj[ 100005 ] ;
map < ll, ll > Mp, Mp1, Mp2 ;
set < ll > st, st1, st2 ;
stack < ll > Stk ;
multiset < ll > S ;
deque < ll > A ;

///---------------------**********--------------------------------

ll n, a, b, c, cnt ;
ll dp[ 200005 ][ 5 ] ;

void Reset()
{
    v.clear() ;

}

void Input()
{
    cin >> n ;
    for( int i = 0 ; i <= n ; i ++ )
    {
        for( int j = 0 ; j <= 2 ; j ++ )
        {
            dp[ i ][ j ] = -1 ;
        }
    }
    for( int i = 0 ; i < n ; i ++ )
    {
        cin >> a ;
        v.pb( a ) ;
    }
    v.pb( 0 ) ;
    v.pb( 0 ) ;
}

ll Fun( int pos, int state )
{
    //cout << pos << " " << state << endl ;
    if( pos >= n ) return 0 ;

    if( dp[ pos ][ state ] != -1 ) return dp[ pos ][ state ] ;

    ll res = INT_MAX ;
    if( state == 0 )
    {
        if( v[ pos ] == 1 )
        {
            res = min( { res, 1 + Fun( pos + 1, 0 ), 1 + Fun( pos + 1, 1 ) } ) ;
        }
        else
        {
            if( v[ pos + 1 ] == 0 ) res = min( res, Fun( pos + 2, 1 ) ) ;
            else res = min( res, Fun( pos + 1, 1 ) ) ;
        }
    }
    else
    {
        res = min( { res, Fun( pos + 1, 0 ), Fun( pos + 2, 0 ) } ) ;
    }
    return dp[ pos ][ state ] = res ;
}

void Calculation()
{
    ll ans = Fun( 0, 0 ) ;
    cout << ans << endl ;
}

void solve()
{
    Reset() ;
    Input() ;
    Calculation() ;
}

int main()
{
    FaRaBi ;
    int t ;
    cin >> t ;
    //t = 1 ;
    while( t-- ) solve() ;
    return 0 ;
}


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

#define          FaRaBi                 ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define          ll                     long long int
#define          output                 freopen("output.txt","wt", stdout)
#define          ld                     long double
#define          pii                    pair < int, int>
#define          pll                    pair < ll, ll>
#define          MOD                    1000000007
#define          ff                     first
#define          ss                     second
#define          pb                     push_back
#define          pf                     printf
#define          mp                     make_pair
#define          gcd(a, b)              __gcd(a,b)
#define          lcm(a, b)              ((a)*(b)/gcd(a,b))
#define          PI                     acos(-1.0)
#define          zero(a)                memset(a,0,sizeof a)
#define          all(v)                 v.begin(),v.end()
#define          Max(v)                 *max_element(all(v))
#define          Min(v)                 *min_element(all(v))
#define          Upper(c,x)             (upper_bound(c.begin(),c.end(),x)-c.begin())
#define          Lower(c,x)             (lower_bound(c.begin(),c.end(),x)-c.begin())
#define          Unique(X)              (X).erase(unique(all(X)),(X).end())
#define          no                     cout << "NO" << endl ;
#define          yes                    cout << "YES" << endl ;
#define          segment_tree           int Lnode = node << 1 , Rnode = Lnode + 1 , mid = ( b + e ) >> 1 ;
#define          siz                    10005

///--------------------**********----------------------------------

vector < ll > v, v1, v2, v3, v4 ;
vector < pll > vec ;
vector < ll > adj[ 100005 ] ;
map < ll, ll > Mp, Mp1, Mp2 ;
set < ll > st, st1, st2 ;
stack < ll > Stk ;
multiset < ll > S ;
deque < ll > A ;

///---------------------**********--------------------------------

ll n, a, b, c, cnt ;
ll dp[ 200005 ][ 5 ] ;

void Reset()
{
    v.clear() ;
    for( int i = 0 ; i <= n ; i ++ )
    {
        for( int j = 0 ; j <= 2 ; j ++ )
        {
            dp[ i ][ j ] = -1 ;
        }
    }
}

void Input()
{
    cin >> n ;
    Reset() ;
    for( int i = 0 ; i < n ; i ++ )
    {
        cin >> a ;
        v.pb( a ) ;
    }
    v.pb( 0 ) ;
}

ll Fun( int pos, int state )
{
    if( pos >= n ) return 0 ;

    if( dp[ pos ][ state ] != -1 ) return dp[ pos ][ state ] ;

    ll res = 0 ;
    if( state == 0 )
    {
        res = min( v[ pos ] + Fun( pos + 1, 1 ), v[ pos ] + v[ pos + 1 ] + Fun( pos + 2, 1 ) ) ;
    }
    else
    {
        res = min( Fun( pos + 1, 0 ), Fun( pos + 2, 0 ) ) ;
    }
    return dp[ pos ][ state ] = res ;
}

void Calculation()
{
    cout << Fun( 0, 0 ) << endl ;
}

void solve()
{
    //Reset() ;
    Input() ;
    Calculation() ;
}

int main()
{
    FaRaBi ;
    int t ;
    cin >> t ;
    //t = 1 ;
    while( t-- ) solve() ;
    return 0 ;
}


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

#define          FaRaBi                 ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define          ll                     long long int
#define          output                 freopen("output.txt","wt", stdout)
#define          ld                     long double
#define          pii                    pair < int, int>
#define          pll                    pair < ll, ll>
#define          MOD                    1000000007
#define          ff                     first
#define          ss                     second
#define          pb                     push_back
#define          pf                     printf
#define          mp                     make_pair
#define          gcd(a, b)              __gcd(a,b)
#define          lcm(a, b)              ((a)*(b)/gcd(a,b))
#define          PI                     acos(-1.0)
#define          zero(a)                memset(a,0,sizeof a)
#define          all(v)                 v.begin(),v.end()
#define          Max(v)                 *max_element(all(v))
#define          Min(v)                 *min_element(all(v))
#define          Upper(c,x)             (upper_bound(c.begin(),c.end(),x)-c.begin())
#define          Lower(c,x)             (lower_bound(c.begin(),c.end(),x)-c.begin())
#define          Unique(X)              (X).erase(unique(all(X)),(X).end())
#define          no                     cout << "NO" << endl ;
#define          yes                    cout << "YES" << endl ;
#define          segment_tree           int Lnode = node << 1 , Rnode = Lnode + 1 , mid = ( b + e ) >> 1 ;
#define          siz                    10005

///--------------------**********----------------------------------

vector < ll > v, v1, v2, v3, v4 ;
vector < pll > vec ;
vector < ll > adj[ 100005 ] ;
map < ll, ll > Mp, Mp1, Mp2 ;
set < ll > st, st1, st2 ;
stack < ll > Stk ;
multiset < ll > S ;
deque < ll > A ;

///---------------------**********--------------------------------

ll n, a, b, c, cnt ;
ll dp[ 200005 ][ 5 ] ;

void Reset()
{
    v.clear() ;
    for( int i = 0 ; i <= n ; i ++ )
    {
        for( int j = 0 ; j <= 2 ; j ++ )
        {
            dp[ i ][ j ] = -1 ;
        }
    }
}

void Input()
{
    cin >> n ;
    Reset() ;
    for( int i = 0 ; i < n ; i ++ )
    {
        cin >> a ;
        v.pb( a ) ;
    }
}

ll Fun( int pos, int state )
{
    if( pos >= n ) return 0 ;

    if( dp[ pos ][ state ] != -1 ) return dp[ pos ][ state ] ;

    ll res = 0 ;
    if( state == 0 )
    {
        res = v[ pos ] + Fun( pos + 1, 1 ) ;
        if( pos + 1 < n )
        {
            res = min( res, v[ pos ] + v[ pos + 1 ] + Fun( pos + 2, 1 ) ) ;
        }
    }
    else
    {
        res = min( Fun( pos + 1, 0 ), Fun( pos + 2, 0 ) ) ;
    }
    return dp[ pos ][ state ] = res ;
}

void Calculation()
{
    cout << Fun( 0, 0 ) << endl ;
}

void solve()
{
    //Reset() ;
    Input() ;
    Calculation() ;
}

int main()
{
    FaRaBi ;
    int t ;
    cin >> t ;
    //t = 1 ;
    while( t-- ) solve() ;
    return 0 ;
}


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

#define          FaRaBi                 ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define          ll                     long long int
#define          output                 freopen("output.txt","wt", stdout)
#define          ld                     long double
#define          pii                    pair < int, int>
#define          pll                    pair < ll, ll>
#define          MOD                    1000000007
#define          ff                     first
#define          ss                     second
#define          pb                     push_back
#define          pf                     printf
#define          mp                     make_pair
#define          gcd(a, b)              __gcd(a,b)
#define          lcm(a, b)              ((a)*(b)/gcd(a,b))
#define          PI                     acos(-1.0)
#define          zero(a)                memset(a,0,sizeof a)
#define          all(v)                 v.begin(),v.end()
#define          Max(v)                 *max_element(all(v))
#define          Min(v)                 *min_element(all(v))
#define          Upper(c,x)             (upper_bound(c.begin(),c.end(),x)-c.begin())
#define          Lower(c,x)             (lower_bound(c.begin(),c.end(),x)-c.begin())
#define          Unique(X)              (X).erase(unique(all(X)),(X).end())
#define          no                     cout << "NO" << endl ;
#define          yes                    cout << "YES" << endl ;
#define          segment_tree           int Lnode = node << 1 , Rnode = Lnode + 1 , mid = ( b + e ) >> 1 ;
#define          siz                    10005

///--------------------**********----------------------------------

vector < ll > v, v1, v2, v3, v4 ;
vector < pll > vec ;
vector < ll > adj[ 100005 ] ;
map < ll, ll > Mp, Mp1, Mp2 ;
set < ll > st, st1, st2 ;
stack < ll > Stk ;
multiset < ll > S ;
deque < ll > A ;

///---------------------**********--------------------------------

ll n, a, b, c, cnt ;
ll dp[ 200005 ][ 5 ] ;

void Reset()
{
    v.clear() ;
    for( int i = 0 ; i <= n ; i ++ )
    {
        for( int j = 0 ; j <= 2 ; j ++ )
        {
            dp[ i ][ j ] = -1 ;
        }
    }
}

void Input()
{
    cin >> n ;
    Reset() ;
    for( int i = 0 ; i < n ; i ++ )
    {
        cin >> a ;
        v.pb( a ) ;
    }
}

ll Fun( int pos, int state, int cnt )
{
    if( pos >= n ) return 0 ;

    if( dp[ pos ][ state ] != -1 ) return dp[ pos ][ state ] ;

    ll res = 0 ;
    if( state == 0 )
    {
        if( cnt == 2 )
        {
            res = Fun( pos + 1, 1, 0 ) ;
        }
        else
        {
            if( v[ pos ] == 1 )
            {
                res = min( 1 + Fun( pos + 1, 0, cnt + 1 ), 1 + Fun( pos + 1, 1, 0 ) ) ;
            }
            else
            {
                if( pos + 1 < n )
                {
                    if( v[ pos + 1 ] == 0 ) res = Fun( pos + 2, 1, 0 ) ;
                    else res = Fun( pos + 1, 1, 0 ) ;
                }
                else
                {
                    res = min( Fun( pos + 1, 1, 0 ), Fun( pos + 1, 0, cnt + 1 ) ) ;
                }
            }
        }

    }
    else
    {
        res = min( Fun( pos + 1, 0, 0 ) , Fun( pos + 2, 0, 0 ) ) ;
    }
    return dp[ pos ][ state ] = res ;
}

void Calculation()
{
    cout << Fun( 0, 0, 0 ) << endl ;
}

void solve()
{
    //Reset() ;
    Input() ;
    Calculation() ;
}

int main()
{
    FaRaBi ;
    int t ;
    cin >> t ;
    //t = 1 ;
    while( t-- ) solve() ;
    return 0 ;
}


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

#define          FaRaBi                 ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define          ll                     long long int
#define          output                 freopen("output.txt","wt", stdout)
#define          ld                     long double
#define          pii                    pair < int, int>
#define          pll                    pair < ll, ll>
#define          MOD                    1000000007
#define          ff                     first
#define          ss                     second
#define          pb                     push_back
#define          pf                     printf
#define          mp                     make_pair
#define          gcd(a, b)              __gcd(a,b)
#define          lcm(a, b)              ((a)*(b)/gcd(a,b))
#define          PI                     acos(-1.0)
#define          zero(a)                memset(a,0,sizeof a)
#define          all(v)                 v.begin(),v.end()
#define          Max(v)                 *max_element(all(v))
#define          Min(v)                 *min_element(all(v))
#define          Upper(c,x)             (upper_bound(c.begin(),c.end(),x)-c.begin())
#define          Lower(c,x)             (lower_bound(c.begin(),c.end(),x)-c.begin())
#define          Unique(X)              (X).erase(unique(all(X)),(X).end())
#define          no                     cout << "NO" << endl ;
#define          yes                    cout << "YES" << endl ;
#define          segment_tree           int Lnode = node << 1 , Rnode = Lnode + 1 , mid = ( b + e ) >> 1 ;
#define          siz                    10005

///--------------------**********----------------------------------

vector < ll > v, v1, v2, v3, v4 ;
vector < pll > vec ;
vector < ll > adj[ 100005 ] ;
map < ll, ll > Mp, Mp1, Mp2 ;
set < ll > st, st1, st2 ;
stack < ll > Stk ;
multiset < ll > S ;
deque < ll > A ;

///---------------------**********--------------------------------

ll n, a, b, c, cnt ;

void Reset()
{
    v.clear() ;
}

void Input()
{
    cin >> n ;
    for( int i = 0 ; i < n ; i ++ )
    {
        cin >> a ;
        v.pb( a ) ;
    }
}

void Calculation()
{
    int ck = 0, cnt = 0 ;
    for( int i = 0 ; i < n ; i ++ )
    {
        if( ck == 0 )
        {
            if( v[ i ] == 1 )
            {
                cnt ++ ;
                if( i + 1 < n && v[ i + 1 ] == 1 )
                {
                    ck = 1 ;
                }
                else if( i + 2 < n && v[ i + 2 ] == 0 )
                {
                    i += 2 ;
                    ck = 1 ;
                }
                else if( i + 1 < n && v[ i + 1 ] == 0 )
                {
                    i ++ ;
                    ck = 1 ;
                }

            }
            else
            {
                if( i + 1 < n && v[ i + 1 ] == 1 )
                {
                    ck = 1 ;
                }
                else if( i + 1 < n && v[ i + 1 ] == 0 )
                {
                    i ++ ;
                    ck = 1 ;
                }
            }
        }
        else
        {
            if( v[ i ] == 0 )
            {
                cnt ++ ;
                if( i + 1 < n && v[ i + 1 ] == 0 )
                {
                    ck = 0 ;
                }
                else if( i + 2 < n && v[ i + 2 ] == 1 )
                {
                    i += 2 ;
                    ck = 0 ;
                }
                else if( i + 1 < n && v[ i + 1 ] == 1 )
                {
                    i ++ ;
                    ck = 0 ;
                }
            }
            else
            {
                if( i + 1 < n && v[ i + 1 ] == 0 )
                {
                    ck = 0 ;
                }
                else if( i + 1 < n && v[ i + 1 ] == 1 )
                {
                    i ++ ;
                    ck = 0 ;
                }
            }
        }
    }
    cout << cnt << endl ;
}

void solve()
{
    Reset() ;
    Input() ;
    Calculation() ;
}

int main()
{
    FaRaBi ;
    int t ;
    cin >> t ;
    //t = 1 ;
    while( t-- ) solve() ;
    return 0 ;
}


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

#define          FaRaBi                 ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define          ll                     long long int
#define          output                 freopen("output.txt","wt", stdout)
#define          ld                     long double
#define          pii                    pair < int, int>
#define          pll                    pair < ll, ll>
#define          MOD                    1000000007
#define          ff                     first
#define          ss                     second
#define          pb                     push_back
#define          pf                     printf
#define          mp                     make_pair
#define          gcd(a, b)              __gcd(a,b)
#define          lcm(a, b)              ((a)*(b)/gcd(a,b))
#define          PI                     acos(-1.0)
#define          zero(a)                memset(a,0,sizeof a)
#define          all(v)                 v.begin(),v.end()
#define          Max(v)                 *max_element(all(v))
#define          Min(v)                 *min_element(all(v))
#define          Upper(c,x)             (upper_bound(c.begin(),c.end(),x)-c.begin())
#define          Lower(c,x)             (lower_bound(c.begin(),c.end(),x)-c.begin())
#define          Unique(X)              (X).erase(unique(all(X)),(X).end())
#define          no                     cout << "NO" << endl ;
#define          yes                    cout << "YES" << endl ;
#define          segment_tree           int Lnode = node << 1 , Rnode = Lnode + 1 , mid = ( b + e ) >> 1 ;
#define          siz                    10005

///--------------------**********----------------------------------

vector < ll > v, v1, v2, v3, v4 ;
vector < pll > vec ;
vector < ll > adj[ 100005 ] ;
map < ll, ll > Mp, Mp1, Mp2 ;
set < ll > st, st1, st2 ;
stack < ll > Stk ;
multiset < ll > S ;
deque < ll > A ;

///---------------------**********--------------------------------

ll n, a, b, c, cnt ;
ll dp[ 200005 ][ 5 ] ;

void Reset()
{
    v.clear() ;
    for( int i = 0 ; i <= n ; i ++ )
    {
        for( int j = 0 ; j <= 2 ; j ++ )
        {
            dp[ i ][ j ] = -1 ;
        }
    }
}

void Input()
{
    cin >> n ;
    Reset() ;
    for( int i = 0 ; i < n ; i ++ )
    {
        cin >> a ;
        v.pb( a ) ;
    }
}

ll Fun( int pos, int state, int cnt )
{
    if( pos >= n ) return 0 ;

    if( dp[ pos ][ state ] != -1 ) return dp[ pos ][ state ] ;

    ll res = 0 ;

    if( cnt == 2 )
    {
        if( state == 0 )
        {
            if( v[ pos ] == 1 )
            {
                res = 1 + Fun( pos + 1, 1, 0 ) ;
            }
            else
            {
                res = Fun( pos + 1 , 1, 0 ) ;
            }
        }
        else
        {
            res = Fun( pos + 1, 0, 0 ) ;
        }
    }
    else
    {
        if( state == 0 )
        {
            if( v[ pos ] == 1 )
            {
                res = min( 1 + Fun( pos + 1, 0, cnt + 1 ), 1 + Fun( pos + 1, 1, 0 ) ) ;
            }
            else
            {
                if( pos + 1 < n )
                {
                    if( v[ pos + 1 ] == 0 ) res = Fun( pos + 2, 1, 0 ) ;
                    else res = Fun( pos + 1, 1, 0 ) ;
                }
                else
                {
                    res = min( Fun( pos + 1, 1, 0 ), Fun( pos + 1, 0, cnt + 1 ) ) ;
                }
            }
        }
        else
        {
            res = min( Fun( pos + 1, 0, 0 ), Fun( pos + 2, 0, 0 ) ) ;
        }
    }

    return dp[ pos ][ state ] = res ;
}

void Calculation()
{
    cout << Fun( 0, 0, 0 ) << endl ;
}

void solve()
{
    //Reset() ;
    Input() ;
    Calculation() ;
}

int main()
{
    FaRaBi ;
    int t ;
    cin >> t ;
    //t = 1 ;
    while( t-- ) solve() ;
    return 0 ;
}


/*
   Username : moondancer
   Name : Moon Dancer
*/

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

#define int long long

signed main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int a[n];
        for (int i = 0; i < n; ++i)
        {
            cin>>a[i];
        }
        int ans=a[0];
        int s=0;
        for(int i=1; i<n; ++i){
            if(a[i])
                s++;
            else{
                ans+=(s/3);
                s=0;
            }
        }
        ans+=(s/3);
        cout<<ans<<"\n";
    }
    return 0;
}


/*
   Username : moondancer
   Name : Moon Dancer
*/

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

#define int long long

signed main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int a[n];
        for (int i = 0; i < n; ++i)
        {
            cin>>a[i];
        }
        int dp[n][2];
        dp[0][0]=a[0];
        dp[1][0]=a[0]+a[1];
        dp[0][1]=1e18;
        dp[1][1]=dp[0][0];
        for(int i=2; i<n; ++i){
            dp[i][0]=min(dp[i-1][1] + a[i], dp[i-2][1] + a[i-1] + a[i]);
            dp[i][1]=min(dp[i-1][0], dp[i-2][0]);
        }
        int ans=min(dp[n-1][0], dp[n-1][1]);
        cout<<ans<<"\n";
    }
    return 0;
}


/*
   Username : moondancer
   Name : Moon Dancer
*/

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

#define int long long

signed main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int a[n];
        for (int i = 0; i < n; ++i)
        {
            cin>>a[i];
        }
        if(n==1 || n==2){
            cout<<a[0]<<"\n";
            continue;
        }
        bool f=1;
        int ans=0;
        for (int i = 0; i < n; ++i)
        {
            if(f){
                if(i+1 < n){
                    if(a[i] == 1 && a[i+1] == 0){
                        ans++;
                        i++;
                    }
                    else if(a[i])
                        ans++;
                }
                else{
                    if(a[i] == 1)
                        ans++;
                }
            }
            else{
                if(i+1 < n){
                    if(a[i+1]){
                        i++;
                    }
                }
                else{
                    ;
                }
            }
            f^=1;
        }
        cout<<ans<<"\n";
    }
    return 0;
}


/*
   Username : moondancer
   Name : Moon Dancer
*/

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

#define int long long

signed main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int a[n];
        for (int i = 0; i < n; ++i)
        {
            cin>>a[i];
        }
        int ans=0;
        if(a[0]) ans++;
        for (int i = 1; i < n-2; ++i)
        {
            if(a[i] && a[i+1] && a[i+2]){
                ans++;
                i+=2;
            }
        }
        cout<<ans<<"\n";
    }
    return 0;
}


/*
   Username : moondancer
   Name : Moon Dancer
*/

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

#define int long long

signed main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int a[n];
        for (int i = 0; i < n; ++i)
        {
            cin>>a[i];
        }
        if(n==1 || n==2){
            cout<<a[0]<<"\n";
            continue;
        }
        bool f=1;
        int ans=0;
        for (int i = 0; i < n; ++i)
        {
            if(f){
                if(i+1 < n){
                    if(a[i] == 1 && a[i+1] == 0){
                        ans++;
                        i++;
                    }
                    else if(a[i])
                        ans++;
                    else if(a[i] == 0 && a[i+1] == 0)
                        i++;
                }
                else{
                    if(a[i] == 1)
                        ans++;
                }
            }
            else{
                if(i+1 < n){
                    if(a[i+1]){
                        i++;
                    }
                }
                else{
                    ;
                }
            }
            f^=1;
        }
        cout<<ans<<"\n";
    }
    return 0;
}


/*
   Username : moondancer
   Name : Moon Dancer
*/

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

#define int long long

signed main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int a[n];
        for (int i = 0; i < n; ++i)
        {
            cin>>a[i];
        }
        bool f=1;
        int ans=0;
        for (int i = 0; i < n; ++i)
        {
            if(f){
                if(i+1 < n){
                    if(a[i] == 1 && a[i+1] == 0){
                        ans++;
                        i++;
                    }
                    else if(a[i])
                        ans++;
                }
                else{
                    if(a[i] == 1)
                        ans++;
                }
            }
            else{
                if(i+1 < n){
                    if(a[i+1]){
                        i++;
                    }
                }
                else{
                    ;
                }
            }
            f^=1;
        }
        cout<<ans<<"\n";
    }
    return 0;
}


/*
   Username : moondancer
   Name : Moon Dancer
*/

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

#define int long long

int dp[200001][2], a[200001];
int n;

int solve(int i, int j){
    if(i < 0){
        return (int)(1e18);
    }
    if(dp[i][j] != -1){
        return dp[i][j];
    }
    if(j == 0)
        dp[i][j] = min( solve(i-1,1) + a[i], solve(i-2, 1) + a[i-1] + a[i]);
    else
        dp[i][j] = min( solve(i-1, 0), solve(i-2, 0));
    return dp[i][j];
}

signed main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int t;
    cin>>t;
    while(t--){
        cin>>n;
        for (int i = 0; i < n; ++i)
        {
            cin>>a[i];
        }
        for(int i=0; i<n; ++i){
            dp[i][0]=dp[i][1]=-1;
        }
        dp[0][0]=a[0];
        dp[0][1]=1e18;
        dp[1][0]=a[0]+a[1];
        dp[1][1]=dp[0][0];
        solve(n-1,0);
        solve(n-1,1);
        cout<<min(dp[n-1][0], dp[n-1][1])<<"\n";
    }
    return 0;
}


#include <iostream>
#include <cmath>
#include<bits/stdc++.h>

using namespace std;
#define int long long

void solve()
{
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int a[n];
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        int cnt = 0,nSkip=0;
        for(int i=0;i<n;i++){
            if(cnt%2==0){
                if(a[i]==1){
                    nSkip++;
                    if(i!=n-1&&a[i+1]==1) cnt++;
                    else{
                        i++;
                        cnt++;
                    }
                }
                else{
                        if(i!=n-1&&a[i+1]==1) cnt++;
                        else{
                            cnt++;
                            i++;
                        }

            }
            else{
                if(a[i]==1){
                    if(i!=n-1&&a[i+1]==0)
                    cnt++;
                    else{
                        i++;
                        cnt++;
                    }

                }
                else{
                    cnt++;
                    i++;
                }

            }
        }
        cout<<nSkip<<endl;
    }
}

int32_t main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    solve();
    return 0;
}


#include <iostream>
#include <cmath>
#include<bits/stdc++.h>

using namespace std;
#define int long long

void solve()
{
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int a[n];
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        int cnt = 0,nSkip=0;
        for(int i=0;i<n;i++){
            if(cnt%2==0){
                if(a[i]==1){
                    nSkip++;
                    if(i!=n-1&&a[i+1]==1) cnt++;
                    else{
                        i++;
                        cnt++;
                    }
                }
            }
            else{
                cnt++;
                i++;
            }
        }
        cout<<nSkip<<endl;
    }
}

int32_t main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    solve();
    return 0;
}


#include <iostream>
#include <cmath>
#include<bits/stdc++.h>

using namespace std;
#define int long long

void solve()
{
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int a[n];
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        int cnt = 0,nSkip=0;
        for(int i=0;i<n;i++){
            if(cnt%2==0){
                if(a[i]==1){
                    nSkip++;
                    if(i!=n-1&&a[i+1]==1) cnt++;
                    else{
                        i++;
                        cnt++;
                    }
                }
            }
            else{
                if(a[i]==1&&a[i+1]==0){
                    cnt++;
                }
                else{
                    cnt++;
                    i++;
                }

            }
        }
        cout<<nSkip<<endl;
    }
}

int32_t main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    solve();
    return 0;
}


#include <iostream>
#include <cmath>
#include<bits/stdc++.h>

using namespace std;
#define int long long

void solve()
{
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int a[n];
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        int cnt = 0,nSkip=0;
        for(int i=0;i<n;i++){
            if(cnt%2==0){
                if(a[i]==1){
                    nSkip++;
                    if(i!=n-1&&a[i+1]==1) cnt++;
                    else{
                        i++;
                        cnt++;
                    }
                }
            }
            else{
                if(a[i]==1){
                    if(i!=n-1&&a[i+1]==0)
                    cnt++;
                    else{
                        i++;
                        cnt++;
                    }

                }
                else{
                    cnt++;
                    i++;
                }

            }
        }
        cout<<nSkip<<endl;
    }
}

int32_t main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    solve();
    return 0;
}


#include <iostream>
#include <cmath>
#include<bits/stdc++.h>

using namespace std;
#define int long long

void solve()
{
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int a[n];
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        int cnt = 0,nSkip=0;
        for(int i=0;i<n;i++){
            if(cnt%2==0){
                if(a[i]==1){
                    nSkip++;
                    if(i!=n-1&&a[i+1]==1) cnt++;
                    else{
                        i++;
                        cnt++;
                    }
                }
            }
            else{
                if(i!=n-1&&a[i]==1&&a[i+1]==0){
                    cnt++;
                }
                else{
                    cnt++;
                    i++;
                }

            }
        }
        cout<<nSkip<<endl;
    }
}

int32_t main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    solve();
    return 0;
}


#include <iostream>
#include <cmath>
#include<bits/stdc++.h>

using namespace std;
#define int long long

void solve()
{
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int a[n];
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        int cnt = 0,nSkip=0;
        for(int i=0;i<n;i++){
            if(cnt%2==0){
                if(a[i]==1){
                    nSkip++;
                    if(a[i+1]==1) cnt++;
                    else{
                        i++;
                        cnt++;
                    }
                }
            }
            else{
                cnt++;
                i++;
            }
        }
        cout<<nSkip<<endl;
    }
}

int32_t main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    solve();
    return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<bitset>
using namespace std;
const int MAXN=151;
const int INF=2000000000;
struct Edge
{
	int x,y,d;
	friend bool operator<(const Edge &p,const Edge &q)
	{
		return p.d<q.d;
	}
}E[MAXN];
int n,m,pre,Ans=INF;
int head[MAXN],to[MAXN*2+5],next[MAXN*2+5],cnt=1;
inline void adde(int f,int t)
{
	cnt++,to[cnt]=t,next[cnt]=head[f],head[f]=cnt;
}
int dis[MAXN],q[MAXN],l,r;
void BFS(int S)
{
	q[l=r=1]=S,dis[S]=0;
	while(l<=r)
	{
		int x=q[l++];
		for(int i=head[x];i;i=next[i])
			if(dis[to[i]]==INF) dis[to[i]]=dis[x]+1,q[++r]=to[i];
	}
}
struct Matrix
{
	int r,c;
	bitset<MAXN> a[MAXN];
	Matrix(){r=c=0;for(int i=1;i<=150;i++) a[i].reset();}
	Matrix(int n)
	{
		r=c=n;
		for(int i=1;i<=n;i++)
			a[i].reset(),a[i][i]=1;
	}
}ans,A;
Matrix mul(const Matrix &p,const Matrix &q)
{
	Matrix res;
	res.r=p.r,res.c=q.c;
	for(int i=1;i<=p.r;i++)
		for(int j=1;j<=p.c;j++)
			if(p.a[i][j]) res.a[i]|=q.a[j];
	return res;
}
Matrix qpow(const Matrix &x,int n)
{
	Matrix p=x;
	Matrix con(::n);
	while(n)
	{
		if(n&1) con=mul(con,p);
		p=mul(p,p);
		n>>=1;
	}
	return con;
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++) scanf("%d%d%d",&E[i].x,&E[i].y,&E[i].d);
	sort(E+1,E+m+1);
	if(E[1].d!=0) {puts("Impossible");return 0;}
	A.r=A.c=n;
	ans.r=1,ans.c=n;
	ans.a[1][1]=1;
	for(int i=1;i<=m;i++)
	{
		ans=mul(ans,qpow(A,E[i].d-pre));
		for(int j=1;j<=n;j++) dis[j]=INF;
		BFS(n);
		for(int j=1;j<=n;j++) if(ans.a[1][j]&&dis[j]!=INF) Ans=min(Ans,pre+dis[j]);
		A.a[E[i].x][E[i].y]=1;
		adde(E[i].y,E[i].x);
		pre=E[i].d;
	}
	for(int i=1;i<=n;i++) dis[i]=INF;
	BFS(n);
	for(int i=1;i<=n;i++)
		if(ans.a[1][i]&&dis[i]!=INF) Ans=min(Ans,pre+dis[i]);
	if(Ans==INF) puts("Impossible");
	else printf("%d\n",Ans);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<bitset>
using namespace std;
const int MAXN=151;
const int INF=2000000000;
struct Edge
{
	int x,y,d;
	friend bool operator<(const Edge &p,const Edge &q)
	{
		return p.d<q.d;
	}
}E[MAXN];
int n,m,pre,Ans=INF;
int head[MAXN],to[MAXN*2+5],next[MAXN*2+5],cnt=1;
inline void adde(int f,int t)
{
	cnt++,to[cnt]=t,next[cnt]=head[f],head[f]=cnt;
}
int dis[MAXN],q[MAXN],l,r;
void BFS(int S)
{
	q[l=r=1]=S,dis[S]=0;
	while(l<=r)
	{
		int x=q[l++];
		for(int i=head[x];i;i=next[i])
			if(dis[to[i]]==INF) dis[to[i]]=dis[x]+1,q[++r]=to[i];
	}
}
struct Matrix
{
	int r,c;
	bitset<MAXN> a[MAXN];
	Matrix(){r=c=0;for(int i=1;i<=150;i++) a[i].reset();}
	Matrix(int n)
	{
		r=c=n;
		for(int i=1;i<=n;i++)
			a[i].reset(),a[i][i]=1;
	}
}ans,A;
Matrix mul(const Matrix &p,const Matrix &q)
{
	Matrix res;
	res.r=p.r,res.c=q.c;
	for(int i=1;i<=p.r;i++)
		for(int j=1;j<=p.c;j++)
			if(p.a[i][j]) res.a[j]|=q.a[i];
	return res;
}
Matrix qpow(const Matrix &x,int n)
{
	Matrix p=x;
	Matrix con(::n);
	while(n)
	{
		if(n&1) con=mul(con,p);
		p=mul(p,p);
		n>>=1;
	}
	return con;
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++) scanf("%d%d%d",&E[i].x,&E[i].y,&E[i].d);
	sort(E+1,E+m+1);
	if(E[1].d!=0) puts("Impossible");
	A.r=A.c=n;
	ans.r=1,ans.c=n;
	ans.a[1][1]=1;
	for(int i=1;i<=m;i++)
	{
		ans=mul(ans,qpow(A,E[i].d-pre));
		for(int j=1;j<=n;j++) dis[j]=INF;
		BFS(n);
		for(int j=1;j<=n;j++)
			if(ans.a[1][j]&&dis[j]!=INF) Ans=min(Ans,pre+dis[j]);
		A.a[E[i].x][E[i].y]=1;
		adde(E[i].y,E[i].x);
		pre=E[i].d;
	}
	BFS(n);
	for(int i=1;i<=n;i++)
		if(ans.a[1][i]&&dis[i]!=INF) Ans=min(Ans,pre+dis[i]);
	if(Ans==INF) puts("Impossible");
	else printf("%d\n",Ans);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<bitset>
using namespace std;
const int MAXN=151;
const int INF=2000000000;
struct Edge
{
	int x,y,d;
	friend bool operator<(const Edge &p,const Edge &q)
	{
		return p.d<q.d;
	}
}E[MAXN];
int n,m,pre,Ans=INF;
int head[MAXN],to[MAXN*2+5],next[MAXN*2+5],cnt=1;
inline void adde(int f,int t)
{
	cnt++,to[cnt]=t,next[cnt]=head[f],head[f]=cnt;
}
int dis[MAXN],q[MAXN],l,r;
void BFS(int S)
{
	q[l=r=1]=S,dis[S]=0;
	while(l<=r)
	{
		int x=q[l++];
		for(int i=head[x];i;i=next[i])
			if(dis[to[i]]==INF) dis[to[i]]=dis[x]+1,q[++r]=to[i];
	}
}
struct Matrix
{
	int r,c;
	bitset<MAXN> a[MAXN];
	Matrix(){r=c=0;for(int i=1;i<=150;i++) a[i].reset();}
	Matrix(int n)
	{
		r=c=n;
		for(int i=1;i<=n;i++)
			a[i].reset(),a[i][i]=1;
	}
}ans,A;
Matrix mul(const Matrix &p,const Matrix &q)
{
	Matrix res;
	res.r=p.r,res.c=q.c;
	for(int i=1;i<=p.r;i++)
		for(int j=1;j<=p.c;j++)
			if(p.a[i][j]) res.a[j]|=q.a[i];
	return res;
}
Matrix qpow(const Matrix &x,int n)
{
	Matrix p=x;
	Matrix con(::n);
	while(n)
	{
		if(n&1) con=mul(con,p);
		p=mul(p,p);
		n>>=1;
	}
	return con;
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++) scanf("%d%d%d",&E[i].x,&E[i].y,&E[i].d);
	sort(E+1,E+m+1);
	if(E[1].d!=0) {puts("Impossible");return 0;}
	A.r=A.c=n;
	ans.r=1,ans.c=n;
	ans.a[1][1]=1;
	for(int i=1;i<=m;i++)
	{
		ans=mul(ans,qpow(A,E[i].d-pre));
		for(int j=1;j<=n;j++) dis[j]=INF;
		BFS(n);
		for(int j=1;j<=n;j++)
			if(ans.a[1][j]&&dis[j]!=INF) Ans=min(Ans,pre+dis[j]);
		A.a[E[i].x][E[i].y]=1;
		adde(E[i].y,E[i].x);
		pre=E[i].d;
	}
	BFS(n);
	for(int i=1;i<=n;i++)
		if(ans.a[1][i]&&dis[i]!=INF) Ans=min(Ans,pre+dis[i]);
	if(Ans==INF) puts("Impossible");
	else printf("%d\n",Ans);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<bitset>
using namespace std;
const int MAXN=155;
const int INF=2000000000;
struct Edge
{
	int x,y,d;
	friend bool operator<(const Edge &p,const Edge &q)
	{
		return p.d<q.d;
	}
}E[MAXN];
int n,m,pre,Ans=INF;
int head[MAXN],to[MAXN*2+5],next[MAXN*2+5],cnt=1;
inline void adde(int f,int t)
{
	cnt++,to[cnt]=t,next[cnt]=head[f],head[f]=cnt;
}
int dis[MAXN],q[MAXN],l,r;
void BFS(int S)
{
	q[l=r=1]=S,dis[S]=0;
	while(l<=r)
	{
		int x=q[l++];
		for(int i=head[x];i;i=next[i])
			if(dis[to[i]]==INF) dis[to[i]]=dis[x]+1,q[++r]=to[i];
	}
}
struct Matrix
{
	int r,c;
	bitset<MAXN> a[MAXN];
	Matrix(){r=c=0;for(int i=1;i<=150;i++) a[i].reset();}
	Matrix(int n)
	{
		r=c=n;
		for(int i=1;i<=n;i++)
			a[i].reset(),a[i][i]=1;
	}
}ans,A;
Matrix mul(const Matrix &p,const Matrix &q)
{
	Matrix res;
	res.r=p.r,res.c=q.c;
	for(int i=1;i<=p.r;i++)
		for(int j=1;j<=p.c;j++)
			if(p.a[i][j]) res.a[i]|=q.a[j];
	return res;
}
Matrix qpow(const Matrix &x,int n)
{
	Matrix p=x;
	Matrix con(::n);
	while(n)
	{
		if(n&1) con=mul(con,p);
		p=mul(p,p);
		n>>=1;
	}
	return con;
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++) scanf("%d%d%d",&E[i].x,&E[i].y,&E[i].d);
	sort(E+1,E+m+1);
	if(E[1].d!=0) {puts("Impossible");return 0;}
	A.r=A.c=n;
	ans.r=1,ans.c=n;
	ans.a[1][1]=1;
	int L=1;
	while(L<=m)
	{
		int R=L;
		while(R+1<=m&&E[R+1].d==E[L].d) R++;
		ans=mul(ans,qpow(A,E[L].d-pre));
		for(int i=L;i<=R;i++)
		{
			A.a[E[i].x][E[i].y]=1;
			adde(E[i].y,E[i].x);
		}
		for(int j=1;j<=n;j++) dis[j]=INF;
		BFS(n);
		pre=E[L].d;
		for(int j=1;j<=n;j++) if(ans.a[1][j]&&dis[j]!=INF) Ans=min(Ans,pre+dis[j]);
		L=R+1;
	}
	for(int i=1;i<=n;i++) dis[i]=INF;
	BFS(n);
	for(int i=1;i<=n;i++)
		if(ans.a[1][i]&&dis[i]!=INF) Ans=min(Ans,pre+dis[i]);
	if(Ans==INF) puts("Impossible");
	else printf("%d\n",Ans);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<bitset>
using namespace std;
const int MAXN=151;
const int INF=2000000000;
struct Edge
{
	int x,y,d;
	friend bool operator<(const Edge &p,const Edge &q)
	{
		return p.d<q.d;
	}
}E[MAXN];
int n,m,pre,Ans=INF;
int head[MAXN],to[MAXN*2+5],next[MAXN*2+5],cnt=1;
inline void adde(int f,int t)
{
	cnt++,to[cnt]=t,next[cnt]=head[f],head[f]=cnt;
}
int dis[MAXN],q[MAXN],l,r;
void BFS(int S)
{
	q[l=r=1]=S,dis[S]=0;
	while(l<=r)
	{
		int x=q[l++];
		for(int i=head[x];i;i=next[i])
			if(dis[to[i]]==INF) dis[to[i]]=dis[x]+1,q[++r]=to[i];
	}
}
struct Matrix
{
	int r,c;
	bitset<MAXN> a[MAXN];
	Matrix(){r=c=0;for(int i=1;i<=150;i++) a[i].reset();}
	Matrix(int n)
	{
		r=c=n;
		for(int i=1;i<=n;i++)
			a[i].reset(),a[i][i]=1;
	}
}ans,A;
Matrix mul(const Matrix &p,const Matrix &q)
{
	Matrix res;
	res.r=p.r,res.c=q.c;
	for(int i=1;i<=p.r;i++)
		for(int j=1;j<=p.c;j++)
			if(p.a[i][j]) res.a[i]|=q.a[j];
	return res;
}
Matrix qpow(const Matrix &x,int n)
{
	Matrix p=x;
	Matrix con(::n);
	while(n)
	{
		if(n&1) con=mul(con,p);
		p=mul(p,p);
		n>>=1;
	}
	return con;
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++) scanf("%d%d%d",&E[i].x,&E[i].y,&E[i].d);
	sort(E+1,E+m+1);
	if(E[1].d!=0) {puts("Impossible");return 0;}
	A.r=A.c=n;
	ans.r=1,ans.c=n;
	ans.a[1][1]=1;
	for(int i=1;i<=m;i++)
	{
		ans=mul(ans,qpow(A,E[i].d-pre));
		for(int j=1;j<=n;j++) dis[j]=INF;
		BFS(n);
		for(int j=1;j<=n;j++) if(ans.a[1][j]&&dis[j]!=INF) Ans=min(Ans,pre+dis[j]);
		A.a[E[i].x][E[i].y]=1;
		adde(E[i].y,E[i].x);
		pre=E[i].d;
	}
	BFS(n);
	for(int i=1;i<=n;i++)
		if(ans.a[1][i]&&dis[i]!=INF) Ans=min(Ans,pre+dis[i]);
	if(Ans==INF) puts("Impossible");
	else printf("%d\n",Ans);
	return 0;
}


#include <bits/stdc++.h>
//Damned if I don't take what I want

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

typedef pair<int, pii> pip;
#define D(x) (x.first)
#define S(x) (x.second.first)
#define T(x) (x.second.second)

const int MAXN = 152;
//const int MAXN = 3;
const ll MAXT = 1e9;
typedef bitset<MAXN> BIT;
typedef BIT mat[MAXN];

pip edg[MAXN];

mat temp, adj, pw, ans, multemp;
mat ansrec[MAXN]; ll tof[MAXN];

int n, m;

inline void cp(mat a, mat b){
	for (int i = 0; i < n; i++)
		a[i] = b[i];
}

void mul(mat c, mat a, mat b){
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			multemp[i][j] = b[j][i];

	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			c[i][j] = (a[i] & multemp[j]).any();
}

void applypw(int p){
	cp(pw, adj);
	for (; p; p >>= 1){
		if (p & 1){
			cp(temp, ans);
			mul(ans, temp, pw);
		}

		cp(temp, pw);
		mul(pw, temp, temp);
	}
}

bool getrec(){
	ll x = 2 * MAXT;

	for (int i = 0; i < n; i++) adj[i].reset(), ans[i].reset(), ans[i].set(i);
	cp(ansrec[0], ans);

	int r;
	for (int i = 0; i < m; i = r){
		if (D(edg[i]) > x) break;

		r = i;
		while(r < m && D(edg[r]) == D(edg[i])){
			adj[ S(edg[r]) ].set( T(edg[r]) );
			r++;
		}

		int t = min( ll(r == m ? n : D(edg[r]) - D(edg[i]) ), x - D(edg[i]) );

		applypw(t);
		cp(ansrec[r], ans);
	}
}

bool ok(ll x){
	for (int i = 0; i < n; i++) adj[i].reset(), ans[i].reset(), ans[i].set(i);

	int r;
	for (int i = 0; i < m; i = r){
		if (D(edg[i]) > x) break;

		r = i;
		while(r < m && D(edg[r]) == D(edg[i])){
			adj[ S(edg[r]) ].set( T(edg[r]) );
			r++;
		}

		int t = min( ll(r == m ? n : D(edg[r]) - D(edg[i]) ), x - D(edg[i]) );
		int next = (r == m ? MAXT * 2 : D(edg[r]) );

		if (x > next) continue;
		cp(ans, ansrec[i]);

		/*cerr << "after applying: " << t << endl;
		for (int i = 0; i < n; i++)
			cerr << adj[i] << endl;
		cerr << "to" << endl;
		for (int i = 0; i < n; i++)
			cerr << ans[i] << endl;*/
		applypw(t);

		/*cerr << "we get " << endl;
		for (int i = 0; i < n; i++)
			cerr << ans[i] << endl;
		cerr << endl;*/
	}

	return ans[0][n-1];
}

int main(){
	scanf("%d%d", &n, &m);
	for (int i = 0; i < m; i++){
		pip &e = edg[i];

		scanf("%d%d%d", &S(e), &T(e), &D(e) );
		S(e)--; T(e)--;
		//cerr << "edge : " << S(e) << ", " << T(e) << endl;
	}

	edg[ m++ ] = pip(0, pii(n - 1, n - 1) );
	sort(edg, edg + m);
	getrec();

 	ll l = 0, r = 2 * MAXT;
	//ll l = 0, r = 2;

	while(l + 1 < r){
		ll mid = (l + r) / 2;
		if (ok(mid)) r = mid;
		else l = mid;
	}

	if (r == 2 * MAXT) cout << "Impossible\n";
	else cout << r << "\n";

}


#include <bits/stdc++.h>
//Damned if I don't take what I want

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

typedef pair<int, pii> pip;
#define D(x) (x.first)
#define S(x) (x.second.first)
#define T(x) (x.second.second)

const int MAXN = 152;
//const int MAXN = 3;
const ll MAXT = 1e9;
typedef bitset<MAXN> BIT;
typedef BIT mat[MAXN];

pip edg[MAXN];

mat temp, adj, pw, ans, multemp;

int n, m;

inline void cp(mat a, mat b){
	for (int i = 0; i < n; i++)
		a[i] = b[i];
}

void mul(mat c, mat a, mat b){
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			multemp[i][j] = b[j][i];

	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			c[i][j] = (a[i] & multemp[j]).any();
}

void applypw(int p){
	cp(pw, adj);
	for (; p; p >>= 1){
		if (p & 1){
			cp(temp, ans);
			mul(ans, temp, pw);
		}

		cp(temp, pw);
		mul(pw, temp, temp);
	}
}

bool ok(ll x){
	for (int i = 0; i < n; i++) adj[i].reset(), ans[i].reset(), ans[i].set(i);

	int r;
	for (int i = 0; i < m; i = r){
		if (D(edg[i]) > x) break;

		r = i;
		while(r < m && D(edg[r]) == D(edg[i])){
			adj[ S(edg[r]) ].set( T(edg[r]) );
			r++;
		}

		int t = min( ll(r == m ? n : D(edg[r]) - D(edg[i]) ), x - D(edg[i]) );

		/*cerr << "after applying: " << t << endl;
		for (int i = 0; i < n; i++)
			cerr << adj[i] << endl;
		cerr << "to" << endl;
		for (int i = 0; i < n; i++)
			cerr << ans[i] << endl;*/
		applypw(t);

		/*cerr << "we get " << endl;
		for (int i = 0; i < n; i++)
			cerr << ans[i] << endl;
		cerr << endl;*/
	}

	return ans[0][n-1];
}

int main(){
	scanf("%d%d", &n, &m);
	for (int i = 0; i < m; i++){
		pip &e = edg[i];

		scanf("%d%d%d", &S(e), &T(e), &D(e) );
		S(e)--; T(e)--;
		//cerr << "edge : " << S(e) << ", " << T(e) << endl;
	}

	edg[ m++ ] = pip(0, pii(n - 1, n - 1) );
	sort(edg, edg + m);

	ll l = 0, r = 2 * MAXT;
	//ll l = 1, r = 3;

	while(l + 1 < r){
		ll mid = (l + r) / 2;
		if (ok(mid)) r = mid;
		else l = mid;
	}

	if (r == 2 * MAXT) cout << "Impossible\n";
	else cout << r << "\n";

}


#include <bits/stdc++.h>
// Cogito, ergo sum

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

typedef pair<int, pii> pip;
#define D(x) (x.first)
#define S(x) (x.second.first)
#define T(x) (x.second.second)

const int MAXN = 152;
//const int MAXN = 3;
const ll MAXT = 1e9;
typedef bitset<MAXN> BIT;
typedef BIT mat[MAXN];

pip edg[MAXN];

mat temp, adj, pw, ans, multemp;

int n, m;

inline void cp(mat a, mat b){
	for (int i = 0; i < n; i++)
		a[i] = b[i];
}

void mul(mat c, mat a, mat b){
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			multemp[i][j] = b[j][i];

	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			c[i][j] = (a[i] & multemp[j]).any();
}

void applypw(int p){
	cp(pw, adj);
	for (; p; p >>= 1){
		if (p & 1){
			cp(temp, ans);
			mul(ans, temp, pw);
		}

		cp(temp, pw);
		mul(pw, temp, temp);
	}
}

bool ok(ll x){
	for (int i = 0; i < n; i++) adj[i].reset(), ans[i].reset(), ans[i].set(i);

	int r;
	for (int i = 0; i < m; i = r){
		if (D(edg[i]) > x) break;

		r = i;
		while(r < m && D(edg[r]) == D(edg[i])){
			adj[ S(edg[r]) ].set( T(edg[r]) );
			r++;
		}

		int t = min( ll(r == m ? n : D(edg[r]) - D(edg[i]) ), x - D(edg[i]) );

		/*cerr << "after applying: " << t << endl;
		for (int i = 0; i < n; i++)
			cerr << adj[i] << endl;
		cerr << "to" << endl;
		for (int i = 0; i < n; i++)
			cerr << ans[i] << endl;*/
		applypw(t);

		/*cerr << "we get " << endl;
		for (int i = 0; i < n; i++)
			cerr << ans[i] << endl;
		cerr << endl;*/
	}

	return ans[0][n-1];
}

int main(){
	scanf("%d%d", &n, &m);
	for (int i = 0; i < m; i++){
		pip &e = edg[i];

		scanf("%d%d%d", &S(e), &T(e), &D(e) );
		S(e)--; T(e)--;
		//cerr << "edge : " << S(e) << ", " << T(e) << endl;
	}

	edg[ m++ ] = pip(0, pii(n - 1, n - 1) );
	sort(edg, edg + m);

	ll l = 0, r = 2 * MAXT;
	//ll l = 1, r = 3;

	while(l + 1 < r){
		ll mid = (l + r) / 2;
		if (ok(mid)) r = mid;
		else l = mid;
	}

	if (r == 2 * MAXT) cout << "Impossible\n";
	else cout << r << "\n";

}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>

using namespace std;

#ifdef WIN32
	#define LL "%I64d"
#else
	#define LL "%lld"
#endif

#ifdef Lightning
	#define debug(orz...) printf(orz)
	const int DEBUG = 1;
#else
	#define debug(...)
	const int DEBUG = 0;
#endif

#define file_able if (0)
#define setfile(pro_name) freopen(pro_name".in", "r", stdin), freopen(pro_name".out", "w", stdout)
#define closefile() fclose(stdin), fclose(stdout)

#define cmax(_x, _y) (_x < (_y) ? _x = _y : 0)
#define cmin(_x, _y) (_x > (_y) ? _x = _y : 0)
#define gmax(_x, _y) ((_x) > (_y) ? (_x) : (_y))
#define gmin(_x, _y) ((_x) < (_y) ? (_x) : (_y))

namespace defs
{
	// #define getc() (S == T && (T = (S = B) + fread(B, 1, 1 << 15, stdin), S == T) ? EOF : *S++)

	// char B[1 << 15], *S = B, *T = B;

	#define getc() getchar()

	template <class Type> Type read()
	{
		Type aa;
		int bb = 1, ch;
		while (ch = getc(), (ch < '0' || ch > '9') && ch != '-');
		ch == '-' ? aa = 0, bb = -1 : aa = ch - '0';
		while (ch = getc(), ch >= '0' && ch <= '9') aa = aa * 10 + ch - '0';
		return bb == 1 ? aa : -aa;
	}

	const unsigned int rand_x = 11309;
	const unsigned int rand_y = 1000000019;

	unsigned int rand_cur;

	unsigned int rand_int()
	{
		return rand_cur = rand_cur * rand_x + rand_y;
	}

	const int inf = 1071026353;
	const int mod = 1000000007;
	const int modx = 998244353;
}

namespace graph
{
	struct mat
	{
		int h[150][5], l[5][150];

		void clear()
		{
			memset(h, 0, sizeof h);
			memset(l, 0, sizeof l);
		}

		void set(int i, int j)
		{
			h[i][j / 30] |= 1 << (j % 30);
			l[i / 30][j] |= 1 << (i % 30);
		}

		void init(bool m[150][150])
		{
			clear();
			for (int i = 0; i < 150; ++i)
			{
				for (int j = 0; j < 150; ++j)
				{
					if (m[i][j])
					{
						set(i, j);
					}
				}
			}
		}

		bool get(int i, int j)
		{
			return h[i][j / 30] & (1 << (j % 30));
		}

		mat operator *(const mat &p) const
		{
			mat ret;
			bool m[150][150];
			for (int i = 0; i < 150; ++i)
			{
				for (int j = 0; j < 150; ++j)
				{
					m[i][j] = 0;
					for (int k = 0; k < 5; ++k)
					{
						if (h[i][k] & p.l[k][j])
						{
							m[i][j] = 1;
							break;
						}
					}
				}
			}
			ret.init(m);
			return ret;
		}

		mat operator ^(int p) const
		{
			if (p == 1) return *this;
			mat ret = *this, tmp = *this;
			for (--p; p; p >>= 1)
			{
				if (p & 1) ret = ret * tmp;
				tmp = tmp * tmp;
			}
			return ret;
		}
	};
}

namespace lightningwork
{
	// my space

	struct edge
	{
		int u, v, t;

		bool operator <(const edge &e) const
		{
			return t < e.t;
		}
	} e[233];

	int (*read)() = defs::read<int>;

	int n, m, num[233];

	int to[233], pre[233], last[233], en;

	bool connect[150][150], arrive[2][150];

	graph::mat mat, tmp;

	void addedge(int f, int t)
	{
		to[++en] = t;
		pre[en] = last[f];
		last[f] = en;
	}

	void work()
	{
		n = read();
		m = read();
		for (int i = 1; i <= m; ++i)
		{
			e[i].u = read() - 1;
			e[i].v = read() - 1;
			e[i].t = read();
			num[i + 1] = e[i].t;
		}
		sort(num + 1, num + m + 2);
		sort(e + 1, e + m + 1);
		int tot = 0;
		for (int i = 1; i <= m + 1; ++i)
		{
			if (i == 1 || num[i] > num[i - 1])
			{
				num[++tot] = num[i];
			}
		}
		mat.clear();
		mat.set(0, 0);
		num[tot + 1] = defs::inf;
		for (int i = 1, j = 1; i <= tot; ++i)
		{
			while (j <= m && e[j].t <= num[i])
			{
				connect[e[j].u][e[j].v] = 1;
				addedge(e[j].u, e[j].v);
				++j;
			}
			for (int k = 0; k < n; ++k)
			{
				arrive[0][k] = mat.get(0, k);
			}
			for (int t = 0; t < n && t < num[i + 1] - num[i]; ++t)
			{
				int _t = t & 1;
				if (arrive[_t][n - 1])
				{
					printf("%d\n", num[i] + t);
					return;
				}
				memset(arrive[_t ^ 1], 0, sizeof arrive[_t ^ 1]);
				for (int k = 0; k < n; ++k)
				{
					if (arrive[_t][k] == 0) continue;
					for (int l = last[k]; l; l = pre[l])
					{
						arrive[_t ^ 1][to[l]] = 1;
					}
				}
			}
			if (i < tot)
			{
				tmp.init(connect);
				mat = mat * (tmp ^ (num[i + 1] - num[i]));
			}
		}
		puts("Impossible");
	}
}

int main()
{
	file_able setfile("flight");
	lightningwork::work();
	file_able closefile();
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <queue>
#include <bitset>
using namespace std;
#define N 155
bool vis[N];bitset<N> a[N];
int dis[N],n,m;
struct node
{
	bitset<N> a[N];
	friend node operator*(const node &a,const node &b)
	{
		node c;memset(c.a,0,sizeof(c.a));
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=n;j++)
			{
				if(a.a[i][j])c.a[i]|=b.a[j];
			}
		}
		return c;
	}
}map,ret;
struct no
{
	int x,y,d;
	friend bool operator<(const no &a,const no &b)
	{
		return a.d<b.d;
	}
}E[N];
/*
void print(const node &a)
{
	puts("");
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			printf("%d ",a.a[i][j]);
		}
		puts("");
	}
	puts("");
}
*/
void q_pow(int x)
{
	for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)map.a[i][j]=a[i][j];
	while(x)
	{
		if(x&1)ret=ret*map;
		map=map*map,x=x>>1;
	}
}
bool bfs()
{
	queue <int >q;memset(dis,0,sizeof(dis));memset(vis,0,sizeof(vis));
	for(int i=1;i<=n;i++)if(ret.a[1][i])q.push(i),vis[i]=1,dis[i]=0;
	while(!q.empty())
	{
		int x=q.front();q.pop();
		for(int i=1;i<=n;i++)
		{
			if(a[x][i]&&!vis[i])vis[i]=1,dis[i]=dis[x]+1,q.push(i);
		}
	}
	return vis[n];
}
int main()
{
	// freopen("travel.in","r",stdin);
	// freopen("travel.out","w",stdout);
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++)scanf("%d%d%d",&E[i].x,&E[i].y,&E[i].d);
	sort(E+1,E+m+1);int last=0,j=0,ans=0x7fffffff;ret.a[1][1]=1;
	while(1)
	{
		for(int i=j+1;i<=m;i++)
		{
			if(E[i].d>last)break;
			a[E[i].x][E[i].y]=1;j++;
		}
		if(bfs()){ans=min(last+dis[n],ans);}
		if(j==m)break;
		q_pow(E[j+1].d-last);//print(ret);
		last=E[j+1].d;
	}
	if(ans==0x7fffffff)puts("Impossible");
	else printf("%d\n",ans);
	return 0;
}