#include <iostream>
#include <vector>
#include <algorithm>
#include <string.h>

using namespace std;

//Compiler version g++ 6.3.0

std::vector<int> numb;
int n;
int cach[200005][3];

int getPoint(int pos, int turn){
    if(pos >= n)    return 0;
    int ret1 = 0, ret2 = 0;
    if(turn == 0){
        ret1 = numb[pos];
        ret2 = numb[pos];
        if(pos+1 < n)   ret2 += numb[pos+1];
    }

    if(cach[pos][turn] != -1)	return cach[pos][turn];

    cach[pos][turn] = std::min(ret1 + getPoint(pos+1, 1 - turn), ret2 + getPoint(pos+2, 1 - turn));

    return cach[pos][turn];
}

int main()
{
    int t;
    cin >> t;

    for(int tt = 0;tt<t;tt++){
        cin >> n;
        numb.clear();
        numb.resize(n);
        for(int i = 0;i<200005;i++){
        	for(int j = 0;j<3;j++){
        		cach[i][j] = -1;
        	}
        }

        for(int i = 0;i<n;i++)  cin >> numb[i];

        int point = getPoint(0, 0);

        std::cout << point << std::endl;
    }

}


profile pic
@gk.shakil
My Files
Save
Your Directory 1/30
Sort by:
Recent
a.cpp
  Private,  Sep 21st 2020, 10:38 am
a.cpp
1
#include <iostream>
2
#include <vector>
3
#include <algorithm>
4

5
using namespace std;
6

7
//Compiler version g++ 6.3.0
8

9
int main()
10
{
11
    int t,n;
12
    std::string numb;
13
    cin >> t;
14

15
    for(int tt = 0;tt<t;tt++){
16
        cin >> n;
17
        std::vector<int> numb;
18
        numb.resize(n);
19

20
        for(int i = 0;i<n;i++)  cin >> numb[i];
21

22
        int turn = 1;
23
        int point = 0;
24
        for(int i = 0;i<n;i++){
25
            if(turn == 1){
26
                point += numb[i];
27
                if(i+1 < n && numb[i+1] == 0)   i++;
28
            }
29
            else if(turn == 2){
30
                if(i + 1 < n && numb[i+1] == 1) i++;
31
            }
32

33
            if(turn == 1)   turn = 2;
34
            else    turn = 1;
35
        }
36

37
        std::cout << point << std::endl;
38
    }
39

40
}
Line: 30 Col: 52


#include <iostream>
#include <vector>
#include <algorithm>
#include <string.h>

using namespace std;

//Compiler version g++ 6.3.0

int main()
{
    int t;
    cin >> t;

    std::vector<int> numb;
	int n;
	int cach[200005][3];

    for(int tt = 0;tt<t;tt++){
        cin >> n;
        numb.clear();
        numb.resize(n);

        for(int i = 0;i<n;i++)  cin >> numb[i];

        //player0 = me
        //player1 = friend

        cach[0][1] = numb[0];
        cach[0][0] = numb[0];
        for(int pos = 1;pos<n;pos++){
        	for(int player = 0;player<2;player++){
        		int withPrevious = 0;
        		if(pos - 2 >= 0)	withPrevious = cach[pos-2][1 - player];
        		if( (pos - 1) == 0)	withPrevious +=  numb[0];
        		else withPrevious += player * numb[pos-1];

        		cach[pos][player] = (player * numb[pos]) + std::min(withPrevious,  cach[pos-1][1 - player]);
        	}
        }

        std::cout << std::min(cach[n-1][0], cach[n-1][1]) << std::endl;
    }

}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

//Compiler version g++ 6.3.0

int main()
{
    int t,n;
    std::string numb;
    cin >> t;

    for(int tt = 0;tt<t;tt++){
        cin >> n;
        std::vector<int> numb;
        numb.resize(n);

        for(int i = 0;i<n;i++)  cin >> numb[i];

        int turn = 1;
        int point = 0;
        for(int i = 0;i<n;i++){
            if(turn == 1){
                point += numb[i];
                if(i+1 < n && numb[i+1] == 0)   i++;
            }
            else if(turn == 2){
                if(i + 1 < n && numb[i+1] == 1) i++;
            }

            if(turn == 1)   turn = 2;
            else    turn = 1;
        }

        std::cout << point << std::endl;
    }

}


#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
#define LL long long
const int maxn= 1e6+10;
int a[maxn],sum[maxn];
int dp[maxn][2];
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
       memset(dp,INF,sizeof dp);
       int n;
       scanf("%d",&n);
       for(int i=1;i<=n;i++)
       {
           scanf("%d",a+i);
           sum[i]=sum[i-1]+a[i];
       }
       dp[1][0]=a[1];
       dp[2][0]=a[1]+a[2];
       for(int i=2;i<=n;i++)
       {
           if(i-1>=1)
           {
                dp[i][0]=min(dp[i-1][1]+a[i],dp[i][0]);
                dp[i][1]=min(dp[i-1][0],dp[i][1]);
           }
           if(i-2>=1)
           {
                dp[i][0]=min(dp[i-2][1]+a[i]+a[i-1],dp[i][0]);
                dp[i][1]=min(dp[i-2][0],dp[i][1]);
           }
       }
       /*for(int i=1;i<=n;i++)
       {
            cout<<dp[i][0]<<" ";
       }
       cout<<endl;
       for(int i=1;i<=n;i++)
       {
            cout<<dp[i][1]<<" ";
       }
       cout<<endl;*/
       cout<<min(dp[n][0],dp[n][1])<<endl;
    }
    return 0;
}


#include<iostream>
using namespace std;

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

int cal(int p, int q){
    if(p >= n) return 0;
    if(dp[p]) return dp[p];
    return dp[p] = min(q*a[p] + cal(p+1, q^1), (q*a[p] + q*a[p+1]) + cal(p+2, q^1));
}

int main(){
    int t;
    cin >> t;
    while(t--){
        cin >> n;
        int b = 1, c = 0, ans = 0;
        for(int i = 0; i < n; i++) cin >> a[i];
        cout << cal(0, 1) << '\n';
        for(int i = 0; i <= n; i++) a[i]=dp[i]=0;
    }
    return 0;
}


#include<iostream>
using namespace std;
int main(){
    int t, n;
    cin >> t;
    while(t--){
        cin >> n;
        int a[n], ans;
        for(int i = 0; i < n; i++) cin >> a[i];
        ans = (a[0] == 1);
        for(int i = 1; i < n; i++){
            if(a[i] == 0) continue;
            int j = i;
            while(a[j] == 1 && j < n) j++;
            ans += (j-i)/3;
            i = j-1;
        }
        cout << ans << '\n';
        ans = 0;
    }
    return 0;
}


#include<iostream>
using namespace std;

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

int cal(int p, int q){
    if(p >= n) return 0;
    if(dp[p][q]) return dp[p][q];
    return dp[p][q] = min(q*a[p] + cal(p+1, q^1), (q*a[p] + q*a[p+1]) + cal(p+2, q^1));
}

int main(){
    int t;
    cin >> t;
    while(t--){
        cin >> n;
        int b = 1, c = 0, ans = 0;
        for(int i = 0; i < n; i++) cin >> a[i];
        cout << cal(0, 1) << '\n';
        for(int i = 0; i <= n; i++) a[i]=dp[i][0]=dp[i][1]=0;
    }
    return 0;
}


/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>
#include <string>
#include <bits/stdc++.h>
#define lli long long int
#define pb push_back
#define vi vector<int>
#define asort(a) sort(a.begin(),a.end())
#define dsort(a) sort(a.rbegin(),a.rend())
#define f(i, a, b) for (long long i = a; i < b; i++)
#define rf(i, a, b) for (int i = b; i >= a; i--)
#define MOD 1000000007
using namespace std;
template<class A>istream&operator>>(istream&in,vector<A>&a){for(A &i:a)in>>i;return in;}

// lli pw(lli a, lli b)
// {
//     lli c = 1, m = a % MOD;
//     while (b)
//     {
//         if (b & 1)
//             c = (c * m) % MOD;
//         m = (m * m) % MOD;
//         b /= 2;
//     }
//     return c;
// }

void solve(int x11){
    int n;
    cin >> n;
    vector<int> a(n);
    f(i,0,n) cin >> a[i];
    int ans = 0;
    ans += a[0] == 1;
    for (int i = 1; i < n; i++) {
        if (a[i] == 0) {
            continue;
        }
        int j = i;
        while (j < n && a[j] == 1) {
            ++j;
        }
        ans += (j - i) / 3;
        i = j - 1;
    }
    cout << ans << endl;

}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    cin>>t;
    f(i, 0, t)
    {
        solve(i);
    }

}


#include <iostream>
#include <string>
#include <bits/stdc++.h>
#define lli long long int
#define pb push_back
#define vi vector<int>
#define asort(a) sort(a.begin(),a.end())
#define dsort(a) sort(a.rbegin(),a.rend())
#define f(i, a, b) for (long long i = a; i < b; i++)
#define rf(i, a, b) for (int i = b; i >= a; i--)
#define MOD 1000000007
using namespace std;
template<class A>istream&operator>>(istream&in,vector<A>&a){for(A &i:a)in>>i;return in;}

void solve(int x11){
    int n;
    cin >> n;
    vector<int> a(n);
    f(i,0,n) cin >> a[i];
    int ans = 0;
    ans += a[0];
    f(i,1,n){
        if(a[i] == 1){
            int j=i;
            while(j<n && a[i]==1)j++;
            ans+=(j-i)/3;
            i=j-1;
        }
    }
    cout<<ans<<endl;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    cin>>t;
    f(i, 0, t)
    {
        solve(i);
    }

}


#include <iostream>
#include <string>
#include <bits/stdc++.h>
#define lli long long int
#define pb push_back
#define vi vector<int>
#define asort(a) sort(a.begin(),a.end())
#define dsort(a) sort(a.rbegin(),a.rend())
#define f(i, a, b) for (long long i = a; i < b; i++)
#define rf(i, a, b) for (int i = b; i >= a; i--)
#define MOD 1000000007
using namespace std;
template<class A>istream&operator>>(istream&in,vector<A>&a){for(A &i:a)in>>i;return in;}

void solve(int x11){
    int n;
    cin >> n;
    vector<int> a(n);
    f(i,0,n) cin >> a[i];
    int ans = 0;
    ans += a[0];
    f(i,1,n){
        if(a[i] == 0){
            continue;
        }
            int j=i;
            while(j<n && a[j]==1)j++;
            ans+=(j-i)/3;
            i=j-1;

    }
    cout<<ans<<endl;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    cin>>t;
    f(i, 0, t)
    {
        solve(i);
    }

}


/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>
#include <string>
#include <bits/stdc++.h>
#define lli long long int
#define pb push_back
#define vi vector<int>
#define asort(a) sort(a.begin(),a.end())
#define dsort(a) sort(a.rbegin(),a.rend())
#define f(i, a, b) for (long long i = a; i < b; i++)
#define rf(i, a, b) for (int i = b; i >= a; i--)
#define MOD 1000000007
using namespace std;
template<class A>istream&operator>>(istream&in,vector<A>&a){for(A &i:a)in>>i;return in;}

// lli pw(lli a, lli b)
// {
//     lli c = 1, m = a % MOD;
//     while (b)
//     {
//         if (b & 1)
//             c = (c * m) % MOD;
//         m = (m * m) % MOD;
//         b /= 2;
//     }
//     return c;
// }

void solve(int x11){
int n;
        cin >> n;
        vector<int> a(n);
        for (auto &it : a) cin >> it;
        int ans = 0;
        ans += a[0] == 1;
        for (int i = 1; i < n; ++i) {
            if (a[i] == 1) {
                int j = i;
                while (j < n && a[j] == 1) {
                    ++j;
                }
                ans += (j - i) / 3;
                i = j - 1;
            }
        }
        cout << ans << endl;

}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    //cin>>t;
    f(i, 0, t)
    {
        solve(i);
    }

}


#include <iostream>
#include <string>
#include <bits/stdc++.h>
#define lli long long int
#define pb push_back
#define vi vector<int>
#define asort(a) sort(a.begin(),a.end())
#define dsort(a) sort(a.rbegin(),a.rend())
#define f(i, a, b) for (long long i = a; i < b; i++)
#define rf(i, a, b) for (int i = b; i >= a; i--)
#define MOD 1000000007
using namespace std;
template<class A>istream&operator>>(istream&in,vector<A>&a){for(A &i:a)in>>i;return in;}

void solve(int x11){
    int n;
    cin >> n;
    vector<int> a(n);
    f(i,0,n) cin >> a[i];
    int ans = 0;
    ans += a[0];
    f(i,1,n){
        if(a[i] == 0){
            continue;
        }
            int j=i;
            while(j<n && a[i]==1)j++;
            ans+=(j-i)/3;
            i=j-1;

    }
    cout<<ans<<endl;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    cin>>t;
    f(i, 0, t)
    {
        solve(i);
    }

}


#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)x.size()
#define len(x) (int)x.length()
#define ac(x, i) get<i>(x)
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll ,ll>
#define pli pair<ll, int>
#define ff first
#define ss second
#define trace(x)    cerr << #x << ": " << x << "\n"
#define trav0(a, n) cerr << #a << "[" << n <<  "] = ["; for (int i = 0; i < n; i++) cerr << a[i] << ", "; cerr << "]\n"
#define trav1(a, n) cerr << #a << "[" << n <<  "] = ["; for (int i = 1; i <= n; i++) cerr << a[i] << ", "; cerr << "]\n"
#define trap0(a, n) cerr << #a << "[" << n <<  "] = ["; for (int i = 0; i < n; i++) cerr << "(" << a[i].ff << ", " << a[i].ss << "), "; cerr << "]\n"
#define trap1(a, n) cerr << #a << "[" << n <<  "] = ["; for (int i = 1; i <= n; i++) cerr << "(" << a[i].ff << ", " << a[i].ss << "), "; cerr << "]\n"
#define travMap(a)  cerr << #a << "[" << sz(a) <<  "] = ["; for (auto i : a) cerr << "(" << i.ff << ", " << i.ss << "), "; cerr << "]\n"
#define traverse(a) cerr << #a << "[" << sz(a) <<  "] = ["; for (auto i : a) cerr << i << ", "; cerr << "]\n"
#define random_permute(x) shuffle(all(x), mt)
#define FOR(a, b, c) for (int(a) = (b); (a) < (c); ++(a))
#define FORN(a, b, c) for (int(a) = (b); (a) <= (c); ++(a))
#define FORD(a, b, c) for (int(a) = (b); (a) >= (c); --(a))
#define FORSQ(a, b, c) for (int(a) = (b); (a) * (a) <= (c); ++(a))
#define FORC(a, b, c) for (char(a) = (b); (a) <= (c); ++(a))
#define FOREACH(a, b) for (auto&(a) : (b))
#define REP(i, n) FOR(i, 0, n)
#define REPN(i, n) FORN(i, 1, n)

