///..............*****.................///
///   Saiful Alam                      ///
///   Department of CSE                ///
///   Comilla University , Bangladesh. ///
///..............****..................///
#include<bits/stdc++.h>
#define  p5         100005
#define  p6         1000007
#define  PI         acos(-1)
#define  M          1000000007
#define  sc         scanf
#define  ss         second
#define  ff         first
#define  pf         printf
#define  scin(x)    sc("%d",&x)
#define  scin2(x,y) sc("%d%d",&x,&y)
#define  READ(f)    freopen(f, "r", stdin)
#define  WRITE(f)   freopen(f, "w", stdout)
#define  ll         long long
#define  pp         pair <ll,ll>
#define  pq         priority_queue
#define  pb         push_back
#define  mpa         make_pair
#define  yes        pf("YES\n")
#define  no         pf("NO\n")
#define all(v)      v.begin(), v.end()
#define unq(v)      v.erase(unique(all(v)),v.end());
//int month[]={31,28,31,30,31,30,31,31,30,31,30,31};
//int dx[5] = {1, -1, 0, 0 };
//int dy[5] = {0, 0, 1, -1};
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    ll ca;
    cin>>ca;
    while(ca--)
    {
        ll n,i,j,k;
        cin>>n>>k;
        string st;
        cin>>st;
        ll p1=-1,p2=-1,ln=st.size();
        for(i=0; i<ln; i++)
        {
            if(st[i]=='1')
            {
                p1=i;
                break;
            }
        }
        for(i=ln-1; i>=0; i--)
        {
            if(st[i]=='1')
            {
                p2=i;
                break;
            }
        }
        if(p1==-1 && p2==-1)
        {
            cout<<0<<endl;
            continue;
        }
        vector<ll>v;
        ll s=0;
        for(i=p1; i<=p2; i++)
        {
            ll x=st[i]-48;
            v.pb(x);
            s=s+x;
        }
        ll ans=1e15;
        for(i=0; i<k; i++)
        {
            ll cnt=0;
            for(j=i; j<v.size(); j=j+k)
            {
                if(v[j]==1) cnt++;
                else cnt--;
                if(cnt<0) cnt=0;
                ans=min(ans,s-cnt);
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}
//https://github.com/me-shaon/bangla-programming-resources
//Dxz3rhdh41


///..............*****.................///
///   Saiful Alam                      ///
///   Department of CSE                ///
///   Comilla University , Bangladesh. ///
///..............****..................///
#include<bits/stdc++.h>
#define  p5         100005
#define  p6         1000007
#define  PI         acos(-1)
#define  M          1000000007
#define  sc         scanf
#define  ss         second
#define  ff         first
#define  pf         printf
#define  scin(x)    sc("%d",&x)
#define  scin2(x,y) sc("%d%d",&x,&y)
#define  READ(f)    freopen(f, "r", stdin)
#define  WRITE(f)   freopen(f, "w", stdout)
#define  ll         long long
#define  pp         pair <ll,ll>
#define  pq         priority_queue
#define  pb         push_back
#define  mpa         make_pair
#define  yes        pf("YES\n")
#define  no         pf("NO\n")
#define all(v)      v.begin(), v.end()
#define unq(v)      v.erase(unique(all(v)),v.end());
//int month[]={31,28,31,30,31,30,31,31,30,31,30,31};
//int dx[5] = {1, -1, 0, 0 };
//int dy[5] = {0, 0, 1, -1};
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    ll ca;
    cin>>ca;
    while(ca--)
    {
        ll n,i,j,k;
        cin>>n>>k;
        string st;
        cin>>st;
        ll p1=-1,p2=-1,ln=st.size();
        for(i=0; i<ln; i++)
        {
            if(st[i]=='1')
            {
                p1=i;
                break;
            }
        }
        for(i=ln-1; i>=0; i--)
        {
            if(st[i]=='1')
            {
                p2=i;
                break;
            }
        }
        if(p1==-1 && p2==-1)
        {
            cout<<0<<endl;
            continue;
        }
        vector<ll>v;
        ll s=0;
        for(i=p1; i<=p2; i++)
        {
            ll x=st[i]-48;
            v.pb(x);
            s=s+x;
        }
        ll ans=1e15;
        for(i=0; i<k; i++)
        {
            ll o=0,z=0;
            for(j=i; j<v.size(); j=j+k)
            {
                if(v[j]==1) o++;
                else z++;
            }
            ll xx=s-o+z;
            ans=min(ans,xx);
        }
        reverse(all(v));
        for(i=0; i<k; i++)
        {
            ll o=0,z=0;
            for(j=i; j<v.size(); j=j+k)
            {
                if(v[j]==1) o++;
                else z++;
            }
            ll xx=s-o+z;
            ans=min(ans,xx);
        }
        cout<<ans<<endl;
    }
    return 0;
}
//https://github.com/me-shaon/bangla-programming-resources
//Dxz3rhdh41


///..............*****.................///
///   Saiful Alam                      ///
///   Department of CSE                ///
///   Comilla University , Bangladesh. ///
///..............****..................///
#include<bits/stdc++.h>
#define  p5         100005
#define  p6         1000007
#define  PI         acos(-1)
#define  M          1000000007
#define  sc         scanf
#define  ss         second
#define  ff         first
#define  pf         printf
#define  scin(x)    sc("%d",&x)
#define  scin2(x,y) sc("%d%d",&x,&y)
#define  READ(f)    freopen(f, "r", stdin)
#define  WRITE(f)   freopen(f, "w", stdout)
#define  ll         long long
#define  pp         pair <ll,ll>
#define  pq         priority_queue
#define  pb         push_back
#define  mpa         make_pair
#define  yes        pf("YES\n")
#define  no         pf("NO\n")
#define all(v)      v.begin(), v.end()
#define unq(v)      v.erase(unique(all(v)),v.end());
//int month[]={31,28,31,30,31,30,31,31,30,31,30,31};
//int dx[5] = {1, -1, 0, 0 };
//int dy[5] = {0, 0, 1, -1};
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    ll ca;
    cin>>ca;
    while(ca--)
    {
        ll n,i,j,k;
        cin>>n>>k;
        string st;
        cin>>st;
        ll p1=-1,p2=-1,ln=st.size();
        for(i=0; i<ln; i++)
        {
            if(st[i]=='1')
            {
                p1=i;
                break;
            }
        }
        for(i=ln-1; i>=0; i--)
        {
            if(st[i]=='1')
            {
                p2=i;
                break;
            }
        }
        if(p1==-1 && p2==-1)
        {
            cout<<0<<endl;
            continue;
        }
        vector<ll>v;
        ll s=0;
        for(i=p1; i<=p2; i++)
        {
            ll x=st[i]-48;
            v.pb(x);
            s=s+x;
        }
        ll ans=1e15;
        for(i=0; i<k; i++)
        {
            ll o=0,z=0;
            for(j=i; j<v.size(); j=j+k)
            {
                if(v[j]==1) o++;
                else z++;
            }
            ll xx=s-o+z;
            ans=min(ans,xx);
        }
        reverse(all(v));
        for(i=0; i<k; i++)
        {
            ll o=0,z=0;
            for(j=i; j<v.size(); j=j+k)
            {
                if(v[j]==1) o++;
                else z++;
            }
            ll r=o-z;
            if(r<0) r=0;
            ll xx=s-r;
            ans=min(ans,xx);
        }
        cout<<ans<<endl;
    }
    return 0;
}
//https://github.com/me-shaon/bangla-programming-resources
//Dxz3rhdh41


#include<iostream>
#include<algorithm>
#include<cstring>
#include<stdlib.h>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<stack>
#include<math.h>
#include<unordered_map>
#include<string>
#include<bitset>
#include<iomanip>
using namespace std;

#define IO ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define FF(x) fixed << setprecision(x)

typedef long long LL;

const LL MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const LL LLINF = 1e18;
const double EPS = 1e-6;
const int MAXM = 5e5 + 10;
const int MAXN = 1e6 + 10;

int dpz[MAXN], dpn[MAXN];
int sum[MAXN];

