#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define endl '\n'

using namespace std;

int t; 
void read()
{
	cin >> t; 
}

const int maxn = 1e5 + 3; 
const int inf = 1e9;

int dp[26][26], nxt_dp[26][26], dp_without[26][26];
int pr[26], su[26];
void solve()
{
	while (t--)
	{
		string s;
		cin >> s;
		
		int sz = (int)s.size();
		if (sz == 1)
		{
			cout << 0 << endl;
			continue;
		}
		
		for (int i = 0; i < 26; i++)
		for (int j = 0; j < 26; j++)
		{
			if (i != j)
			dp[i][j] = (s[0] - 'a' != i) + (s[1] - 'a' != j);
			
			else
			dp[i][j] = inf; 
		}
		
		for (int i = 0; i < 26; i++)
		for (int j = 0; j < 26; j++)
		{
			dp_without[i][j] = inf;
			for (int k = 0; k < 26; k++)
			if (k != j)
			dp_without[i][j] = min(dp_without[i][j], dp[k][i]);
		}
		
		for (int i = 3; i <= sz; i++)
		{
			for (int j = 0; j < 26; j++)
		    for (int k = 0; k < 26; k++)
		    {
			    if (j == k)
			    {
				    nxt_dp[j][k] = inf;
				    continue;
			    }
			
			    nxt_dp[j][k] = (k != s[i-1] - 'a') + dp_without[j][k];
			}
			
			for (int j = 0; j < 26; j++)
			for (int k = 0; k < 26; k++)
			dp[j][k] = nxt_dp[j][k];
			
			for (int j = 0; j < 26; j++)
			{
				pr[0] = dp[0][j];
				for (int k = 1; k < 26; k++)
				pr[k] = min(pr[k-1], dp[k][j]);
				
				su[25] = dp[25][j];
				for (int k = 24; k >= 0; k--)
				su[k] = min(su[k+1], dp[k][j]);
				
				for (int k = 0; k < 26; k++)
				{
					dp_without[j][k] = inf; 
					if (k != 0)
					dp_without[j][k] = min(dp_without[j][k], pr[k-1]);
					
					if (k != 25)
					dp_without[j][k] = min(dp_without[j][k], su[k+1]);
				}
			}
		}
		
		int ans = inf;
		for (int i = 0; i < 26; i++)
		for (int j = 0; j < 26; j++)
		ans = min(ans, dp[i][j]);
		
		cout << ans << endl; 
	}
}

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

	read();
	solve();
}