typedef unsigned int uint;
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef vector<bool> vb;
typedef vector<string> vs;
const int infi = 1e9;
const ll infl = 1e18;
const int MOD = 1e9 + 7;
//template <typename Map> bool map_compare (Map const &lhs, Map const &rhs) { return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin());}
//auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
//mt19937 mt(seed);
//int randint(int p = MOD) { return mt() % p;}
//ll randlong(ll p = infl) { return mt() % p;}
ll log_2 (ll n) { return (n > 1ll)? 1ll + log_2(n/2): 0ll;}
inline void add(ll&a, ll b) {a += b; if (a >= MOD) a -= MOD;}
inline void sub(ll&a, ll b) {a -= b; if (a < 0) a += MOD;}

/* CONVERT A INTEGER TO A STRING */

inline string IntToString(ll a)
{
    char x[100];
    sprintf(x, "%lld", a);
    string s = x;
    return s;
}

/* CONVERT A STRING TO AN INTEGER */

inline ll StringToInt(string a)
{
    char x[100];
    ll res;
    strcpy(x, a.c_str());
    sscanf(x, "%lld", &res);
    return res;
}

/* TO ENTER A STRING AS A CHARACTER ARRAY AND THEN CONVERT IT TO A SINGLE STRING */

inline string GetString(void)
{
    char x[1000005];
    scanf("%s", x);
    string s = x;
    return s;
}

/* CONVERT A STRING TO UPPER CASE */

inline string uppercase(string s)
{
    int n = sz(s);
    REP(i, n)
    if (s[i] >= 'a' && s[i] <= 'z')
        s[i] = s[i] - 'a' + 'A';
    return s;
}

/* CONVERT A STRING TO LOWER CASE */

inline string lowercase(string s)
{
    int n = sz(s);
    REP(i, n)
    if (s[i] >= 'A' && s[i] <= 'Z')
        s[i] = s[i] - 'A' + 'a';
    return s;
}

inline void OPEN(string s)
{
#ifndef TESTING
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
#endif
}

/* TO DISPLAY AN ARRAY ON THE SCREEN */

void showarr(ll arr[],ll n)
{
	int i;
	for(i=0;i<n;i++)
	{
		cout<<arr[i]<<" ";
	}
	cout<<endl;
  }

/*TO DISPLAY A VECTOR OF INTERGERS */

void showvec(vector <ll> v)
{
	int i;
	for(i=0;i<v.size();i++)
	{
		cout<<v[i]<<" ";
	}
	cout<<endl;
  }

/* TO CHECK IF A STRING IS PALINDROME*/

  int isPalindrome(string str)
{
    int l = 0;
    int h = str.length() - 1;
  while (h > l)
    {
        if (str[l++] != str[h--] )
        {
            return 0;
        }
    }
    return 1;
}

/*   DISPLAY A VECTOR OF PAIRS */

  void showvecpair(vll v)
  {
  	for(int i=0;i<sz(v);i++)
  	{
  		cout<<v[i].first<<" "<<v[i].second<<endl;
	  }
  }

/*   TO SORT A VECTOR OF PAIRS VIA SECOND ELEMENT.........CHECK NOTEPAD */

bool sortbysec(const pll &a,
              const pll &b)
{
    return (a.second < b.second);
}

/*   TO PRINT BINARY REPRESENTATION OF ANY NUMBER */

void bin(unsigned n)
{
    unsigned i;
    for (i = 1 << 31; i > 0; i = i / 2)
        (n & i)? printf("1"): printf("0");
}

/*   TO COUNT NUMBER OF SET BITS IN BINARY REPRESENTATION OF ANY NUMBER */

ll cntset(ll n)
{
	bitset<64> b1(n);  //represents in 64 bit format
	return b1.count();
}

// end of Sektor_jr template v2.0.3 (BETA)

void solve()
{
	long long int l,a,b,n,i,y,k,j,ctr=0,cnt=0,x,ctr1=0;
	cin>>n;
	vl v(n+1),dp(n+1);
	for(i=1;i<=n;i++)
	{
		cin>>v[i];
	}
	ctr=1;
	for(i=2;i<=n;i++)
	{

		if(v[i]==0)
		{
			if(ctr<2)
			{
				ctr++;
				ctr1=0;

	    	}
	   	    else
	    	{
	    		ctr1++;
	    		ctr=0;
			}
		}
		if(v[i]==1)
		{
			if(ctr1<2)
			{
				ctr1++;
				ctr=0;
			}
			else
			{
				ctr++;
				ctr1=0;
				cnt=cnt+1;
				//cout<<" i = "<<i<<endl;

			}
		}
	}
	cout<<(cnt+v[1])<<endl;

}

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

   	//T=1;
	cin>>T;

    for(i=1;i<=T;i++)
    {
    	solve();
	}
    return 0;

}


#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)x.size()
#define len(x) (int)x.length()
#define ac(x, i) get<i>(x)
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll ,ll>
#define pli pair<ll, int>
#define ff first
#define ss second
#define trace(x)    cerr << #x << ": " << x << "\n"
#define trav0(a, n) cerr << #a << "[" << n <<  "] = ["; for (int i = 0; i < n; i++) cerr << a[i] << ", "; cerr << "]\n"
#define trav1(a, n) cerr << #a << "[" << n <<  "] = ["; for (int i = 1; i <= n; i++) cerr << a[i] << ", "; cerr << "]\n"
#define trap0(a, n) cerr << #a << "[" << n <<  "] = ["; for (int i = 0; i < n; i++) cerr << "(" << a[i].ff << ", " << a[i].ss << "), "; cerr << "]\n"
#define trap1(a, n) cerr << #a << "[" << n <<  "] = ["; for (int i = 1; i <= n; i++) cerr << "(" << a[i].ff << ", " << a[i].ss << "), "; cerr << "]\n"
#define travMap(a)  cerr << #a << "[" << sz(a) <<  "] = ["; for (auto i : a) cerr << "(" << i.ff << ", " << i.ss << "), "; cerr << "]\n"
#define traverse(a) cerr << #a << "[" << sz(a) <<  "] = ["; for (auto i : a) cerr << i << ", "; cerr << "]\n"
#define random_permute(x) shuffle(all(x), mt)
#define FOR(a, b, c) for (int(a) = (b); (a) < (c); ++(a))
#define FORN(a, b, c) for (int(a) = (b); (a) <= (c); ++(a))
#define FORD(a, b, c) for (int(a) = (b); (a) >= (c); --(a))
#define FORSQ(a, b, c) for (int(a) = (b); (a) * (a) <= (c); ++(a))
#define FORC(a, b, c) for (char(a) = (b); (a) <= (c); ++(a))
#define FOREACH(a, b) for (auto&(a) : (b))
#define REP(i, n) FOR(i, 0, n)
#define REPN(i, n) FORN(i, 1, n)

typedef unsigned int uint;
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef vector<bool> vb;
typedef vector<string> vs;
const int infi = 1e9;
const ll infl = 1e18;
const int MOD = 1e9 + 7;
//template <typename Map> bool map_compare (Map const &lhs, Map const &rhs) { return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin());}
//auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
//mt19937 mt(seed);
//int randint(int p = MOD) { return mt() % p;}
//ll randlong(ll p = infl) { return mt() % p;}
ll log_2 (ll n) { return (n > 1ll)? 1ll + log_2(n/2): 0ll;}
inline void add(ll&a, ll b) {a += b; if (a >= MOD) a -= MOD;}
inline void sub(ll&a, ll b) {a -= b; if (a < 0) a += MOD;}

/* CONVERT A INTEGER TO A STRING */

inline string IntToString(ll a)
{
    char x[100];
    sprintf(x, "%lld", a);
    string s = x;
    return s;
}

/* CONVERT A STRING TO AN INTEGER */

inline ll StringToInt(string a)
{
    char x[100];
    ll res;
    strcpy(x, a.c_str());
    sscanf(x, "%lld", &res);
    return res;
}

/* TO ENTER A STRING AS A CHARACTER ARRAY AND THEN CONVERT IT TO A SINGLE STRING */

inline string GetString(void)
{
    char x[1000005];
    scanf("%s", x);
    string s = x;
    return s;
}

/* CONVERT A STRING TO UPPER CASE */

inline string uppercase(string s)
{
    int n = sz(s);
    REP(i, n)
    if (s[i] >= 'a' && s[i] <= 'z')
        s[i] = s[i] - 'a' + 'A';
    return s;
}

/* CONVERT A STRING TO LOWER CASE */

inline string lowercase(string s)
{
    int n = sz(s);
    REP(i, n)
    if (s[i] >= 'A' && s[i] <= 'Z')
        s[i] = s[i] - 'A' + 'a';
    return s;
}

inline void OPEN(string s)
{
#ifndef TESTING
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
#endif
}

/* TO DISPLAY AN ARRAY ON THE SCREEN */

void showarr(ll arr[],ll n)
{
	int i;
	for(i=0;i<n;i++)
	{
		cout<<arr[i]<<" ";
	}
	cout<<endl;
  }

/*TO DISPLAY A VECTOR OF INTERGERS */

void showvec(vector <ll> v)
{
	int i;
	for(i=0;i<v.size();i++)
	{
		cout<<v[i]<<" ";
	}
	cout<<endl;
  }

/* TO CHECK IF A STRING IS PALINDROME*/

  int isPalindrome(string str)
{
    int l = 0;
    int h = str.length() - 1;
  while (h > l)
    {
        if (str[l++] != str[h--] )
        {
            return 0;
        }
    }
    return 1;
}

/*   DISPLAY A VECTOR OF PAIRS */

  void showvecpair(vll v)
  {
  	for(int i=0;i<sz(v);i++)
  	{
  		cout<<v[i].first<<" "<<v[i].second<<endl;
	  }
  }

/*   TO SORT A VECTOR OF PAIRS VIA SECOND ELEMENT.........CHECK NOTEPAD */

bool sortbysec(const pll &a,
              const pll &b)
{
    return (a.second < b.second);
}

/*   TO PRINT BINARY REPRESENTATION OF ANY NUMBER */

void bin(unsigned n)
{
    unsigned i;
    for (i = 1 << 31; i > 0; i = i / 2)
        (n & i)? printf("1"): printf("0");
}

/*   TO COUNT NUMBER OF SET BITS IN BINARY REPRESENTATION OF ANY NUMBER */

ll cntset(ll n)
{
	bitset<64> b1(n);  //represents in 64 bit format
	return b1.count();
}

// end of Sektor_jr template v2.0.3 (BETA)

void solve()
{
	long long int l,a,b,n,i,y,k,j,ctr=0,cnt=0,x,ctr1=0;
	cin>>n;
	vl v(n+1),dp(n+1);
	for(i=1;i<=n;i++)
	{
		cin>>v[i];
	}
	ctr=1;
	x=0;
	for(i=2;i<=n;i++)
	{

			if(v[i]==1)
			{
				x++;
			}
			if(v[i]==0)
			{
				cnt=cnt+x/3;
				x=0;
			}
	}
	cnt=cnt+x/3;

	cout<<(cnt+v[1])<<endl;

}

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

   	//T=1;
	cin>>T;

    for(i=1;i<=T;i++)
    {
    	solve();
	}
    return 0;

}


#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)x.size()
#define len(x) (int)x.length()
#define ac(x, i) get<i>(x)
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll ,ll>
#define pli pair<ll, int>
#define ff first
#define ss second
#define trace(x)    cerr << #x << ": " << x << "\n"
#define trav0(a, n) cerr << #a << "[" << n <<  "] = ["; for (int i = 0; i < n; i++) cerr << a[i] << ", "; cerr << "]\n"
#define trav1(a, n) cerr << #a << "[" << n <<  "] = ["; for (int i = 1; i <= n; i++) cerr << a[i] << ", "; cerr << "]\n"
#define trap0(a, n) cerr << #a << "[" << n <<  "] = ["; for (int i = 0; i < n; i++) cerr << "(" << a[i].ff << ", " << a[i].ss << "), "; cerr << "]\n"
#define trap1(a, n) cerr << #a << "[" << n <<  "] = ["; for (int i = 1; i <= n; i++) cerr << "(" << a[i].ff << ", " << a[i].ss << "), "; cerr << "]\n"
#define travMap(a)  cerr << #a << "[" << sz(a) <<  "] = ["; for (auto i : a) cerr << "(" << i.ff << ", " << i.ss << "), "; cerr << "]\n"
#define traverse(a) cerr << #a << "[" << sz(a) <<  "] = ["; for (auto i : a) cerr << i << ", "; cerr << "]\n"
#define random_permute(x) shuffle(all(x), mt)
#define FOR(a, b, c) for (int(a) = (b); (a) < (c); ++(a))
#define FORN(a, b, c) for (int(a) = (b); (a) <= (c); ++(a))
#define FORD(a, b, c) for (int(a) = (b); (a) >= (c); --(a))
#define FORSQ(a, b, c) for (int(a) = (b); (a) * (a) <= (c); ++(a))
#define FORC(a, b, c) for (char(a) = (b); (a) <= (c); ++(a))
#define FOREACH(a, b) for (auto&(a) : (b))
#define REP(i, n) FOR(i, 0, n)
#define REPN(i, n) FORN(i, 1, n)

typedef unsigned int uint;
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef vector<bool> vb;
typedef vector<string> vs;
const int infi = 1e9;
const ll infl = 1e18;
const int MOD = 1e9 + 7;
//template <typename Map> bool map_compare (Map const &lhs, Map const &rhs) { return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin());}
//auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
//mt19937 mt(seed);
//int randint(int p = MOD) { return mt() % p;}
//ll randlong(ll p = infl) { return mt() % p;}
ll log_2 (ll n) { return (n > 1ll)? 1ll + log_2(n/2): 0ll;}
inline void add(ll&a, ll b) {a += b; if (a >= MOD) a -= MOD;}
inline void sub(ll&a, ll b) {a -= b; if (a < 0) a += MOD;}

/* CONVERT A INTEGER TO A STRING */

inline string IntToString(ll a)
{
    char x[100];
    sprintf(x, "%lld", a);
    string s = x;
    return s;
}

/* CONVERT A STRING TO AN INTEGER */

inline ll StringToInt(string a)
{
    char x[100];
    ll res;
    strcpy(x, a.c_str());
    sscanf(x, "%lld", &res);
    return res;
}