int main()
{
	IO;

	int t;
	cin >> t;
	while (t--)
	{
		int n, k;
		cin >> n >> k;

		string str;
		cin >> str;

		if (n == 1 && str[0] == '1')
		{
			cout << "0" << endl;
			continue;
		}

		memset(dpz, 0, sizeof(dpz));
		memset(dpn, 0, sizeof(dpn));
		memset(sum, 0, sizeof(sum));

		for (int i = 0; i <= str.length(); i++)
		{
			sum[i + 1] += sum[i];
			if (str[i] == '1')
			{
				sum[i + 1] += 1;
			}
		}

		for (int i = 1; i <= n; i++)
		{
			if (i - k > 0)
			{
				int j = i - k;
				dpz[i] = min(dpz[j] + sum[i - 1] - sum[j], sum[i - 1]) + (str[i - 1] == '0');
			}
			else
			{
				dpz[i] = sum[i - 1] + (str[i - 1] == '0');
			}
			//cout << dpz[i] << " ";

		}
		// cout << endl;

		for (int i = n; i >= 1; i--)
		{
			if (i + k < n)
			{
				int j = i + k;
				dpn[i] = min(dpn[j] + sum[j - 1] - sum[i], sum[n] - sum[i]) + (str[i - 1] == '0');
			}
			else
			{
				dpn[i] = sum[n] - sum[i] + (str[i - 1] == '0');
			}
			//cout << dpn[i] << " ";
		}
		//cout << endl;

		int ans = INF;
		for (int i = 1; i <= n - k; i++)
		{
			ans = min(ans, dpz[i] + dpn[i + k] + sum[i + k - 1] - sum[i]);
		}

		ans = min(ans, sum[n]);

		cout << ans << endl;

	}

	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<stdlib.h>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<stack>
#include<math.h>
#include<unordered_map>
#include<string>
#include<bitset>
#include<iomanip>
using namespace std;

#define IO ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define FF(x) fixed << setprecision(x)

typedef long long LL;

const LL MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const LL LLINF = 1e18;
const double EPS = 1e-6;
const int MAXM = 5e5 + 10;
const int MAXN = 1e6 + 10;

int dpz[MAXN], dpn[MAXN];
int sum[MAXN];

inline int read() {
	int s = 0, w = 1; char ch = getchar();
	while (ch < 48 || ch > 57) { if (ch == '-') w = -1; ch = getchar(); }
	while (ch >= 48 && ch <= 57) s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar();
	return s * w;
}

int main()
{
	//IO;

	int t;
	cin >> t;
	while (t--)
	{
		int n, k;
		cin >> n >> k;

		string str;
		cin >> str;

		if (n == 1 && str[0] == '1')
		{
			cout << "0" << endl;
			continue;
		}

		memset(dpz, 0, (n + 1) * sizeof(int));
		memset(dpn, 0, (n + 1) * sizeof(int));
		memset(sum, 0, (n + 1) * sizeof(int));

		for (int i = 0; i <= str.length(); i++)
		{
			sum[i + 1] += sum[i];
			if (str[i] == '1')
			{
				sum[i + 1] += 1;
			}
		}

		for (int i = 1; i <= n; i++)
		{
			if (i - k > 0)
			{
				int j = i - k;
				dpz[i] = min(dpz[j] + sum[i - 1] - sum[j], sum[i - 1]) + (str[i - 1] == '0');
			}
			else
			{
				dpz[i] = sum[i - 1] + (str[i - 1] == '0');
			}
		//	cout << dpz[i] << " ";
		}
	////	cout << endl;

		for (int i = n; i >= 1; i--)
		{
			if (i + k < n)
			{
				int j = i + k;
				dpn[i] = min(dpn[j] + sum[j - 1] - sum[i], sum[n] - sum[i]) + (str[i - 1] == '0');
			}
			else
			{
				dpn[i] = sum[n] - sum[i] + (str[i - 1] == '0');
			}
			//cout << dpn[i] << " ";
		}
		//cout << endl;

		int ans = INF;
		for (int i = 1; i <= n - k; i++)
		{
			ans = min(ans, dpz[i] + dpn[i + k] + sum[i + k - 1] - sum[i]);
			ans = min(ans, dpz[i] + sum[n] - sum[i]);
			ans = min(ans, dpn[i + k] + sum[i + k]);
		}

		ans = min(ans, sum[n]);

		cout << ans << endl;

	}

	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<stdlib.h>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<stack>
#include<math.h>
#include<unordered_map>
#include<string>
#include<bitset>
#include<iomanip>
using namespace std;

#define IO ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define FF(x) fixed << setprecision(x)

typedef long long LL;

const LL MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const LL LLINF = 1e18;
const double EPS = 1e-6;
const int MAXM = 5e5 + 10;
const int MAXN = 1e6 + 10;

int dpz[MAXN], dpn[MAXN];
int sum[MAXN];

inline int read() {
	int s = 0, w = 1; char ch = getchar();
	while (ch < 48 || ch > 57) { if (ch == '-') w = -1; ch = getchar(); }
	while (ch >= 48 && ch <= 57) s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar();
	return s * w;
}

int main()
{
	//IO;

	int t;
	cin >> t;
	while (t--)
	{
		int n, k;
		cin >> n >> k;

		string str;
		cin >> str;

		if (n == 1 && str[0] == '1')
		{
			cout << "0" << endl;
			continue;
		}

		memset(dpz, 0, sizeof(dpz));
		memset(dpn, 0, sizeof(dpn));
		memset(sum, 0, sizeof(sum));

		for (int i = 0; i <= str.length(); i++)
		{
			sum[i + 1] += sum[i];
			if (str[i] == '1')
			{
				sum[i + 1] += 1;
			}
		}

		for (int i = 1; i <= n; i++)
		{
			if (i - k > 0)
			{
				int j = i - k;
				dpz[i] = min(dpz[j] + sum[i - 1] - sum[j], sum[i - 1]) + (str[i - 1] == '0');
			}
			else
			{
				dpz[i] = sum[i - 1] + (str[i - 1] == '0');
			}
		}

		for (int i = n; i >= 1; i--)
		{
			if (i + k < n)
			{
				int j = i + k;
				dpn[i] = min(dpn[j] + sum[j - 1] - sum[i], sum[n] - sum[i]) + (str[i - 1] == '0');
			}
			else
			{
				dpn[i] = sum[n] - sum[i] + (str[i - 1] == '0');
			}
		}

		int ans = INF;
		for (int i = 1; i <= n - k; i++)
		{
			ans = min(ans, dpz[i] + dpn[i + k] + sum[i + k - 1] - sum[i]);
		}

		ans = min(ans, sum[n]);

		cout << ans << endl;

	}

	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<stdlib.h>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<stack>
#include<math.h>
#include<unordered_map>
#include<string>
#include<bitset>
#include<iomanip>
using namespace std;

#define IO ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define FF(x) fixed << setprecision(x)

typedef long long LL;

const LL MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const LL LLINF = 1e18;
const double EPS = 1e-6;
const int MAXM = 5e5 + 10;
const int MAXN = 1e6 + 10;

int dpz[MAXN], dpn[MAXN];
int sum[MAXN];

inline int read() {
	int s = 0, w = 1; char ch = getchar();
	while (ch < 48 || ch > 57) { if (ch == '-') w = -1; ch = getchar(); }
	while (ch >= 48 && ch <= 57) s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar();
	return s * w;
}

int main()
{
	IO;

	int t;
	cin >> t;
	while (t--)
	{
		int n, k;
		n = read();
		k = read();

		string str;
		cin >> str;

		if (n == 1 && str[0] == '1')
		{
			cout << "0" << endl;
			continue;
		}

		memset(dpz, 0, sizeof(dpz));
		memset(dpn, 0, sizeof(dpn));
		memset(sum, 0, sizeof(sum));

		for (int i = 0; i <= str.length(); i++)
		{
			sum[i + 1] += sum[i];
			if (str[i] == '1')
			{
				sum[i + 1] += 1;
			}
		}

		for (int i = 1; i <= n; i++)
		{
			if (i - k > 0)
			{
				int j = i - k;
				dpz[i] = min(dpz[j] + sum[i - 1] - sum[j], sum[i - 1]) + (str[i - 1] == '0');
			}
			else
			{
				dpz[i] = sum[i - 1] + (str[i - 1] == '0');
			}
		}

		for (int i = n; i >= 1; i--)
		{
			if (i + k < n)
			{
				int j = i + k;
				dpn[i] = min(dpn[j] + sum[j - 1] - sum[i], sum[n] - sum[i]) + (str[i - 1] == '0');
			}
			else
			{
				dpn[i] = sum[n] - sum[i] + (str[i - 1] == '0');
			}
		}

		int ans = INF;
		for (int i = 1; i <= n - k; i++)
		{
			ans = min(ans, dpz[i] + dpn[i + k] + sum[i + k - 1] - sum[i]);
		}

		ans = min(ans, sum[n]);

		cout << ans << endl;

	}

	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<stdlib.h>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<stack>
#include<math.h>
#include<unordered_map>
#include<string>
#include<bitset>
#include<iomanip>
using namespace std;

#define IO ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define FF(x) fixed << setprecision(x)

typedef long long LL;

const LL MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const LL LLINF = 1e18;
const double EPS = 1e-6;
const int MAXM = 5e5 + 10;
const int MAXN = 1e6 + 10;

int dpz[MAXN], dpn[MAXN];
int sum[MAXN];

inline int read() {
	int s = 0, w = 1; char ch = getchar();
	while (ch < 48 || ch > 57) { if (ch == '-') w = -1; ch = getchar(); }
	while (ch >= 48 && ch <= 57) s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar();
	return s * w;
}

int main()
{
	//IO;

	int t;
	cin >> t;
	while (t--)
	{
		int n, k;
		cin >> n >> k;

		string str;
		cin >> str;

		if (n == 1 && str[0] == '1')
		{
			cout << "0" << endl;
			continue;
		}

		memset(dpz, 0, (n + 1) * sizeof(int));
		memset(dpn, 0, (n + 1) * sizeof(int));
		memset(sum, 0, (n + 1) * sizeof(int));

		for (int i = 0; i <= str.length(); i++)
		{
			sum[i + 1] += sum[i];
			if (str[i] == '1')
			{
				sum[i + 1] += 1;
			}
		}

		for (int i = 1; i <= n; i++)
		{
			if (i - k > 0)
			{
				int j = i - k;
				dpz[i] = min(dpz[j] + sum[i - 1] - sum[j], sum[i - 1]) + (str[i - 1] == '0');
			}
			else
			{
				dpz[i] = sum[i - 1] + (str[i - 1] == '0');
			}
		}

		for (int i = n; i >= 1; i--)
		{
			if (i + k < n)
			{
				int j = i + k;
				dpn[i] = min(dpn[j] + sum[j - 1] - sum[i], sum[n] - sum[i]) + (str[i - 1] == '0');
			}
			else
			{
				dpn[i] = sum[n] - sum[i] + (str[i - 1] == '0');
			}
		}

		int ans = INF;
		for (int i = 1; i <= n - k; i++)
		{
			ans = min(ans, dpz[i] + dpn[i + k] + sum[i + k - 1] - sum[i]);
		}

		ans = min(ans, sum[n]);

		cout << ans << endl;

	}

	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<stdlib.h>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<stack>
#include<math.h>
#include<unordered_map>
#include<string>
#include<bitset>
#include<iomanip>
using namespace std;

#define IO ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define FF(x) fixed << setprecision(x)

typedef long long LL;

const LL MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const LL LLINF = 1e18;
const double EPS = 1e-6;
const int MAXM = 5e5 + 10;
const int MAXN = 1e6 + 10;

int dpz[MAXN], dpn[MAXN];
int sum[MAXN];

int main()
{
	IO;

	int t;
	cin >> t;
	while (t--)
	{
		int n, k;
		cin >> n >> k;

		string str;
		cin >> str;

		if (n == 1 && str[0] == '1')
		{
			cout << "0" << endl;
			continue;
		}

		memset(dpz, 0, sizeof(dpz));
		memset(dpn, 0, sizeof(dpn));
		memset(sum, 0, sizeof(sum));

		for (int i = 0; i <= str.length(); i++)
		{
			sum[i + 1] += sum[i];
			if (str[i] == '1')
			{
				sum[i + 1] += 1;
			}
		}

		for (int i = 1; i <= n; i++)
		{
			if (i - k > 0)
			{
				int j = i - k;
				dpz[i] = min(dpz[j] + sum[i - 1] - sum[j], sum[i - 1]) + (str[i - 1] == '0');
			}
			else
			{
				dpz[i] = sum[i - 1] + (str[i - 1] == '0');
			}
		}

		for (int i = n; i >= 1; i--)
		{
			if (i + k < n)
			{
				int j = i + k;
				dpn[i] = min(dpn[j] + sum[j - 1] - sum[i], sum[n] - sum[i]) + (str[i - 1] == '0');
			}
			else
			{
				dpn[i] = sum[n] - sum[i] + (str[i - 1] == '0');
			}
		}

		int ans = INF;
		for (int i = 1; i <= n - k; i++)
		{
			ans = min(ans, dpz[i] + dpn[i + k] + sum[i + k - 1] - sum[i]);
		}

		ans = min(ans, sum[n]);

		cout << ans << endl;

	}

	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<stdlib.h>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<stack>
#include<math.h>
#include<unordered_map>
#include<string>
#include<bitset>
#include<iomanip>
using namespace std;

#define IO ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define FF(x) fixed << setprecision(x)

typedef long long LL;

const LL MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const LL LLINF = 1e18;
const double EPS = 1e-6;
const int MAXM = 5e5 + 10;
const int MAXN = 1e6 + 10;

int dpz[MAXN], dpn[MAXN];
int sum[MAXN];

inline int read() {
	int s = 0, w = 1; char ch = getchar();
	while (ch < 48 || ch > 57) { if (ch == '-') w = -1; ch = getchar(); }
	while (ch >= 48 && ch <= 57) s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar();
	return s * w;
}

int main()
{
	//IO;

	int t;
	cin >> t;
	while (t--)
	{
		int n, k;
		cin >> n >> k;

		string str;
		cin >> str;

		if (n == 1 && str[0] == '1')
		{
			cout << "0" << endl;
			continue;
		}

		memset(dpz, 0, (n + 1) * sizeof(int));
		memset(dpn, 0, (n + 1) * sizeof(int));
		memset(sum, 0, (n + 1) * sizeof(int));

		for (int i = 0; i <= str.length(); i++)
		{
			sum[i + 1] += sum[i];
			if (str[i] == '1')
			{
				sum[i + 1] += 1;
			}
		}

		for (int i = 1; i <= n; i++)
		{
			if (i - k > 0)
			{
				int j = i - k;
				dpz[i] = min(dpz[j] + sum[i - 1] - sum[j], sum[i - 1]) + (str[i - 1] == '0');
			}
			else
			{
				dpz[i] = sum[i - 1] + (str[i - 1] == '0');
			}
		//	cout << dpz[i] << " ";
		}
	//	cout << endl;

		for (int i = n; i >= 1; i--)
		{
			if (i + k < n)
			{
				int j = i + k;
				dpn[i] = min(dpn[j] + sum[j - 1] - sum[i], sum[n] - sum[i]) + (str[i - 1] == '0');
			}
			else
			{
				dpn[i] = sum[n] - sum[i] + (str[i - 1] == '0');
			}
		//	cout << dpn[i] << " ";
		}
	//	cout << endl;

		int ans = INF;
		for (int i = 1; i <= n; i++)
		{
			if (i + k <= n)
			{
				ans = min(ans, dpz[i] + dpn[i + k] + sum[i + k - 1] - sum[i]);
			}

			ans = min(ans, dpz[i] + sum[n] - sum[i]);
			ans = min(ans, dpn[i] + sum[i - 1]);
		}

		ans = min(ans, sum[n]);

		cout << ans << endl;

	}

	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<stdlib.h>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<stack>
#include<math.h>
#include<unordered_map>
#include<string>
#include<bitset>
#include<iomanip>
using namespace std;

#define IO ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define FF(x) fixed << setprecision(x)

typedef long long LL;

const LL MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const LL LLINF = 1e18;
const double EPS = 1e-6;
const int MAXM = 5e5 + 10;
const int MAXN = 1e6 + 10;

int dpz[MAXN], dpn[MAXN];
int sum[MAXN];

char str[MAXN];

int main()
{
	IO;

	int t;
	cin >> t;
	while (t--)
	{
		int n, k;
		cin >> n >> k;

		scanf("%s",str);

		if (n == 1 && str[0] == '1')
		{
			cout << "0" << endl;
			continue;
		}

		memset(dpz, 0, sizeof(dpz));
		memset(dpn, 0, sizeof(dpn));
		memset(sum, 0, sizeof(sum));

		for (int i = 0; i <= strlen(str); i++)
		{
			sum[i + 1] += sum[i];
			if (str[i] == '1')
			{
				sum[i + 1] += 1;
			}
		}

		for (int i = 1; i <= n; i++)
		{
			if (i - k > 0)
			{
				int j = i - k;
				dpz[i] = min(dpz[j] + sum[i - 1] - sum[j], sum[i - 1]) + (str[i - 1] == '0');
			}
			else
			{
				dpz[i] = sum[i - 1] + (str[i - 1] == '0');
			}
		}

		for (int i = n; i >= 1; i--)
		{
			if (i + k < n)
			{
				int j = i + k;
				dpn[i] = min(dpn[j] + sum[j - 1] - sum[i], sum[n] - sum[i]) + (str[i - 1] == '0');
			}
			else
			{
				dpn[i] = sum[n] - sum[i] + (str[i - 1] == '0');
			}
		}

		int ans = INF;
		for (int i = 1; i <= n - k; i++)
		{
			ans = min(ans, dpz[i] + dpn[i + k] + sum[i + k - 1] - sum[i]);
		}

		ans = min(ans, sum[n]);

		cout << ans << endl;

	}

	return 0;
}


#include <bits/stdc++.h>
#define db(x) cout << (x) << '\n';
#define dbg(x) cerr << #x ": " << (x)<< '\n';
using namespace std;
typedef long long ll;

typedef pair<int,int> pii;
typedef vector<pii> vii;
typedef vector<int> vi;
typedef vector<vector<int>> adyList;   ///Sin pesos
//typedef vector<vector<pii>> adyList;   ///Con pesos

#define VISITED 1
#define UNVISITED -1
#define EXPLORED -1
#define MAXN  1000007
vi dfs_num;
adyList Graph;
int cont[MAXN][2];
vector<pii> unos[MAXN];
int main()
{

    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int t;
    cin>>t;
    while(t--)
    {
        int n,k;
        cin>>n>>k;
        string s;
        cin>>s;
        int total = 0;
        for(int i=0; i<k; i++)
        {
            int p = 0;
            int c = 0;
            int t=0;
            bool ok = 0;
            for(int j=0; j*k+i<n; j++)
            {
                if(s[j*k+i] == '1')
                {
//                    cout << "1";
                    cont[i][0]++;
                    total++;
                    p++;
                    if(t>0)
                    {
                        unos[i].push_back({t,c});
                        t=0;
                    }
                    c=0;
                    ok=1;
                }
                else
                {
//                    cout << "0";
                    if(ok)
                    {
                        t = p;
                        p=0;
                    }
                    ok = 0;
                    c++;
                    ok=0;
                }
            }
            if(ok)
                unos[i].push_back({p,0});
                else if(t>0)
                    unos[i].push_back({t,0});
        }
//        db("")
        for(int i=0;i<k;i++)
        {
            if(unos[i].size()==0)
                continue;
//                for(auto x :unos[i])
//                    cout << x.first << " "<<x.second << "\n";
            int tp = (int)unos[i].size();
            int dp[tp][21];
            dp[tp-1][0] = unos[i][tp-1].first;
            dp[tp-1][1] = 0;
            int acum = unos[i][tp-1].first;
            for(int j=tp-2;j>=0;j--)
            {
                dp[j][0] = unos[i][j].first+min(dp[j+1][0],dp[j+1][1]);
                dp[j][1] = min(dp[j+1][1]+unos[i][j].second,acum);
                acum+=unos[i][j].first;
            }
//            db(dp[0][0])
//            db(dp[0][1])
            cont[i][1] = min(dp[0][0],dp[0][1]);
//            db("")
        }
        int minm = MAXN;
        for(int i=0; i<k; i++)
            minm = min(minm,total-cont[i][0]+cont[i][1]);
        db(minm)
        for(int i=0; i<k; i++)
            cont[i][0] = cont[i][1] = 0;
        for(int i=0;i<k;i++)
            unos[i].clear();
    }

    return 0;
}


#include <bits/stdc++.h>
#define db(x) cout << (x) << '\n';
#define dbg(x) cerr << #x ": " << (x)<< '\n';
using namespace std;
typedef long long ll;

typedef pair<int,int> pii;
typedef vector<pii> vii;
typedef vector<int> vi;
typedef vector<vector<int>> adyList;   ///Sin pesos
//typedef vector<vector<pii>> adyList;   ///Con pesos

#define VISITED 1
#define UNVISITED -1
#define EXPLORED -1
#define MAXN  1000007
vi dfs_num;
adyList Graph;
//bool mask[MAXN];
int cont[MAXN][5];
vector<pii> unos[MAXN];
int main()
{

    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int t;
    cin>>t;
    while(t--)
    {
        int n,k;
        cin>>n>>k;
        string s;
        cin>>s;
//        for(int i=0;i<n;i++)
//            mask[i]=s[i]-'0';
        for(int i=0; i<k; i++)
            cont[i][2] = n;
        for(int i=n-1; i>=0; i--)
        {
            if(s[i] == '1')
            {
                cont[i%k][3] = max(i,cont[i%k][3]);
            }
        }
        int total = 0;
        for(int i=0; i<k; i++)
        {
            int p = 0;
            int c = 0;
            int t=0;
            bool ok;
            for(int j=0; j*k+i<n; j++)
            {
                if(s[j*k+i] == '1')
                {
                    cont[i][2] = min(i,cont[i][2]);
                    cont[i][0]++;
                    total++;
                    p++;
                    if(t>0)
                    {
                        unos[i].push_back({t,c});
                        t=0;
                    }
                    c=0;
                    ok=1;
                }
                else
                {
                    if(cont[i][2] < i &&  i < cont[i][3])
                        cont[i][1]++;
                    ok = 0;
                    t = p;
                    p=0;
                    c++;
                    ok=0;
                }
            }
            if(ok)
                unos[i].push_back({p,0});
        }
        for(int i=0;i<k;i++)
        {
            if(unos[i].size()==0)
                continue;
            int tp = (int)unos[i].size();
            int dp[tp][2];
            dp[tp-1][0] = unos[i][tp-1].first;
            dp[tp-1][1] = 0;
            int acum = 0;
            for(int j=tp-2;j>=0;j--)
            {
                acum+=unos[i][j].first;
                dp[j][0] = unos[i][j].first+min(dp[j+1][0],dp[j+1][1]);
                dp[j][1] = min(dp[j+1][1]+unos[i][j].second,cont[i][0]-acum);
            }
            cont[i][4] = min(dp[0][0],dp[0][1]);
        }
        int minm = MAXN;
        for(int i=0; i<k; i++)
            minm = min(minm,total-cont[i][0]+cont[i][4]);
        db(minm)
        for(int i=0; i<k; i++)
            cont[i][0] = cont[i][1]=cont[i][2]=cont[i][3] = cont[i][4] = 0;
        for(int i=0;i<k;i++)
            unos[i].clear();
    }

    return 0;
}


#include <bits/stdc++.h>
#define db(x) cout << (x) << '\n';
#define dbg(x) cerr << #x ": " << (x)<< '\n';
using namespace std;
typedef long long ll;

typedef pair<int,int> pii;
typedef vector<pii> vii;
typedef vector<int> vi;
typedef vector<vector<int>> adyList;   ///Sin pesos
//typedef vector<vector<pii>> adyList;   ///Con pesos

#define VISITED 1
#define UNVISITED -1
#define EXPLORED -1
#define MAXN  1000007
vi dfs_num;
adyList Graph;
bool mask[MAXN];
int cont[MAXN][4];

int main()
{

    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int t;
    cin>>t;
    while(t--)
    {
        int n,k;
        cin>>n>>k;
        string s;
        cin>>s;
//        for(int i=0;i<n;i++)
//            mask[i]=s[i]-'0';
        for(int i=0;i<k;i++)
            cont[i][2] = n;
        for(int i=n-1;i>=0;i--)
        {
            if(s[i] == '1')
            {
                cont[i%k][3] = max(i,cont[i%k][3]);
            }
        }
        int total = 0;
        for(int i=0;i<n;i++)
        {
            if(s[i] == '1')
            {
                cont[i%k][2] = min(i,cont[i%k][2]);
                cont[i%k][0]++;
                total++;
            }
            else
            {
                if(cont[i%k][2] < i &&  i < cont[i%k][3])
                    cont[i%k][1]++;
            }
        }
        int minm = MAXN;
        for(int i=0;i<k;i++)
            minm = min(minm,total-cont[i][0]+cont[i][1]);
        db(minm)
        for(int i=0;i<k;i++)
            cont[i][0] = cont[i][1]=cont[i][2]=cont[i][3] = 0;

    }

    return 0;
}


#include <bits/stdc++.h>
#define db(x) cout << (x) << '\n';
#define dbg(x) cerr << #x ": " << (x)<< '\n';
using namespace std;
typedef long long ll;

typedef pair<int,int> pii;
typedef vector<pii> vii;
typedef vector<int> vi;
typedef vector<vector<int>> adyList;   ///Sin pesos
//typedef vector<vector<pii>> adyList;   ///Con pesos

#define VISITED 1
#define UNVISITED -1
#define EXPLORED -1
#define MAXN  1000007
vi dfs_num;
adyList Graph;
//bool mask[MAXN];
int cont[MAXN][5];
vector<pii> unos[MAXN];
int main()
{

    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int t;
    cin>>t;
    while(t--)
    {
        int n,k;
        cin>>n>>k;
        string s;
        cin>>s;
//        for(int i=0;i<n;i++)
//            mask[i]=s[i]-'0';
        for(int i=0; i<k; i++)
            cont[i][2] = n;
        for(int i=n-1; i>=0; i--)
        {
            if(s[i] == '1')
            {
                cont[i%k][3] = max(i,cont[i%k][3]);
            }
        }
        int total = 0;
        for(int i=0; i<k; i++)
        {
            int p = 0;
            int c = 0;
            int t=0;
            bool ok =1;
            for(int j=0; j*k+i<n; j++)
            {
                if(s[j*k+i] == '1')
                {
                    cont[i][2] = min(i,cont[i][2]);
                    cont[i][0]++;
                    total++;
                    p++;
                    if(t>0)
                    {
                        unos[i].push_back({t,c});
                        t=0;
                    }
                    c=0;
                }
                else
                {
                    if(cont[i][2] < i &&  i < cont[i][3])
                        cont[i][1]++;
                    ok = 0;
                    t = p;
                    p=0;
                    c++;
                }
            }
        }
        for(int i=0;i<k;i++)
        {
            if(unos[i].size()==0)
                continue;
            int tp = (int)unos[i].size();
            int dp[tp][2];
            dp[tp-1][0] = unos[i][tp-1].first;
            int acum = 0;
            for(int j=tp-2;j>=0;j--)
            {
                acum+=unos[i][j].first;
                dp[j][0] = unos[i][j].first+min(dp[j+1][0],dp[j+1][1]);
                dp[j][1] = min(dp[j+1][1]+unos[i][j].second,cont[i][0]-acum);
            }
            cont[i][4] = min(dp[0][0],dp[0][1]);
        }
        int minm = MAXN;
        for(int i=0; i<k; i++)
            minm = min(minm,total-cont[i][0]+cont[i][4]);
        db(minm)
        for(int i=0; i<k; i++)
            cont[i][0] = cont[i][1]=cont[i][2]=cont[i][3] = cont[i][4] = 0;
        for(int i=0;i<k;i++)
            unos[i].clear();
    }

    return 0;
}


#include <bits/stdc++.h>
#define db(x) cout << (x) << '\n';
#define dbg(x) cerr << #x ": " << (x)<< '\n';
using namespace std;
typedef long long ll;

typedef pair<int,int> pii;
typedef vector<pii> vii;
typedef vector<int> vi;
typedef vector<vector<int>> adyList;   ///Sin pesos
//typedef vector<vector<pii>> adyList;   ///Con pesos

#define VISITED 1
#define UNVISITED -1
#define EXPLORED -1
#define MAXN  1000007
vi dfs_num;
adyList Graph;
bool mask[MAXN];
int cont[MAXN][4];

int main()
{

    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int t;
    cin>>t;
    while(t--)
    {
        int n,k;
        cin>>n>>k;
        string s;
        cin>>s;
        memset(cont,0,sizeof cont );
//        for(int i=0;i<n;i++)
//            mask[i]=s[i]-'0';
        for(int i=0;i<k;i++)
            cont[i][2] = n;
        for(int i=n-1;i>=0;i--)
        {
            if(s[i] == '1')
            {
                cont[i%k][3] = max(i,cont[i%k][3]);
            }
        }
        int total = 0;
        for(int i=0;i<n;i++)
        {
            if(s[i] == '1')
            {
                cont[i%k][2] = min(i,cont[i%k][2]);
                cont[i%k][0]++;
                total++;
            }
            else
            {
                if(cont[i%k][2] < i &&  i < cont[i%k][3])
                    cont[i%k][1]++;
            }
        }
        int minm = MAXN;
        for(int i=0;i<k;i++)
            minm = min(minm,total-cont[i][0]+cont[i][1]);
        db(minm)

    }

    return 0;
}


#include <bits/stdc++.h>
#define db(x) cout << (x) << '\n';
#define dbg(x) cerr << #x ": " << (x)<< '\n';
using namespace std;
typedef long long ll;

typedef pair<int,int> pii;
typedef vector<pii> vii;
typedef vector<int> vi;
typedef vector<vector<int>> adyList;   ///Sin pesos
//typedef vector<vector<pii>> adyList;   ///Con pesos

#define VISITED 1
#define UNVISITED -1
#define EXPLORED -1
#define MAXN  1000007
vi dfs_num;
adyList Graph;
//bool mask[MAXN];
int cont[MAXN][5];
vector<pii> unos[MAXN];
int main()
{

    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int t;
    cin>>t;
    while(t--)
    {
        int n,k;
        cin>>n>>k;
        string s;
        cin>>s;
//        for(int i=0;i<n;i++)
//            mask[i]=s[i]-'0';
        for(int i=0; i<k; i++)
            cont[i][2] = n;
        for(int i=n-1; i>=0; i--)
        {
            if(s[i] == '1')
            {
                cont[i%k][3] = max(i,cont[i%k][3]);
            }
        }
        int total = 0;
        for(int i=0; i<k; i++)
        {
            int p = 0;
            int c = 0;
            int t=0;
            bool ok = 0;
            for(int j=0; j*k+i<n; j++)
            {
                if(s[j*k+i] == '1')
                {
//                    cout << "1";
                    cont[i][2] = min(i,cont[i][2]);
                    cont[i][0]++;
                    total++;
                    p++;
                    if(t>0)
                    {
                        unos[i].push_back({t,c});
                        t=0;
                    }
                    c=0;
                    ok=1;
                }
                else
                {
//                    cout << "0";
                    if(cont[i][2] < i &&  i < cont[i][3])
                        cont[i][1]++;
                    if(ok)
                    {
                        t = p;
                        p=0;
                    }
                    ok = 0;
                    c++;
                    ok=0;
                }
            }
            if(ok)
                unos[i].push_back({p,0});
                else if(t>0)
                    unos[i].push_back({t,0});
        }
//        db("")
        for(int i=0;i<k;i++)
        {
            if(unos[i].size()==0)
                continue;
//                for(auto x :unos[i])
//                    cout << x.first << " "<<x.second << "\n";
            int tp = (int)unos[i].size();
            int dp[tp][21];
            dp[tp-1][0] = unos[i][tp-1].first;
            dp[tp-1][1] = 0;
            int acum = 0;
            for(int j=tp-2;j>=0;j--)
            {
                acum+=unos[i][j].first;
                dp[j][0] = unos[i][j].first+min(dp[j+1][0],dp[j+1][1]);
                dp[j][1] = min(dp[j+1][1]+unos[i][j].second,cont[i][0]-acum);
            }
//            db(dp[0][0])
//            db(dp[0][1])
            cont[i][4] = min(dp[0][0],dp[0][1]);
//            db("")
        }
        int minm = MAXN;
        for(int i=0; i<k; i++)
            minm = min(minm,total-cont[i][0]+cont[i][4]);
        db(minm)
        for(int i=0; i<k; i++)
            cont[i][0] = cont[i][1]=cont[i][2]=cont[i][3] = cont[i][4] = 0;
        for(int i=0;i<k;i++)
            unos[i].clear();
    }

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long int ll;
#define pb push_back
#define mp make_pair
#define N 200005

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);cout.tie(NULL);

 	int t;
 	cin>>t;
 	string s;
 	int i,n,k,j;
 	while(t--){
 		cin>>n>>k;
 		cin>>s;
 		int count=0;
 		for(i=0;i<n;i++)
 			if(s[i]=='1')
 				count++;
 		int ans=INT_MAX;

 		for(i=0;i<n;i++){
 			int val=0,r=0,g=0;
 			for(j=i;j<n;j+=k){
 				if(s[j]!='1'){
 					val++;
 					g=0;
 				}
 				else{
 					r++;
 					g=1;
 				}

 			}
 			if(g==0)
 				val--;
 			ans=min(ans,val+(count-r));
 		}
 		cout<<ans<<endl;
 	}
 	return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long int ll;
#define pb push_back
#define mp make_pair
#define N 200005

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);cout.tie(NULL);

 	int t;
 	cin>>t;
 	string s;
 	int i,n,k,j;
 	while(t--){
 		cin>>n>>k;
 		cin>>s;
 		int count=0;
 		for(i=0;i<n;i++)
 			if(s[i]=='1')
 				count++;
 		int ans=INT_MAX;

 		for(i=0;i<n;i++){
 			int val=0,r=0,g=0,idx=-1;
 			for(j=i;j<n;j+=k)
 				if(s[j]=='1')
 					idx=j;
 			for(j=i;j<=idx;j+=k){
 				if(s[j]!='1'){
 					val++;

 				}
 				else{
 					r++;
 				}

 			}
 			ans=min(ans,val+(count-r));
 		}
 		cout<<ans<<endl;
 	}
 	return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long int ll;
#define pb push_back
#define mp make_pair
#define N 200005

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);cout.tie(NULL);

 	int t;
 	cin>>t;
 	string s;
 	int i,n,k,j;
 	while(t--){
 		cin>>n>>k;
 		cin>>s;
 		int count=0;
 		for(i=0;i<n;i++)
 			if(s[i]=='1')
 				count++;
 		int ans=INT_MAX;

 		for(i=0;i<k;i++){
 			int curr=0,val=0;
 			for(j=i;j<n;j+=k){
 				if(s[j]=='1') curr++;
 				else curr--;
 				curr = max(0,curr);
 				val=max(val,curr);
 			}
 			ans=min(ans,count-val);
 		}
 		cout<<ans<<endl;
 	}
 	return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long int ll;
#define pb push_back
#define mp make_pair
#define N 200005

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);cout.tie(NULL);

 	int t;
 	cin>>t;
 	string s;
 	int i,n,k,j;
 	while(t--){
 		cin>>n>>k;
 		cin>>s;
 		int count=0;
 		for(i=0;i<n;i++)
 			if(s[i]=='1')
 				count++;
 		int ans=INT_MAX;

 		for(i=0;i<n;i++){
 			int curr=0,val=0;
 			for(j=i;j<n;j+=k){
 				if(s[j]=='1') curr++;
 				else curr--;
 				curr = max(0,curr);
 				val=max(val,curr);
 			}
 			ans=min(ans,count-val);
 		}
 		cout<<ans<<endl;
 	}
 	return 0;
}


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

int main(int no, char ** args){
  long long t, n, k, i, j, latest, min, totaldestroy, mindestroy;
  char val;
  std::cin >> t;
  std::vector<char> v;
  std::cout << std::fixed << std::setprecision(0);
  for(i=0;i<t;i++){
    std::cin >> n ;
    std::cin >> k;
    min = -1;
    v.clear();
    totaldestroy = 0;
    mindestroy = 0;
    for (j = 0; j < n; j++){
      std::cin >> val;
      v.push_back(val);
    }
    for(j=0;j<k;j++){
      std::vector<int> kinfo;
      latest=-1;
      kinfo.push_back(0); kinfo.push_back(0);
      for(auto l = v.begin()+j; l<v.end(); l+=k){
        if(*l == '0') continue;
        if (latest == -1){
          latest = l-v.begin();
          kinfo[1]++;
          //std::cout << "##?latest" << latest << " " << kinfo[1] << '\n';
          continue;
        }
        if (((l-v.begin())-latest) == k){
          kinfo[1]++;
          latest = l-v.begin();
          //std::cout << "##latest" << latest << " " << kinfo[1] << '\n';
          continue;
        }else{
          kinfo[0]+=((((l-v.begin())-latest)/k)-1);
          kinfo[1]++;
          latest = l-v.begin();
          //std::cout << "##make" << latest << " " << kinfo[0] << '\n';
        }
      }
      if(min == -1){
        min = std::min(kinfo[0], kinfo[1]-1);
        min = min<0 ? 0 : min;
        //std::cout << "##?end" << kinfo[0] << " " << kinfo[1] << '\n';
        mindestroy = kinfo[1];
      }
      else if (min < std::min(kinfo[0], kinfo[1]-1)){
        min = std::min(kinfo[0], kinfo[1]-1);
        min = min<0 ? 0 : min;
        //std::cout << "##end" << kinfo[0] << " " << kinfo[1] << '\n';
        mindestroy = kinfo[1];
      }
      totaldestroy += kinfo[1];
    }
    std::cout << min + (totaldestroy-mindestroy);
    std::cout << '\n';
  }
}
//


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

int main(int no, char ** args){
  long long t, n, k, i, j, latest, min, totaldestroy, mindestroy;
  char val;
  std::cin >> t;
  std::vector<char> v;
  std::vector< std::vector<long long> > u;
  std::cout << std::fixed << std::setprecision(0);
  for(i=0;i<t;i++){
    std::cin >> n ;
    std::cin >> k;
    min = -1;
    v.clear();
    u.clear();
    totaldestroy = 0;
    mindestroy = 0;
    for (j = 0; j < n; j++){
      std::cin >> val;
      v.push_back(val);
    }
    for(j=0;j<k;j++){
      std::vector<long long> kinfo;
      latest=-1;
      kinfo.push_back(0); kinfo.push_back(0);
      if (i==70){
        for (std::vector<char>::iterator x = v.begin(); x != v.end(); ++x)
        {
          std::cout<<(*x);
        }
        std::cout<<"x"<< k << "x";
      }
      for(auto l = v.begin()+j; l<v.end(); l+= k){
        if(*l == '0') continue;
        if (latest == -1){
          latest = l-v.begin();
          kinfo[1]++;
          // std::cout << "##?latest" << latest << " " << kinfo[1] << '\n';
          continue;
        }
        if (((l-v.begin())-latest) == k){
          kinfo[1]++;
          latest = l-v.begin();
          // std::cout << "##latest" << latest << " " << kinfo[1] << '\n';
          continue;
        }else{
          kinfo[0]+=((((l-v.begin())-latest)/k)-1);
          kinfo[1]++;
          latest = l-v.begin();
          // std::cout << "##make" << latest << " " << kinfo[0] << '\n';
        }
      }
      u.push_back(kinfo);
      totaldestroy += kinfo[1];
    }
    for(auto x = u.begin(); x!= u.end(); x++){
      if (min == -1) min = (*x)[0] + (totaldestroy- (*x)[1]);
      else min = std::min(min, (*x)[0] + (totaldestroy- (*x)[1]));
    }
    // std::cout << "^^^^^" << min << totaldestroy << ' ' << mindestroy << '\n';
    std::cout << (min);
    std::cout << '\n';
  }
}
//


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

const int MAXN = 2000005;
int trie[MAXN][27];
int similar[MAXN];
int len[MAXN];
int n;
string words[MAXN];
int sz = 1;
void insert(string s) {
	int cur = 0;
	for (int i = 0; i < s.size(); i++) {
		if (trie[cur][s[i]-'a'] == -1) {
			trie[cur][s[i]-'a'] = sz++;
		}
		cur = trie[cur][s[i]-'a'];
		len[cur] = i + 1;
	}
}

void getSimilar(string s) {
	int cur = 0;
	int cur2 = trie[0][s[0]-'a'];
	for (int i = 1; i < s.size(); i++) {
		if (cur != -1) cur = trie[cur][s[i]-'a'];
		cur2 = trie[cur2][s[i]-'a'];
		similar[cur2] = cur;
	}
}

int ord[MAXN];
bool bad[MAXN];
bool cmp(const int & a, const int & b) {
	return len[a] < len[b];
}
int solve() {
	for (int i = 0; i < n; i++) insert(words[i]);
	for (int i = 0; i < n; i++) getSimilar(words[i]);
	for (int i = 0; i < sz; i++) ord[i] = i;
	sort(ord, ord + sz, cmp);
	int cnt = 0;
	for (int i = sz - 1; i > 0; i--) {
		if (bad[i]) continue;
		cnt++;
		if (similar[i] != -1) bad[similar[i]] = true;
	}
	for (int i = 0; i < sz; i++) {
		for (int j = 0; j < 26; j++) trie[i][j] = -1;
		len[i] = 0;
		bad[i] = false;
		similar[i] = -1;
	}
	sz = 1;
	return cnt;
}
char s[MAXN];
int main() {
	memset(trie, -1, sizeof(trie));
	int T;
	scanf("%d", &T);
	while (T--) {
		scanf("%d", &n);
		for (int i = 0; i < n; i++) {
			scanf("%s", s);
			words[i] = string(s);
		}
		printf("%d\n", solve());
	}
}


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

const int MAXN = 2000005;
int trie[MAXN][27];
int similar[MAXN];
int len[MAXN];
int n;
string words[MAXN];
int sz = 1;
void insert(string s) {
	int cur = 0;
	for (int i = 0; i < s.size(); i++) {
		if (trie[cur][s[i]-'a'] == -1) {
			trie[cur][s[i]-'a'] = sz++;
		}
		cur = trie[cur][s[i]-'a'];
		len[cur] = i + 1;
	}
}

void getSimilar(string s) {
	int cur = 0;
	int cur2 = trie[0][s[0]-'a'];
	for (int i = 1; i < s.size(); i++) {
		if (cur != -1) cur = trie[cur][s[i]-'a'];
		cur2 = trie[cur2][s[i]-'a'];
		similar[cur2] = cur;
	}
}

int ord[MAXN];
bool bad[MAXN];
bool cmp(const int & a, const int & b) {
	return len[a] < len[b];
}
int solve() {
	sz = 1;
	for (int i = 0; i < n; i++) insert(words[i]);
	for (int i = 0; i < n; i++) getSimilar(words[i]);
	for (int i = 0; i < sz; i++) ord[i] = i;
	sort(ord, ord + sz, cmp);
	int cnt = 0;
	for (int i = sz - 1; i > 0; i--) {
		int x = ord[i];
		if (bad[x]) continue;
		cnt++;
		if (similar[x] != -1) bad[similar[x]] = true;
	}
	for (int i = 0; i < sz; i++) {
		for (int j = 0; j < 26; j++) trie[i][j] = -1;
		len[i] = 0;
		bad[i] = false;
		similar[i] = -1;
	}

	return cnt;
}
char s[MAXN];
int main() {
	memset(trie, -1, sizeof(trie));
	memset(similar, -1, sizeof(similar));
	int T;
	scanf("%d", &T);
	while (T--) {
		scanf("%d", &n);
		for (int i = 0; i < n; i++) {
			scanf("%s", s);
			words[i] = string(s);
		}
		printf("%d\n", solve());
	}
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
const int maxm = 26;
int tot, n;
int ch[maxn][maxm];
int newNode()
{
    memset(ch[tot], 0, sizeof ch[tot]);
    return tot++;
}
char s[maxn];
void add(char *s)
{
    int len = strlen(s);
    int cur = 0;
    for(int i = 0; i < len; ++ i)
    {
        int v = s[i] - 'a';
        if(ch[cur][v] == 0) ch[cur][v] = newNode();
        cur = ch[cur][v];
    }
}
string str[maxn];
vector<int> G[maxn];
void build(const string &s)
{
    int a = 0;
    int b = ch[0][s[0]-'a'];
    for(int i = 1; i < s.size(); ++ i)
    {
        int v = s[i] - 'a';
        a = ch[a][v];
        b = ch[b][v];
        if(a == 0 || b == 0) return;
        G[a].push_back(b);
        G[b].push_back(a);
    }
}
int f[maxn][2];
bool vis[maxn];
void dfs(int u)
{
    vis[u] = true;
    f[u][0] = 0;
    f[u][1] = 1;
    for(auto v : G[u])
    {
        if(vis[v]) continue;
        dfs(v);
        f[u][0] += max(f[v][0], f[v][1]);
        f[u][1] += f[v][0];
    }
}
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        tot=1;
        cin>>n;
        memset(ch[0],0,sizeof ch[0]);
        for(int i=0;i<n;i++)
        {
            scanf("%s",s);
            add(s);
            str[i]=s;
        }
        for(int i=1;i<tot;i++)
            G[i].clear();
        for(int i=0; i<n;i++)
            build(str[i]);
        for(int i=1;i<=tot;i++)
            vis[i] = false;
        int res=0;
        for(int i=1;i<tot;i++)
        {
            if(!vis[i])
            {
                dfs(i);
                res+=max(f[i][0],f[i][1]);
            }
        }
        cout<<res<<endl;
    }
    return 0;
}
//hello


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<cstdlib>
using namespace std;
char s[1000010];
int n,head[1000010],tot,f[1000010][2],T;
struct rd{int next,to;}road[1000010];
inline void addedge(int x,int y)
{
  road[++tot].next=head[x];
  road[tot].to=y;
  head[x]=tot;
}
struct trie
{
  int ch[1000010][26],fail[1000010],size[1000010],tot,F[1000010];
  inline void clear(){for(int i=1;i<=tot;i++) memset(ch,0,sizeof(ch)),fail[i]=0,size[i]=0; tot=0;}
  inline void add(char s[])
  {
    int n=strlen(s+1),now=0;
    for(int i=1;i<=n;i++)
    {
      if(ch[now][s[i]-'a']==0) ch[now][s[i]-'a']=++tot,size[tot]=size[now]+1;
      now=ch[now][s[i]-'a'];
    }
  }
  inline void build()
  {
    int first=1,last=0;
    for(int i=0;i<26;i++) if(ch[0][i]) F[++last]=ch[0][i];
    while(first<=last)
    {
      int x=F[first];
      for(int i=0;i<26;i++)
        if(ch[x][i]==0) ch[x][i]=ch[fail[x]][i];
        else fail[ch[x][i]]=ch[fail[x]][i],F[++last]=ch[x][i];
      if(size[fail[x]]+1==size[x]) addedge(fail[x],x);
      else addedge(0,x);
      first++;
    }
  }
}A;
inline void dfs(int x)
{
  f[x][0]=f[x][1]=0;
  for(int now=head[x];now;now=road[now].next)
  {
    dfs(road[now].to);
    f[x][0]+=max(f[road[now].to][0],f[road[now].to][1]);
    f[x][1]+=f[road[now].to][0];
  }
  f[x][1]++;
}
int main()
{
  cin>>T;
  while(T--)
  {
    cin>>n;
    tot=0;
    for(int i=0;i<=n;i++) head[i]=0;
    A.clear();
    for(int i=1;i<=n;i++) scanf("%s",s+1),A.add(s);
    A.build();
    dfs(0);
    cout<<f[0][0]<<endl;
  }
  return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<cstdlib>
using namespace std;
char s[1000010];
int n,head[1000010],tot,f[1000010][2],T;
struct rd{int next,to;}road[1000010];
inline void addedge(int x,int y)
{
  road[++tot].next=head[x];
  road[tot].to=y;
  head[x]=tot;
}
struct trie
{
  int ch[1000010][26],fail[1000010],size[1000010],tot,F[1000010];
  inline void clear(){for(int i=0;i<=tot;i++) memset(ch[i],0,sizeof(ch[i])),fail[i]=0,size[i]=0; tot=0;}
  inline void add(char s[])
  {
    int n=strlen(s+1),now=0;
    for(int i=1;i<=n;i++)
    {
      if(ch[now][s[i]-'a']==0) ch[now][s[i]-'a']=++tot,size[tot]=size[now]+1;
      now=ch[now][s[i]-'a'];
    }
  }
  inline void build()
  {
    int first=1,last=0;
    for(int i=0;i<26;i++) if(ch[0][i]) F[++last]=ch[0][i];
    while(first<=last)
    {
      int x=F[first];
      for(int i=0;i<26;i++)
        if(ch[x][i]==0) ch[x][i]=ch[fail[x]][i];
        else fail[ch[x][i]]=ch[fail[x]][i],F[++last]=ch[x][i];
      if(size[fail[x]]+1==size[x]) addedge(fail[x],x);
      else addedge(0,x);
      first++;
    }
  }
}A;
inline void dfs(int x)
{
  f[x][0]=f[x][1]=0;
  for(int now=head[x];now;now=road[now].next)
  {
    dfs(road[now].to);
    f[x][0]+=max(f[road[now].to][0],f[road[now].to][1]);
    f[x][1]+=f[road[now].to][0];
  }
  f[x][1]++;
}
int main()
{
  cin>>T;
  while(T--)
  {
    cin>>n;
    tot=0;
    for(int i=0;i<=n;i++) head[i]=0;
    A.clear();
    for(int i=1;i<=n;i++) scanf("%s",s+1),A.add(s);
    A.build();
    dfs(0);
    cout<<f[0][0]<<endl;
  }
  return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<cstdlib>
using namespace std;
char s[1000010];
int n,head[1000010],tot,f[1000010][2],T;
struct rd{int next,to;}road[1000010];
inline void addedge(int x,int y)
{
  road[++tot].next=head[x];
  road[tot].to=y;
  head[x]=tot;
}
struct trie
{
  int ch[1000010][26],fail[1000010],size[1000010],tot,F[1000010];
  inline void clear(){for(int i=0;i<=tot;i++) memset(ch[i],0,sizeof(ch[i])),fail[i]=0,size[i]=0; tot=0;}
  inline void add(char s[])
  {
    int n=strlen(s+1),now=0;
    for(int i=1;i<=n;i++)
    {
      if(ch[now][s[i]-'a']==0) ch[now][s[i]-'a']=++tot,size[tot]=size[now]+1;
      now=ch[now][s[i]-'a'];
    }
  }
  inline void build()
  {
    int first=1,last=0;
    for(int i=0;i<26;i++) if(ch[0][i]) F[++last]=ch[0][i];
    while(first<=last)
    {
      int x=F[first];
      for(int i=0;i<26;i++)
        if(ch[x][i]==0) ch[x][i]=ch[fail[x]][i];
        else fail[ch[x][i]]=ch[fail[x]][i],F[++last]=ch[x][i];
      if(size[fail[x]]+1==size[x]) addedge(fail[x],x);
      else addedge(0,x);
      first++;
    }
  }
}A;
inline void dfs(int x)
{
  f[x][0]=f[x][1]=0;
  for(int now=head[x];now;now=road[now].next)
  {
    dfs(road[now].to);
    f[x][0]+=max(f[road[now].to][0],f[road[now].to][1]);
    f[x][1]+=f[road[now].to][0];
  }
  f[x][1]++;
}
int main()
{
  cin>>T;
  while(T--)
  {
    cin>>n;
    tot=0;
    A.clear();
    for(int i=1;i<=n;i++) scanf("%s",s+1),A.add(s);
    for(int i=0;i<=A.tot;i++) head[i]=0;
    A.build();
    dfs(0);
    cout<<f[0][0]<<endl;
  }
  return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=1e6+10;
struct TrieNode
{
    int son[26];
} trie[maxn];
int last[maxn];
struct Edge
{
    int to,next;
} ed[maxn];
string st;
int tot,totedge,root;
bool vis[maxn];
int dp[maxn][2];
int newnode()
{
    ++tot;
    memset(&trie[tot],0,sizeof trie[0]);
    return tot;
}
void ins(string& st)
{
    int cur=root;
    for(int i=0;i<st.length();i++)
    {
        if(trie[cur].son[st[i]-'a']==0) trie[cur].son[st[i]-'a']=newnode();
        cur=trie[cur].son[st[i]-'a'];
    }
}
void addedge(int u,int v)
{
    ed[++totedge].to=v;
    ed[totedge].next=last[u];
    last[u]=totedge;
}
void dfs(int x1,int x2)
{
    if(x2!=root)
    {
        addedge(x1,x2);
        addedge(x2,x1);
    }
    for(int ch=0;ch<26;ch++)
    {
        if(trie[x1].son[ch]>0&&trie[x2].son[ch]>0) dfs(trie[x1].son[ch],trie[x2].son[ch]);
    }
}
void dfsdp(int x)
{
    vis[x]=true;
    dp[x][0]=0;
    dp[x][1]=1;
    for(int i=last[x];i>0;i=ed[i].next)
    {
        if(!vis[ed[i].to])
        {
            dfsdp(ed[i].to);
            dp[x][0]+=max(dp[ed[i].to][0],dp[ed[i].to][1]);
            dp[x][1]+=dp[ed[i].to][0];
        }
    }
}
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int n;
        cin>>n;
        tot=1;
        root=1;
        memset(&trie[1],0,sizeof trie[0]);
        for(int i=1;i<=n;i++)
        {
            cin>>st;
            ins(st);
        }
        totedge=0;
        for(int i=1;i<=tot;i++) last[i]=0;
        for(int ch=0;ch<26;ch++)
        {
            if(trie[root].son[ch]>0) dfs(trie[root].son[ch],root);
        }
        for(int i=1;i<=tot;i++) vis[i]=false;
        int ans=0;
        for(int i=1;i<=tot;i++) if(i!=root&&!vis[i])
        {
            dfsdp(i);
            ans+=max(dp[i][0],dp[i][1]);
        }
        cout<<ans<<'\n';
    }
    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key

typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> ii;
typedef vector<int> vi;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;
typedef set<ll>::iterator sit;
typedef map<ll,ll>::iterator mit;
const int MAXN = 2000011, sigma = 26;

int term[2][MAXN], to[MAXN][sigma], sz = 1;
int cnt;
int par[MAXN+111];
vi adj[MAXN+111];

int rt(int u)
{
	if(par[u]<0) return u;
	else return (par[u]=rt(par[u]));
}

void merge(int u, int v)
{
	u=rt(u); v=rt(v);
	if(u==v) return ;
	if(par[u]>par[v]) swap(u,v);
	par[u]+=par[v];
	par[v]=u;
}
bool used[2000111];
void add_str(string &s, bool type)
{
    int cur = 0;
    for(auto c: s)
    {
        if(!to[cur][c - 'a'])
        {
            to[cur][c - 'a'] = sz++;
        }
        cur = to[cur][c - 'a'];
        cnt++;
        if(!type)
        {
			used[cnt]=1;
			if(term[type][cur])
			{
				//cerr<<"MERGE : "<<term[type][cur]<<' '<<cnt<<'\n';
				merge(term[type][cur],cnt);
			}
			else
			{
				term[type][cur]=cnt;
			}
		}
		else
		{
			if(term[0][cur])
			{
				//cerr<<"ADJ : "<<rt(term[0][cur])<<' '<<rt(cnt)<<'\n';
				adj[rt(term[0][cur])].pb(rt(cnt)); //maybe add backward edge just to be safe?
				adj[rt(cnt)].pb(rt(term[0][cur]));
			}
		}
    }
}
bool vis[2000111];
ii dfs(int u, int p)
{
	vis[u]=1;
	ii tmp = mp(0,1);
	for(int i=0;i<adj[u].size();i++)
	{
		int v=adj[u][i];
		if(vis[v]||v==p) continue;
		ii z = dfs(v,u);
		tmp.se+=z.fi;
		tmp.fi+=max(z.fi,z.se);
	}
	return tmp;
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	int t; cin>>t;
	memset(par,-1,sizeof(par));
	while(t--)
	{
		int n; cin>>n;
		sz=1;
		int tot=0;
		cnt=0;
		vector<pair<string,int> > V;
		for(int i=0;i<n;i++)
		{
			string s; cin>>s;
			tot+=2*int(s.length());
			int cur = cnt;
			add_str(s,0);
			string t = s.substr(1,int(s.length()) - 1);
			V.pb(mp(t,cur+1));
			//add_str(t,1);
		}
		for(int i=0;i<n;i++)
		{
			cnt=V[i].se;
			add_str(V[i].fi,1);
		}
		int ans=0;
		for(int i=0;i<=tot+5;i++)
		{
			if(used[i]&&rt(i)==i&&!vis[i])
			{
				//cerr<<i<<'\n';
				ii tmp = dfs(i,-1);
				//cerr<<i<<' '<<tmp.fi<<' '<<tmp.se<<'\n';
				ans+=max(tmp.fi,tmp.se);
			}
		}
		cout<<ans<<'\n';
		for(int i=0;i<=tot+5;i++)
		{
			used[i]=0;par[i]=-1;vis[i]=0;
		}
		for(int i=0;i<=tot+5;i++) adj[i].clear();
		for(int i=0;i<=sz;i++)
		{
			term[0][i]=term[1][i]=0;
			for(int j=0;j<sigma;j++) to[i][j]=0;
		}
	}
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key

typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> ii;
typedef vector<int> vi;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;
typedef set<ll>::iterator sit;
typedef map<ll,ll>::iterator mit;
const int MAXN = 2000011, sigma = 26;

int term[2][MAXN], to[MAXN][sigma], sz = 1;
int cnt;
int par[MAXN+111];
vi adj[MAXN+111];

int rt(int u)
{
	if(par[u]<0) return u;
	else return (par[u]=rt(par[u]));
}

void merge(int u, int v)
{
	u=rt(u); v=rt(v);
	if(u==v) return ;
	if(par[u]>par[v]) swap(u,v);
	par[u]+=par[v];
	par[v]=u;
}
bool used[2000111];
void add_str(string &s, bool type)
{
    int cur = 0;
    for(auto c: s)
    {
        if(!to[cur][c - 'a'])
        {
            to[cur][c - 'a'] = sz++;
        }
        cur = to[cur][c - 'a'];
        cnt++;
        if(!type)
        {
			used[cnt]=1;
			if(term[type][cur])
			{
				//cerr<<"MERGE : "<<term[type][cur]<<' '<<cnt<<'\n';
				merge(term[type][cur],cnt);
			}
			else
			{
				term[type][cur]=cnt;
			}
		}
		else
		{
			if(term[0][cur])
			{
				//cerr<<"ADJ : "<<rt(term[0][cur])<<' '<<cnt<<'\n';
				adj[rt(term[0][cur])].pb(cnt); //maybe add backward edge just to be safe?
				adj[cnt].pb(rt(term[0][cur]));
			}
		}
    }
}
bool vis[2000111];
ii dfs(int u, int p)
{
	vis[u]=1;
	ii tmp = mp(0,1);
	for(int i=0;i<adj[u].size();i++)
	{
		int v=adj[u][i];
		if(vis[v]||v==p) continue;
		ii z = dfs(v,u);
		tmp.se+=z.fi;
		tmp.fi+=max(z.fi,z.se);
	}
	return tmp;
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	int t; cin>>t;
	memset(par,-1,sizeof(par));
	while(t--)
	{
		int n; cin>>n;
		sz=1;
		int tot=0;
		cnt=0;
		vector<pair<string,int> > V;
		for(int i=0;i<n;i++)
		{
			string s; cin>>s;
			tot+=2*int(s.length());
			int cur = cnt;
			add_str(s,0);
			string t = s.substr(1,int(s.length()) - 1);
			V.pb(mp(t,cur+1));
			//add_str(t,1);
		}
		for(int i=0;i<n;i++)
		{
			cnt=V[i].se;
			add_str(V[i].fi,1);
		}
		int ans=0;
		for(int i=0;i<=tot+5;i++)
		{
			if(used[i]&&rt(i)==i&&!vis[i])
			{
				////cerr<<i<<'\n';
				ii tmp = dfs(i,-1);
				//cerr<<i<<' '<<tmp.fi<<' '<<tmp.se<<'\n';
				ans+=max(tmp.fi,tmp.se);
			}
		}
		cout<<ans<<'\n';
		for(int i=0;i<=tot+5;i++)
		{
			used[i]=0;par[i]=-1;vis[i]=0;
		}
		for(int i=0;i<=tot+5;i++) adj[i].clear();
		for(int i=0;i<=sz;i++)
		{
			term[0][i]=term[1][i]=0;
			for(int j=0;j<sigma;j++) to[i][j]=0;
		}
	}
}


#include <stdio.h>
#include <cstring>
#include <vector>
using namespace std;
const int N=1000050;
char word[N];
int Trie[N][26],clock,root;
void Set(int &c, int pos, int sz)
{
	if(!c) c=++clock;
	if(pos>=sz) return;
	Set(Trie[c][word[pos]-'a'],pos+1,sz);
}
int Find(int c, int pos, int sz)
{
	if(!c) return 0;
	if(pos>=sz) return c;
	return Find(Trie[c][word[pos]-'a'],pos+1,sz);
}
vector<int> Tree[N];
void DFS(int c, int depth)
{
	if(depth>1)
	{
		int tmp=Find(root,1,depth);
		if(tmp)
		{
			Tree[tmp].push_back(c);
			Tree[c].push_back(tmp);
		}
	}
	int i;
	for(i=0;i<26;i++)
	{
		if(Trie[c][i])
		{
			word[depth]='a'+i;
			DFS(Trie[c][i],depth+1);
		}
	}
}
bool vis[N];
int sol[2];
void Solve(int cur, int prev, int depth)
{
	vis[cur]=true;
	sol[depth]++;
	int i;
	for(i=0;i<Tree[cur].size();i++)
	{
		int v=Tree[cur][i];
		if(v!=prev)
		{
			Solve(v,cur,depth^1);
		}
	}
}
int ans;
int max(int a, int b){ return a>b?a:b;}
int main()
{
	int t,n,i,j;
	scanf("%i",&t);
	while(t--)
	{
		scanf("%i",&n);
		for(i=0;i<n;i++)
		{
			scanf("%s",&word);
			Set(root,0,(unsigned) strlen(word));
		}
		DFS(root,0);
		for(i=2;i<=clock;i++)
		{
			if(!vis[i])
			{
				sol[0]=0;
				sol[1]=0;
				Solve(i,0,0);
				ans+=max(sol[0],sol[1]);
			}
		}
		printf("%i\n",ans);
		for(i=0;i<=clock;i++) for(j=0;j<26;j++) Trie[i][j]=0;
		for(i=0;i<=clock;i++) Tree[i].clear();
		for(i=0;i<=clock;i++) vis[i]=false;
		root=clock=ans=0;
	}
	return 0;
}


#include <stdio.h>
#include <cstring>
#include <vector>
using namespace std;
const int N=1000050;
char word[N];
int Trie[N][26],clock,root;
void Set(int &c, int pos, int sz)
{
	if(!c) c=++clock;
	if(pos>=sz) return;
	Set(Trie[c][word[pos]-'a'],pos+1,sz);
}
int Find(int c, int pos, int sz)
{
	if(!c) return 0;
	if(pos>=sz) return c;
	return Find(Trie[c][word[pos]-'a'],pos+1,sz);
}
vector<int> Tree[N];
void DFS(int c, int depth)
{
	if(depth>1)
	{
		int tmp=Find(root,1,depth);
		if(tmp)
		{
			Tree[tmp].push_back(c);
			Tree[c].push_back(tmp);
		}
	}
	int i;
	for(i=0;i<26;i++)
	{
		if(Trie[c][i])
		{
			word[depth]='a'+i;
			DFS(Trie[c][i],depth+1);
		}
	}
}
bool vis[N];
int sol[2];
int Sum[N];
int Best[N];
int max(int a, int b){ return a>b?a:b;}
void Solve(int cur, int prev, int depth)
{
	vis[cur]=true;
	sol[depth]++;
	Best[cur]=1;
	Sum[cur]=0;
	int i;
	for(i=0;i<Tree[cur].size();i++)
	{
		int v=Tree[cur][i];
		if(v!=prev)
		{
			Solve(v,cur,depth^1);
			Best[cur]+=Sum[v];
			Sum[cur]+=Best[v];
		}
	}
	Best[cur]=max(Best[cur],Sum[cur]);
}
int ans;
int main()
{
	int t,n,i,j;
	scanf("%i",&t);
	while(t--)
	{
		scanf("%i",&n);
		for(i=0;i<n;i++)
		{
			scanf("%s",&word);
			Set(root,0,(unsigned) strlen(word));
		}
		DFS(root,0);
		for(i=2;i<=clock;i++)
		{
			if(!vis[i])
			{
				sol[0]=0;
				sol[1]=0;
				Solve(i,0,0);
				//ans+=max(sol[0],sol[1]);
				ans+=max(Best[i],Sum[i]);
			}
		}
		printf("%i\n",ans);
		for(i=0;i<=clock;i++) for(j=0;j<26;j++) Trie[i][j]=0;
		for(i=0;i<=clock;i++) Tree[i].clear();
		for(i=0;i<=clock;i++) vis[i]=false;
		root=clock=ans=0;
	}
	return 0;
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <utility>
using namespace std;
#define REP(I,N) for (I=0;I<N;I++)
#define rREP(I,N) for (I=N-1;I>=0;I--)
#define rep(I,S,N) for (I=S;I<N;I++)
#define rrep(I,S,N) for (I=N-1;I>=S;I--)
#define FOR(I,S,N) for (I=S;I<=N;I++)
#define rFOR(I,S,N) for (I=N;I>=S;I--)
typedef unsigned long long ULL;
typedef long long LL;
const int INF=0x3f3f3f3f;
const LL INFF=0x3f3f3f3f3f3f3f3fll;
const LL M=1e9+7;
const LL maxn=1e6+7;
const double eps=0.00000001;
LL gcd(LL a,LL b){return b?gcd(b,a%b):a;}
template<typename T>inline T abs(T a) {return a>0?a:-a;}
template<typename T>inline T powMM(T a,T b){T ret=1;for (;b;b>>=1ll,a=a*a%M) if (b&1) ret=1ll*ret*a%M;return ret;}

int n;
set<int> edge[maxn];
int cnt;
int dp[maxn][2];
string a[maxn];
int nxt[maxn][26];
bool mark[maxn];//indegree!=0
void dfs(int x){
	dp[x][1]=1;dp[x][0]=0;
	for (int v:edge[x]){
		dfs(v);
		dp[x][0]+=dp[v][1];//不选
		dp[x][1]+=dp[v][0];//选 (max)
	}
	dp[x][1]=max(dp[x][1],dp[x][0]);
}
void solve(){
	scanf("%d",&n);
	int i;
	FOR(i,1,n){
//		printf("%d %d\n",i,cnt);
		cin>>a[i];
		string str=a[i];
		int i,len=str.length(),now=0;
		REP(i,len){
			if (!nxt[now][str[i]-'a']) nxt[now][str[i]-'a']=++cnt;
			now=nxt[now][str[i]-'a'];
		}
	}
//	printf("%d\n",cnt);
	FOR(i,1,n){
		string &str=a[i];
		int len=str.length(),i,now1=0,now2=0;
		now1=nxt[now1][str[0]-'a'];
		rep(i,1,len){
			now1=nxt[now1][str[i]-'a'];
			now2=nxt[now2][str[i]-'a'];
			if (now1&&now2){
				edge[now2].insert(now1);
				mark[now1]=1;
			}else break;
		}
	}
//	FOR(i,1,cnt)
//		for (int v:edge[i]) printf("%d->%d\n",i,v);
	int ans=0;
	FOR(i,1,cnt) if (!mark[i]){
		dfs(i);
		ans+=dp[i][1];
	}
//	FOR(i,1,cnt) printf("%d %d\n",dp[i][0],dp[i][1]);
	printf("%d\n",ans);
	FOR(i,0,cnt){
		mark[i]=0;
		edge[i].clear();
		memset(nxt[i],0,sizeof(nxt[i]));
	}cnt=0;
}
int main(){
	int T;
	scanf("%d",&T);
	while (T--) solve();
}
/*
33001
9
aaaaabbbb
a
bbababb
ab
abb
baa
bbbaabbabb
bbaaa
bbbbbaabaa
*/


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <utility>
using namespace std;
#define REP(I,N) for (I=0;I<N;I++)
#define rREP(I,N) for (I=N-1;I>=0;I--)
#define rep(I,S,N) for (I=S;I<N;I++)
#define rrep(I,S,N) for (I=N-1;I>=S;I--)
#define FOR(I,S,N) for (I=S;I<=N;I++)
#define rFOR(I,S,N) for (I=N;I>=S;I--)
typedef unsigned long long ULL;
typedef long long LL;
const int INF=0x3f3f3f3f;
const LL INFF=0x3f3f3f3f3f3f3f3fll;
const LL M=1e9+7;
const LL maxn=1e6+7;
const double eps=0.00000001;
LL gcd(LL a,LL b){return b?gcd(b,a%b):a;}
template<typename T>inline T abs(T a) {return a>0?a:-a;}
template<typename T>inline T powMM(T a,T b){T ret=1;for (;b;b>>=1ll,a=a*a%M) if (b&1) ret=1ll*ret*a%M;return ret;}

int n;
vector<int> edge[maxn];
int cnt;
int dp[maxn][2];
string a[maxn];
int nxt[maxn][26];
bool mark[maxn];//indegree!=0
void dfs(int x){
	dp[x][1]=1;dp[x][0]=0;
	for (int v:edge[x]){
		dfs(v);
		dp[x][0]+=dp[v][1];//不选
		dp[x][1]+=dp[v][0];//选 (max)
	}
	dp[x][1]=max(dp[x][1],dp[x][0]);
}
void solve(){
	scanf("%d",&n);
	int i;
	FOR(i,1,n){
//		printf("%d %d\n",i,cnt);
		cin>>a[i];
		string str=a[i];
		int i,len=str.length(),now=0;
		REP(i,len){
			if (!nxt[now][str[i]-'a']) nxt[now][str[i]-'a']=++cnt;
			now=nxt[now][str[i]-'a'];
		}
	}
//	printf("%d\n",cnt);
	FOR(i,1,n){
		string &str=a[i];
		int len=str.length(),i,now1=0,now2=0;
		now1=nxt[now1][str[0]-'a'];
		rep(i,1,len){
			now1=nxt[now1][str[i]-'a'];
			now2=nxt[now2][str[i]-'a'];
			if (now1&&now2){
				edge[now2].push_back(now1);
				mark[now1]=1;
			}else break;
		}
	}
//	FOR(i,1,cnt)
//		for (int v:edge[i]) printf("%d->%d\n",i,v);
	int ans=0;
	FOR(i,1,cnt) if (!mark[i]){
		dfs(i);
		ans+=dp[i][1];
	}
//	FOR(i,1,cnt) printf("%d %d\n",dp[i][0],dp[i][1]);
	printf("%d\n",ans);
	FOR(i,0,cnt){
		mark[i]=0;
		edge[i].clear();
		memset(nxt[i],0,sizeof(nxt[i]));
	}cnt=0;
}
int main(){
	int T;
	scanf("%d",&T);
	while (T--) solve();
}
/*
33001
9
aaaaabbbb
a
bbababb
ab
abb
baa
bbbaabbabb
bbaaa
bbbbbaabaa
*/


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <utility>
using namespace std;
#define REP(I,N) for (I=0;I<N;I++)
#define rREP(I,N) for (I=N-1;I>=0;I--)
#define rep(I,S,N) for (I=S;I<N;I++)
#define rrep(I,S,N) for (I=N-1;I>=S;I--)
#define FOR(I,S,N) for (I=S;I<=N;I++)
#define rFOR(I,S,N) for (I=N;I>=S;I--)
typedef unsigned long long ULL;
typedef long long LL;
const int INF=0x3f3f3f3f;
const LL INFF=0x3f3f3f3f3f3f3f3fll;
const LL M=1e9+7;
const LL maxn=1e6+7;
const double eps=0.00000001;
LL gcd(LL a,LL b){return b?gcd(b,a%b):a;}
template<typename T>inline T abs(T a) {return a>0?a:-a;}
template<typename T>inline T powMM(T a,T b){T ret=1;for (;b;b>>=1ll,a=a*a%M) if (b&1) ret=1ll*ret*a%M;return ret;}

int n;
vector<int> edge[maxn];
int cnt;
int dp[maxn][2];
string a[maxn];
int nxt[maxn][26];
void solve(){
	scanf("%d",&n);
	int i;
	FOR(i,1,n){
		cin>>a[i];
		string str=a[i];
		int i,len=str.length(),now=0;
		REP(i,len){
			if (!nxt[now][str[i]-'a']) nxt[now][str[i]-'a']=++cnt;
			now=nxt[now][str[i]-'a'];
		}
	}
	FOR(i,1,n){
		string &str=a[i];
		int len=str.length(),i,now1=0,now2=0;
		now1=nxt[now1][str[0]-'a'];
		rep(i,1,len){
			now1=nxt[now1][str[i]-'a'];
			now2=nxt[now2][str[i]-'a'];
			if (now1&&now2){
				if (now1>now2) swap(now1,now2);
//				printf("%d->%d\n",now1,now2);
				edge[now1].push_back(now2);
			}else break;
		}
	}
	int ans=0;
	rFOR(i,1,cnt){
		dp[i][1]=1;dp[i][0]=0;
		for (int v:edge[i]){
			ans-=dp[v][1];
			dp[i][0]+=dp[v][1];//不选
			dp[i][1]+=dp[v][0];//选
		}
		dp[i][1]=max(dp[i][1],dp[i][0]);
		ans+=dp[i][1];
	}
	printf("%d\n",ans);
	FOR(i,0,cnt){
		edge[i].clear();
		memset(nxt[i],0,sizeof(nxt[i]));
	}cnt=0;
}
int main(){
	int T;
	scanf("%d",&T);
	while (T--) solve();
}
/*
2
3
aba
baba
aaab
2
aa
a
*/


#include "bits/stdc++.h"
using namespace std;
const int N = 1e6 + 6;
int t;
int n;
char str[N];
int child[N][26];
int cur;
void insert(){
	int node = 1;
	for(int i = 0 ; str[i] ; ++i){
		int c = str[i] - 'a';
		if(!child[node][c]){
			child[node][c] = ++cur;
		}
		node = child[node][c];
	}
}
int lcs[N];
int que[N];
int dep[N];
int ql , qr;
inline int find(int node , int c){
	if(child[node][c]){
		return child[node][c];
	}
	if(node == 1){
		return 1;
	}
	return find(lcs[node] , c);
}
void bfs(){
	ql = 0;
	qr = 0;
	lcs[1] = 1;
	dep[1] = 0;
	que[qr++] = 1;
	while(ql < qr){
		int node = que[ql++];
		for(int i = 0 ; i < 26 ; ++i){
			if(!child[node][i]){
				continue;
			}
			int next = child[node][i];
			if(node == 1){
				lcs[next] = node;
			}
			else{
				lcs[next] = find(lcs[node] , i);
			}
			dep[next] = dep[node] + 1;
			que[qr++] = next;
		}
	}
}
vector < int > v[N];
int visited[N];
int tc;
int ans;
int dp[N][2];
void dfs(int node){
	visited[node] = tc;
	dp[node][1] = 1;
	dp[node][0] = 0;
	for(int next : v[node]){
		if(visited[next] == tc){
			continue;
		}
		dfs(next);
		dp[node][0] += dp[next][1];
		dp[node][1] += dp[next][0];
	}
	dp[node][1] = max(dp[node][1] , dp[node][0]);
}
int main(){
	scanf("%d" , &t);
	while(t--){
		scanf("%d" , &n);
		for(int i = 1 ; i <= cur ; ++i){
			memset(child[i] , 0 , sizeof(child[i]));
		}
		cur = 1;
		for(int i = 1 ; i <= n ; ++i){
			scanf("%s" , str);
			insert();
		}
		bfs();
		for(int i = 1 ; i <= cur ; ++i){
			v[i].clear();
		}
		for(int i = 2 ; i <= cur ; ++i){
			if(dep[lcs[i]] + 1 == dep[i] && lcs[i] != 1){
				v[lcs[i]].emplace_back(i);
				v[i].emplace_back(lcs[i]);
			}
		}
		++tc;
		ans = 0;
		for(int i = 2 ; i <= cur ; ++i){
			if(visited[i] != tc){
				dfs(i);
				ans += dp[i][1];
			}
		}
		printf("%d\n" , ans);
	}
}


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

typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef vector<int> VI;
typedef pair<int,int> PII;

#define REP(i,n) for(int i=0;i<(n);++i)
#define SIZE(c) ((int)((c).size()))
#define FOR(i,a,b) for (int i=(a); i<(b); ++i)
#define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i)
#define FORD(i,a,b) for (int i=(a)-1; i>=(b); --i)
#define ALL(v) (v).begin(), (v).end()

#define pb push_back
#define mp make_pair
#define st first
#define nd second

namespace aho {
  const int SIGMA = 26;    // licznosc alfabetu
  const int MAXBUF = 1000005; // maksymalna liczba wezlow + 1

  inline int alpha(char c) { return c-'a'; } // numer znaku w alfabecie

  // Algorytm zwraca nastepujaca strukture
  struct node {
      node *prefix, *next[SIGMA]; // funkcja prefiksowa, funkcja przejsc
      const char *end;            // wzorzec, ktorego koncem jest dany stan
      node *accept;               // nastepnik na liscie stanow akceptujacych
      bool take;
      int d;
  /* Uwaga: Tablice next mozna zmienic na mape. Wtedy potrzebna jest funkcja:
      node *get_next(int a) {
          node *v = this;
          while (!v->next.count(a)) v = v->prefix;
          return v->next[a];
      }                                                                         */
      node *get_accept() { return end ? this : accept; }
      void evaluate(int a, queue<node*> &Q) { // mapa: zmienic funkcje tak, aby
          if (next[a]) {        // przyjmowala iterator, i wpisac tylko tresc ifa
              next[a]->prefix = prefix->next[a]; // mapa: prefix->get_next(i->FI)
              next[a]->accept = next[a]->prefix->get_accept();
              Q.push(next[a]);
          }
          else next[a] = prefix->next[a]; // mapa: pominac
      }
  };

  node buf[MAXBUF]; // mapa: po zakonczeniu wyczyscic wszystkie nexty
  int bufc;         // przed konstrukcja automatu wyzerowac!

  node *get_node() { // pobiera nowy wezel z bufora
    FOR(a,0,SIGMA) buf[bufc].next[a] = 0; // mapa: pominac
    buf[bufc].end = 0;
    buf[bufc].accept = 0;
    buf[bufc].take = true;
    buf[bufc].d = 0;
    return buf+bufc++;
  }

  // Dodawanie wzorca do struktury automatu
  void add_str(node *root, const char *str) { // na poczatku root = get_node()
      for (int i=0; str[i]; ++i) {
          int a = alpha(str[i]);
          if (!root->next[a]) {
            root->next[a] = get_node(); // mapa: zmienic warunek
            root->next[a]->d = root->d + 1;
          }
          root = root->next[a];                         // na !root->next.count(a)
      }
      root->end = str;
  }

  // Obliczenie funkcji prefiksowej i przejsc po dodaniu wszystkich wzorcow
  void evaluate(node *root) {
      root->prefix = get_node();
      FOR(a,0,SIGMA) root->prefix->next[a] = root;
      queue<node*> Q; Q.push(root);
      while (!Q.empty()) {
          node *cur = Q.front(); Q.pop();
          FOR(a,0,SIGMA) cur->evaluate(a, Q); // mapa: FORE(i,cur->next)
      }                                       //         cur->evaluate(i, Q);
  }

  int get(node* root) {
    vector<node*> bfs;
    bfs.pb(root);
    for (int k = 0; k < bfs.size(); ++k) {
      REP(a, SIGMA) if (bfs[k]->next[a] && bfs[k]->next[a]->d == bfs[k]->d + 1) {
        bfs.pb(bfs[k]->next[a]);
      }
    }

    int result = 0;
    FORD(k, bfs.size(), 1) {
      // printf("(%d)%d %d %d\n", bfs[k] - buf, bfs[k]->d, bfs[k]->take, bfs[k]->prefix ? bfs[k]->prefix->d : -1);
      if (bfs[k]->take) {
        ++result;
        if (bfs[k]->prefix && bfs[k]->prefix->d == bfs[k]->d - 1)
          bfs[k]->prefix->take = false;
      }
    }

    return result;
  }
};

const int MOD = 1000000007;

char buf[1000005];

void scase() {
  int N;
  scanf("%d", &N);

  aho::bufc = 0;

  aho::node* root = aho::get_node();

  REP(i,N) {
    scanf("%s", buf);
    aho::add_str(root, buf);
  }

  evaluate(root);
  // return;
  int result = aho::get(root);
  printf("%d\n", result);
}

int main() {
  // ios_base::sync_with_stdio(0);

  int T;
  scanf("%d", &T);
  while (T--) scase();
}


//let's do it
 #include <iostream>
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define inf 1000000000000
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define S second
#define F first
#define boost1 ios::sync_with_stdio(false);
#define boost2 cin.tie(0);
#define mem(a,val) memset(a,val,sizeof a)
#define endl "\n"
#define maxn 1000005

ll dp[maxn][2],len[maxn],vis[maxn],mod1=mod,mod2=mod+2;
map<pair<ll,ll>,ll>assign;
set<pair<pair<ll,ll>,pair<ll,ll> > >store;
vector<ll>v[maxn];
string str[maxn];

inline ll add1(ll x,ll y)
{
	x+=y;
	if(x>=mod1)
	x-=mod1;
	return x;
}
inline ll mul1(ll x,ll y)
{
	return (x*y)%mod1;
}
inline ll add2(ll x,ll y)
{
	x+=y;
	if(x>=mod2)
	x-=mod2;
	return x;
}
inline ll mul2(ll x,ll y)
{
	return (x*y)%mod2;
}
void dfs(ll cur,ll par)
{
	vis[cur]=1;
	dp[cur][1]=1;
	dp[cur][0]=0;
	for(ll i=0;i<v[cur].size();i++)
	{
		ll x=v[cur][i];
		if(x==par)
		continue;
		dfs(x,cur);
		dp[cur][0]+=max(dp[x][0],dp[x][1]);
		dp[cur][1]+=dp[x][0];
	}
}
int main()
{
	boost1;boost2;
	ll t,i,j,n,x,y,pre1,pre2,base1=127,base2=129,nodes,ans,pre11,pre22;
	cin>>t;
	while(t--)
	{
		cin>>n;
		for(i=1;i<=n;i++)
		{
			cin>>str[i];
			len[i]=str[i].length();
		}
		store.clear();
		for(i=1;i<=n;i++)
		{
			store.insert({{str[i][0],str[i][0]},{-1,-1}});
			pre1=str[i][0];
			pre2=0;
			pre11=str[i][0];
			pre22=0;
			if(len[i]==1)
			continue;
			pre1=add1(mul1(pre1,base1),str[i][1]);
			pre2=str[i][1];
			pre11=add2(mul2(pre11,base2),str[i][1]);
			pre22=str[i][1];
			store.insert({{pre1,pre11},{pre2,pre22}});
			for(j=2;j<len[i];j++)
			{
				pre1=add1(mul1(pre1,base1),str[i][j]);
				pre2=add1(mul1(pre2,base1),str[i][j]);
				pre11=add2(mul2(pre11,base2),str[i][j]);
				pre22=add2(mul2(pre22,base2),str[i][j]);
				store.insert({{pre1,pre11},{pre2,pre22}});
			}
		}
		/*for(auto it:store)
		cout<<it.F<<" "<<it.S<<endl;
		cout<<"here"<<endl;*/
		assign.clear();
		nodes=0;
		for(auto it:store)
		{
			nodes++;
			assign[it.F]=nodes;
		}
		for(i=0;i<=nodes;i++)
		{
			v[i].clear();
			dp[i][0]=dp[i][1]=0;
			vis[i]=0;
		}
		for(auto it:store)
		{
			x=assign[it.F];
			if(assign.find(it.S)!=assign.end())
			{
				y=assign[it.S];
				v[x].pb(y);
				v[y].pb(x);
			}
		}
		//cout<<"here"<<endl;
		/*for(i=1;i<=nodes;i++)
		{
			for(auto it:v[i])
			cout<<i<<" "<<it<<endl;
		}*/
		ans=0;
		for(i=1;i<=nodes;i++)
		{
			if(vis[i])
			continue;
			dfs(i,-1);
			ans+=max(dp[i][0],dp[i][1]);
		}
		cout<<ans<<endl;
	}
	return 0;
}
/*
1
3
aba
baba
aaab
*/


//let's do it
 #include <iostream>
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define inf 1000000000000
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define S second
#define F first
#define boost1 ios::sync_with_stdio(false);
#define boost2 cin.tie(0);
#define mem(a,val) memset(a,val,sizeof a)
#define endl "\n"
#define maxn 1000005

ll dp[maxn][2],len[maxn],vis[maxn];
map<ll,ll>assign;
set<pair<ll,ll> >store;
vector<ll>v[maxn];
string str[maxn];

inline ll add(ll x,ll y)
{
	x+=y;
	if(x>=mod)
	x-=mod;
	return x;
}
inline ll sub(ll x,ll y)
{
	return (x-y+mod)%mod;
}
inline ll mul(ll x,ll y)
{
	return (x*y)%mod;
}
void dfs(ll cur,ll par)
{
	vis[cur]=1;
	dp[cur][1]=1;
	dp[cur][0]=0;
	for(ll i=0;i<v[cur].size();i++)
	{
		ll x=v[cur][i];
		if(x==par)
		continue;
		dfs(x,cur);
		dp[cur][0]+=max(dp[x][0],dp[x][1]);
		dp[cur][1]+=dp[x][0];
	}
}
int main()
{
	boost1;boost2;
	ll t,i,j,n,x,y,pre1,pre2,base=127,nodes,ans;
	cin>>t;
	while(t--)
	{
		cin>>n;
		for(i=1;i<=n;i++)
		{
			cin>>str[i];
			len[i]=str[i].length();
		}
		store.clear();
		for(i=1;i<=n;i++)
		{
			store.insert({str[i][0],-1});
			pre1=str[i][0];
			pre2=0;
			if(len[i]==1)
			continue;
			pre1=add(mul(pre1,base),str[i][1]);
			pre2=str[i][1];
			store.insert({pre1,pre2});
			for(j=2;j<len[i];j++)
			{
				pre1=add(mul(pre1,base),str[i][j]);
				pre2=add(mul(pre2,base),str[i][j]);
				store.insert({pre1,pre2});
			}
		}
		/*for(auto it:store)
		cout<<it.F<<" "<<it.S<<endl;
		cout<<"here"<<endl;*/
		assign.clear();
		nodes=0;
		for(auto it:store)
		{
			nodes++;
			assign[it.F]=nodes;
		}
		for(i=0;i<=nodes;i++)
		{
			v[i].clear();
			dp[i][0]=dp[i][1]=0;
			vis[i]=0;
		}
		for(auto it:store)
		{
			x=assign[it.F];
			if(assign.find(it.S)!=assign.end())
			{
				y=assign[it.S];
				v[x].pb(y);
				v[y].pb(x);
			}
		}
		//cout<<"here"<<endl;
		/*for(i=1;i<=nodes;i++)
		{
			for(auto it:v[i])
			cout<<i<<" "<<it<<endl;
		}*/
		ans=0;
		for(i=1;i<=nodes;i++)
		{
			if(vis[i])
			continue;
			dfs(i,-1);
			ans+=max(dp[i][0],dp[i][1]);
		}
		cout<<ans<<endl;
	}
	return 0;
}
/*
1
3
aba
baba
aaab
*/


#include<cstdio>
#include<cctype>
#include<iostream>

using namespace std;

#define maxn 1001001

inline int read(){
	int r=0,f=0;
	char c;
	while(!isdigit(c=getchar()))f|=(c=='-');
	while(isdigit(c))r=(r<<1)+(r<<3)+(c^48),c=getchar();
	return f?-r:r;
}

inline int max(int a,int b){
	return a>b?a:b;
}

struct E{
	int v,nxt;
	E() {}
	E(int v,int nxt):v(v),nxt(nxt) {}
}e[maxn<<1];

string w[maxn];

bool vis[maxn];

int Del[maxn][2];

int n,s_e,tot,head[maxn],f[maxn][2],ch[maxn][33];

inline void a_e(int u,int v){
	e[++s_e]=E(v,head[u]);
	head[u]=s_e;
}

inline void Insert(string s){
	int p=0;
    for(int i=0;i<(int)s.size();i++){
		int c=s[i]-'a';
		if(!ch[p][c]){
			ch[p][c]=++tot;
			Del[tot][0]=p;
			Del[tot][1]=c;
		}
		p=ch[p][c];
	}
}

inline void add(string s){
	int x=0,y=ch[0][s[0]-'a'];
	for(int i=1;i<(int)s.size();i++){
		int c=s[i]-'a';
	    x=ch[x][c];
		y=ch[y][c];
		if(!x||!y)return;
		a_e(x,y);
		a_e(y,x);
	}
}

void dfs(int u){
	vis[u]=1;
	f[u][0]=0,f[u][1]=1;
	for(int i=head[u];i;i=e[i].nxt){
		if(vis[e[i].v])continue;
		dfs(e[i].v);
        int v=e[i].v;
		f[u][0]+=max(f[v][0],f[v][1]);
		f[u][1]+=f[v][0];
	}
}

inline void work(){
	s_e=0;
    while(tot){
		ch[Del[tot][0]][Del[tot][1]]=0;
		head[tot]=0,vis[tot]=0;
		tot--;
	}
    cin>>n;
	for(int i=1;i<=n;i++){
		cin>>w[i];
		Insert(w[i]);
	}
	for(int i=1;i<=n;i++)add(w[i]);
	int ans=0;
	for(int i=1;i<=tot;i++){
		if(vis[i])continue;
		dfs(i);
		ans+=max(f[i][0],f[i][1]);
	}
	printf("%d\n",ans);
}

int main(){
	int t=read();
	while(t--)work();
	return 0;
}


#include<cstdio>
#include<cctype>
#include<iostream>

using namespace std;

#define maxn 1001001

inline int read(){
	int r=0,f=0;
	char c;
	while(!isdigit(c=getchar()))f|=(c=='-');
	while(isdigit(c))r=(r<<1)+(r<<3)+(c^48),c=getchar();
	return f?-r:r;
}

inline int max(int a,int b){
	return a>b?a:b;
}

struct E{
	int v,nxt;
	E() {}
	E(int v,int nxt):v(v),nxt(nxt) {}
}e[maxn<<1];

string w[maxn];

bool vis[maxn];

int Del[maxn][2];

int n,s_e,tot,head[maxn],f[maxn][2],ch[maxn][33];

inline void a_e(int u,int v){
	e[++s_e]=E(v,head[u]);
	head[u]=s_e;
}

inline void Insert(string s){
	int p=0;
    for(int i=0;i<(int)s.size();i++){
		int c=s[i]-'a';
		if(!ch[p][c]){
			ch[p][c]=++tot;
			Del[tot][0]=p;
			Del[tot][1]=c;
		}
		p=ch[p][c];
	}
}

inline void add(string s){
	int x=0,y=ch[0][s[0]-'a'];
	for(int i=1;i<(int)s.size();i++){
		int c=s[i]-'a';
	    x=ch[x][c];
		y=ch[y][c];
		if(!x||!y)return;
		a_e(x,y);
		a_e(y,x);
	}
}

void dfs(int u,int fa){
	vis[u]=1;
	f[u][0]=0,f[u][1]=1;
	for(int i=head[u];i;i=e[i].nxt){
		int v=e[i].v;
		if(v==fa)continue;
		dfs(v,u);
		f[u][0]+=max(f[v][0],f[v][1]);
		f[u][1]+=f[v][0];
	}
}

inline void work(){
	s_e=0;
    while(tot){
		ch[Del[tot][0]][Del[tot][1]]=0;
		head[tot]=0,vis[tot]=0;
		tot--;
	}
    cin>>n;
	for(int i=1;i<=n;i++){
		cin>>w[i];
		Insert(w[i]);
	}
	for(int i=1;i<=n;i++)add(w[i]);
	int ans=0;
	for(int i=1;i<=tot;i++){
		if(vis[i])continue;
		dfs(i,0);
		ans+=max(f[i][0],f[i][1]);
	}
	printf("%d\n",ans);
}

int main(){
	int t=read();
	while(t--)work();
	return 0;
}


#include<cstdio>
#include<cctype>
#include<iostream>

using namespace std;

#define maxn 1001001

inline int read(){
	int r=0,f=0;
	char c;
	while(!isdigit(c=getchar()))f|=(c=='-');
	while(isdigit(c))r=(r<<1)+(r<<3)+(c^48),c=getchar();
	return f?-r:r;
}

inline int max(int a,int b){
	return a>b?a:b;
}

struct E{
	int v,nxt;
	E() {}
	E(int v,int nxt):v(v),nxt(nxt) {}
}e[maxn<<1];

string w[maxn];

bool vis[maxn];

int Del[maxn][2];

int n,s_e,tot,head[maxn],f[maxn][2],ch[maxn][33];

inline void a_e(int u,int v){
	e[++s_e]=E(v,head[u]);
	head[u]=s_e;
}

inline void Insert(string s){
	int p=0;
    for(int i=0;i<(int)s.size();i++){
		int c=s[i]-'a';
		if(!ch[p][c]){
			ch[p][c]=++tot;
			Del[tot][0]=p;
			Del[tot][1]=c;
		}
		p=ch[p][c];
	}
}

inline void add(string s){
	int x=0,y=ch[0][s[0]-'a'];
	for(int i=1;i<(int)s.size();i++){
		int c=s[i]-'a';
	    x=ch[x][c];
		y=ch[y][c];
		if(!x||!y)return;
		a_e(x,y);
		a_e(y,x);
	}
}

void dfs(int u,int fa){
	vis[u]=1;
	f[u][0]=0,f[u][1]=1;
	for(int i=head[u];i;i=e[i].nxt){
		int v=e[i].v;
		if(v==fa)continue;
		dfs(v,u);
		f[u][0]+=max(f[v][0],f[v][1]);
		f[u][1]+=f[v][0];
	}
}

inline void work(){
	s_e=0;
    while(tot){
		ch[Del[tot][0]][Del[tot][1]]=0;
		head[tot]=0,vis[tot]=0;
		tot--;
	}
    cin>>n;
	for(int i=1;i<=n;i++){
		cin>>w[i];
		Insert(w[i]);
	}
	for(int i=1;i<=n;i++)add(w[i]);
	int ans=0;
	for(int i=1;i<=tot;i++){
		if(vis[i])continue;
		dfs(i,-1);
		ans+=max(f[i][0],f[i][1]);
	}
	printf("%d\n",ans);
}

int main(){
	int t=read();
	while(t--)work();
	return 0;
}


#include<cstdio>
#include<cctype>
#include<iostream>

using namespace std;

#define maxn 1001001

inline int read(){
	int r=0,f=0;
	char c;
	while(!isdigit(c=getchar()))f|=(c=='-');
	while(isdigit(c))r=(r<<1)+(r<<3)+(c^48),c=getchar();
	return f?-r:r;
}

inline int max(int a,int b){
	return a>b?a:b;
}

struct E{
	int v,nxt;
	E() {}
	E(int v,int nxt):v(v),nxt(nxt) {}
}e[maxn<<1];

string w[maxn];

bool vis[maxn];

int Del[maxn][2];

int n,s_e,tot,head[maxn],f[maxn][2],ch[maxn][33];

inline void a_e(int u,int v){
	e[++s_e]=E(v,head[u]);
	head[u]=s_e;
}

inline void Insert(string s){
	int p=0;
    for(int i=0;i<(int)s.size();i++){
		int c=s[i]-'a';
		if(!ch[p][c]){
			ch[p][c]=++tot;
			Del[tot][0]=p;
			Del[tot][1]=c;
		}
		p=ch[p][c];
	}
}

inline void add(string s){
	int x=0,y=ch[0][s[0]-'a'];
	for(int i=1;i<(int)s.size();i++){
		int c=s[i]-'a';
	    x=ch[x][c];
		y=ch[y][c];
		if(!x||!y)return;
		a_e(x,y);
		a_e(y,x);
	}
}

void dfs(int u){
	vis[u]=1;
	f[u][0]=0,f[u][1]=1;
	for(int i=head[u];i;i=e[i].nxt){
		if(vis[e[i].v])continue;
		dfs(e[i].v);
        int v=e[i].v;
		f[u][0]+=max(f[v][0],f[v][1]);
		f[u][1]+=f[v][0];
	}
}

inline void work(){
	s_e=0;
    while(tot){
		ch[Del[tot][0]][Del[tot][1]]=0;
		head[tot]=0,vis[tot]=0;
		tot--;
	}
    cin>>n;
	for(int i=1;i<=n;i++){
		cin>>w[i];
		Insert(w[i]);
	}
	for(int i=1;i<=n;i++)add(w[i]);
	int ans=0;
	for(int i=1;i<=tot;i++){
		if(vis[i])continue;
		dfs(i);
		ans+=max(f[i][0],f[i][1]);
	}
	printf("%d\n",ans);
}

int main(){
	freopen("1.in","r",stdin);
	freopen("1.out","w",stdout);
	int t=read();
	while(t--)work();
	return 0;
}


#include<cstdio>
#include<cctype>
#include<iostream>

using namespace std;

#define maxn 1001001

inline int read(){
	int r=0,f=0;
	char c;
	while(!isdigit(c=getchar()))f|=(c=='-');
	while(isdigit(c))r=(r<<1)+(r<<3)+(c^48),c=getchar();
	return f?-r:r;
}

inline int max(int a,int b){
	return a>b?a:b;
}

struct E{
	int v,nxt;
	E() {}
	E(int v,int nxt):v(v),nxt(nxt) {}
}e[maxn<<1];

string w[maxn];

bool vis[maxn];

int Del[maxn][2];

int n,s_e,tot,head[maxn],f[maxn][2],ch[maxn][33];

inline void a_e(int u,int v){
	e[++s_e]=E(v,head[u]);
	head[u]=s_e;
}

inline void Insert(string s){
	int p=0;
    for(int i=0;i<(int)s.size();i++){
		int c=s[i]-'a';
		if(!ch[p][c]){
			ch[p][c]=++tot;
			Del[tot][0]=p;
			Del[tot][1]=c;
		}
		p=ch[p][c];
	}
}

inline void add(string s){
	int x=0,y=ch[0][s[0]-'a'];
	for(int i=1;i<(int)s.size();i++){
		int c=s[i]-'a';
	    x=ch[x][c];
		y=ch[y][c];
		if(!x||!y)return;
		a_e(x,y);
		a_e(y,x);
	}
}

void dfs(int u){
	vis[u]=1;
	f[u][0]=0,f[u][1]=1;
	for(int i=head[u];i;i=e[i].nxt){
		int v=e[i].v;
		if(vis[v])continue;
		dfs(v);
		f[u][0]+=max(f[v][0],f[v][1]);
		f[u][1]+=f[v][0];
	}
}

inline void work(){
	s_e=0;
    while(tot){
		ch[Del[tot][0]][Del[tot][1]]=0;
		head[tot]=0,vis[tot]=0;
		tot--;
	}
    cin>>n;
	for(int i=1;i<=n;i++){
		cin>>w[i];
		Insert(w[i]);
	}
	for(int i=1;i<=n;i++)add(w[i]);
	int ans=0;
	for(int i=1;i<=tot;i++){
		if(vis[i])continue;
		dfs(i);
		ans+=max(f[i][0],f[i][1]);
	}
	printf("%d\n",ans);
}

int main(){
	int t=read();
	while(t--)work();
	return 0;
}


#include<cstdio>
#include<cctype>
#include<iostream>

using namespace std;

#define maxn 1001001

inline int read(){
	int r=0,f=0;
	char c;
	while(!isdigit(c=getchar()))f|=(c=='-');
	while(isdigit(c))r=(r<<1)+(r<<3)+(c^48),c=getchar();
	return f?-r:r;
}

inline int max(int a,int b){
	return a>b?a:b;
}

struct E{
	int v,nxt;
	E() {}
	E(int v,int nxt):v(v),nxt(nxt) {}
}e[maxn<<1];

string w[maxn];

bool vis[maxn];

int Del[maxn][2];

int n,s_e,tot,head[maxn],f[maxn][2],ch[maxn][33];

inline void a_e(int u,int v){
	e[++s_e]=E(v,head[u]);
	head[u]=s_e;
}

inline void Insert(string s){
	int p=0;
    for(int i=0;i<(int)s.size();i++){
		int c=s[i]-'a';
		if(!ch[p][c]){
			ch[p][c]=++tot;
			Del[tot][0]=p;
			Del[tot][1]=c;
		}
		p=ch[p][c];
	}
}

inline void add(string s){
	int x=0,y=ch[0][s[0]-'a'];
	for(int i=1;i<(int)s.size();i++){
		int c=s[i]-'a';
	    x=ch[x][c];
		y=ch[y][c];
		if(!x||!y)return;
		a_e(x,y);
		a_e(y,x);
	}
}

void dfs(int u,int fa){
	vis[u]=1;
	f[u][0]=0,f[u][1]=1;
	for(int i=head[u];i;i=e[i].nxt){
		int v=e[i].v;
		if(v==fa)continue;
		dfs(v,u);
		f[u][0]+=max(f[v][0],f[v][1]);
		f[u][1]+=f[v][0];
	}
}

inline void work(){
	s_e=0;
    while(tot--){
		ch[Del[tot][0]][Del[tot][1]]=0;
		head[tot]=0,vis[tot]=0;
	}
    cin>>n;
	for(int i=1;i<=n;i++){
		cin>>w[i];
		Insert(w[i]);
	}
	for(int i=1;i<=n;i++)add(w[i]);
	int ans=0;
	for(int i=1;i<=tot;i++){
		if(vis[i])continue;
		dfs(i,0);
		ans+=max(f[i][0],f[i][1]);
	}
	printf("%d\n",ans);
}

int main(){
	int t=read();
	while(t--)work();
	return 0;
}


#include<cstdio>
#include<cctype>
#include<iostream>

using namespace std;

#define maxn 1001001

inline int read(){
	int r=0,f=0;
	char c;
	while(!isdigit(c=getchar()))f|=(c=='-');
	while(isdigit(c))r=(r<<1)+(r<<3)+(c^48),c=getchar();
	return f?-r:r;
}

inline int max(int a,int b){
	return a>b?a:b;
}

struct E{
	int v,nxt;
	E() {}
	E(int v,int nxt):v(v),nxt(nxt) {}
}e[maxn<<1];

string w[maxn];

bool vis[maxn];

int Del[maxn][2];

int n,s_e,tot,head[maxn],f[maxn][2],ch[maxn][33];

inline void a_e(int u,int v){
	e[++s_e]=E(v,head[u]);
	head[u]=s_e;
}

inline void Insert(string s){
	int p=0;
    for(int i=0;i<(int)s.size();i++){
		int c=s[i]-'a';
		if(!ch[p][c]){
			ch[p][c]=++tot;
			Del[tot][0]=p;
			Del[tot][1]=c;
		}
		p=ch[p][c];
	}
}

inline void add(string s){
	int x=0,y=ch[0][s[0]-'a'];
	for(int i=1;i<(int)s.size();i++){
		int c=s[i]-'a';
	    x=ch[x][c];
		y=ch[y][c];
		if(!x||!y)return;
		a_e(x,y);
		a_e(y,x);
	}
}

void dfs(int u,int fa){
	vis[u]=1;
	f[u][0]=0,f[u][1]=1;
	for(int i=head[u];i;i=e[i].nxt){
		int v=e[i].v;
		if(v^fa){
    		dfs(v,u);
    		f[u][0]+=max(f[v][0],f[v][1]);
    		f[u][1]+=f[v][0];
        }
	}
}

inline void work(){
	s_e=0;
    while(tot){
		ch[Del[tot][0]][Del[tot][1]]=0;
		head[tot]=0,vis[tot]=0;
		tot--;
	}
    cin>>n;
	for(int i=1;i<=n;i++){
		cin>>w[i];
		Insert(w[i]);
	}
	for(int i=1;i<=n;i++)add(w[i]);
	int ans=0;
	for(int i=1;i<=tot;i++){
		if(vis[i])continue;
		dfs(i,-1);
		ans+=max(f[i][0],f[i][1]);
	}
	printf("%d\n",ans);
}

int main(){
	int t=read();
	while(t--)work();
	return 0;
}


#include<cstdio>
#include<cctype>
#include<iostream>

using namespace std;

#define maxn 1001001

inline int read(){
	int r=0,f=0;
	char c;
	while(!isdigit(c=getchar()))f|=(c=='-');
	while(isdigit(c))r=(r<<1)+(r<<3)+(c^48),c=getchar();
	return f?-r:r;
}

inline int max(int a,int b){
	return a>b?a:b;
}

struct E{
	int v,nxt;
	E() {}
	E(int v,int nxt):v(v),nxt(nxt) {}
}e[maxn<<1];

string w[maxn];

bool vis[maxn];

int Del[maxn][2];

int n,s_e,tot,head[maxn],f[maxn][2],ch[maxn][33];

inline void a_e(int u,int v){
	e[++s_e]=E(v,head[u]);
	head[u]=s_e;
}

inline void Insert(string s){
	int p=0;
    for(int i=0;i<(int)s.size();i++){
		int c=s[i]-'a';
		if(!ch[p][c]){
			ch[p][c]=++tot;
			Del[tot][0]=p;
			Del[tot][1]=c;
		}
		p=ch[p][c];
	}
}

inline void add(string s){
	int x=0,y=ch[0][s[0]-'a'];
	for(int i=1;i<(int)s.size();i++){
		int c=s[i]-'a';
	    x=ch[x][c];
		y=ch[y][c];
		if(!x||!y)return;
		a_e(x,y);
		a_e(y,x);
	}
}

void dfs(int u,int fa){
	vis[u]=1;
	f[u][0]=0,f[u][1]=1;
	for(int i=head[u];i;i=e[i].nxt){
		int v=e[i].v;
		if(v==fa)continue;
		dfs(v,u);
		f[u][0]+=max(f[v][0],f[v][1]);
		f[u][1]+=f[v][0];
	}
}

inline void work(){
	s_e=0;
	for(int i=1;i<=n;i++)head[i]=0;
    while(tot--){
		ch[Del[tot][0]][Del[tot][1]]=0;
		vis[tot]=0;
	}
    cin>>n;
	for(int i=1;i<=n;i++){
		cin>>w[i];
		Insert(w[i]);
	}
	for(int i=1;i<=n;i++)add(w[i]);
	int ans=0;
	for(int i=1;i<=tot;i++){
		if(vis[i])continue;
		dfs(i,0);
		ans+=max(f[i][0],f[i][1]);
	}
	printf("%d\n",ans);
}

int main(){
	int t=read();
	while(t--)work();
	return 0;
}


#include<cstdio>
#include<cctype>
#include<iostream>

using namespace std;

#define maxn 1001001

inline int read(){
	int r=0,f=0;
	char c;
	while(!isdigit(c=getchar()))f|=(c=='-');
	while(isdigit(c))r=(r<<1)+(r<<3)+(c^48),c=getchar();
	return f?-r:r;
}

inline int max(int a,int b){
	return a>b?a:b;
}

struct E{
	int v,nxt;
	E() {}
	E(int v,int nxt):v(v),nxt(nxt) {}
}e[maxn<<1];

string w[maxn];

bool vis[maxn];

int Del[maxn][2];

int n,s_e,tot,head[maxn],f[maxn][2],ch[maxn][33];

inline void a_e(int u,int v){
	e[++s_e]=E(v,head[u]);
	head[u]=s_e;
}

inline void Insert(string s){
	int p=0;
    for(int i=0;i<(int)s.size();i++){
		int c=s[i]-'a';
		if(!ch[p][c]){
			ch[p][c]=++tot;
			Del[tot][0]=p;
			Del[tot][1]=c;
		}
		p=ch[p][c];
	}
}

inline void add(string s){
	int x=0,y=ch[0][s[0]-'a'];
	for(int i=1;i<(int)s.size();i++){
		int c=s[i]-'a';
	    x=ch[x][c];
		y=ch[y][c];
		if(!x||!y)return;
		a_e(x,y);
		a_e(y,x);
	}
}

void dfs(int u,int fa){
	vis[u]=1;
	f[u][0]=0,f[u][1]=1;
	for(int i=head[u];i;i=e[i].nxt){
		int v=e[i].v;
		if(v==fa)continue;
		dfs(v,u);
		f[u][0]+=max(f[v][0],f[v][1]);
		f[u][1]+=f[v][0];
	}
}

inline void work(){
	s_e=0;
    while(tot){
		ch[Del[tot][0]][Del[tot][1]]=0;
		head[tot]=0,vis[tot]=0;
		tot--;
	}
    cin>>n;
	for(int i=1;i<=n;i++){
		cin>>w[i];
		Insert(w[i]);
	}
	for(int i=1;i<=n;i++)add(w[i]);
	int ans=0;
	for(int i=1;i<=tot;i++){
		if(vis[i])continue;
		dfs(i,i);
		ans+=max(f[i][0],f[i][1]);
	}
	printf("%d\n",ans);
}

int main(){
	int t=read();
	while(t--)work();
	return 0;
}


#include<functional>
#include <algorithm>
#include  <iostream>
#include   <ctype.h>
#include   <cstring>
#include   <fstream>
#include    <cstdio>
#include    <vector>
#include    <string>
#include     <cmath>
#include     <deque>
#include     <stack>
#include     <queue>
#include      <list>
#include       <set>
#include       <map>
#define INF 0X7FFFFFFF
#define PI acos(-1.0)
typedef long long ll;
using namespace std;
const int N = 1e6 + 10;
inline int getint()
{
	int w = 0, q = 0;
	char c = getchar();
	while ((c<'0' || c>'9') && c != '-') c = getchar();
	if (c == '-') q = 1, c = getchar();
	while (c >= '0'&&c <= '9') w = w * 10 + c - '0', c = getchar();
	return q ? -w : w;
}
struct Node {
	int next[26];
};

string str;
Node node[N];
int o;
int n;
int newpoint() {
	o++;
	memset(node[o].next, -1, sizeof(node[o].next));
	return o;
}
void updata(int &rt, int t) {
	if (rt == -1) {
		rt = newpoint();
	}
	if (t == str.size()) {
		return;
	}
	updata(node[rt].next[str[t] - 'a'], t + 1);
}
int ans;
vector<int> e[N];
int sum[N][2];
int u[N];
void dfs(int m,int p) {
	if (p > 0) {
		u[m] = 1;
		e[p].push_back(m);
	}
	for (int i = 0; i < 26; i++) {
		if (node[m].next[i] != -1 && node[p].next[i] != -1) {
			dfs(node[m].next[i], node[p].next[i]);
		}
	}
}
void dp(int v) {
	sum[v][0] = 0;
	sum[v][1] = 1;
	for (int i = 0; i < e[v].size(); i++) {
		dp(e[v][i]);
		sum[v][0] += sum[e[v][i]][1];
		sum[v][1] += sum[e[v][i]][0];
	}
}
int main() {
	int T = getint();
	while (T--) {
		n = getint();
		int head = 0;
		o = 0;
		memset(node[0].next, -1, sizeof(node[0].next));
		for (int i = 0; i < n; i++) {
			cin >> str;
			updata(head, 0);
		}

		for (int i = 0; i < 26; i++) {
			if (node[0].next[i] != -1) {
				dfs(node[0].next[i], 0);
			}
		}
		ans = 0;
		for (int i = 1; i <= o; i++) {
			if (!u[i]) {
				dp(i);
				ans += max(sum[i][0], sum[i][1]);
			}
		}
		printf("%d\n", ans);
		memset(u, 0, sizeof(u));
		memset(sum, 0, sizeof(sum));
		for (int i = 0; i <= o; i++) {
			e[i].clear();
		}
	}
	return 0;
}


#include<functional>
#include <algorithm>
#include  <iostream>
#include   <ctype.h>
#include   <cstring>
#include   <fstream>
#include    <cstdio>
#include    <vector>
#include    <string>
#include     <cmath>
#include     <deque>
#include     <stack>
#include     <queue>
#include      <list>
#include       <set>
#include       <map>
#define INF 0X7FFFFFFF
#define PI acos(-1.0)
typedef long long ll;
using namespace std;
const int N = 1e6 + 10;
inline int getint()
{
	int w = 0, q = 0;
	char c = getchar();
	while ((c<'0' || c>'9') && c != '-') c = getchar();
	if (c == '-') q = 1, c = getchar();
	while (c >= '0'&&c <= '9') w = w * 10 + c - '0', c = getchar();
	return q ? -w : w;
}
struct Node {
	int next[26];
};

string str;
Node node[N];
int o;
int n;
int newpoint() {
	o++;
	memset(node[o].next, -1, sizeof(node[o].next));
	return o;
}
void updata(int &rt, int t) {
	if (rt == -1) {
		rt = newpoint();
	}
	if (t == str.size()) {
		return;
	}
	updata(node[rt].next[str[t] - 'a'], t + 1);
}
int ans;
vector<int> e[N];
int sum[N][2];
int u[N];
void dfs(int m,int p) {
	if (p > 0) {
		u[m] = 1;
		e[p].push_back(m);
	}
	for (int i = 0; i < 26; i++) {
		if (node[m].next[i] != -1 && node[p].next[i] != -1) {
			dfs(node[m].next[i], node[p].next[i]);
		}
	}
}
void dp(int v) {
	sum[v][0] = 0;
	sum[v][1] = 1;
	for (int i = 0; i < e[v].size(); i++) {
		dp(e[v][i]);
		sum[v][0] += sum[e[v][i]][1];
		sum[v][1] += sum[e[v][i]][0];
	}
}
int main() {
	int T = getint();
	while (T--) {
		n = getint();
		int head = 0;
		o = 0;
		memset(node[0].next, -1, sizeof(node[0].next));
		for (int i = 0; i < n; i++) {
			cin >> str;
			updata(head, 0);
		}

		for (int i = 0; i < 26; i++) {
			if (node[0].next[i] != -1) {
				dfs(node[0].next[i], 0);
			}
		}
		ans = 0;
		for (int i = 1; i <= o; i++) {
			if (!u[i]) {
				dp(i);
				ans += max(sum[i][0], sum[i][1]);
			}
		}
		printf("%d\n", ans);
		for (int i = 0; i <= o; i++) {
			e[i].clear();
			u[i] = 0;
			sum[i][0] = 0;
			sum[i][1] = 0;
		}
	}
	return 0;
}