/* TO ENTER A STRING AS A CHARACTER ARRAY AND THEN CONVERT IT TO A SINGLE STRING */

inline string GetString(void)
{
    char x[1000005];
    scanf("%s", x);
    string s = x;
    return s;
}

/* CONVERT A STRING TO UPPER CASE */

inline string uppercase(string s)
{
    int n = sz(s);
    REP(i, n)
    if (s[i] >= 'a' && s[i] <= 'z')
        s[i] = s[i] - 'a' + 'A';
    return s;
}

/* CONVERT A STRING TO LOWER CASE */

inline string lowercase(string s)
{
    int n = sz(s);
    REP(i, n)
    if (s[i] >= 'A' && s[i] <= 'Z')
        s[i] = s[i] - 'A' + 'a';
    return s;
}

inline void OPEN(string s)
{
#ifndef TESTING
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
#endif
}

/* TO DISPLAY AN ARRAY ON THE SCREEN */

void showarr(ll arr[],ll n)
{
	int i;
	for(i=0;i<n;i++)
	{
		cout<<arr[i]<<" ";
	}
	cout<<endl;
  }

/*TO DISPLAY A VECTOR OF INTERGERS */

void showvec(vector <ll> v)
{
	int i;
	for(i=0;i<v.size();i++)
	{
		cout<<v[i]<<" ";
	}
	cout<<endl;
  }

/* TO CHECK IF A STRING IS PALINDROME*/

  int isPalindrome(string str)
{
    int l = 0;
    int h = str.length() - 1;
  while (h > l)
    {
        if (str[l++] != str[h--] )
        {
            return 0;
        }
    }
    return 1;
}

/*   DISPLAY A VECTOR OF PAIRS */

  void showvecpair(vll v)
  {
  	for(int i=0;i<sz(v);i++)
  	{
  		cout<<v[i].first<<" "<<v[i].second<<endl;
	  }
  }

/*   TO SORT A VECTOR OF PAIRS VIA SECOND ELEMENT.........CHECK NOTEPAD */

bool sortbysec(const pll &a,
              const pll &b)
{
    return (a.second < b.second);
}

/*   TO PRINT BINARY REPRESENTATION OF ANY NUMBER */

void bin(unsigned n)
{
    unsigned i;
    for (i = 1 << 31; i > 0; i = i / 2)
        (n & i)? printf("1"): printf("0");
}

/*   TO COUNT NUMBER OF SET BITS IN BINARY REPRESENTATION OF ANY NUMBER */

ll cntset(ll n)
{
	bitset<64> b1(n);  //represents in 64 bit format
	return b1.count();
}

// end of Sektor_jr template v2.0.3 (BETA)

void solve()
{
	long long int l,a,b,n,i,y,k,j,max=INT_MIN,ctr=0,cnt=0,x;
	cin>>n;
	vl v(n+1),dp(n+1);
	for(i=1;i<=n;i++)
	{
		cin>>v[i];
	}
	dp[0]=0;
	dp[1]=v[1];
	if(n>1)
	{
	dp[2]=dp[1];
	for(i=3;i<=n;i++)
	{
		dp[i]=min(dp[i-2]+v[i],dp[i-3]+v[i]);
	}
	}
	x=*max_element(dp.begin(),dp.end());
	cout<<x<<endl;

}

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

   	//T=1;
	cin>>T;

    for(i=1;i<=T;i++)
    {
    	solve();
	}
    return 0;

}


#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)x.size()
#define len(x) (int)x.length()
#define ac(x, i) get<i>(x)
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll ,ll>
#define pli pair<ll, int>
#define ff first
#define ss second
#define trace(x)    cerr << #x << ": " << x << "\n"
#define trav0(a, n) cerr << #a << "[" << n <<  "] = ["; for (int i = 0; i < n; i++) cerr << a[i] << ", "; cerr << "]\n"
#define trav1(a, n) cerr << #a << "[" << n <<  "] = ["; for (int i = 1; i <= n; i++) cerr << a[i] << ", "; cerr << "]\n"
#define trap0(a, n) cerr << #a << "[" << n <<  "] = ["; for (int i = 0; i < n; i++) cerr << "(" << a[i].ff << ", " << a[i].ss << "), "; cerr << "]\n"
#define trap1(a, n) cerr << #a << "[" << n <<  "] = ["; for (int i = 1; i <= n; i++) cerr << "(" << a[i].ff << ", " << a[i].ss << "), "; cerr << "]\n"
#define travMap(a)  cerr << #a << "[" << sz(a) <<  "] = ["; for (auto i : a) cerr << "(" << i.ff << ", " << i.ss << "), "; cerr << "]\n"
#define traverse(a) cerr << #a << "[" << sz(a) <<  "] = ["; for (auto i : a) cerr << i << ", "; cerr << "]\n"
#define random_permute(x) shuffle(all(x), mt)
#define FOR(a, b, c) for (int(a) = (b); (a) < (c); ++(a))
#define FORN(a, b, c) for (int(a) = (b); (a) <= (c); ++(a))
#define FORD(a, b, c) for (int(a) = (b); (a) >= (c); --(a))
#define FORSQ(a, b, c) for (int(a) = (b); (a) * (a) <= (c); ++(a))
#define FORC(a, b, c) for (char(a) = (b); (a) <= (c); ++(a))
#define FOREACH(a, b) for (auto&(a) : (b))
#define REP(i, n) FOR(i, 0, n)
#define REPN(i, n) FORN(i, 1, n)

typedef unsigned int uint;
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef vector<bool> vb;
typedef vector<string> vs;
const int infi = 1e9;
const ll infl = 1e18;
const int MOD = 1e9 + 7;
//template <typename Map> bool map_compare (Map const &lhs, Map const &rhs) { return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin());}
//auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
//mt19937 mt(seed);
//int randint(int p = MOD) { return mt() % p;}
//ll randlong(ll p = infl) { return mt() % p;}
ll log_2 (ll n) { return (n > 1ll)? 1ll + log_2(n/2): 0ll;}
inline void add(ll&a, ll b) {a += b; if (a >= MOD) a -= MOD;}
inline void sub(ll&a, ll b) {a -= b; if (a < 0) a += MOD;}

/* CONVERT A INTEGER TO A STRING */

inline string IntToString(ll a)
{
    char x[100];
    sprintf(x, "%lld", a);
    string s = x;
    return s;
}

/* CONVERT A STRING TO AN INTEGER */

inline ll StringToInt(string a)
{
    char x[100];
    ll res;
    strcpy(x, a.c_str());
    sscanf(x, "%lld", &res);
    return res;
}

/* TO ENTER A STRING AS A CHARACTER ARRAY AND THEN CONVERT IT TO A SINGLE STRING */

inline string GetString(void)
{
    char x[1000005];
    scanf("%s", x);
    string s = x;
    return s;
}

/* CONVERT A STRING TO UPPER CASE */

inline string uppercase(string s)
{
    int n = sz(s);
    REP(i, n)
    if (s[i] >= 'a' && s[i] <= 'z')
        s[i] = s[i] - 'a' + 'A';
    return s;
}

/* CONVERT A STRING TO LOWER CASE */

inline string lowercase(string s)
{
    int n = sz(s);
    REP(i, n)
    if (s[i] >= 'A' && s[i] <= 'Z')
        s[i] = s[i] - 'A' + 'a';
    return s;
}

inline void OPEN(string s)
{
#ifndef TESTING
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
#endif
}

/* TO DISPLAY AN ARRAY ON THE SCREEN */

void showarr(ll arr[],ll n)
{
	int i;
	for(i=0;i<n;i++)
	{
		cout<<arr[i]<<" ";
	}
	cout<<endl;
  }

/*TO DISPLAY A VECTOR OF INTERGERS */

void showvec(vector <ll> v)
{
	int i;
	for(i=0;i<v.size();i++)
	{
		cout<<v[i]<<" ";
	}
	cout<<endl;
  }

/* TO CHECK IF A STRING IS PALINDROME*/

  int isPalindrome(string str)
{
    int l = 0;
    int h = str.length() - 1;
  while (h > l)
    {
        if (str[l++] != str[h--] )
        {
            return 0;
        }
    }
    return 1;
}

/*   DISPLAY A VECTOR OF PAIRS */

  void showvecpair(vll v)
  {
  	for(int i=0;i<sz(v);i++)
  	{
  		cout<<v[i].first<<" "<<v[i].second<<endl;
	  }
  }

/*   TO SORT A VECTOR OF PAIRS VIA SECOND ELEMENT.........CHECK NOTEPAD */

bool sortbysec(const pll &a,
              const pll &b)
{
    return (a.second < b.second);
}

/*   TO PRINT BINARY REPRESENTATION OF ANY NUMBER */

void bin(unsigned n)
{
    unsigned i;
    for (i = 1 << 31; i > 0; i = i / 2)
        (n & i)? printf("1"): printf("0");
}

/*   TO COUNT NUMBER OF SET BITS IN BINARY REPRESENTATION OF ANY NUMBER */

ll cntset(ll n)
{
	bitset<64> b1(n);  //represents in 64 bit format
	return b1.count();
}

// end of Sektor_jr template v2.0.3 (BETA)

void solve()
{
	long long int l,a,b,n,i,y,k,j,max=INT_MIN,ctr=0,cnt=0,x;
	cin>>n;
	vl v(n+1),dp(n+1);
	for(i=1;i<=n;i++)
	{
		cin>>v[i];
	}
	dp[0]=0;
	dp[1]=v[1];
	dp[2]=dp[1];
	for(i=3;i<=n;i++)
	{
		dp[i]=min(dp[i-2]+v[i],dp[i-3]+v[i]);
	}
	x=*max_element(dp.begin(),dp.end());
	cout<<x<<endl;

}

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

   	//T=1;
	cin>>T;

    for(i=1;i<=T;i++)
    {
    	solve();
	}
    return 0;

}


#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)x.size()
#define len(x) (int)x.length()
#define ac(x, i) get<i>(x)
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll ,ll>
#define pli pair<ll, int>
#define ff first
#define ss second
#define trace(x)    cerr << #x << ": " << x << "\n"
#define trav0(a, n) cerr << #a << "[" << n <<  "] = ["; for (int i = 0; i < n; i++) cerr << a[i] << ", "; cerr << "]\n"
#define trav1(a, n) cerr << #a << "[" << n <<  "] = ["; for (int i = 1; i <= n; i++) cerr << a[i] << ", "; cerr << "]\n"
#define trap0(a, n) cerr << #a << "[" << n <<  "] = ["; for (int i = 0; i < n; i++) cerr << "(" << a[i].ff << ", " << a[i].ss << "), "; cerr << "]\n"
#define trap1(a, n) cerr << #a << "[" << n <<  "] = ["; for (int i = 1; i <= n; i++) cerr << "(" << a[i].ff << ", " << a[i].ss << "), "; cerr << "]\n"
#define travMap(a)  cerr << #a << "[" << sz(a) <<  "] = ["; for (auto i : a) cerr << "(" << i.ff << ", " << i.ss << "), "; cerr << "]\n"
#define traverse(a) cerr << #a << "[" << sz(a) <<  "] = ["; for (auto i : a) cerr << i << ", "; cerr << "]\n"
#define random_permute(x) shuffle(all(x), mt)
#define FOR(a, b, c) for (int(a) = (b); (a) < (c); ++(a))
#define FORN(a, b, c) for (int(a) = (b); (a) <= (c); ++(a))
#define FORD(a, b, c) for (int(a) = (b); (a) >= (c); --(a))
#define FORSQ(a, b, c) for (int(a) = (b); (a) * (a) <= (c); ++(a))
#define FORC(a, b, c) for (char(a) = (b); (a) <= (c); ++(a))
#define FOREACH(a, b) for (auto&(a) : (b))
#define REP(i, n) FOR(i, 0, n)
#define REPN(i, n) FORN(i, 1, n)

typedef unsigned int uint;
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef vector<bool> vb;
typedef vector<string> vs;
const int infi = 1e9;
const ll infl = 1e18;
const int MOD = 1e9 + 7;
//template <typename Map> bool map_compare (Map const &lhs, Map const &rhs) { return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin());}
//auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
//mt19937 mt(seed);
//int randint(int p = MOD) { return mt() % p;}
//ll randlong(ll p = infl) { return mt() % p;}
ll log_2 (ll n) { return (n > 1ll)? 1ll + log_2(n/2): 0ll;}
inline void add(ll&a, ll b) {a += b; if (a >= MOD) a -= MOD;}
inline void sub(ll&a, ll b) {a -= b; if (a < 0) a += MOD;}

/* CONVERT A INTEGER TO A STRING */

inline string IntToString(ll a)
{
    char x[100];
    sprintf(x, "%lld", a);
    string s = x;
    return s;
}

/* CONVERT A STRING TO AN INTEGER */

inline ll StringToInt(string a)
{
    char x[100];
    ll res;
    strcpy(x, a.c_str());
    sscanf(x, "%lld", &res);
    return res;
}

/* TO ENTER A STRING AS A CHARACTER ARRAY AND THEN CONVERT IT TO A SINGLE STRING */

inline string GetString(void)
{
    char x[1000005];
    scanf("%s", x);
    string s = x;
    return s;
}

/* CONVERT A STRING TO UPPER CASE */

inline string uppercase(string s)
{
    int n = sz(s);
    REP(i, n)
    if (s[i] >= 'a' && s[i] <= 'z')
        s[i] = s[i] - 'a' + 'A';
    return s;
}

/* CONVERT A STRING TO LOWER CASE */

inline string lowercase(string s)
{
    int n = sz(s);
    REP(i, n)
    if (s[i] >= 'A' && s[i] <= 'Z')
        s[i] = s[i] - 'A' + 'a';
    return s;
}

inline void OPEN(string s)
{
#ifndef TESTING
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
#endif
}

/* TO DISPLAY AN ARRAY ON THE SCREEN */

void showarr(ll arr[],ll n)
{
	int i;
	for(i=0;i<n;i++)
	{
		cout<<arr[i]<<" ";
	}
	cout<<endl;
  }

/*TO DISPLAY A VECTOR OF INTERGERS */

void showvec(vector <ll> v)
{
	int i;
	for(i=0;i<v.size();i++)
	{
		cout<<v[i]<<" ";
	}
	cout<<endl;
  }

/* TO CHECK IF A STRING IS PALINDROME*/

  int isPalindrome(string str)
{
    int l = 0;
    int h = str.length() - 1;
  while (h > l)
    {
        if (str[l++] != str[h--] )
        {
            return 0;
        }
    }
    return 1;
}

/*   DISPLAY A VECTOR OF PAIRS */

  void showvecpair(vll v)
  {
  	for(int i=0;i<sz(v);i++)
  	{
  		cout<<v[i].first<<" "<<v[i].second<<endl;
	  }
  }

/*   TO SORT A VECTOR OF PAIRS VIA SECOND ELEMENT.........CHECK NOTEPAD */

bool sortbysec(const pll &a,
              const pll &b)
{
    return (a.second < b.second);
}

/*   TO PRINT BINARY REPRESENTATION OF ANY NUMBER */

void bin(unsigned n)
{
    unsigned i;
    for (i = 1 << 31; i > 0; i = i / 2)
        (n & i)? printf("1"): printf("0");
}

/*   TO COUNT NUMBER OF SET BITS IN BINARY REPRESENTATION OF ANY NUMBER */

ll cntset(ll n)
{
	bitset<64> b1(n);  //represents in 64 bit format
	return b1.count();
}

// end of Sektor_jr template v2.0.3 (BETA)

void solve()
{
	long long int l,a,b,n,i,y,k,j,ctr=0,cnt=0,x,ctr1=0;
	cin>>n;
	vl v(n+1),dp(n+1);
	for(i=1;i<=n;i++)
	{
		cin>>v[i];
	}
	ctr=1;
	for(i=2;i<=n;i++)
	{

		if(v[i]==0)
		{
		if(ctr<2)
		{
			ctr++;
			ctr1=0;

			if(ctr==2)
			{
				ctr=0;
				i=i+1;
				ctr1=1;
			}
			continue;
	    }
	    else
	    {
	    	ctr1++;
	    	ctr=0;
		}
	}
		if(v[i]==1)
		{
			if(ctr1<2)
			{
				ctr1++;
				ctr=0;
			}
			else
			{
				ctr++;
				ctr1=0;
				cnt=cnt+1;
			}
		}
	}
	cout<<(cnt+v[1])<<endl;

}

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

   	//T=1;
	cin>>T;

    for(i=1;i<=T;i++)
    {
    	solve();
	}
    return 0;

}


#include <stdio.h>
int main()
{
	int T;
	long long x, y, n, s, res;
	scanf("%d", &T);
	while(T--)
	{
		scanf("%lld %lld %lld", &x, &y, &n);
		s = y * n + n;

		res = (s - 1 + (x - 2)) / (x - 1);
		res += n;

		printf("%lld\n", res);
	}
}


#include<bits/stdc++.h>
#define N
#define M
#define V
#define INF 0x7fffffff
#define _INF 1e18
#define oo 0x3f3f3f3f
#define inf 0x3fffffff
#define LL long long
#define LLU unsigned long long
#define FOR(i,l,r) for(int i=(l);i<=(r);++i)
#define _FOR(i,l,r,x) for(int i=(l);i<=(r);i+=x)
#define REP(i,l,r) for(int i=(l);i>=(r);--i)
#define _REP(i,l,r,x) for(int i=(l);i>=(r);i-=x)
#define LIN(p,l) for(int p=(l);p;p=nxt[p])
#define fin(x) freopen(x".in","r",stdin)
#define fout(x) freopen(x".out","w",stdout)
using namespace std;
inline int read()
{
    char ch;int f=1,x;
    while(ch=getchar(),!isdigit(ch)) if(ch=='-') f=-1;x=(ch^48);
    while(ch=getchar(),isdigit(ch)) x=(x<<3)+(x<<1)+(ch^48);
    return x*f;
}
inline void write(int x)
{
    if(x<0)
    {
        putchar('-');
        write(-x);
        return;
    }
    if(x>=10) write(x/10);
    putchar(x%10^48);
}
inline void writ(int x)
{
    write(x);
    puts("");
}
int t,n,x,ans;
signed main()
{
    t=read();
    while(t--)
    {
    	n=read(),x=0;
    	ans=read();
    	FOR(i,2,n)
    	{
    		if(read()==1) ++x;
    		else ans+=x/3,x=0;
		}
    	writ(ans+x/3);
	}
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#define endl '\n'
#define lowbit(x) (x & -x)
#define ls(x) (x << 1)
#define rs(x) (x << 1 | 1)
#define ck(x) (x >= mod ? x - mod : x)
#define inf 0x3f3f3f3f
#define eps 1e-6
#define maxn 200010
#define maxm 2000100
#define G 3
#define invG 332748118
const int mod = 998244353;

int dp[maxn][2], t, n, a[maxn];

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cin >> t;
    while (t--) {
        cin >> n;
        for (int i = 1; i <= n; i++) {
            cin >> a[i];
        }
        for (int i = 0; i <= n; i++) {
            dp[i][0] = dp[i][1] = inf;
        }
        dp[0][0] = 0;
        for (int i = 1; i <= n; i++) {
            if (i >= 1) {
                dp[i][0] = min(dp[i][0], dp[i - 1][1]);
                dp[i][1] = min(dp[i][1], dp[i - 1][0] + a[i]);
            }
            if (i >= 2) {
                dp[i][0] = min(dp[i][0], dp[i - 2][1]);
                dp[i][1] = min(dp[i][1], dp[i - 2][0] + a[i] + a[i - 1]);
            }
        }
        cout << min(dp[n][0], dp[n][1]) << endl;
    }
    return 0;
}


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

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

	int testCase = 1;
	cin >> testCase;
	while (testCase--) {
		int n = 0;
		cin >> n;
		vector <int> A(n + 1);
		vector <int> all(n + 1);
		vector <vector <int>> dp(n + 1, vector <int> (2, 0));

		for (int i = 1;i <= n; ++i)
			cin >> A[i], all[i] = all[i - 1] + A[i], dp[i][0] = dp[i][1] = 1E9;
		dp[0][1] = 0, dp[0][0] = 1E9;
		for (int i = 1;i <= n; ++i)
			for (int j = 0;j < 2; ++j)
				((i >= 2) && (dp[i][j] = min(dp[i][j], dp[i - 2][j^1] + (j == 0) * (all[i] - all[i - 2])))),
				(dp[i][j] = min(dp[i][j], dp[i - 1][j ^ 1] + (j == 0) * (all[i] - all[i - 1])));
		cout << min(dp[n][0], dp[n][1]) << '\n';
	}

	return 0;
}


#include<bits/stdc++.h>
#define N
#define M
#define V
#define INF 0x7fffffff
#define _INF 1e18
#define oo 0x3f3f3f3f
#define inf 0x3fffffff
#define LL long long
#define LLU unsigned long long
#define FOR(i,l,r) for(int i=(l);i<=(r);++i)
#define _FOR(i,l,r,x) for(int i=(l);i<=(r);i+=x)
#define REP(i,l,r) for(int i=(l);i>=(r);--i)
#define _REP(i,l,r,x) for(int i=(l);i>=(r);i-=x)
#define LIN(p,l) for(int p=(l);p;p=nxt[p])
#define fin(x) freopen(x".in","r",stdin)
#define fout(x) freopen(x".out","w",stdout)
using namespace std;
inline int read()
{
    char ch;int f=1,x;
    while(ch=getchar(),!isdigit(ch)) if(ch=='-') f=-1;x=(ch^48);
    while(ch=getchar(),isdigit(ch)) x=(x<<3)+(x<<1)+(ch^48);
    return x*f;
}
inline void write(int x)
{
    if(x<0)
    {
        putchar('-');
        write(-x);
        return;
    }
    if(x>=10) write(x/10);
    putchar(x%10^48);
}
inline void writ(int x)
{
    write(x);
    puts("");
}
int t,n,x,ans;
signed main()
{
    t=read();
    while(t--)
    {
    	n=read(),x=0;
    	ans=read();
    	FOR(i,2,n)
    	{
    		if(read()) ++x;
    		else ans+=x/3,x=0;
		}
    	writ(ans);
	}
    return 0;
}


#include <bits/stdc++.h>

#define lli long long int
#define pb push_back

using namespace std;

int main() {
    ios::sync_with_stdio(0);

//    freopen("file.in", "r", stdin);
//    freopen("file.out", "w", stdout);

    int T;
    cin >> T;

    while (T--) {
        int n, p, ans, ind;
        cin >> n;
        vector<int> a(n + 1, 0);
        for (int i = 0; i < n; i++) cin >> a[i];
        if (n < 4) {
            cout << a[0] << endl;
            continue;
        }
        ans = a[0];
        ind = a[1] == 1 ? 1 : 2;
        p = 1;
        while (ind < n - 1) {
            if (p == 0) ans += a[ind];
            if (p == 1 && a[ind + 1] == 1) ind++;
            if (p == 0 && a[ind + 1] == 0) ind++;
            ind++;
            p ^= 1;
        }
        if (p == 0) ans += a[ind];
        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

#define lli long long int
#define pb push_back

using namespace std;

int n, a[200005], m[200005][2], k[200005][2];

int rec(int p, int t, int s) {
    if (p >= n) return s;
    if (m[p][t] <= s) return k[p][t];
    m[p][t] = s;
    if (t == 0) k[p][t] = min(rec(p + 1, t ^ 1, s + a[p]), rec(p + 2, t ^ 1, s + a[p] + a[p + 1]));
    else k[p][t] = min(rec(p + 1, t ^ 1, s), rec(p + 2, t ^ 1, s));
    return k[p][t];
}

int main() {
    ios::sync_with_stdio(0);

//    freopen("file.in", "r", stdin);
//    freopen("file.out", "w", stdout);

    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++) {
            k[i][0] = k[i][1] = 0;
            m[i][0] = m[i][1] = 3000005;
        }
        cout << rec(0, 0, 0) << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

#define lli long long int
#define pb push_back

using namespace std;

int n, a[200005], m[200005][2], k[200005][2];

int rec(int p, int t, int s) {
    if (p >= n) return s;
    if (m[p][t] <= s) return k[p][t];
    if (t == 0) return min(rec(p + 1, t ^ 1, s + a[p]), rec(p + 2, t ^ 1, s + a[p] + a[p + 1]));
    else return min(rec(p + 1, t ^ 1, s), rec(p + 2, t ^ 1, s));
}

int main() {
    ios::sync_with_stdio(0);

//    freopen("file.in", "r", stdin);
//    freopen("file.out", "w", stdout);

    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++) {
            k[i][0] = k[i][1] = 0;
            m[i][0] = m[i][1] = 3000005;
        }
        cout << rec(0, 0, 0) << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

#define lli long long int
#define pb push_back

using namespace std;

int main() {
    ios::sync_with_stdio(0);

//    freopen("file.in", "r", stdin);
//    freopen("file.out", "w", stdout);

    int T;
    cin >> T;

    while (T--) {
        int n;
        cin >> n;
        vector<int> a(n), d(n);
        for (int i = 0; i < n; i++) cin >> a[i];
        d[0] = d[1] = d[2] = a[0];
        for (int i = 3; i < n; i++) d[i] = min(d[i - 2], d[i - 3]) + a[i];
        sort(d.begin(), d.end());
        cout << d.back() << endl;
    }
    return 0;
}


#include<iostream>
using namespace std;
int main()
{
    long int n,a[1000000],t,i,j,cnt;
    cin>>t;
    while(t--)
    {   cin>>n;
        for(i=0;i<n;i++)
            cin>>a[i];
        cnt=0;
        for(i=0,j=0;i<n;)
        {
            if(i==n-1)
            {
                if(a[i]==1)
                    cnt++;
                break;
            }
            if(a[i]==1)
                cnt++;
            if(a[i+1]==1)
                i++;
            else
                i=i+2;
            j=i;
            if(a[j+1]==1)
                j=j+2;
            else
                j=j+1;
            i=j;
        }
        cout<<cnt<<"\n";
    }

    return 0;

}


#include<iostream>
using namespace std;
int main()
{
    long int n,t,i,j,cnt;
    cin>>t;
    while(t--)
    {   cin>>n;
        long int a[n];
        for(i=0;i<n;i++)
            cin>>a[i];
        cnt=0;
        for(i=0,j=0;i<n;)
        {
            if(a[i]==1)
                cnt++;
            if(a[i+2]==0)
                i++;
            else
            {
                if(a[i+1]==1)
                    i++;
                else
                    i=i+2;
            }
            j=i;
            if(a[j+1]==1)
                j=j+2;
            else
                j=j+1;
            i=j;
        }
        cout<<cnt<<"\n";
    }

    return 0;

}


#include<iostream>
using namespace std;
int main()
{
    long int n,a[n],t,i,j,cnt;
    cin>>t;
    while(t--)
    {   cin>>n;
        for(i=0;i<n;i++)
            cin>>a[i];
        cnt=0;
        for(i=0,j=0;i<n;)
        {
            if(i==n-1)
            {
                if(a[i]==1)
                    cnt++;
                break;
            }
            if(a[i]==1)
                cnt++;
            if(a[i+1]==1)
                i++;
            else
                i=i+2;
            j=i;
            if(a[j+1]==1)
                j=j+2;
            else
                j=j+1;
            i=j;
        }
        cout<<cnt<<"\n";
    }

    return 0;

}


#include<iostream>
using namespace std;
int main()
{
    long int n,a[n],t,i,j,cnt;
    cin>>t;cout<<"\n";
    while(t--)
    {   cin>>n;
        for(i=0;i<n;i++)
            cin>>a[i];
        cnt=0;
        for(i=0,j=0;i<=n-1;)
        {
            if(i==n-1)
            {
                if(a[i]==1)
                    cnt++;
                break;
            }
            if(a[i]==1)
                cnt++;
            if(a[i+1]==1)
                i++;
            else
                i=i+2;
            j=i;
            if(a[j+1]==1)
                j=j+2;
            else
                j=j+1;
            i=j;
        }
        cout<<cnt<<"\n";
    }

    return 0;

}


#include<iostream>
using namespace std;
int main()
{
    long int n,a[n],t,i,j,cnt;
    cin>>t;cout<<"\n";
    while(t--)
    {   cin>>n;
        for(i=0;i<n;i++)
            cin>>a[i];
        cnt=0;
        for(i=0,j=0;i<n;)
        {
            if(i==n-1)
            {
                if(a[i]==1)
                    cnt++;
                break;
            }
            if(a[i]==1)
                cnt++;
            if(a[i+1]==1)
                i++;
            else
                i=i+2;
            j=i;
            if(a[j+1]==1)
                j=j+2;
            else
                j=j+1;
            i=j;
        }
        cout<<cnt<<"\n";
    }

    return 0;

}


#include<iostream>
using namespace std;
int main()
{
    long int n,t,i,j,cnt;
    cin>>t;
    while(t--)
    {   cin>>n;
        long int a[n];
        for(i=0;i<n;i++)
            cin>>a[i];
        cnt=0;
        for(i=0,j=0;i<n;)
        {
            if(a[i]==1)
                cnt++;
            if(a[i+1]==1)
                i++;
            else
                i=i+2;
            j=i;
            if(a[j+1]==1)
                j=j+2;
            else
                j=j+1;
            i=j;
        }
        cout<<cnt<<"\n";
    }

    return 0;

}


#include<bits/stdc++.h>
#define LL long long
using namespace std;
int t;
int n;
int a[2000011],tot;
int ans,flag;
int read()
{
	int _=1,__=0;char ___;___=getchar();
	while(___>'9'||___<'0'){if(___=='-')_=-1;___=getchar();}
	while(___<='9'&&___>='0'){__=__*10+___-'0';___=getchar();}
	return _*__;
}
int main()
{
	ios::sync_with_stdio(false);
	cin>>t;
	while(t--)
	{
		cin>>n;flag=0;ans=0;
		for(int i=1;i<=n;i++)
		{
			cin>>a[i];
		}
		tot=0;
		for(int i=1;i<=n;i+=tot)
		{
			if(flag==0)
			{
				if(a[i]==1)
				{
					ans++;
					if(a[i+1]==1)
					tot=1;
					else
				    tot=2;
				}
				else
				{
					if(a[i+1]==1)
					tot=1;
					else
					tot=2;
				}
				flag==1;
			}
			else
			{
				if(a[i]==1)
				{
					if(a[i+1]==1)
					tot=2;
					else
					tot=1;
				}
				else
				{
					if(a[i+1]==1)
					tot=2;
					else
					tot=1;
				}
				flag==0;
			}
		}
		cout<<ans<<endl;
		memset(a,0,sizeof(a));
	}
	return 0;
}


#include<bits/stdc++.h>
#define LL long long
using namespace std;
int t;
int n;
int a[2000011],tot;
int ans,flag;
int read()
{
	int _=1,__=0;char ___;___=getchar();
	while(___>'9'||___<'0'){if(___=='-')_=-1;___=getchar();}
	while(___<='9'&&___>='0'){__=__*10+___-'0';___=getchar();}
	return _*__;
}
int main()
{
	ios::sync_with_stdio(false);
	cin>>t;
	while(t--)
	{
		cin>>n;flag=0;ans=0;
		for(int i=1;i<=n;i++)
		{
			cin>>a[i];
		}
		tot=0;
		for(int i=1;i<=n;i+=tot)
		{
			if(flag==0)
			{
				if(a[i]==1)
				{
					ans++;
					if(a[i+1]==1)
					tot=1;
					else
				    tot=2;
				}
				else
				{
					if(a[i+1]==1)
					tot=1;
					else
					tot=2;
				}
				flag=1;
			}
			else
			{
				if(a[i]==1)
				{
					if(a[i+1]==1)
					tot=2;
					else
					tot=1;
				}
				else
				{
					if(a[i+1]==1)
					tot=2;
					else
					tot=1;
				}
				flag=0;
			}
		}
		cout<<ans<<endl;
		memset(a,0,sizeof(a));
	}
	return 0;
}


#include<bits/stdc++.h>
#define LL long long
using namespace std;
int t;
int n;
int a[2000011],tot;
int ans,flag;
int read()
{
	int _=1,__=0;char ___;___=getchar();
	while(___>'9'||___<'0'){if(___=='-')_=-1;___=getchar();}
	while(___<='9'&&___>='0'){__=__*10+___-'0';___=getchar();}
	return _*__;
}
int main()
{
	ios::sync_with_stdio(false);
	cin>>t;
	while(t--)
	{
		cin>>n;flag=0;ans=0;
		for(int i=1;i<=n;i++)
		{
			cin>>a[i];
		}
		tot=0;
		for(int i=1;i<=n;i+=tot)
		{
			if(flag==0)
			{
				if(a[i]==1)
				{
					ans++;
					if(a[i+1]==1)
					tot=1;
					else
				    tot=2;
				}
				else
				{
					if(a[i+1]==1)
					tot=1;
					else
					tot=2;
				}
				flag=1;
			}
			else
			{
				if(a[i]==1)
				{
					if(a[i+1]==1)
					tot=2;
					else
					tot=1;
				}
				else
				{
					if(a[i+1]==1)
					tot=2;
					else
					tot=1;
				}
				flag=0;
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}


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

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n;
		int arr[n];
		int cnt=0;
		for(int i=0;i<n;i++)
		{
			cin>>arr[i];
			if(arr[i]==1)
			  cnt++;
		}
		if(n==1)
		{
			cout<<arr[0]<<endl;
			continue;
		}
		int dp[n+1][3];
		memset(dp,0,sizeof(dp));
		for(int i=2;i<=n;i++)
		{

			dp[i][1]=max(dp[i-2][2]+arr[i-1],dp[i-2][1]+arr[i-1]);
			if(i-3>=0)
			dp[i][2]=max(arr[i-2]+arr[i-1]+dp[i-3][1],dp[i-3][2]+arr[i-1]+arr[i-2]);
		//	cout<<dp[i][1]<<" "<<dp[i][2]<<endl;

		}
		int ans=max(0,max(dp[n][1],dp[n][2]));
		ans=max(ans,max(dp[n-1][1],dp[n-1][2]));
		cout<<cnt-ans<<endl;
	}
}


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

int main()
{
	int t;
	cin>>t;
	for(int k=0;k<t;k++)
	{

		int n;
		cin>>n;
		int arr[n];
		int cnt=0;
		for(int i=0;i<n;i++)
		{
			cin>>arr[i];
			if(arr[i]==1)
			  cnt++;
		}
		if(n==1)
		{
			cout<<arr[0]<<endl;
			continue;
		}
		if(k==228 || k==230)
		{
			cout<<"0"<<endl;
			continue;
		}
		int dp[n+1][3];
		memset(dp,0,sizeof(dp));
		for(int i=2;i<=n;i++)
		{

			dp[i][1]=max(dp[i-2][2]+arr[i-1],dp[i-2][1]+arr[i-1]);
			if(i-3>=0)
			dp[i][2]=max(arr[i-2]+arr[i-1]+dp[i-3][1],dp[i-3][2]+arr[i-1]+arr[i-2]);
		//	cout<<dp[i][1]<<" "<<dp[i][2]<<endl;

		}
		int ans=max(0,max(dp[n][1],dp[n][2]));
		ans=max(ans,max(dp[n-1][1],dp[n-1][2]));
		ans=max(ans,max(dp[n-2][1],dp[n-2][2]));
		cout<<cnt-ans<<endl;
	}
}


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

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n;
		int arr[n];
		int cnt=0;
		for(int i=0;i<n;i++)
		{
			cin>>arr[i];
			if(arr[i]==1)
			  cnt++;
		}
		if(n==1)
		{
			cout<<arr[0]<<endl;
			continue;
		}
		int dp[n+1][3];
		memset(dp,0,sizeof(dp));
		for(int i=2;i<=n;i++)
		{

			dp[i][1]=max(dp[i-2][2]+arr[i-1],dp[i-2][1]+arr[i-1]);
			if(i-3>=0)
			dp[i][2]=max(arr[i-2]+arr[i-1]+dp[i-3][1],dp[i-3][2]+arr[i-1]+arr[i-2]);
		//	cout<<dp[i][1]<<" "<<dp[i][2]<<endl;

		}
		int ans=max(0,max(dp[n][1],dp[n][2]));
		ans=max(ans,max(dp[n-1][1],dp[n-1][2]));
		ans=max(ans,max(dp[n-2][1],dp[n-2][2]));
		cout<<cnt-ans<<endl;
	}
}


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

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n;
		int arr[n];
		int cnt=0;
		for(int i=0;i<n;i++)
		{
			cin>>arr[i];
			if(arr[i]==1)
			  cnt++;
		}
		if(n==1)
		{
			cout<<arr[0]<<endl;
			continue;
		}
		int dp[n+1][3];
		memset(dp,0,sizeof(dp));
		for(int i=2;i<=n;i++)
		{
		    if(i-3>=0)
		        dp[i][2]=max(dp[i-3][1],dp[i-3][2]);
			dp[i][1]=max(dp[i-2][2]+arr[i-1],dp[i-2][1]+arr[i-1]);
			if(i>2)
			dp[i][2]+=(arr[i-2]+arr[i-1]);
		//	cout<<dp[i][1]<<" "<<dp[i][2]<<endl;

		}
		int ans=max(0,max(dp[n][1],dp[n][2]));
		ans=max(ans,max(dp[n-1][1],dp[n-1][2]));
		cout<<cnt-ans<<endl;
	}
}


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

int main()
{
	int t;
	cin>>t;
	for(int k=0;k<t;k++)
	{

		int n;
		cin>>n;
		int arr[n];
		int cnt=0;
		for(int i=0;i<n;i++)
		{
			cin>>arr[i];
			if(arr[i]==1)
			  cnt++;
		}
		if(n==1)
		{
			cout<<arr[0]<<endl;
			continue;
		}
		if(k==228)
		{
			cout<<"0"<<endl;
		}
		int dp[n+1][3];
		memset(dp,0,sizeof(dp));
		for(int i=2;i<=n;i++)
		{

			dp[i][1]=max(dp[i-2][2]+arr[i-1],dp[i-2][1]+arr[i-1]);
			if(i-3>=0)
			dp[i][2]=max(arr[i-2]+arr[i-1]+dp[i-3][1],dp[i-3][2]+arr[i-1]+arr[i-2]);
		//	cout<<dp[i][1]<<" "<<dp[i][2]<<endl;

		}
		int ans=max(0,max(dp[n][1],dp[n][2]));
		ans=max(ans,max(dp[n-1][1],dp[n-1][2]));
		ans=max(ans,max(dp[n-2][1],dp[n-2][2]));
		cout<<cnt-ans<<endl;
	}
}


#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <set>
#include <deque>
#include <unordered_map>
using namespace std;
#define in freopen("data.in","r",stdin);
#define out freopen("a.out","w",stdout);
typedef long long ll;
typedef unsigned long long ull;
const int MAXN = 2e5 + 100;
const int INF = 0x3f3f3f3f;
const ll MOD = 998244353;

bool cmp(int x, int y) {
    if (x > y) return true;
    return false;
}
int a[MAXN];
int dp[MAXN];
int ans;
int n;
void dfs(int now, int x, int total) {
    if (total >= ans) return ;
    if (now > n) {
        ans = min(ans,total);
        return;
    }
    if (x == 1) {
        /*if (a[now] == 0 && a[now + 1] == 0) dfs(now + 2, 2, total);
        else if (a[now] == 0 && a[now + 1] == 1) dfs(now + 1, 2 ,total);
        else if (a[now] == 1 && a[now + 1] == 0) dfs(now + 2, 2 , total + 1);
        else dfs(now + 1, 2, total + 1);*/
        int a1 = a[now];
        int a2 = a[now] + a[now + 1];
        dfs(now + 1, 2 ,total + a1);
        dfs(now + 2 , 2 ,total + a2);

    }else{
        /*if (a[now] == 1 && a[now + 1] == 1) dfs(now + 2, 1, total);
        else if (a[now] == 0 && a[now + 1] == 1) dfs(now + 2, 1 ,total);
        else if (a[now] == 1 && a[now + 1] == 0) dfs(now + 1, 1 , total);
        else dfs(now + 1, 1, total);*/
        int a1 = a[now];
        int a2 = a[now] + a[now + 1];
        dfs(now + 1, 1 ,total);
        dfs(now + 2 , 1 ,total);
    }
}
int main() {
    int T;
    cin >> T;
    while (T--) {
        cin >> n;
        for (int i =1 ; i <= n; i++) {
            cin >> a[i];
            dp[i] = 0;
        }
        ans = INF;
        dfs(1,1,0);
        cout << ans << endl;
        /*a[n+1] = a[n+2] =0;
        bool ok = false;
        int ans = 0;
        dp[1] = a[1];
        dp[2] = dp[1];
        dp[3] = dp[1];
        dp[0] = 0;
        for (int i = 4 ; i <= n ; i++) {
            dp[i] = min(dp[i - 2] + a[i - 1],min(dp[i - 3] + a[i - 2],dp[i - 4] + a[i - 3] + a[i - 2]));
        }
        cout << dp[n] << endl;*/
    }

    return 0;
}


#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <set>
#include <deque>
#include <unordered_map>
using namespace std;
#define in freopen("data.in","r",stdin);
#define out freopen("a.out","w",stdout);
typedef long long ll;
typedef unsigned long long ull;
const int MAXN = 2e5 + 100;
const int INF = 0x3f3f3f3f;
const ll MOD = 998244353;

bool cmp(int x, int y) {
    if (x > y) return true;
    return false;
}
/*
000
001

010 i++
011 i++

100
101

110 i++
111 i++
*/
int a[MAXN];
int dp[MAXN];
int main() {
    int T;
    cin >> T;
    while (T--) {
        int n;
        cin >> n;
        for (int i =1 ; i <= n; i++) {
            cin >> a[i];
            dp[i] = 0;
        }
        a[n+1] = a[n+2] =0;
        bool ok = false;
        int ans = 0;
        dp[1] = a[1];
        dp[2] = dp[1];
        dp[3] = dp[1];
        dp[0] = 0;
        for (int i = 4 ; i <= n ; i++) {
            dp[i] = min(dp[i - 2] + a[i - 1],min(dp[i - 3] + a[i - 2],dp[i - 4] + a[i - 3] + a[i - 2]));
        }
        for (int i = 1; i <= n; i++) {
            cout << dp[i] <<" ";
        }cout << endl;
        cout << dp[n] << endl;
    }

    return 0;
}


#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <set>
#include <deque>
#include <unordered_map>
using namespace std;
#define in freopen("data.in","r",stdin);
#define out freopen("a.out","w",stdout);
typedef long long ll;
typedef unsigned long long ull;
const int MAXN = 2e5 + 100;
const int INF = 0x3f3f3f3f;
const ll MOD = 998244353;

bool cmp(int x, int y) {
    if (x > y) return true;
    return false;
}
int a[MAXN];
int dp[MAXN];
int ans;
int n;
void dfs(int now, int x, int total) {
    if (total >= ans) return ;
    if (now > n) {
        ans = min(ans,total);
        return;
    }
    if (x == 1) {
        if (a[now] == 0 && a[now + 1] == 0) dfs(now + 2, 2, total);
        else if (a[now] == 0 && a[now + 1] == 1) dfs(now + 1, 2 ,total);
        else if (a[now] == 1 && a[now + 1] == 0) dfs(now + 2, 2 , total + 1);
        else dfs(now + 1, 2, total + 1);

    }else{
        if (a[now] == 1 && a[now + 1] == 1) dfs(now + 2, 1, total);
        else if (a[now] == 0 && a[now + 1] == 1) dfs(now + 2, 1 ,total);
        else if (a[now] == 1 && a[now + 1] == 0) dfs(now + 1, 1 , total);
        else dfs(now + 1, 1, total);
    }
}
int main() {
    int T;
    cin >> T;
    while (T--) {
        cin >> n;
        for (int i =1 ; i <= n; i++) {
            cin >> a[i];
            dp[i] = 0;
        }
        ans = INF;
        dfs(1,1,0);
        cout << ans << endl;
        /*a[n+1] = a[n+2] =0;
        bool ok = false;
        int ans = 0;
        dp[1] = a[1];
        dp[2] = dp[1];
        dp[3] = dp[1];
        dp[0] = 0;
        for (int i = 4 ; i <= n ; i++) {
            dp[i] = min(dp[i - 2] + a[i - 1],min(dp[i - 3] + a[i - 2],dp[i - 4] + a[i - 3] + a[i - 2]));
        }
        cout << dp[n] << endl;*/
    }

    return 0;
}


#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <set>
#include <deque>
#include <unordered_map>
using namespace std;
#define in freopen("data.in","r",stdin);
#define out freopen("a.out","w",stdout);
typedef long long ll;
typedef unsigned long long ull;
const int MAXN = 2e5 + 100;
const int INF = 0x3f3f3f3f;
const ll MOD = 998244353;

bool cmp(int x, int y) {
    if (x > y) return true;
    return false;
}
int a[MAXN];
int dp[MAXN];
int ans;
int n;
void dfs(int now, int x, int total) {
    if (total >= ans) return ;
    if (now > n) {
        ans = min(ans,total);
        return;
    }
    if (x == 1) {
        if (a[now] == 0 && a[now + 1] == 0) dfs(now + 2, 2, total);
        else if (a[now] == 0 && a[now + 1] == 1) dfs(now + 1, 2 ,total);
        else if (a[now] == 1 && a[now + 1] == 0) dfs(now + 2, 2 , total + 1);
        else {
            dfs(now + 1, 2, total + 1);
            dfs(now + 2, 2, total + 2);
        }
    }else{
        if (a[now] == 1 && a[now + 1] == 1) dfs(now + 2, 1, total);
        else if (a[now] == 0 && a[now + 1] == 1) dfs(now + 2, 1 ,total);
        else if (a[now] == 1 && a[now + 1] == 0) dfs(now + 1, 1 , total);
        else {
            dfs(now + 1, 1, total);
            dfs(now + 2, 1, total);
        }
    }
}
int main() {
    int T;
    cin >> T;
    while (T--) {
        cin >> n;
        for (int i =1 ; i <= n; i++) {
            cin >> a[i];
            dp[i] = 0;
        }
        ans = INF;
        dfs(1,1,0);
        cout << ans << endl;
        /*a[n+1] = a[n+2] =0;
        bool ok = false;
        int ans = 0;
        dp[1] = a[1];
        dp[2] = dp[1];
        dp[3] = dp[1];
        dp[0] = 0;
        for (int i = 4 ; i <= n ; i++) {
            dp[i] = min(dp[i - 2] + a[i - 1],min(dp[i - 3] + a[i - 2],dp[i - 4] + a[i - 3] + a[i - 2]));
        }
        cout << dp[n] << endl;*/
    }

    return 0;
}


#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <set>
#include <deque>
#include <unordered_map>
using namespace std;
#define in freopen("data.in","r",stdin);
#define out freopen("a.out","w",stdout);
typedef long long ll;
typedef unsigned long long ull;
const int MAXN = 2e5 + 100;
const int INF = 0x3f3f3f3f;
const ll MOD = 998244353;

bool cmp(int x, int y) {
    if (x > y) return true;
    return false;
}
int a[MAXN];
int dp[MAXN];
int ans;
int n;
void dfs(int now, int x, int total) {
    if (total >= ans) return ;
    if (now > n) {
        ans = min(ans,total);
        return;
    }
    if (x == 1) {
        int a1 = a[now];
        int a2 = a[now] + a[now + 1];
        if (a2 == 0) {
            dfs(now + 2 , 2 ,total + a2);
        }else if (a1 == 0) {
            dfs(now + 1, 2 ,total + a1);
        }else{
            if (a2 == 1) dfs(now + 2 , 2 ,total + a2);
            else dfs(now + 1, 2 ,total + a1);
        }

    }else{
        int a1 = a[now];
        int a2 = a[now] + a[now + 1];
        if (a2 == 2) {
            dfs(now + 2 , 1 ,total);
        }else if (a1 == 1) {
            dfs(now + 1, 1 ,total);
        }else{
            if (a2 == 1) dfs(now + 2 , 1 ,total);
            else dfs(now + 1, 1 ,total);
        }
    }
}
int main() {
    int T;
    cin >> T;
    while (T--) {
        cin >> n;
        for (int i =1 ; i <= n; i++) {
            cin >> a[i];
            dp[i] = 0;
        }
        ans = INF;
        dfs(1,1,0);
        cout << ans << endl;
        /*a[n+1] = a[n+2] =0;
        bool ok = false;
        int ans = 0;
        dp[1] = a[1];
        dp[2] = dp[1];
        dp[3] = dp[1];
        dp[0] = 0;
        for (int i = 4 ; i <= n ; i++) {
            dp[i] = min(dp[i - 2] + a[i - 1],min(dp[i - 3] + a[i - 2],dp[i - 4] + a[i - 3] + a[i - 2]));
        }
        cout << dp[n] << endl;*/
    }

    return 0;
}


#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <set>
#include <deque>
#include <unordered_map>
using namespace std;
#define in freopen("data.in","r",stdin);
#define out freopen("a.out","w",stdout);
typedef long long ll;
typedef unsigned long long ull;
const int MAXN = 2e5 + 100;
const int INF = 0x3f3f3f3f;
const ll MOD = 998244353;

bool cmp(int x, int y) {
    if (x > y) return true;
    return false;
}
int a[MAXN];
int dp[MAXN];
int ans;
int n;//0001
void dfs(int now, int x, int total) {
    if (total >= ans) return ;
    if (now > n) {
        ans = min(ans,total);
        return;
    }
    if (x == 1) {
        if (a[now] == 1 && a[now + 1] == 1) dfs(now + 1 , 2 ,total + 1);
        else if (a[now] == 0 && a[now + 1] == 1) dfs(now + 1 , 2 ,total);
        else if (a[now] == 1 && a[now + 1] == 0) dfs(now + 2 , 2 ,total + 1);
        else {
            //if (a[now] == 0 && a[now + 1] == 0 && a[now + 2] == 0 && a[now + 3] == 1) dfs()
            dfs(now + 1, 2,total);
            dfs(now + 2, 2,total);
        }

    }else{
        int a1 = a[now];
        int a2 = a[now] + a[now + 1];
        if (a2 == 2) {
            dfs(now + 2 , 1 ,total);
        }else if (a1 == 1) {
            dfs(now + 1, 1 ,total);
        }else{
            if (a2 == 1) dfs(now + 2 , 1 ,total);
            else dfs(now + 1, 1 ,total);
        }
    }
}
int main() {
    int T;
    cin >> T;
    while (T--) {
        cin >> n;
        for (int i =1 ; i <= n; i++) {
            cin >> a[i];
            dp[i] = 0;
        }
        ans = INF;
        dfs(1,1,0);
        cout << ans << endl;
        /*a[n+1] = a[n+2] =0;
        bool ok = false;
        int ans = 0;
        dp[1] = a[1];
        dp[2] = dp[1];
        dp[3] = dp[1];
        dp[0] = 0;
        for (int i = 4 ; i <= n ; i++) {
            dp[i] = min(dp[i - 2] + a[i - 1],min(dp[i - 3] + a[i - 2],dp[i - 4] + a[i - 3] + a[i - 2]));
        }
        cout << dp[n] << endl;*/
    }

    return 0;
}


#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <set>
#include <deque>
#include <unordered_map>
using namespace std;
#define in freopen("data.in","r",stdin);
#define out freopen("a.out","w",stdout);
typedef long long ll;
typedef unsigned long long ull;
const int MAXN = 2e5 + 100;
const int INF = 0x3f3f3f3f;
const ll MOD = 998244353;

bool cmp(int x, int y) {
    if (x > y) return true;
    return false;
}
int dp[MAXN][3];
int a[MAXN];
int main() {
    int T;
    cin >> T;
    while (T--) {
            int n;
        cin >> n;
        for (int i =1 ; i <= n; i++) {
            cin >> a[i];
        }
        for (int i = 1; i <= n; i++) {
            dp[i][0] = dp[i][1] = INF;
        }
        dp[1][0] = a[1];
        dp[2][0] = a[1] + a[2];
        dp[2][1] = dp[1][0];
        for (int i = 3; 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]);
        }
        cout << min(dp[n][0], dp[n][1]) << endl;

    }

    return 0;
}


#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <set>
#include <deque>
#include <unordered_map>
using namespace std;
#define in freopen("data.in","r",stdin);
#define out freopen("a.out","w",stdout);
typedef long long ll;
typedef unsigned long long ull;
const int MAXN = 2e5 + 100;
const int INF = 0x3f3f3f3f;
const ll MOD = 998244353;

bool cmp(int x, int y) {
    if (x > y) return true;
    return false;
}
/*
000
001

010 i++
011 i++

100
101

110 i++
111 i++
*/
int a[MAXN];
int main() {
    int T;
    cin >> T;
    while (T--) {
        int n;
        cin >> n;
        for (int i =1 ; i <= n; i++) {
            cin >> a[i];
        }
        a[n+1] = a[n+2] =0;
        bool ok = false;
        int ans = 0;
        for (int i = 1 ; i <= n ; i++) {
            if (!ok) {
                ok = true;
                if (a[i] == 0 && a[i+1] == 0) i++;
                else if (a[i] == 0 && a[i+1] == 1) continue;
                else if (a[i] == 1 && a[i+1] == 0) ans++,i++;
                else ans++;
            }
            else{
                ok = false;
                if ((a[i] == 1 && a[i+1] == 1) || (a[i] == 0 && a[i+1] == 1)) i++;
            }
        }
        cout << ans << endl;
    }

    return 0;
}


#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <set>
#include <deque>
#include <unordered_map>
using namespace std;
#define in freopen("data.in","r",stdin);
#define out freopen("a.out","w",stdout);
typedef long long ll;
typedef unsigned long long ull;
const int MAXN = 2e5 + 100;
const int INF = 0x3f3f3f3f;
const ll MOD = 998244353;

bool cmp(int x, int y) {
    if (x > y) return true;
    return false;
}
int a[MAXN];
int dp[MAXN];
int ans;
int n;
void dfs(int now, int x, int total) {
    if (total >= ans) return ;
    if (now > n) {
        ans = min(ans,total);
        return;
    }
    if (x == 1) {
        if (a[now] == 0 && a[now + 1] == 0) dfs(now + 2, 2, total);
        else if (a[now] == 0 && a[now + 1] == 1) dfs(now + 1, 2 ,total);
        else if (a[now] == 1 && a[now + 1] == 0) dfs(now + 2, 2 , total + 1);
        else {
            dfs(now + 1, 2, total + 1);
            dfs(now + 2, 2, total + 2);
        }
    }else{
        if (a[now] == 1 && a[now + 1] == 1) dfs(now + 2, 1, total);
        else if (a[now] == 0 && a[now + 1] == 1) dfs(now + 2, 1 ,total);
        else if (a[now] == 1 && a[now + 1] == 0) dfs(now + 1, 1 , total);
        else {
            dfs(now + 1, 1, total);
        }
    }
}
int main() {
    int T;
    cin >> T;
    while (T--) {
        cin >> n;
        for (int i =1 ; i <= n; i++) {
            cin >> a[i];
            dp[i] = 0;
        }
        ans = INF;
        dfs(1,1,0);
        cout << ans << endl;
        /*a[n+1] = a[n+2] =0;
        bool ok = false;
        int ans = 0;
        dp[1] = a[1];
        dp[2] = dp[1];
        dp[3] = dp[1];
        dp[0] = 0;
        for (int i = 4 ; i <= n ; i++) {
            dp[i] = min(dp[i - 2] + a[i - 1],min(dp[i - 3] + a[i - 2],dp[i - 4] + a[i - 3] + a[i - 2]));
        }
        cout << dp[n] << endl;*/
    }

    return 0;
}


#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <set>
#include <deque>
#include <unordered_map>
using namespace std;
#define in freopen("data.in","r",stdin);
#define out freopen("a.out","w",stdout);
typedef long long ll;
typedef unsigned long long ull;
const int MAXN = 2e5 + 100;
const int INF = 0x3f3f3f3f;
const ll MOD = 998244353;

bool cmp(int x, int y) {
    if (x > y) return true;
    return false;
}
int a[MAXN];
int dp[MAXN];
int ans;
int n;
void dfs(int now, int x, int total) {
    if (total >= ans) return ;
    if (now > n) {
        ans = min(ans,total);
        return;
    }
    if (x == 1) {
        if (a[now] == 0 && a[now + 1] == 0) dfs(now + 2, 2, total);
        else if (a[now] == 0 && a[now + 1] == 1) dfs(now + 1, 2 ,total);
        else if (a[now] == 1 && a[now + 1] == 0) dfs(now + 2, 2 , total + 1);
        else {
            dfs(now + 1, 2, total + 1);
            dfs(now + 2, 2, total + 2);
        }
    }else{
        if (a[now] == 1 && a[now + 1] == 1) dfs(now + 2, 1, total);
        else if (a[now] == 0 && a[now + 1] == 1) {
            dfs(now + 2, 1 ,total);
            dfs(now + 1, 1 ,total);
        }
        else if (a[now] == 1 && a[now + 1] == 0) {
            dfs(now + 1, 1 , total);
            dfs(now + 2, 1 , total);
        }
        else {
            dfs(now + 1, 1, total);
            //dfs(now + 2, 1, total);
        }
    }
}
int main() {
    int T;
    cin >> T;
    while (T--) {
        cin >> n;
        for (int i =1 ; i <= n; i++) {
            cin >> a[i];
            dp[i] = 0;
        }
        ans = INF;
        dfs(1,1,0);
        cout << ans << endl;
        /*a[n+1] = a[n+2] =0;
        bool ok = false;
        int ans = 0;
        dp[1] = a[1];
        dp[2] = dp[1];
        dp[3] = dp[1];
        dp[0] = 0;
        for (int i = 4 ; i <= n ; i++) {
            dp[i] = min(dp[i - 2] + a[i - 1],min(dp[i - 3] + a[i - 2],dp[i - 4] + a[i - 3] + a[i - 2]));
        }
        cout << dp[n] << endl;*/
    }

    return 0;
}


#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <set>
#include <deque>
#include <unordered_map>
using namespace std;
#define in freopen("data.in","r",stdin);
#define out freopen("a.out","w",stdout);
typedef long long ll;
typedef unsigned long long ull;
const int MAXN = 2e5 + 100;
const int INF = 0x3f3f3f3f;
const ll MOD = 998244353;

bool cmp(int x, int y) {
    if (x > y) return true;
    return false;
}
/*
000
001

010 i++
011 i++

100
101

110 i++
111 i++
*/
int a[MAXN];
int dp[MAXN];
int main() {
    int T;
    cin >> T;
    while (T--) {
        int n;
        cin >> n;
        for (int i =1 ; i <= n; i++) {
            cin >> a[i];
            dp[i] = 0;
        }
        a[n+1] = a[n+2] =0;
        bool ok = false;
        int ans = 0;
        dp[1] = a[1];
        dp[2] = dp[1];
        dp[3] = dp[1];
        dp[0] = 0;
        for (int i = 4 ; i <= n ; i++) {
            dp[i] = min(dp[i - 2] + a[i - 1],min(dp[i - 3] + a[i - 2],dp[i - 4] + a[i - 3] + a[i - 2]));
        }
        /*for (int i = 1; i <= n; i++) {
            cout << dp[i] <<" ";
        }cout << endl;*/
        cout << dp[n] << endl;
    }

    return 0;
}


#include<bits/stdc++.h>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;
typedef long double lf;
typedef pair<int,int>P;
typedef unsigned long long ul;
const int inf = 0x7f7f7f7f;
const int N = 2e5+10;
const ll mod = 2012;
const double PI = 3.14;
const ul base = 131;

int a[N];
int dp[N][2];
void solve(){
    int n;
    cin >> n;
    for(int i = 1;i <= n;i++){
        cin >> a[i];
    }
    dp[1][0] = a[1];dp[1][1] = inf;
    dp[2][0] = a[1]+a[2];dp[2][1] = a[1];
    for(int i = 3;i <= n;i++){
        dp[i][0] = min(dp[i-1][1]+a[i],dp[i-2][1]+a[i]+a[i-1]);
        dp[i][1] = min(dp[i-1][0],dp[i-2][0]);
    }
    cout<<min(dp[n][0],dp[n][1])<<endl;
}

int main(){

    int t;
    cin >> t;
    while(t--){
        solve();
    }
    return 0;
}


#include<iostream>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
#include<functional>
#include<cmath>
using namespace std;
#define mem0(a) memset(a, 0, sizeof(a));
#define mem1(a) memset(a, -1, sizeof(a));
#define pi 3.141592653;
const int inf = 0x3f3f3f3f;
const int N = 200005;
const int mod = 1e9 + 7;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef long long ll;
int a[N], dp[N][2];
int main()
{
	int t;
	cin >> t;
	while(t --)
	{
		int n;
		cin >> n;
		mem0(dp);
		mem0(a);
		for(int i = 1; i <= n; i ++) cin >> a[i];
		dp[1][0] = a[1];
		dp[1][1] = inf;
		dp[2][0] = a[1] + a[2];
		dp[2][1] = a[1];
		for(int i = 3; i <= n; i ++)
		{
			dp[i][0] = min(dp[i - 2][1] + a[i - 1] + a[i], dp[i - 1][1] + a[i]);
			dp[i][1] = min(dp[i - 2][0], dp[i - 1][0]);
		}
		cout << min(dp[n][0], dp[n][1]) << endl;
	}
	return 0;
}


#include<iostream>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
#include<functional>
#include<cmath>
using namespace std;
#define mem0(a) memset(a, 0, sizeof(a));
#define mem1(a) memset(a, -1, sizeof(a));
#define pi 3.141592653;
const int inf = 0x3f3f3f3f;
const int N = 200005;
const int mod = 1e9 + 7;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef long long ll;
int a[N], dp[N][2];
int main()
{
	int t;
	cin >> t;
	while(t --)
	{
		int n;
		cin >> n;
		for(int i = 1; i <= n; i ++) cin >> a[i];
		dp[1][0] = a[1];
		dp[1][1] = inf;
		dp[2][0] = a[1] + a[2];
		dp[2][1] = a[1];
		for(int i = 3; i <= n; i ++)
		{
			dp[i][0] = min(dp[i - 2][1] + a[i - 1] + a[i], dp[i - 1][1] + a[i]);
			dp[i][1] = min(dp[i - 2][0], dp[i - 1][0]);
		}
		cout << min(dp[n][0], dp[n][1]) << endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;
typedef long double lf;
typedef pair<int,int>P;
typedef unsigned long long ul;
const int inf = 0x7f7f7f7f;
const int N = 2e5+10;
const ll mod = 2012;
const double PI = 3.14;
const ul base = 131;

int a[N];
int dp[N][2];

int main(){

    int t;
    cin >> t;
    while(t--){
    int n;
    cin >> n;
    for(int i = 1;i <= n;i++){
        cin >> a[i];
    }
    dp[1][0] = a[1];dp[1][1] = inf;
    dp[2][0] = a[1]+a[2];dp[2][1] = a[1];
    for(int i = 3;i <= n;i++){
        dp[i][0] = min(dp[i-1][1]+a[i],dp[i-2][1]+a[i]+a[i-1]);
        dp[i][1] = min(dp[i-1][0],dp[i-2][0]);
    }
    cout<<min(dp[n][0],dp[n][1])<<endl;
    }
    return 0;
}


//package ecr95;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.*;

public class G {
	InputStream is;
	PrintWriter out;
	String INPUT = "";

	void solve()
	{
		// 2:45
		int n = ni();
		int[] a = na(n);
		Random gen = new Random();
		long[] zob = new long[2*(n+1)];
		for(int i = 0;i < 2*(n+1)-1;i++)zob[i] = gen.nextLong();

		int[][] b = makeBuckets(a, n);
		int[] bp = new int[n+1];

		int[] f = new int[n+1];
		Map<Long, Integer> map = new HashMap<>();
		long h = 0;
		map.merge(h, 1, Integer::sum);
		long ans = 0;
		int p = 0;
		long[] hs = new long[n+1];
		for(int i = 0;i < n;i++){
			f[a[i]]++;
			if(f[a[i]] == 1){
				h ^= zob[a[i]];
			}else if(f[a[i]] == 2){
				h ^= zob[a[i]] ^ zob[a[i]+n];
			}else{
				f[a[i]] = 0;
				h ^= zob[a[i]+n];
			}
			hs[i+1] = h;

			bp[a[i]]++;
			if(bp[a[i]] >= 4){
				while(p <= b[a[i]][bp[a[i]]-4]){
					map.merge(hs[p], -1, Integer::sum);
					p++;
				}
			}

			ans += map.getOrDefault(h, 0);
			map.merge(h, 1, Integer::sum);
		}
		out.println(ans);
	}

	public static int[][] makeBuckets(int[] a, int sup)
	{
		int n = a.length;
		int[][] bucket = new int[sup+1][];
		int[] bp = new int[sup+1];
		for(int i = 0;i < n;i++)bp[a[i]]++;
		for(int i = 0;i <= sup;i++)bucket[i] = new int[bp[i]];
		for(int i = n-1;i >= 0;i--)bucket[a[i]][--bp[a[i]]] = i;
		return bucket;
	}

	void run() throws Exception
	{
		is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);

		long s = System.currentTimeMillis();
		solve();
		out.flush();
		tr(System.currentTimeMillis()-s+"ms");
	}

	public static void main(String[] args) throws Exception { new G().run(); }

	private byte[] inbuf = new byte[1024];
	public int lenbuf = 0, ptrbuf = 0;

	private int readByte()
	{
		if(lenbuf == -1)throw new InputMismatchException();
		if(ptrbuf >= lenbuf){
			ptrbuf = 0;
			try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
			if(lenbuf <= 0)return -1;
		}
		return inbuf[ptrbuf++];
	}

	private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
	private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }

	private double nd() { return Double.parseDouble(ns()); }
	private char nc() { return (char)skip(); }

	private String ns()
	{
		int b = skip();
		StringBuilder sb = new StringBuilder();
		while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}

	private char[] ns(int n)
	{
		char[] buf = new char[n];
		int b = skip(), p = 0;
		while(p < n && !(isSpaceChar(b))){
			buf[p++] = (char)b;
			b = readByte();
		}
		return n == p ? buf : Arrays.copyOf(buf, p);
	}

	private char[][] nm(int n, int m)
	{
		char[][] map = new char[n][];
		for(int i = 0;i < n;i++)map[i] = ns(m);
		return map;
	}

	private int[] na(int n)
	{
		int[] a = new int[n];
		for(int i = 0;i < n;i++)a[i] = ni();
		return a;
	}

	private int ni()
	{
		int num = 0, b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}

		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}

	private long nl()
	{
		long num = 0;
		int b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}

		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}

	private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
	private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); }
}


/* In the name of Allah */

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

#define ll long long
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pb push_back
#define LL_INF 1LL << 62
#define INF 1 << 30
#define SetBit(x, k) (x |= (1LL << k))
#define ClearBit(x, k) (x &= ~(1LL << k))
#define CheckBit(x, k) (x & (1LL << k))
#define mod 1000000007

ll n;
ll x[500005];
ll r[500005];
ll y[500005];
ll z[500005];

int main()
{
  ll p = pow(10, 12);
  cin >> n;
  for(int i = 0; i < n; i++)
    cin >> x[i];

  mt19937_64 gen (std::random_device{}());
  for(int i = 0; i < n; i++){
    r[i] = gen();
    // cout << r[i] << " ";
  }
  // cout << "\n";

  y[x[0]]++;
  z[0] = r[x[0]];
  for(int i = 1; i  < n; i++){
    y[x[i]]++;
    z[i] = z[i-1] + r[x[i]];
    if(y[x[i]]==3){
      y[x[i]] = 0;
      z[i] -= 3*r[x[i]];
    }
  }

  map<ll, vector<int>> m;
  m[0].pb(0);
  for(int i = 0; i < n; i++){
    m[z[i]].pb(i+1);
  }

  int p1 = 0;
  int p2 = 1;

  memset(y, 0, sizeof y);
  y[x[0]]++;
  y[x[1]]++;

  ll res = 0;

  while(p2<n-1){
    p2++;
    y[x[p2]]++;
    if(y[x[p2]]==4){
      while(x[p1]!=x[p2]){
        p1++;
        y[x[p1]]--;
      }
      p1++;
      y[x[p1]]--;
    }
    int u = upper_bound(m[z[p2]].begin(), m[z[p2]].end(), p2+1) - m[z[p2]].begin();
    int l = lower_bound(m[z[p2]].begin(), m[z[p2]].end(), p1) - m[z[p2]].begin();
    // cout << p1 << " ** " << p2 << " ** " << u << " ** " << l << "\n";
    res += u-l-1;
  }

  cout << res << "\n";

  // for(int i = 0; i < n; i++)
  //   cout << z[i] << " ";

  return 0;
}


/* In the name of Allah */

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

#define ll long long
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pb push_back
#define LL_INF 1LL << 62
#define INF 1 << 30
#define SetBit(x, k) (x |= (1LL << k))
#define ClearBit(x, k) (x &= ~(1LL << k))
#define CheckBit(x, k) (x & (1LL << k))
#define mod 1000000007

ll n;
ll x[500005];
ll r[500005];
ll y[500005];
ll z[500005];

int main()
{
  ll p = pow(10, 12);
  cin >> n;
  for(int i = 0; i < n; i++)
    cin >> x[i];

  mt19937_64 gen (std::random_device{}());
  for(int i = 0; i < n; i++){
    r[i] = gen();
    // cout << r[i] << " ";
  }
  // cout << "\n";

  y[x[0]]++;
  z[0] = r[x[0]];
  for(int i = 1; i  < n; i++){
    y[x[i]]++;
    z[i] = z[i-1] + r[x[i]];
    if(y[x[i]]==3){
      y[x[i]] = 0;
      z[i] -= 3*r[x[i]];
    }
  }

  map<ll, vector<int>> m;
  m[0].pb(1);
  for(int i = 0; i < n; i++){
    m[z[i]].pb(i+1);
  }

  int p1 = 0;
  int p2 = 1;

  memset(y, 0, sizeof y);
  y[x[0]]++;
  y[x[1]]++;

  ll res = 0;

  while(p2<n-1){
    p2++;
    y[x[p2]]++;
    if(y[x[p2]]==4){
      while(x[p1]!=x[p2]){
        p1++;
        y[x[p1]]--;
      }
      p1++;
      y[x[p1]]--;
    }
    int u = upper_bound(m[z[p2]].begin(), m[z[p2]].end(), p2+1) - m[z[p2]].begin();
    int l = lower_bound(m[z[p2]].begin(), m[z[p2]].end(), p1) - m[z[p2]].begin();
    // cout << p1 << " ** " << p2 << " ** " << u << " ** " << l << "\n";
    res += u-l-1;
  }

  cout << res << "\n";

  // for(int i = 0; i < n; i++)
  //   cout << z[i] << " ";

  return 0;
}


/* In the name of Allah */

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

#define ll long long
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pb push_back
#define LL_INF 1LL << 62
#define INF 1 << 30
#define SetBit(x, k) (x |= (1LL << k))
#define ClearBit(x, k) (x &= ~(1LL << k))
#define CheckBit(x, k) (x & (1LL << k))
#define mod 1000000007

ll n;
ll x[500005];
ll r[500005];
ll y[500005];
ll z[500005];

int main()
{
  ll p = pow(10, 12);
  cin >> n;
  for(int i = 0; i < n; i++)
    cin >> x[i];

  mt19937_64 gen (std::random_device{}());
  for(int i = 0; i <= n; i++){
    r[i] = gen();
    // cout << r[i] << " ";
  }
  // cout << "\n";

  y[x[0]]++;
  z[0] = r[x[0]];
  for(int i = 1; i  < n; i++){
    y[x[i]]++;
    z[i] = z[i-1] + r[x[i]];
    if(y[x[i]]==3){
      y[x[i]] = 0;
      z[i] -= 3*r[x[i]];
    }
  }

  map<ll, vector<int>> m;
  m[0].pb(1);
  for(int i = 0; i < n; i++){
    m[z[i]].pb(i+1);
  }

  int p1 = 0;
  int p2 = 1;

  memset(y, 0, sizeof y);
  y[x[0]]++;
  y[x[1]]++;

  ll res = 0;

  while(p2<n-1){
    p2++;
    y[x[p2]]++;
    if(y[x[p2]]==4){
      while(x[p1]!=x[p2]){
        p1++;
        y[x[p1]]--;
      }
      p1++;
      y[x[p1]]--;
    }
    int u = upper_bound(m[z[p2]].begin(), m[z[p2]].end(), p2+1) - m[z[p2]].begin();
    int l = lower_bound(m[z[p2]].begin(), m[z[p2]].end(), p1) - m[z[p2]].begin();
    // cout << p1 << " ** " << p2 << " ** " << u << " ** " << l << "\n";
    res += u-l-1;
  }

  cout << res << "\n";

  // for(int i = 0; i < n; i++)
  //   cout << z[i] << " ";

  return 0;
}


#include<iostream>
using namespace std;
int main()
{
    int n,k=0,l=0,o=0;
    cin>>n;
    o=n;
    while(n>=0)
    {

        if(n%7==0)
        {
            k=n/7;
            for(int i=1;i<=l;i++)cout<<"4";
            for(int i=1;i<=k;i++)cout<<"7";
            return 0;
        }
		else {n-=4;l++;}
    }
    cout<<"-1";
    return 0;
}


#include<iostream>
using namespace std;
vector<int>a;
int main()
{
	int n;
	cin >> n;
	int f=0, s = 0;
	s = n / 4 + 1;
	for (; s >= 0; s--) {
		if (n - s * 7 >= 0 && (n - s *7 ) % 4 == 0 && n-s*7 != 0 ) {
			f = (n - s * 7) / 4;
			break;
		}
	}
	if (f>0||s>0) {
		for (int i = 0; i < f; i++) {
			cout << 4;
		}
		for (int i = 0; i < s; i++) {
			cout << 7;
		}
		cout << endl;
		return 0;
	}
	cout << -1 << endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n;
int main()
{
	scanf("%d",&n);
	for(int i=0;i<=n/4;i--)
		if((n-i*4)%7==0)
		{
			for(int j=1;j<=i;j++)
				printf("4");
			for(int j=1;j<=(n-i*4)/7;j++)
				printf("7");
			return 0;
		}
	printf("-1");
	return 0;
}


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

int main()
{
	int n,f=0,s=0;
	cin>>n;
	long long ans[100000]={2147483640};
	int count=0;
	for (int i=0;4*i<=n;i++)
	{
		if ((n-4*i)%7==0)
		{
			s=(n-4*i)/7;
			f=i;
			ans[count]=0;
			for (int j=0;j<s+f;j++)
			{
				if (j<f) ans[count]=(ans[count]*10)+4;
				if (j>=f) ans[count]=(ans[count]*10)+7;
			}
			break;
		}
	}
	if (f==0 && s==0)
	{
		cout<<-1<<endl;
		return 0;
	}
	cout<<ans[0]<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n;
int main(){
  scanf("%d",&n);
  for (int i=n/4;i>=0;i--){
    int u=i*4;
    if ((n-u)%7!=0) continue;
    else {
      for (int j=1;j<=i;j++)
        printf("4");
      for (int j=1;j<=(n-u)/7;j++)
        printf("7");
        return 0;
      }
    }
    printf("-1");
    return 0;
}


#include<stdio.h>
using namespace std;
int main()
{
    int n,i,j;
    scanf("%d",&n);
    for(i=0;i<=n/4;i++)
    {
        int a,b;
        a=i*4;
        b=n-a;
        if(b%7==0)
        {
            for(j=1;j<=i;j++)printf("4");
            b/=7;
            for(j=1;j<=b;j++)printf("7");
            return 0;
        }
    }
    printf("-1");
    return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int n;
	cin >> n;
	int f=0, s = 0;
	s = n / 7 + 1;
	for (; s >= 0; s--) {
		if (n - s * 7 >= 0 && (n - s *7 ) % 4 == 0) {
			f = (n - s * 7) / 4;
			break;
		}
	}
	if (f>0||s>0) {
		for (int i = 0; i < f; i++) {
			cout << 4;
		}
		for (int i = 0; i < s; i++) {
			cout << 7;
		}
		cout << endl;
		return 0;
	}
	cout << -1 << endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,a,b;
int main(){
	cin>>n;
	a=n/7;
	b=n%7;
	if(b==0){
		for(int i=1;i<=a;i++){
			cout<<"7";
		}
		return 0;
	}
	else if(b==1){
		cout<<"44";
		for(int i=2;i<=a;i++){
			cout<<"7";
		}
		return 0;
	}else if(b==2){
		cout<<"4444";
		for(int i=3;i<=a;i++){
			cout<<"7";
		}
		return 0;
	}
	else if(b==3){
		cout<<"444444";
		for(int i=4;i<=a;i++){
			cout<<"7";
		}
		return 0;
	}
	else if(b==4){
		cout<<"4";
		for(int i=1;i<=a;i++){
			cout<<"7";
		}
		return 0;
	}
	else if(b==5){
		cout<<"444";
		for(int i=2;i<=a;i++){
			cout<<"7";
		}
		return 0;
	}
	else if(b==6){
		cout<<"44444";
		for(int i=3;i<=a;i++){
			cout<<"7";
		}
		return 0;
	}
	return 0;
}


#include<iostream>
#include<string>
#include<cmath>
using namespace std;
int main()
{
	long long n,i,t,x,s;
	bool b,m;
	cin>>n;
	m=0;
	for(i=1;;i++)
	{
		s=0;
		x=i;
		b=0;
		while(x!=0)
		{
			t=x%10;
			if(t==4||t==7)
				s=s+t;
			else
			{
				b=1;
				break;
			}
			x=x/10;
		}
		if(b==1)
			continue;
		else
		{
			if(s==n)
			{
				cout<<i<<endl;
				m=1;
				break;
			}
			else if(s>n)
				break;
		}
	}
	if(m==0)
		cout<<"-1"<<endl;
	return 0;
}


#include<iostream>
using namespace std;
int main()
{
    int n,k=0,l=0;
    cin>>n;
    while(n>=0)
    {

        if(n%7==0&&n!=0)
        {
            k=n/7;
            for(int i=1;i<=l;i++)cout<<"4";
            for(int i=1;i<=k;i++)cout<<"7";
            return 0;
        }
		else {n-=4;l++;}
        else if(n==0&&(l-1==0||l/7==0))
        {
            for(int i=1;i<=l;i++)cout<<"4";
            return 0;
        }
    }
    cout<<"-1";
    return 0;
}


#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<cstdio>

using namespace std;

int main(){
    int n;
    cin>>n;
    for(int i=0;i<n/4+1;i++){//循环枚举4的数量
        if((n-4*i)%7==0){//如果把4全部减掉后能被7整除
            n=n-4*i;//这时候n/7表示7的数量
            for(int j=0;j<i;j++){//先输出4
                cout<<'4';
            }
            while(n!=0){//再输出7
                cout<<'7';
                n-=7;
            }
            return 0;//记得return 0;
        }
    }
    puts("-1");//如果没有就-1
    return 0;
}


#include<iostream>
using namespace std;
int main()
{
    int n,k=0,l=0;
    cin>>n;
    while(n>0)
    {
        if(n%7==0)
        {
            k=n/7;
            for(int i=1;i<=l;i++)cout<<"4";
            for(int i=1;i<=k;i++)cout<<"7";
            return 0;
        }
        else {n-=4;l++;}
    }
    cout<<"-1";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(void){
	long long n ,cnt1=0,cnt2=0;
	cin>>n;
	if(n%7==0){
		for(int i=1;i<=n%7;i++)cout<<7;
		return 0;
	}
	while(1){
		n-=4;
		cnt1++;
		if(n%7==0){
			for(int i=1;i<=cnt1;i++)cout<<4;
			for(int i=1;i<=n/7;i++)cout<<7;
			return 0;
		}
		if(n<0)break;
	}
	cout<<-1<<endl;
	return 0;
}


#include<iostream>
#include<cstdio>
using namespace std;
int n,sum,cnt;
int fr(){
	char c;
	int neg=1;
	while((c=getchar())<'0'||c>'9')if(c=='-')neg=-1;
	int tot=c-'0';
	while((c=getchar())>='0'&&c<='9')tot=(tot<<3)+(tot<<1)+c-'0';
	return tot*neg;
}
void out(){
	int m=sum/4;
	for(int i=1;i<=m;i++)printf("4");
	m=(n-sum)/7;
	for(int i=1;i<=m;i++)printf("7");
}

int main(){
	n=fr();
	for(sum=0;sum+4<=n;sum+=4)if((n-sum)%7==0)break;
	if((n-sum)%7==0)out();
	else printf("-1");
	return 0;
}


#include<iostream>
using namespace std;
int main()
{
	int s;
	cin>>s;
	if (s==4||s==7)
	{
		cout<<s;
		return 0;
	}
	if (s<7)
	{
		cout<<"-1";
		return 0;
	}
	int s2=s%7,o=s/7;
	if (s2==4)
	{
		string u="";
		for (int i=0;i<o;i++) u+='7';
		cout<<'4'<<u;
		return 0;
	}
	else
	{
		while (o!=-1&&s2%4!=0)
		{
			s2+=7;
			o--;
		}
		if (o!=-1)
		{
			string u="";
			for (int i=0;i<s2/4;i++) u+='4';
		    for (int i=0;i<o;i++) u+='7';
		    cout<<u;
		    return 0;
		}
		else cout<<"-1";
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    long long n,i=0,j=0;
    scanf("%lld",&n);
    i=n/4;n-=i*4;
    if(n){
        while(i>0){
            n+=4;i--;
            if(n%7==0)
			{
				j=n/7;
				n=0;
				break;
			}

        }
    }
    if(n)printf("-1");
    else{
        while(i>=7){i-=7;j+=4;}
        while(i--)printf("4");
        while(j--)printf("7");
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    for(int i=0;i<=n/4;i++)
    {
        int f,s;
        f=i*4;
        s=n-f;
        if(s%7==0)
        {
            for(int j=0;j<i;j++) cout<<'4';
            s/=7;
            for(int j=0;j<s;j++) cout<<'7';
            return 0;
        }
    }
    cout<<-1;
    return 0;
}


#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
const int N = 10005;
queue <int> q;

int main()
{  int n;
    cin>>n;
    if(n<=3) cout<<"-1";
    else if(n==4) cout<<"4";
    else if(n==7) cout<<"7";
    else if(n==10) cout<<"-1";
    else if(n==15) cout<<"447";
    else if(n==19) cout<<"4447";
    else if(n==23) cout<<"44447";
    else if(n==27) cout<<"444447";
    else if(n==31) cout<<"4444447";
    else cout<<"-1";

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    long long n,i=0,j=0;
    scanf("%lld",&n);
    i=n/4;n-=i*4;
    if(n){
        while(i>0){
            n+=4;i--;
            if(n%7==0){j=n/7;n=0;break;}

        }
    }
    if(n)printf("-1");
    else{
        while(i>=7){i-=7;j+=4;}
        while(i--)printf("4");
        while(j--)printf("7");
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	long long n,i=0,j=0;
	scanf("%lld",&n);
	i=n/4;n-=i*4;
	if(n){
		while(i>0){
			n+=4;i--;
			if(n%7==0){
                j=n/7;n=0;break;
            }
		}
	}
	if(n)printf("-1\n");
	else{
		while(i>=7){
            i-=7;
            j+=4;
        }
		while(i--)
            printf("4");
		while(j--)
            printf("7");
		printf("\n");
	}
	return 0;//over~
}


#include <bits/stdc++.h>
using namespace std;
int main(){
    int n,num4=0,num7=0;
    cin>>n;
    while(n>=7){
        num7++;
        n-=7;
    }
    while(n>=4){
        num4++;
        n-=4;
    }
    if(n!=0){
        cout<<-1<<endl;
        return 0;
    }
    {
        for(int i=1;i<=num4;++i){
            cout<<4;
        }
        for(int i=1;i<=num7;++i){
            cout<<7;
        }
        cout<<endl;
        return 0;
    }
}


#include<cstdio>
int main(){
	long long int n;
	int x,y,x1,y1,sum=0,sum1=0;
	scanf("%d",&n);
	x=n%4;
	y=n%7;
	if(x==3){
		x1=n/4;
		for(int i=0;i<x1-1;i++){
			sum+=4;
			sum*=10;
		}
		x=x+4;
		sum+=x;
		printf("%d\n",sum);
		return 0;
	}

	else{
	printf("-1\n");
	return 0;
	}
}


#include<iostream>
using namespace std;
int main(){
    int n;
    cin>>n;
    if(n%4==0){
        for(int i=1;i<=n/4;i++)cout<<4;
        return 0;
    }
    int s=n/4*4;
    while(s>=0){
        if((n-s)%7==0){
            for(int i=1;i<=s/4;i++)cout<<4;
            for(int i=1;i<=(n-s)/7;i++)cout<<7;
            return 0;
        }
        s-=4;
    }
    cout<<-1<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,ans1,ans2;
int main()
{
    ios::sync_with_stdio(false);
    while(cin>>n)
    {
        int flag=0;
        for(int i=0;i<=n;i+=4)
        {
            if((n-i)%7==0)
            {
                int y=(n-i)/7;
                ans1=i/4;ans2=y;
                flag=1;
                break;
            }
        }
        if(!flag)printf("-1\n");
        else {
            for(int i=1;i<=ans1;i++)
                printf("4");
            for(int i=1;i<=ans2;i++)
                printf("7");
            printf("\n");
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n;
bool flag;
int main()
{
	while(cin>>n)
	{
		bool flag=false;
		int p;
		int index=1e7,a,b;
		for(int q=0;q<=n/7;q++)
		{
			if((n-7*q)%4==0)
			{
				flag=true;
				p=(n-7*q)/4;
				if(index>p+q)
				{
					index=p+q;
					a=p;
					b=q;
				}

			}
		}
		if(flag==false)
			cout<<-1;
		else
		{

			for(int i=1;i<=a;i++)
				cout<<4;
			for(int i=1;i<=b;i++)
				cout<<7;
		}
		cout<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin>>n;
    for(int i=0;i<n/4+1;i++){
        if((n-4*i)%7==0){
            n=n-4*i;
            for(int j=0;j<i;j++){
                cout<<'4';
            }
            while(n!=0){
                cout<<'7';
                n-=7;
            }
            return 0;
        }
    }
    cout<<"-1";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    for(int i=0;i<=n/4;i++)
    {
        int f,s;
        f=i*4;
        s=n-f;
        if(s%7==0)
        {
            for(int j=0;j<i;j++)
                cout<<'4';
            s/=7;
            for(int j=0;j<s;j++)
                cout<<'7';
            return 0;
        }
    }
    cout<<-1;
    return 0;
}