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

typedef long long int lld;

int dp[5001][5001];

int main() {

    int n, m, k;
    cin >> n >> m >> k;
    int x[n];
    for (int i = 0; i < n; i++)
        cin >> x[i];

    vector<int> value;
    for (int i = 0; i < n; i++) {
        if (i + m - 1 < n) {
            int sum = 0;
            for (int j = i; j < i + m; j++)
                sum += x[j];
            value.push_back(sum);
        } else
            break;
    }
//  for (int i = 0 ; i < value.size() ; i++)cout << value[i] << endl;
    for (int i = 0; i < value.size() + 1; i++) {
        for (int j = 0; j <= k; j++) {
            if (i == 0 || j == 0)
                continue;
            dp[i][j] = value[i - 1];
            if (i - m >= 0)
                dp[i][j] = max(dp[i - 1][j], value[i - 1] + dp[i - m][j - 1]);
        }
    }/*
     for (int i = 0; i < value.size() + 1; i++) {
     for (int j = 0; j <= k; j++) {
     cout << dp[i][j] << " ";
     }
     cout << endl;
     }*/
    cout << dp[value.size()][k] << endl;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long int lld;

struct Node {
    pair<int, int> p;
    int sum = 0;
};
vector<Node> v;
vector<bool> arr;
vector<int> ans;
int z;
int c = 0;

int test(int index, int sum, int k) {
    c++;
    if (k == z) {
        ans.push_back(sum);
        return sum;
    }
    if (c == z)
        return -1;
    int max = -1, ind;
    for (int i = index + 1; i < v.size(); i++) {
        if (v[i].sum > max && !arr[i] && v[i].p.first > v[index].p.second) {
            max = v[i].sum;
            ind = i;
        }
        //cout << v[i].sum << " ";
    }
    //cout << endl << " " << "Max " << max << endl;
    arr[ind] = 1;
    test(index, sum + max, k + 1);
}

int main() {

    int n, m, k;
    cin >> n >> m >> k;
    z = k;
    int x[n];
    for (int i = 0; i < n; i++) {
        cin >> x[i];
    }
    Node temp;
    for (int i = 0;; i++) {
        if (i + m - 1 >= n)
            break;
        temp.p.first = i;
        temp.p.second = i + m - 1;
        v.push_back(temp);
    }
    int sum = 0;
    for (int i = 0; i < (int) v.size(); i++) {
        sum = 0;
        for (int j = v[i].p.first; j <= v[i].p.second; j++) {
            sum += x[j];
        }
        v[i].sum = sum;
    }
    for (int i = 0; i < v.size(); i++) {
        arr.clear();
        arr.resize(5000, 0);
        if (k >= v.size() - i + 1)
            break;
        c = 0;
        test(i, v[i].sum, 1);
    }
    sort(ans.rbegin(), ans.rend());
    cout << ans[0] << endl;
}


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

typedef long long int lld;

int dp[5001][5001];

int main() {

    int n, m, k;
    cin >> n >> m >> k;
    int x[n];
    for (int i = 0; i < n; i++)
        cin >> x[i];

    vector<int> value;
    for (int i = 0; i < n; i++) {
        if (i + m - 1 >= n)
            break;
        int sum = 0;
        for (int j = i; j < i + m; j++)
            sum += x[j];
        value.push_back(sum);

    }
    // for (int i = 0 ; i < value.size() ; i++)cout << value[i] << endl;
    for (int i = 0; i < value.size() + 1; i++) {
        for (int j = 0; j <= k; j++) {
            if (i == 0 || j == 0)
                continue;
            dp[i][j] = value[i - 1];
            if (i >= 2)
                dp[i][j] = max(dp[i - 1][j], dp[i][j]);
            if (i - m >= 0)
                dp[i][j] = max(dp[i - 1][j], value[i - 1] + dp[i - m][j - 1]);
        }
    }
    /*
     for (int i = 0; i < value.size() + 1; i++) {
     for (int j = 0; j <= k; j++) {
     cout << dp[i][j] << " ";
     }
     cout << endl;
     }
     */
    cout << dp[value.size()][k] << endl;
}


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

typedef long long int lld;

int dp[5001][5001];

int main() {

    int n, m, k;
    cin >> n >> m >> k;
    int x[n];
    for (int i = 0; i < n; i++)
        cin >> x[i];

    vector<int> value;
    for (int i = 0; i < n; i++) {
        if (i + m < n) {
            int sum = 0;
            for (int j = i; j < i + m; j++)
                sum += x[j];
            value.push_back(sum);
        } else
            break;
    }

    //for (int i = 0 ; i < value.size() ; i++)cout << value[i] << endl;
    for (int i = 0; i < value.size() + 1; i++) {
        for (int j = 0; j <= k; j++) {
            if (i == 0 || j == 0)
                continue;
            dp[i][j] = value[i - 1];
            if (i - m >= 0)
                dp[i][j] = max(dp[i - 1][j], value[i - 1] + dp[i - m][j - 1]);
        }
    }/*
     for (int i = 0; i < value.size() + 1; i++) {
     for (int j = 0; j <= k; j++) {
     cout << dp[i][j] << " ";
     }
     cout << endl;
     }*/
    cout << dp[value.size()][k] << endl;
}


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

typedef long long int lld;

int dp[5001][5001];

int main() {

    int n, m, k;
    cin >> n >> m >> k;
    int x[n];
    for (int i = 0; i < n; i++)
        cin >> x[i];

    vector<int> value;
    for (int i = 0; i < n; i++) {
        if (i + m - 1 >= n)
            break;
        int sum = 0;
        for (int j = i; j < i + m; j++)
            sum += x[j];
        value.push_back(sum);

    }
 // for (int i = 0 ; i < value.size() ; i++)cout << value[i] << endl;
    for (int i = 0; i < value.size() + 1; i++) {
        for (int j = 0; j <= k; j++) {
            if (i == 0 || j == 0)
                continue;
            dp[i][j] = value[i - 1];
            if (i - m >= 0)
                dp[i][j] = max(dp[i - 1][j], value[i - 1] + dp[i - m][j - 1]);
        }
    }/*
     for (int i = 0; i < value.size() + 1; i++) {
     for (int j = 0; j <= k; j++) {
     cout << dp[i][j] << " ";
     }
     cout << endl;
     }*/
    cout << dp[value.size()][k] << endl;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long int lld;

struct Node {
    pair<int, int> p;
    int sum = 0;
};
vector<Node> v;
vector<bool> arr;
vector<int> ans;
int z;
int c = 0;

int test(int index, int sum, int k) {
    //cout << index << " " << sum << endl;
    if (k == z) {
        ans.push_back(sum);
        return sum;
    }
    int max = -1, ind;
    for (int i = index + 1; i < v.size(); i++) {
        if (v[i].sum > max && !arr[i]) {
            max = v[i].sum;
            ind = i;
        }
        //cout << v[i].sum << " ";
    }
    //cout << endl << " " << "Max " << max << endl;
    arr[ind] = 1;
    test(index, sum + max, k + 1);
}

int main() {

    int n, m, k;
    cin >> n >> m >> k;
    z = k;
    int x[n];
    for (int i = 0; i < n; i++) {
        cin >> x[i];
    }
    Node temp;
    for (int i = 0;; i++) {
        if (i + m - 1 >= n)
            break;
        temp.p.first = i;
        temp.p.second = i + m - 1;
        v.push_back(temp);
    }
    int sum = 0;
    for (int i = 0; i < (int) v.size(); i++) {
        sum = 0;
        for (int j = v[i].p.first; j <= v[i].p.second; j++) {
            sum += x[j];
        }
        v[i].sum = sum;
    }
    for (int i = 0; i < v.size(); i++) {
        arr.clear();
        arr.resize(5000, 0);
        if (k >= v.size() - i + 1)
            break;
        test(i, v[i].sum, 1);
    }
    sort(ans.rbegin(), ans.rend());
    cout << ans[0] << endl;
}


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

typedef long long int lld;

int dp[5001][5001];

int main() {

    int n, m, k;
    cin >> n >> m >> k;
    int x[n];
    for (int i = 0; i < n; i++)
        cin >> x[i];

    vector<int> value;
    for (int i = 0; i < n; i++) {
        int sum = 0;
        for (int j = i; j < i + m; j++) {
            if (j >= n) {
                sum = -1;
                break;
            }
            sum += x[j];
        }
        if (sum == -1)
            break;
        value.push_back(sum);
    }

    //for (int i = 0; i < value.size(); i++)
        //cout << value[i] << endl;
    for (int i = 0; i < value.size() + 1; i++) {
        for (int j = 0; j <= k; j++) {
            if (i == 0 || j == 0)
                continue;
            dp[i][j] = value[i - 1];
            if (i - m >= 0)
                dp[i][j] = max(dp[i - 1][j], value[i - 1] + dp[i - m][j - 1]);
        }
    }/*
     for (int i = 0; i < value.size() + 1; i++) {
     for (int j = 0; j <= k; j++) {
     cout << dp[i][j] << " ";
     }
     cout << endl;
     }*/
    cout << dp[value.size()][k] << endl;
}


#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
int n,m,k,p[5100],sum[5100],ans_now,ans,x_now;
int main()
{
	scanf("%d%d%d",&n,&m,&k);
	for(int i=1;i<=n;i++)scanf("%d",&p[i]);
	for(k=k;k>=1;k--)
	{
		ans_now=0;
		memset(sum,0,sizeof(sum));
		for(int i=1;i<=n-m+1;i++)
		{
			for(int j=i;j<=i+m-1;j++)
			{
				sum[i]+=p[j];
			}
		//	printf("%d ",sum[i]);
			if(sum[i]>ans_now)
			{
				ans_now=sum[i];
				x_now=i;
			}
		}
		for(int j=x_now;j<=x_now+m-1;j++)p[j]=0;
		ans+=ans_now;
	//	printf("\n%d %d\n",ans_now,ans);
	}
	printf("%d",ans);
	return 0;
}


#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
const int inf=1e9;
int n,m,k,p[5100],sum[5100],ans_now,ans,x_now;
int main()
{
	scanf("%d%d%d",&n,&m,&k);
	for(int i=1;i<=n;i++)scanf("%d",&p[i]);
	for(k=k;k>=1;k--)
	{
		ans_now=0;
		memset(sum,0,sizeof(sum));
		for(int i=1;i<=n-m+1;i++)
		{
			for(int j=i;j<=i+m-1;j++)
			{
			/*	if(p[j]==0)
				{
					sum[i]=-1;break;
				}*/
				sum[i]+=p[j];
			}
		//	printf("%d ",sum[i]);
			if(sum[i]>ans_now)
			{
				ans_now=sum[i];
				x_now=i;
			}
		}
		for(int j=x_now;j<=x_now+m-1;j++)p[j]=-inf;
		ans+=ans_now;
	//	printf("\n%d %d\n",ans_now,ans);
	}
	printf("%d",ans);
	return 0;
}


#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
int n,m,k,p[5100],sum[5100],ans_now,ans,x_now;
int main()
{
	scanf("%d%d%d",&n,&m,&k);
	for(int i=1;i<=n;i++)scanf("%d",&p[i]);
	for(k=k;k>=1;k--)
	{
		ans_now=0;
		memset(sum,0,sizeof(sum));
		for(int i=1;i<=n-m+1;i++)
		{
			for(int j=i;j<=i+m-1;j++)
			{
				sum[i]+=p[j];
			}
		//	printf("%d ",sum[i]);
			if(sum[i]>ans_now)
			{
				ans_now=sum[i];
				x_now=i;
			}
		}
		for(int j=x_now;j<=x_now+m-1;j++)p[j]=0;
		ans+=ans_now;
	//	printf("%d\n",ans);
	}
	printf("%d",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
long long n,m,k,p[5100],sum[5100],dp[5100][5100],t;
int main()
{
	scanf("%lld%lld%lld",&n,&m,&k);
	for(long long i=1;i<=n;i++)
	{
		scanf("%lld",&p[i]);
		t+=p[i];
		if(i>=m)
		{
			sum[i]=t;t-=p[i-m+1];
		}
	}
	memset(dp,-1,sizeof(dp));
	for(long long i=0;i<=n;i++)dp[i][0]=0;
	for(long long i=1;i<=n;i++)
	{
		for(long long j=1;j<=k;j++)
		{
			dp[i][j]=dp[i-1][j];
            if(i>=m&&dp[i-m][j-1]>-1)
            {
				dp[i][j]=max(dp[i][j],dp[i-m][j-1]+sum[i]);
			}
		}
	}
	printf("%lld",dp[n][k]);
	return 0;
}


#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
int n,m,k,p,ans;
priority_queue< int,vector<int>,less<int> >q;
int main()
{
	scanf("%d%d%d",&n,&m,&k);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&p);
		q.push(p);
	}
	for(int i=1;i<=k;i++)
	{
		for(int j=1;j<=m;j++)
		{
			ans+=q.top();
			q.pop();
		}
	}
	printf("%d",ans);
	return 0;
}


#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
int n,m,k,p[5100],sum[5100],ans_now,ans,x_now;
int main()
{
	scanf("%d%d%d",&n,&m,&k);
	for(int i=1;i<=n;i++)scanf("%d",&p[i]);
	for(k=k;k>=1;k--)
	{
		ans_now=0;
		memset(sum,0,sizeof(sum));
		for(int i=1;i<=n-m+1;i++)
		{
			for(int j=i;j<=i+m-1;j++)
			{
				if(p[j]==0)
				{
					sum[i]=-1;break;
				}
				sum[i]+=p[j];
			}
		//	printf("%d ",sum[i]);
			if(sum[i]>ans_now)
			{
				ans_now=sum[i];
				x_now=i;
			}
		}
		for(int j=x_now;j<=x_now+m-1;j++)p[j]=0;
		ans+=ans_now;
	//	printf("\n%d %d\n",ans_now,ans);
	}
	printf("%d",ans);
	return 0;
}


#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
int n,m,k,p,ans;
priority_queue< int,vector<int>,greater<int> >q;
int main()
{
	scanf("%d%d%d",&n,&m,&k);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&p);
		q.push(p);
	}
	for(int i=1;i<=k;i++)
	{
		for(int j=1;j<=m;j++)
		{
			ans+=q.top();
			q.pop();
		}
	}
	printf("%d",ans);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 5000
#define ll long long
using namespace std;
int n,m,k;
ll v[N],qzh[N];
ll dp[N][N];
int main()
{
	scanf("%d%d%d",&n,&m,&k);
	for(int i=1;i<=n;i++)
	{
		scanf("%lld",&v[i]);
		qzh[i]=qzh[i-1]+v[i];
	}
	for(int i=m;i<=n;i++)
	for(int j=1;j<=k;j++)
	dp[i][j]=max(dp[i-1][j],dp[i-m][j-1]+qzh[i]-qzh[i-m]);
	ll ans=0;
	for(int i=1;i<=n;i++)
	for(int j=1;j<=k;j++)
	ans=max(ans,dp[i][j]);
	printf("%lld",ans);
	return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
#define ll long long
#define M 5005
ll dp[M][M],sum[M];
int main(){
	int m,n,k,l;
	scanf("%d%d%d",&m,&n,&k);
	for(int i=1;i<=m;++i)scanf("%d",&l),sum[i]=sum[i-1]+l;
	for(int i=1;i<=m;++i)
		for(int j=n;j<=i;j+=n)
		dp[i][j]=max(dp[i][j],max(dp[i-1][j],dp[i-n][j-n]+sum[i]-sum[i-n]));
	printf("%lld\n",dp[m][k*n]);
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll sum[100000],n,m,k,dp[10000][500],a[10000];
int main()
{
	cin>>n>>m>>k;
	for(int i=1;i<=n;++i)
	{
		cin>>a[i];
		sum[i]=sum[i-1]+a[i];
	}
	for(int i=m;i<=n;++i)
	{
		for(int j=1;j<=k;++j)
		{
			dp[i][j]=max(dp[i-m][j-1]+sum[i]-sum[i-m],max(dp[i][j],dp[i-1][j]));
		}
	}
	ll mm=0;
	for(int i=1;i<=n;++i) mm=max(mm,dp[i][k]);
	cout<<mm<<endl;
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
long long n,m,k,a[5005],s[5005],f[5005][5005];//注意用longlong
int main()
{
    scanf("%lld %lld %lld",&n,&m,&k);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    long long tot=0;
    for(int i=1;i<=n;i++)
    {
        tot+=a[i];
        if(i>=m)
        {
            s[i]=tot;
            tot-=a[i-m+1];
        }
    }
    memset(f,-1,sizeof(f));
    for(int i=0;i<=n;i++) f[i][0]=0;//初值
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=k;j++)
        {
            f[i][j]=f[i-1][j];
            if(i>=m && f[i-m][j-1]>-1) f[i][j]=max(f[i][j],f[i-m][j-1]+s[i]);
        }
    }
    printf("%lld",f[n][k]);//输出
    return 0;
}


#ceilevenoddformula
import math
import bisect
import copy
import time
from collections import Counter
#map(int,input().split())
#int(x) for x in input().split()
for _ in range(int(input())):
	n=int(input())
	x=list(input())
	ans=0
	for i in range(n):
		a,b,c=x[i],x[(i+1)%n],x[(i+2)%n]
		if a!=c:
			if a=='L' and c=='R' and b=='L':
				continue
			elif a=='R' and c=='L':
				continue
		else:
			if a=='L' and c=='L' and b=='L':
				ans+=1
				x[(i+1)%n]='R'
			if a=='R' and c=='R' and b=='R':
				ans+=1
				x[(i+1)%n]='L'
	print(ans)


#ceilevenoddformula
import math
import bisect
import copy
import time
from collections import Counter
#map(int,input().split())
#int(x) for x in input().split()
for _ in range(int(input())):
	n=int(input())
	x=list(input())
	ans=0
	last=-1
	s=0
	en=n-1
	c=0
	i=0
	c1=0
	if x[0]==x[-1]:
		for j in range(s,en):
			if x[j]==x[j+1]:
				c+=1
			else:
				break
		c+=1
		if c>1:
			s+=c-1
		for j in range(en,s-1):
			if x[j]==x[j-1]:
				c1+=1
			else:
				break
		c1+=1
		if c1>1:
			en-=c1-1

	if c+c1>=3:
		ans=math.ceil((c+c1)/3)
	c=0
	for i in range(s+1,en-1):
		if x[i]==x[i+1]:
			c+=1
		else:
			if c+1>=3:
				ans+=((c+1-3)//3)+1
			c=0

	if c+1>=3:
		ans+=((c+1-3)//3)+1

	print(int(ans))


#ceilevenoddformula
import math
import bisect
import copy
import time
from collections import Counter
#map(int,input().split())
#int(x) for x in input().split()
for _ in range(int(input())):
	n=int(input())
	x=list(input())
	ans=0
	last=-1
	s=0
	en=n-1
	c=0
	i=0
	c1=0
	if x[0]==x[-1]:
		for j in range(s,en):
			if x[j]==x[j+1]:
				c+=1
			else:
				break
		c+=1
		if c>1:
			s+=c-1
		for j in range(en,s-1):
			if x[j]==x[j-1]:
				c1+=1
			else:
				break
		c1+=1
		if c1>1:
			en-=c1-1

	if c+c1>=3:
		ans=math.ceil((c+c1)/3)
	c=0
	for i in range(s+1,en-1):
		if x[i]==x[i+1]:
			c+=1
		else:
			if c+1>=3:
				ans+=math.ceil((c+1)/3)
			c=0
	if c+1>=3:
		ans+=math.ceil((c+1)/3)

	print(ans)


#ceilevenoddformula
import math
import bisect
import copy
import time
from collections import Counter
#map(int,input().split())
#int(x) for x in input().split()
for _ in range(int(input())):
	n=int(input())
	x=list(input())
	ans=0
	last=-1
	s=0
	en=n-1
	c=0
	i=0
	c1=0
	if x[0]==x[-1]:
		for j in range(s,en):
			if x[j]==x[j+1]:
				c+=1
			else:
				break
		c+=1
		if c>1:
			s+=c-1
		for j in range(en,s,-1):
			if x[j]==x[j-1]:
				c1+=1
				#print(x[j],x[j-1])
			else:
				break
		c1+=1
		if c1>1:
			en-=c1-1

	if s==en:
		ans=math.ceil((c+c1)/3)
	elif c+c1>=3:
		ans=(c+c1-3)//3
		ans+=1

	#print(s,en,c,c1)
	c=0
	for i in range(s+1,en-1):
		if x[i]==x[i+1]:
			c+=1
		else:
			if c+1>=3:
				ans+=((c+1-3)//3)+1
			c=0
	#print(c,ans)
	if c+1>=3:
		ans+=((c+1-3)//3)+1

	print(int(ans))


#ceilevenoddformula
import math
import bisect
import copy
import time
from collections import Counter
#map(int,input().split())
#int(x) for x in input().split()
for _ in range(int(input())):
	n=int(input())
	x=list(input())
	ans=0
	last=-1
	s=0
	en=n-1
	c=0
	i=0
	c1=0
	if x[0]==x[-1]:
		for j in range(s,en):
			if x[j]==x[j+1]:
				c+=1
			else:
				break
		c+=1
		if c>1:
			s+=c-1
		for j in range(en,s,-1):
			if x[j]==x[j-1]:
				c1+=1
				#print(x[j],x[j-1])
			else:
				break
		c1+=1
		if c1>1:
			en-=c1-1

	if c+c1>=3:
		ans=(c+c1-3)//3
		ans+=1
	#print(s,en,c,c1)
	c=0
	for i in range(s+1,en-1):
		if x[i]==x[i+1]:
			c+=1
		else:
			if c+1>=3:
				ans+=((c+1-3)//3)+1
			c=0
	#print(c,ans)
	if c+1>=3:
		ans+=((c+1-3)//3)+1

	print(int(ans))


#ceilevenoddformula
import math
import bisect
import copy
import time
from collections import Counter
#map(int,input().split())
#int(x) for x in input().split()
for _ in range(int(input())):
    n = int(input())
    stre = list(input())
    ans=0
    p = -1
    su = []
    for i in range(n):
        if stre[i] != p:
            su.append(0)
        su[-1] += 1
        p = stre[i]

    if len(su) == 1:
        ans += (su[0]-1)//3 + 1
    else:
        if stre[0] == stre[-1] and len(su) > 1:
            t = su.pop(-1)
            su[0] += t

        for i in su:
            ans += (i-3)//3 +1

    print(ans)


#ceilevenoddformula
import math
import bisect
import copy
import time
from collections import Counter
#map(int,input().split())
#int(x) for x in input().split()
for _ in range(int(input())):
	n=int(input())
	x=list(input())
	ans=0
	last=-1
	s=0
	en=n-1
	c=0
	i=0
	c1=0
	if x[0]==x[-1]:
		for j in range(s,en):
			if x[j]==x[j+1]:
				c+=1
			else:
				break
		c+=1
		if c>1:
			s+=c-1
		for j in range(en,s-1):
			if x[j]==x[j-1]:
				c1+=1
			else:
				break
		c1+=1
		if c1>1:
			en-=c1-1

	if c+c1>=3:
		ans=math.ceil((c+c1)/3)
	c=0
	for i in range(s+1,en-1):
		if x[i]==x[i+1]:
			c+=1
		else:
			if c+1>=3:
				ans+=math.ceil((c+1)/3)
			c=0
	if c+1>=3:
		ans+=math.ceil((c+1-2)/2)

	print(ans)


#ceilevenoddformula
import math
import bisect
import copy
import time
from collections import Counter
#map(int,input().split())
#int(x) for x in input().split()
for _ in range(int(input())):
	n=int(input())
	x=list(input())
	ans=0
	last=-1
	s=0
	en=n-1
	c=0
	i=0
	c1=0
	if x[0]==x[-1]:
		for j in range(s,en):
			if x[j]==x[j+1]:
				c+=1
			else:
				break
		c+=1
		if c>1:
			s+=c-1
		for j in range(en,s-1):
			if x[j]==x[j-1]:
				c1+=1
			else:
				break
		c1+=1
		if c1>1:
			en-=c1-1

	if c+c1>=3:
		ans=math.ceil((c+c1-2)/2)
	c=0
	for i in range(s+1,en-1):
		if x[i]==x[i+1]:
			c+=1
		else:
			if c+1>=3:
				ans+=math.ceil((c+1-2)/2)
			c=0
	if c+1>=3:
		ans+=math.ceil((c+1-2)/2)

	print(ans)


def onlogisch(i):
    global s

    return s[i % n] == s[(i + 1) % n] == s[(i - 1) % n]

def flip(i):
    global s
    global changes

    changes += 1

    if s[i] == "L":
        s[i] = "R"
    else:
        s[i] = "L"

for _ in range(int(input())):
    n = int(input())
    s = list(input())

    changes = 0

    for i in range(n):
        if onlogisch(i) and onlogisch(i + 1) and onlogisch(i - 1):
            flip(i)

    for i in range(n):
        if onlogisch(i) and (onlogisch(i + 1) or onlogisch(i - 1)):
            flip(i)

    for i in range(n):
        if onlogisch(i):
            flip(i)

    print(changes)


def onlogisch(i):
    global s

    return s[i % n] == s[(i + 1) % n] == s[(i - 1) % n]

def flip(i):
    global s
    global changes

    changes += 1

    if s[i] == "L":
        s[i] = "R"
    else:
        s[i] = "L"

for _ in range(int(input())):
    n = int(input())
    s = input()

    lengths = []

    length = 1

    for i in range(1, len(s)):
        if s[i] != s[i - 1]:
            lengths.append(length)
            length = 1
        else:
            length += 1

    lengths.append(length)

    if len(lengths) > 1:
        if s[0] == s[-1]:
            last = lengths.pop()
            lengths[0] += last

        print(sum(l // 3 for l in lengths))
    else:
        print((lengths[0] + 2) // 3)


for _ in range(int(input())):
    n = int(input())
    s = list(input())

    changes = 0

    for i in range(n):
        l = s[(i - 1) % n]
        r = s[(i + 1) % n]

        if s[i] == l == r:
            changes += 1

            if s[i] == "L":
                s[i] = "R"
            else:
                s[i] = "L"

    print(changes)


#!/bin/python3
import math; import os; import random; import re; import sys
from collections import defaultdict, Counter, deque
import heapq
from functools import lru_cache
from itertools import accumulate, chain, combinations, permutations, groupby, zip_longest
from math import floor, ceil, sqrt

def findLast(arr, ch):
	i = len(arr)-1
	while i >= 0 and arr[i] != ch:
		i -= 1
	return i

def allSame(n):
	# print(n, "ALL SAME PRINT")
	return n//3 + int(n%3==2)

def handleSub(sub):
	if 'L' not in sub or 'R' not in sub:
		return allSame(len(sub))
	else:
		i = sub.find('R')
		return allSame(i) + allSame(len(sub)-i)

def solve(arr):
	i = findLast(arr, 'L' if arr[0]=='R' else 'R')
	# print(arr, "I")
	if i == -1: return allSame(len(arr))
	if arr[0] == 'R': arr = arr[i+1:] + arr[:i+1]
	else: arr = arr[i+2:] + arr[:i]
	s = ''.join(arr)
	return sum(handleSub(sub) for sub in s.split('RL') if sub)

t = int(input())
results = []
for _ in range(t):
	n = int(input())
	arr = [ch for ch in input()]
	results += solve(arr),
for res in results:
	print(res)


#!/bin/python3
import math; import os; import random; import re; import sys
from collections import defaultdict, Counter, deque
import heapq
from functools import lru_cache
from itertools import accumulate, chain, combinations, permutations, groupby, zip_longest
from math import floor, ceil, sqrt

def allSame(n):
	return n//3 + int(n%3==2)

def handleSub(sub):
	if 'L' not in sub or 'R' not in sub:
		return allSame(len(sub))
	else:
		i = sub.find('R')
		return allSame(i) + allSame(len(sub)-i)

def solve(s):
	i = s.rfind('R' if s[0]=='L' else 'L')
	if i == -1: return 1 + allSame(len(s)-2)
	if s[0] == 'L':
		s = s[i:] + s[:i]
	else: s = s[i+1:] + s[:i+1]
	subs = [sub for sub in s.split('RL') if sub]
	return sum(handleSub(sub) for sub in subs)

t = int(input())
results = []
for _ in range(t):
	n = int(input())
	results += solve(input()),
for res in results:
	print(res)


#!/bin/python3
import math; import os; import random; import re; import sys
from collections import defaultdict, Counter, deque
import heapq
from functools import lru_cache
from itertools import accumulate, chain, combinations, permutations, groupby, zip_longest
from math import floor, ceil, sqrt

def allSame(n):
	return n//3 + int(n%3==2)

def handleSub(sub):
	if 'L' not in sub or 'R' not in sub:
		return allSame(len(sub))
	else:
		i = sub.find('R')
		return allSame(i) + allSame(len(sub)-i)

def solve(s):
	f = s[0]
	i = s.rfind('L' if f=='R' else 'R')
	if i == -1: return 1 + allSame(len(s)-2)
	if f == 'R': s = s[i+1:] + s[:i+1]
	else: s = s[i+2:] + s[:i]
	return sum(handleSub(sub) for sub in s.split('RL') if sub)

t = int(input())
results = []
for _ in range(t):
	n = int(input())
	results += solve(input()),
for res in results:
	print(res)


"""
                            pppppppppppppppppppp
                         ppppp  ppppppppppppppppppp
                      ppppppp    ppppppppppppppppppppp
                      pppppppp  pppppppppppppppppppppp
                      pppppppppppppppppppppppppppppppp
                              pppppppppppppppppppppppp
       ppppppppppppppppppppppppppppppppppppppppppppppp  pppppppppppppppppppp
      pppppppppppppppppppppppppppppppppppppppppppppppp  ppppppppppppppppppppp
     ppppppppppppppppppppppppppppppppppppppppppppppppp  pppppppppppppppppppppp
    ppppppppppppppppppppppppppppppppppppppppppppppp    pppppppppppppppppppppppp
   pppppppppppppppppppppppppppppppppppppppppppppp     pppppppppppppppppppppppppp
  ppppppppppppppppppppppppppppppppppppppppppppp      pppppppppppppppppppppppppppp
  pppppppppppppppppppppppppppppppp               pppppppppppppppppppppppppppppppp
  pppppppppppppppppppppppppppp     pppppppppppppppppppppppppppppppppppppppppppppp
  ppppppppppppppppppppppppppp    pppppppppppppppppppppppppppppppppppppppppppppppp
    pppppppppppppppppppppppp  pppppppppppppppppppppppppppppppppppppppppppppppppp
     ppppppppppppppppppppppp  ppppppppppppppppppppppppppppppppppppppppppppppppp
      pppppppppppppppppppppp  ppppppppppppppppppppppppppppppppppppppppppppppp
       ppppppppppppppppppppp  ppppppppppppppppppppppppppppppppppppppppppppp
                              pppppppppppppppppppppppp
                              pppppppppppppppppppppppppppppppp
                              pppppppppppppppppppppp  pppppppp
                              ppppppppppppppppppppp    ppppppp
                                 ppppppppppppppppppp  ppppp
                                    pppppppppppppppppppp
"""

import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush, nsmallest
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
from decimal import Decimal
# sys.setrecursionlimit(pow(10, 6))
# sys.stdin = open("input.txt", "r")
# sys.stdout = open("output.txt", "w")
mod = pow(10, 9) + 7
mod2 = 998244353
def data(): return sys.stdin.readline().strip()
def out(var): sys.stdout.write(str(var)+"\n")
def outa(*var, end="\n"): sys.stdout.write(' '.join(map(str, var)) + end)
def l(): return list(sp())
def sl(): return list(ssp())
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]

def calculate(arr):
    answer = 0
    cnt = 0
    index = n
    for i in range(n-1, -1, -1):
        if arr[i] == arr[0]:
            cnt += 1
        else:
            index = i+1
            break
    if cnt == n:
        return cnt // 3 + (1 if cnt % 3 else 0)
    new = 1
    for i in range(index):
        if arr[i] == arr[0]:
            cnt += 1
        else:
            new = i
            break
    if cnt >= 3:
        answer += (cnt // 3)
    cnt = 1
    for i in range(new+1, index):
        if arr[i-1] == arr[i]:
            cnt += 1
        else:
            answer += (cnt // 3)
            cnt = 1
    answer += (cnt // 3)
    return answer

for _ in range(int(data())):
    n = int(data())
    a = list(data())
    out(min(calculate(a), calculate(a[::-1])))


"""
                            pppppppppppppppppppp
                         ppppp  ppppppppppppppppppp
                      ppppppp    ppppppppppppppppppppp
                      pppppppp  pppppppppppppppppppppp
                      pppppppppppppppppppppppppppppppp
                              pppppppppppppppppppppppp
       ppppppppppppppppppppppppppppppppppppppppppppppp  pppppppppppppppppppp
      pppppppppppppppppppppppppppppppppppppppppppppppp  ppppppppppppppppppppp
     ppppppppppppppppppppppppppppppppppppppppppppppppp  pppppppppppppppppppppp
    ppppppppppppppppppppppppppppppppppppppppppppppp    pppppppppppppppppppppppp
   pppppppppppppppppppppppppppppppppppppppppppppp     pppppppppppppppppppppppppp
  ppppppppppppppppppppppppppppppppppppppppppppp      pppppppppppppppppppppppppppp
  pppppppppppppppppppppppppppppppp               pppppppppppppppppppppppppppppppp
  pppppppppppppppppppppppppppp     pppppppppppppppppppppppppppppppppppppppppppppp
  ppppppppppppppppppppppppppp    pppppppppppppppppppppppppppppppppppppppppppppppp
    pppppppppppppppppppppppp  pppppppppppppppppppppppppppppppppppppppppppppppppp
     ppppppppppppppppppppppp  ppppppppppppppppppppppppppppppppppppppppppppppppp
      pppppppppppppppppppppp  ppppppppppppppppppppppppppppppppppppppppppppppp
       ppppppppppppppppppppp  ppppppppppppppppppppppppppppppppppppppppppppp
                              pppppppppppppppppppppppp
                              pppppppppppppppppppppppppppppppp
                              pppppppppppppppppppppp  pppppppp
                              ppppppppppppppppppppp    ppppppp
                                 ppppppppppppppppppp  ppppp
                                    pppppppppppppppppppp
"""

import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush, nsmallest
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
from decimal import Decimal
# sys.setrecursionlimit(10 ** 6)
# sys.stdin = open("input.txt", "r")
# sys.stdout = open("output.txt", "w")
mod = pow(10, 9) + 7
mod2 = 998244353
def data(): return sys.stdin.readline().strip()
def out(var, end="\n"): sys.stdout.write(str(var)+"\n")
def outa(*var, end="\n"): sys.stdout.write(' '.join(map(str, var)) + end)
def l(): return list(sp())
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]

for _ in range(int(data())):
    n = int(data())
    arr = list(data())
    answer = 0
    for i in range(n):
        if i == 0:
            if arr[1] != arr[-1]:
                # arr[0] += "ANY"
                pass
            elif arr[1] == "L":
                if arr[0] == "L":
                    arr[0] = "R"
                    answer += 1
            elif arr[-1] == "R":
                if arr[0] == "R":
                    arr[0] = "L"
                    answer += 1
            continue
        elif i == n-1:
            if arr[0][0] != arr[-2][0]:
                pass
            elif arr[0][0] == "L":
                if arr[-1] != "R":
                    answer += 1
            elif arr[-2][0] == "R":
                if arr[-1] != "L":
                    answer += 1
            break
        if arr[i-1][0] != arr[i+1]:
            # arr[i] += "ANY"
            pass
        elif arr[i-1][0] == "R":
            if arr[i] != "L":
                answer += 1
                arr[i] = "L"
        elif arr[i+1] == "L":
            if arr[i] != "R":
                answer += 1
                arr[i] = "R"
    # print(arr)
    out(answer)


"""
                            pppppppppppppppppppp
                         ppppp  ppppppppppppppppppp
                      ppppppp    ppppppppppppppppppppp
                      pppppppp  pppppppppppppppppppppp
                      pppppppppppppppppppppppppppppppp
                              pppppppppppppppppppppppp
       ppppppppppppppppppppppppppppppppppppppppppppppp  pppppppppppppppppppp
      pppppppppppppppppppppppppppppppppppppppppppppppp  ppppppppppppppppppppp
     ppppppppppppppppppppppppppppppppppppppppppppppppp  pppppppppppppppppppppp
    ppppppppppppppppppppppppppppppppppppppppppppppp    pppppppppppppppppppppppp
   pppppppppppppppppppppppppppppppppppppppppppppp     pppppppppppppppppppppppppp
  ppppppppppppppppppppppppppppppppppppppppppppp      pppppppppppppppppppppppppppp
  pppppppppppppppppppppppppppppppp               pppppppppppppppppppppppppppppppp
  pppppppppppppppppppppppppppp     pppppppppppppppppppppppppppppppppppppppppppppp
  ppppppppppppppppppppppppppp    pppppppppppppppppppppppppppppppppppppppppppppppp
    pppppppppppppppppppppppp  pppppppppppppppppppppppppppppppppppppppppppppppppp
     ppppppppppppppppppppppp  ppppppppppppppppppppppppppppppppppppppppppppppppp
      pppppppppppppppppppppp  ppppppppppppppppppppppppppppppppppppppppppppppp
       ppppppppppppppppppppp  ppppppppppppppppppppppppppppppppppppppppppppp
                              pppppppppppppppppppppppp
                              pppppppppppppppppppppppppppppppp
                              pppppppppppppppppppppp  pppppppp
                              ppppppppppppppppppppp    ppppppp
                                 ppppppppppppppppppp  ppppp
                                    pppppppppppppppppppp
"""

import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush, nsmallest
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
from decimal import Decimal
# sys.setrecursionlimit(pow(10, 6))
# sys.stdin = open("input.txt", "r")
# sys.stdout = open("output.txt", "w")
mod = pow(10, 9) + 7
mod2 = 998244353
def data(): return sys.stdin.readline().strip()
def out(var): sys.stdout.write(str(var)+"\n")
def outa(*var, end="\n"): sys.stdout.write(' '.join(map(str, var)) + end)
def l(): return list(sp())
def sl(): return list(ssp())
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]

for _ in range(int(data())):
    n = int(data())
    arr = list(data())
    answer = 0
    cnt = 0
    index = n
    for i in range(n-1, -1, -1):
        if arr[i] == arr[0]:
            cnt += 1
        else:
            index = i+1
            break
    if cnt == n:
        out(cnt // 3 + (1 if cnt % 3 else 0))
        continue
    # print(index, "INDEX")
    new = 1
    for i in range(index):
        if arr[i] == arr[0]:
            cnt += 1
        else:
            new = i
            break
    # print(cnt, new, "HELLO")
    if cnt >= 3:
        if arr[-1] != arr[0]:
            answer += (cnt // 3)
        else:
            answer += (cnt // 3 + (1 if cnt % 3 else 0))
    cnt = 1
    for i in range(new+1, index):
        if arr[i-1] == arr[i]:
            cnt += 1
        else:
            # print(cnt, "CNT")
            answer += (cnt // 3)
            cnt = 1
    # print(cnt, "CNT")
    answer += (cnt // 3)
    out(answer)


t = int(input())
for tt in range(t):
    n = int(input())
    s = input()
    s = [x for x in s]

    res = 0
    for i in range(n):
        if s[i] == s[(i+1)%n] == s[(i+2)%n]:
            res += 1
            if s[(i+3)%n] == s[(i+2)%n]:
                s[(i+2)%n] = "L" if s[(i+2)%n] == "R" else "R"
            else:
                s[(i+1)%n] = "L" if s[(i+1)%n] == "R" else "R"
    print(res)


t = int(input())
for tt in range(t):
    n = int(input())
    s = input()
    s = [x for x in s]

    res = 0
    for i in range(n):
        if s[i] == s[(i+1)%n] == s[(i+2)%n]:
            res += 1
            s[(i+1)%n] = "L" if s[(i+1)%n] == "R" else "R"
    print(res)


t = int(input())
for tt in range(t):
    n = int(input())
    s = input()
    s = [x for x in s]

    res = 0
    i = 0
    l = 0
    while i < n-1 and s[i] == s[i+1]:
        i += 1

    while (l+n-1)%n != i and s[(l+n-1)%n] == s[i]:
        l -= 1

    if (l+n-1)%n == i:
        res = (n+2)//3
        print(res)
        break

    res += (i-l+1)//3
    highest = n
    if l != 0:
        highest = (l+n)%n
    i += 1
    # l, r = 0, 0
    while i < highest:
        r = i
        while r < highest and s[r] == s[(r+1)%n]:
            r += 1

        res += (r-i+1)//3
        i = r + 1
    print(res)


t = int(input())
for tt in range(t):
    n = int(input())
    s = input()
    s = [x for x in s]

    res = 0
    i = 0
    l = 0
    while i < n-1 and s[i] == s[i+1]:
        i += 1

    while (l+n-1)%n != i and s[(l+n-1)%n] == s[i]:
        l -= 1

    if (l+n-1)%n == i:
        res = (n+2)//3
        print(res)
        continue

    res += (i-l+1)//3
    highest = n
    if l != 0:
        highest = (l+n)%n
    i += 1
    # l, r = 0, 0
    while i < highest:
        r = i
        while r < highest and s[r] == s[(r+1)%n]:
            r += 1

        res += (r-i+1)//3
        i = r + 1
    print(res)


for _ in range(int(input())):
    n = int(input())
    a = list(input())
    ans = 0
    if len(set(a)) == 1:
        print((n - 1) // 2)
    else:
        if a[0] == a[-1]:
            yes = 0
            now = 1
            f = 0
            for i in range(1, n):
                if a[i] == a[i - 1] and yes != 0:
                    now += 1
                elif a[i] == a[i - 1] and yes == 0:
                    f += 1
                elif a[i] != a[i - 1] and yes == 0:
                    yes = 1
                else:
                    ans += (now - 1) // 2
                    now = 1
            now += f
            ans += (now - 1) // 2
            print(ans)
        else:
            now = 1
            f = 0
            for i in range(1, n):
                if a[i] == a[i - 1]:
                    now += 1
                else:
                    ans += (now - 1) // 2
                    now = 1
            ans += (now - 1) // 2
            print(ans)


for _ in range(int(input())):
    n = int(input())
    a = list(input())
    ans = 0
    if len(set(a)) == 1:
        print(n // 2)
    else:
        if a[0] == a[-1]:
            yes = 0
            now = 1
            f = 0
            for i in range(1, n):
                if a[i] == a[i - 1] and yes != 0:
                    now += 1
                elif a[i] == a[i - 1] and yes == 0:
                    f += 1
                elif a[i] != a[i - 1] and yes == 0:
                    yes = 1
                else:
                    ans += (now - 1) // 2
                    now = 1
            now += f
            ans += (now - 1) // 2
            print(ans)
        else:
            now = 1
            f = 0
            for i in range(1, n):
                if a[i] == a[i - 1]:
                    now += 1
                else:
                    ans += (now - 1) // 2
                    now = 1
            ans += (now - 1) // 2
            print(ans)


for _ in range(int(input())):
    n = int(input())
    a = list(input())
    ans = 0
    if len(set(a)) == 1:
        print(1 + (n - 1) // 3)
    else:
        if a[0] == a[-1]:
            yes = 0
            now = 1
            f = 1
            for i in range(1, n):
                if a[i] == a[i - 1] and yes != 0:
                    now += 1
                elif a[i] == a[i - 1] and yes == 0:
                    f += 1
                elif a[i] != a[i - 1] and yes == 0:
                    yes = 1
                else:
                    ans += now // 3
                    now = 1
            now += f
            ans += now // 3
            print(ans)
        else:
            now = 1
            for i in range(1, n):
                if a[i] == a[i - 1]:
                    now += 1
                else:
                    ans += now // 3
                    now = 1
            ans += now // 3
            print(ans)


for _ in range(int(input())):
    n = int(input())
    a = list(input())
    ans = 0
    if len(set(a)) == 1:
        print(n // 2)
    else:
        if a[0] == a[-1]:
            yes = 0
            now = 1
            f = 1
            for i in range(1, n):
                if a[i] == a[i - 1] and yes != 0:
                    now += 1
                elif a[i] == a[i - 1] and yes == 0:
                    f += 1
                elif a[i] != a[i - 1] and yes == 0:
                    yes = 1
                else:
                    ans += (now - 1) // 2
                    now = 1
            now += f
            ans += (now - 1) // 2
            print(ans)
        else:
            now = 1
            f = 0
            for i in range(1, n):
                if a[i] == a[i - 1]:
                    now += 1
                else:
                    ans += (now - 1) // 2
                    now = 1
            ans += (now - 1) // 2
            print(ans)


t = int(input())
for _ in range(t):
    n = int(input())
    s = input()
    if 'L' not in s or 'R' not in s:
        print(int((n + 2) / 3))
    else:
        pos, foo= n - 1, ''
        while s[pos] == s[0]:
            foo += s[pos]
            pos -= 1
        for i in range(pos + 1):
            foo += s[i]
        s = foo
        cnt, res = 1, 0
        for i in range(1, n):
            if s[i] == s[i - 1]:
                cnt += 1
            else:
                res += int(cnt / 3)
                cnt = 1
        res += int(cnt / 3)
        print(res)


t = int(input())
for _ in range(t):
    n = int(input())
    s = input()
    if 'L' not in s or 'R' not in s:
        print(int((n + 2) / 3))
        continue
    pos, foo= n - 1, ''
    while s[pos] == s[0]:
        foo += s[pos]
        pos -= 1
    for i in range(pos + 1):
        foo += s[i]
    s = foo
    cnt, res = 1, 0
    for i in range(1, n):
        if s[i] == s[i - 1]:
            cnt += 1
        else:
            res += int(cnt / 3)
            cnt = 1
    res += int(cnt / 3)
    print(res)


t = int(input())
for _ in range(t):
    n = int(input())
    s = input()
    pos = -1
    for i in range(1, n):
        if s[i] != s[i - 1]:
            pos = i
    if pos == -1:
        print(int((n + 2) / 3))
    else:
        if s[0] == s[n - 1]:
            s = s[pos:] + s[:pos]
        cnt, res = 1, 0
        for i in range(1, n):
            if s[i] == s[i - 1]:
                cnt += 1
            else:
                res += int(cnt / 3)
                cnt = 1
        res += int(cnt / 3)
        print(res)


# ------------------- fast io --------------------
import os
import sys
from io import BytesIO, IOBase

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# ------------------- fast io --------------------
from math import gcd, ceil

def pre(s):
    n = len(s)
    pi = [0] * n
    for i in range(1, n):
        j = pi[i - 1]
        while j and s[i] != s[j]:
            j = pi[j - 1]
        if s[i] == s[j]:
            j += 1
        pi[i] = j
    return pi

def prod(a):
    ans = 1
    for each in a:
        ans = (ans * each)
    return ans

def lcm(a, b): return a * b // gcd(a, b)

def binary(x, length=16):
    y = bin(x)[2:]
    return y if len(y) >= length else "0" * (length - len(y)) + y

def calc(a, v):
    mxx = -1
    ans = 0
    for i in range(len(a)):
        if mxx == -1:mxx = a[i]
        ans += max(mxx, a[i]) - a[i]
        mxx = max(mxx, a[i])
        a[i] = mxx
    #print(ans, a)
    bb = 0
    for x in sorted(list(set(a)), reverse=True):
        ans += v - bb - x
        bb = v - x
    return ans

for _ in range(int(input()) if True else 1):
    n = int(input())
    #a, b = map(int, input().split())
    #c, d = map(int, input().split())
    s = input()
    s += s[0]
    count = 0
    i = 0
    while i < len(s) - 2:
        if s[i:i+3] in ["LLL", "RRR"]:
            count += 1
            i += 2
        i += 1
    print(count)


# ------------------- fast io --------------------
import os
import sys
from io import BytesIO, IOBase

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# ------------------- fast io --------------------
from math import gcd, ceil

def pre(s):
    n = len(s)
    pi = [0] * n
    for i in range(1, n):
        j = pi[i - 1]
        while j and s[i] != s[j]:
            j = pi[j - 1]
        if s[i] == s[j]:
            j += 1
        pi[i] = j
    return pi

def prod(a):
    ans = 1
    for each in a:
        ans = (ans * each)
    return ans

def lcm(a, b): return a * b // gcd(a, b)

def binary(x, length=16):
    y = bin(x)[2:]
    return y if len(y) >= length else "0" * (length - len(y)) + y

def calc(a, v):
    mxx = -1
    ans = 0
    for i in range(len(a)):
        if mxx == -1:mxx = a[i]
        ans += max(mxx, a[i]) - a[i]
        mxx = max(mxx, a[i])
        a[i] = mxx
    #print(ans, a)
    bb = 0
    for x in sorted(list(set(a)), reverse=True):
        ans += v - bb - x
        bb = v - x
    return ans

def find(s):
    start = 0
    n = len(s)
    if s[0] == s[-1]:
        start = n-1
        while start > 0 and s[start-1] == s[start]:
            start -= 1
    s += s
    cur = s[start]
    val = 0
    a=[]
    for i in range(start, start+n):
        if s[i] == cur:
            val += 1
        else:
            cur = s[i]
            a += [val]
            val = 1
    a += [val]
    #print(a,start)
    ans = 0
    for i in a:
        ans += i // 3 if i != n else (i+2)//3
    return ans

for _ in range(int(input()) if True else 1):
    n = int(input())
    #a, b = map(int, input().split())
    #c, d = map(int, input().split())
    s = input()
    print(find(s))


# ------------------- fast io --------------------
import os
import sys
from io import BytesIO, IOBase

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# ------------------- fast io --------------------
from math import gcd, ceil

def pre(s):
    n = len(s)
    pi = [0] * n
    for i in range(1, n):
        j = pi[i - 1]
        while j and s[i] != s[j]:
            j = pi[j - 1]
        if s[i] == s[j]:
            j += 1
        pi[i] = j
    return pi

def prod(a):
    ans = 1
    for each in a:
        ans = (ans * each)
    return ans

def lcm(a, b): return a * b // gcd(a, b)

def binary(x, length=16):
    y = bin(x)[2:]
    return y if len(y) >= length else "0" * (length - len(y)) + y

def calc(a, v):
    mxx = -1
    ans = 0
    for i in range(len(a)):
        if mxx == -1:mxx = a[i]
        ans += max(mxx, a[i]) - a[i]
        mxx = max(mxx, a[i])
        a[i] = mxx
    #print(ans, a)
    bb = 0
    for x in sorted(list(set(a)), reverse=True):
        ans += v - bb - x
        bb = v - x
    return ans

def find(s):
    start = 0
    n = len(s)
    if s[0] == s[-1]:
        start = n-1
        while start > 0 and s[start-1] == s[start]:
            start -= 1
    s += s
    cur = s[start]
    val = 0
    a=[]
    for i in range(start, start+n):
        if s[i] == cur:
            val += 1
        else:
            cur = s[i]
            a += [val]
            val = 1
    a += [val]
    #print(a,start)
    ans = 0
    for i in a:
        ans += i // 3 if i != n else i//3+1
    return ans

for _ in range(int(input()) if True else 1):
    n = int(input())
    #a, b = map(int, input().split())
    #c, d = map(int, input().split())
    s = input()
    print(find(s))


from sys import stdin
inp = lambda : stdin.readline().strip()

t = int(inp())

for _ in range(t):
    n = int(inp())
    a = list(inp())
    ans = 0
    for i in range(n):
        if a[i] == a[i-1] and a[i] == a[(i+1)%n]:
            if a[i] == 'R':
                a[i] = 'L'
            else:
                a[i] = 'R'
            ans += 1
    print(ans)


from sys import stdin
inp = lambda : stdin.readline().strip()

t = int(inp())

for _ in range(t):
    n = int(inp())
    a = list(inp())
    ans = 0
    d = {'R':'L','L':'R'}
    for i in range(len(a)):
        if a[i] == a[i-1] and a[i] == a[(i+1)%n]:
            if a[(i+2)%n] == a[i]:
                a[(i+1)%n] = d[a[i]]
            else:
                a[i] = d[a[i]]
            ans += 1
    print(ans)


from sys import stdin
inp = lambda : stdin.readline().strip()

t = int(inp())

for _ in range(t):
    n = int(inp())
    a = list(inp())
    ans = 0
    d = {'R':'L','L':'R'}
    for i in range(n-1):
        if a[i] != a[i+1]:
            a = a[i+1:] + a[:i+1]
            break
    for i in range(len(a)):
        if a[i] == a[i-1] and a[i] == a[(i+1)%len(a)]:
            if a[(i+2)%len(a)] == a[i]:
                a[(i+1)%len(a)] = d[a[i]]
            else:
                a[i] = d[a[i]]
            ans += 1
    print(ans)


import sys
import math,bisect
from collections import defaultdict
from itertools import groupby,accumulate
from heapq import heapify,heappop,heappush
from collections import deque,Counter,OrderedDict
#input = iter(sys.stdin.buffer.read().decode().splitlines()).next
def neo(): return map(int,input().split())
def Neo(): return list(map(int,input().split()))
for _ in range(int(input())):
    n = int(input())
    s = list(input())
    Ans = 0
    if s[-1] == 'R'and s[0] != 'L':
        Ans += 1
        s[0] = 'L'
    s = s+[s[0]]
    #print(s)
    for i in range(1,n):
        if s[i-1] == 'R' and s[i+1] == 'L':
            continue
        elif s[i-1] == 'L' and s[i+1] == 'R':
            continue
        elif s[i-1] == 'R'and s[i] != 'L':
            Ans += 1
            s[i] = 'L'
        elif s[i+1] == 'L' and s[i] != 'R':
            Ans += 1
            s[i] = 'R'
        s[-1] = s[0]
    print(Ans)


t = int(input())
for _ in range(t):
    n = int(input())
    s = input()
    for i in range(n-1):
        if s[i]!=s[i+1]:
            s = s[i+1:] + s[:i+1]
            break
    else:
        print((n+2)//3)
        continue
    ans = 0
    cnt = 1
    for i in range(n-1):
        if s[i]==s[i+1]:
            cnt += 1
        else:
            ans += cnt//3
            cnt = 1
    ans += cnt//3
    print(ans)


import sys
import math,bisect
from collections import defaultdict
from itertools import groupby,accumulate
from heapq import heapify,heappop,heappush
from collections import deque,Counter,OrderedDict
#input = iter(sys.stdin.buffer.read().decode().splitlines()).next
def neo(): return map(int,input().split())
def Neo(): return list(map(int,input().split()))
for _ in range(int(input())):
    n = int(input())
    s = list(input())
    Ans = 0
    if s[-1] == 'R' and s[1] == 'L':
        Ans += 0
    elif s[-1] == 'L' and s[1] == 'R':
        Ans += 0
    elif s[-1] == 'R'and s[0] != 'L':
        Ans += 1
        s[0] = 'L'
    elif s[1] == 'L' and s[0] != 'R':
        Ans += 1
        s[0] = 'R'
    s = s+[s[0]]
    #print(s)
    for i in range(1,n):
        if s[i-1] == 'R' and s[i+1] == 'L':
            continue
        elif s[i-1] == 'L' and s[i+1] == 'R':
            continue
        elif s[i-1] == 'R'and s[i] != 'L':
            Ans += 1
            s[i] = 'L'
        elif s[i+1] == 'L' and s[i] != 'R':
            Ans += 1
            s[i] = 'R'
        s[-1] = s[0]
    #print(s)
    print(Ans)


import sys
import math,bisect
from collections import defaultdict
from itertools import groupby,accumulate
from heapq import heapify,heappop,heappush
from collections import deque,Counter,OrderedDict
#input = iter(sys.stdin.buffer.read().decode().splitlines()).next
def neo(): return map(int,input().split())
def Neo(): return list(map(int,input().split()))
for _ in range(int(input())):
    n = int(input())
    s = list(input())
    s = [s[-1]]+s+[s[0]]
    Ans = 0
    #print(s)
    for i in range(1,n):
        if s[i-1] == 'R' and s[i+1] == 'L':
            continue
        elif s[i-1] == 'L' and s[i+1] == 'R':
            continue
        elif s[i-1] == 'R'and s[i] != 'L':
            Ans += 1
            s[i] = 'L'
        elif s[i+1] == 'L' and s[i] != 'R':
            Ans += 1
            s[i] = 'R'
    print(Ans)


import os
import sys
from io import BytesIO, IOBase
from math import ceil

def main():
    for t in range(int(input())):
        n=int(input())
        a = input().rstrip()
        n=len(a)
        su=0
        b=a.count("L")
        if b==n or b==0:
            print(ceil(n/3))
        else:
            ll,lc,rl,rc=1,0,1,n-1
            for i in range(1,n):
                if a[i]!=a[i-1]:
                    lc=i
                    break
                ll+=1
            for i in range(n-1,0,-1):
                if a[i] != a[i - 1]:
                    lr= i
                    break
                rl+= 1
            if a[0]==a[-1]:
                su+=((ll+rl)//3)
            else:
                su=su+ll//3+rl//3
            l=0
            for i in range(lc,rc-1):
                if a[i]==a[i+1]:
                    l=l+(2 if l==0 else 1)
                if a[i]!=a[i+1] or i==rc-2:
                    if l>=3:
                        su+=(l//3)
                    l=0
            print(su)
# region fastio

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

if __name__ == "__main__":
    main()


import os
import sys
from io import BytesIO, IOBase
from math import ceil

def main():
    for t in range(int(input())):
        n=int(input())
        a = input().rstrip()
        n=len(a)
        su=0
        b=a.count("L")
        if b==n or b==0:
            print(ceil(n/3))
        else:
            ll,lc,rl,rc=1,0,1,n-1
            for i in range(1,n):
                if a[i]!=a[i-1]:
                    lc=i
                    break
                ll+=1
            for i in range(n-1,0,-1):
                if a[i] != a[i - 1]:
                    rc= i
                    break
                rl+= 1
            if a[0]==a[-1]:
                su+=((ll+rl)//3)
            else:
                su=su+ll//3+rl//3
            l=0
            for i in range(lc,rc-1):
                if a[i]==a[i+1]:
                    l=l+(2 if l==0 else 1)
                if a[i]!=a[i+1] or i==rc-2:
                    if l>=3:
                        su+=(l//3)
                    l=0
            print(su)
# region fastio

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")

sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

if __name__ == "__main__":
    main()


#o = int(input())

for  _ in range(int(input())):
    n = int(input())
#    A = list(map(int, input().split(" ")))
    A = list(input())
    R, L = "R", "L"

    dp = []
    cur = 1
    i = 1
    visit = set()
    while True:
        i = i%n
        if A[i] == A[(i-1)%n]:
            cur += 1
            i += 1
        else:
            dp.append(cur)
            cur = 1
            if i + 1 not in visit:
                visit.add(i+1)
            else:
                break

            i += 1
        if cur == n:
            dp.append(n)
            break

    if len(dp)%2 == 1 and len(dp) != 1:
        dp.pop(0)
#    print(dp)
    count = 0
    for i in dp:
        if i <= 2:
            continue
        if i == 3:
            count += 1

        elif i%2 == 1:
            count += (i-1)//2 - 1
        else:
            count += i//2 - 1
    print(count)

#    count = 0
#    for i in range(n):
#        l, r = (i-1)%n, (i+1)%n
#        if A[l] == R and A[r] == L:
#            continue
#        elif A[l] == L and A[r] == R:
#            continue
#        elif A[l] == A[r] == A[i]:
#            count += 1
#            A[i] = L if A[i] == R else R
#
#    print(count)

#    for i in range(1, n):
#        diff = A[i]-A[i-1]
#        if diff >= 0:
#            continue
#        else:
#            count -= diff
#
#    print(count)


#o = int(input())

for  _ in range(int(input())):
    n = int(input())
#    A = list(map(int, input().split(" ")))
    A = list(input())
    R, L = "R", "L"

    count = 0
    for i in range(n):
        l, r = (i-1)%n, (i+1)%n
        if A[l] == R and A[r] == L:
            continue
        elif A[l] == L and A[r] == R:
            continue
        elif A[l] == A[r] == A[i]:
            count += 1
            A[i] = L if A[i] == R else R

    print(count)

#    for i in range(1, n):
#        diff = A[i]-A[i-1]
#        if diff >= 0:
#            continue
#        else:
#            count -= diff
#
#    print(count)


#o = int(input())

for  _ in range(int(input())):
    n = int(input())
#    A = list(map(int, input().split(" ")))
    A = list(input())
    R, L = "R", "L"

    dp = []
    cur = 1
    i = 1
    visit = set()
    while True:
        i = i%n
        if A[i] == A[(i-1)%n]:
            cur += 1
            i += 1
        else:
            dp.append(cur)
            cur = 1
            if i + 1 not in visit:
                visit.add(i+1)
            else:
                break

            i += 1
        if cur == n:
            dp.append(n)
            break

    if len(dp)%2 == 1 and len(dp) != 1:
        dp.pop(0)
#    print(dp)
    count = 0
    for i in dp:
        if i <= 2:
            continue
        if i%2 == 1:
            count += i//2
        else:
            count += i//2 - 1
    print(count)

#    count = 0
#    for i in range(n):
#        l, r = (i-1)%n, (i+1)%n
#        if A[l] == R and A[r] == L:
#            continue
#        elif A[l] == L and A[r] == R:
#            continue
#        elif A[l] == A[r] == A[i]:
#            count += 1
#            A[i] = L if A[i] == R else R
#
#    print(count)

#    for i in range(1, n):
#        diff = A[i]-A[i-1]
#        if diff >= 0:
#            continue
#        else:
#            count -= diff
#
#    print(count)


#o = int(input())

for  _ in range(int(input())):
    n = int(input())
#    A = list(map(int, input().split(" ")))
    A = list(input())
    R, L = "R", "L"

    dp = []
    cur = 1
    i = 1
    visit = set()
    while True:
        i = i%n
        if A[i] == A[(i-1)%n]:
            cur += 1
            i += 1
        else:
            dp.append(cur)
            cur = 1
            if i + 1 not in visit:
                visit.add(i+1)
            else:
                break

            i += 1
        if cur == n:
            dp.append(n)
            break

    if len(dp)%2 == 1 and len(dp) != 1:
        dp.pop(0)
#    print(dp)
    count = 0

    if len(dp) == 1:
        if i <= 2:
            count = 0
        elif i%2 == 1:
            count += (i)//2
        else:
            count += i//2 - 1
        print(count)
        continue

    for i in dp:
        if i <= 2:
            continue
        if i == 3:
            count += 1

        elif i%2 == 1:
            count += (i-1)//2 - 1
        else:
            count += i//2 - 1
    print(count)

#    count = 0
#    for i in range(n):
#        l, r = (i-1)%n, (i+1)%n
#        if A[l] == R and A[r] == L:
#            continue
#        elif A[l] == L and A[r] == R:
#            continue
#        elif A[l] == A[r] == A[i]:
#            count += 1
#            A[i] = L if A[i] == R else R
#
#    print(count)

#    for i in range(1, n):
#        diff = A[i]-A[i-1]
#        if diff >= 0:
#            continue
#        else:
#            count -= diff
#
#    print(count)


#o = int(input())

for  _ in range(int(input())):
    n = int(input())
#    A = list(map(int, input().split(" ")))
    A = list(input())
    R, L = "R", "L"

    dp = []
    cur = 1
    i = 1
    visit = set()
    while True:
        i = i%n
        if A[i] == A[(i-1)%n]:
            cur += 1
            i += 1
        else:
            dp.append(cur)
            cur = 1
            if i + 1 not in visit:
                visit.add(i+1)
            else:
                break

            i += 1
        if cur == n:
            dp.append(n)
            break

    if len(dp)%2 == 1 and len(dp) != 1:
        dp.pop(0)
#    print(dp)
    count = 0
    from math import ceil
    if len(dp) == 1:
        i = dp[0]
        count = int(ceil(i/3))
        print(count)
        continue

    for i in dp:
        count += i//3
#
#        if i <= 2:
#            continue
#        if i == 3:
#            count += 1
#
#        elif i%2 == 1:
#            count += (i-1)//2 - 1
#        else:
#            count += i//2 - 1
    print(count)

#    count = 0
#    for i in range(n):
#        l, r = (i-1)%n, (i+1)%n
#        if A[l] == R and A[r] == L:
#            continue
#        elif A[l] == L and A[r] == R:
#            continue
#        elif A[l] == A[r] == A[i]:
#            count += 1
#            A[i] = L if A[i] == R else R
#
#    print(count)

#    for i in range(1, n):
#        diff = A[i]-A[i-1]
#        if diff >= 0:
#            continue
#        else:
#            count -= diff
#
#    print(count)


for _ in range(int(input())):
	n=int(input())
	s=input()+'.'
	cnt=s.count('L')
	if cnt==n or cnt==0:
		print(n//3+1)
		continue
	l=[]
	c=[s[0],0]
	for i in range(1,n+1):
		if s[i]!=c[0]:
			l.append(i-c[1])
			c=[s[i],i]
	if s[-2]==s[0]:
		l[0]+=l.pop()
	ans=0
	for v in l:
		ans+=v//3
	print(ans)


for _ in range(int(input())):
	n=int(input())
	s=input()+'.'
	cnt=s.count('L')
	if cnt==n or cnt==0:
		print(n//3+(1 if n%3!=0 else 0))
		continue
	l=[]
	c=[s[0],0]
	for i in range(1,n+1):
		if s[i]!=c[0]:
			l.append(i-c[1])
			c=[s[i],i]
	if s[-2]==s[0]:
		l[0]+=l.pop()
	ans=0
	for v in l:
		ans+=v//3
	print(ans)


import sys
input=sys.stdin.readline
for _ in range(int(input())):
	n=int(input())
	#n,k=map(int,input().split())
	s=input().strip()
	#a=list(map(int,input().split()))
	a=list(s)
	ans=0
	for i in range(n):
		l,r=a[i-1],a[(i+1)%n]
		if l==r:
			if l=='L'==a[i]:
				a[i]='R'
				ans+=1
			if l=='R'==a[i]:
				a[i]='L'
				ans+=1
	print(ans)


t=int(input())
for _ in range(t):
    n=int(input())
    s=input()

    def helper(i,st): #number of moves to make replace s with st up to idx i (end of st matches s[i])
        cnt=0
        stI=0
        for j in range(i-len(st)+1,i+1):
            if s[j]!=st[stI]:
                cnt+=1
            stI+=1
        return cnt
    #t=0 for LL start
    #t=1 for LR
    #t=2 for RL
    #t=3 for RR
    memo=[[[-1 for _ in range(2)] for __ in range(n)] for ___ in range(4)]

    def dp(t,i,a): #a is 0 for 'L' and 1 for 'R'
        if memo[t][i][a]==-1:
            if i==0 and a==0:
                if t in (2,3):memo[t][i][a]=float('inf')
                else:memo[t][i][a]=helper(0,'L')
            elif i==0 and a==1:
                if t in (0,1):memo[t][i][a]=float('inf')
                else:memo[t][i][a]=helper(0,'R')
            elif i==1 and a==0:
                if t in (1,3):memo[t][i][a]=float('inf')
                else:
                    if t==0:memo[t][i][a]=helper(1,'LL')
                    elif t==2:memo[t][i][a]=helper(1,'RL')
            elif i==1 and a==1:
                if t in (0,2):memo[t][i][a]=float('inf')
                else:
                    if t==1:memo[t][i][a]=helper(1,'LR')
                    elif t==3:memo[t][i][a]=helper(1,'RR')

            elif i==n-1 and a==0 and t==0: memo[t][i][a]=float('inf') #cant use L|LL
            elif i==n-1 and a==0 and t==1: memo[t][i][a]=dp(t,i-1,1)+helper(i,'L') #must be RL/L to avoid LL/L
            elif i==n-1 and a==1 and t==3: memo[t][i][a]=float('inf') #cant use R|RR
            elif i==n-1 and a==1 and t==2: memo[t][i][a]=dp(t,i-1,0)+helper(i,'R') #must be LR/R to avoid RR/R

            elif a==0:
                memo[t][i][a]=min(dp(t,i-1,1)+helper(i,'L'), #end with 'RL'
                                dp(t,i-2,1)+helper(i,'LL')) #end with 'RLL'
            else: #a==1
                memo[t][i][a]=min(dp(t,i-1,0)+helper(i,'R'), #end with 'LR'
                                dp(t,i-2,0)+helper(i,'RR')) #end with 'LRR'
        return memo[t][i][a]

    for t in range(4):
        for i in range(n):
            for a in range(2):
                dp(t,i,a)

    res=float('inf')
    for t in range(4):
        for a in range(2):
            res=min(res,dp(t,n-1,a))
#            print(t,a,res)
    print(res)

#    for i in range(n):
#        print('i: '+str(i))
#        print('L: '+str(dp(1,i,0)))
#        print('R: '+str(dp(1,i,1)))
#        print('\n')


t=int(input())
for _ in range(t):
    n=int(input())
    s=input()

    #only RRR and LLL is not allowed
    start=-1
    for i in range(1,n):
        if s[i]!=s[i-1]:
            start=i
            break
    if start==-1: #all the same
        print((n+2)//3)
    else:
        seqLen=1
        cnt=0
        i=start
        for j in range(n+1):
            if s[i]!=s[i-1]: #seq break
#                print('broken')
#                print('i:{} seqLen:{}'.format(i,seqLen))
                cnt+=seqLen//3
                seqLen=1
            else:
                seqLen+=1
            i+=1
            if i==n:
                i=0
        print(cnt)


from types import GeneratorType
def bootstrap(f, stack=[]):
    def wrappedfunc(*args, **kwargs):
        if stack:
            return f(*args, **kwargs)
        else:
            to = f(*args, **kwargs)
            while True:
                if type(to) is GeneratorType:
                    stack.append(to)
                    to = next(to)
                else:
                    stack.pop()
                    if not stack:
                        break
                    to = stack[-1].send(to)
            return to
    return wrappedfunc

t=int(input())
for _ in range(t):
    n=int(input())
    s=input()

    def helper(i,st): #number of moves to make replace s with st up to idx i (end of st matches s[i])
        cnt=0
        stI=0
        for j in range(i-len(st)+1,i+1):
            if s[j]!=st[stI]:
                cnt+=1
            stI+=1
        return cnt
    #t=0 for LL start
    #t=1 for LR
    #t=2 for RL
    #t=3 for RR
    memo=[[[-1 for _ in range(2)] for __ in range(n)] for ___ in range(4)]
    @bootstrap
    def dp(t,i,a): #a is 0 for 'L' and 1 for 'R'
        if memo[t][i][a]==-1:
            if i==0 and a==0:
                if t in (2,3):memo[t][i][a]=float('inf')
                else:memo[t][i][a]=helper(0,'L')
            elif i==0 and a==1:
                if t in (0,1):memo[t][i][a]=float('inf')
                else:memo[t][i][a]=helper(0,'R')
            elif i==1 and a==0:
                if t in (1,3):memo[t][i][a]=float('inf')
                else:
                    if t==0:memo[t][i][a]=helper(1,'LL')
                    elif t==2:memo[t][i][a]=helper(1,'RL')
            elif i==1 and a==1:
                if t in (0,2):memo[t][i][a]=float('inf')
                else:
                    if t==1:memo[t][i][a]=helper(1,'LR')
                    elif t==3:memo[t][i][a]=helper(1,'RR')

            elif i==n-1 and a==0 and t==0: memo[t][i][a]=float('inf') #cant use L|LL
            elif i==n-1 and a==0 and t==1: memo[t][i][a]=(yield dp(t,i-1,1))+helper(i,'L') #must be RL/L to avoid LL/L
            elif i==n-1 and a==1 and t==3: memo[t][i][a]=float('inf') #cant use R|RR
            elif i==n-1 and a==1 and t==2: memo[t][i][a]=(yield dp(t,i-1,0))+helper(i,'R') #must be LR/R to avoid RR/R

            elif a==0:
                memo[t][i][a]=min((yield dp(t,i-1,1))+helper(i,'L'), #end with 'RL'
                                (yield dp(t,i-2,1))+helper(i,'LL')) #end with 'RLL'
            else: #a==1
                memo[t][i][a]=min((yield dp(t,i-1,0))+helper(i,'R'), #end with 'LR'
                                (yield dp(t,i-2,0))+helper(i,'RR')) #end with 'LRR'
        yield memo[t][i][a]

    res=float('inf')
    for t in range(4):
        for a in range(2):
            res=min(res,dp(t,n-1,a))
#            print(t,a,res)
    print(res)

#    for i in range(n):
#        print('i: '+str(i))
#        print('L: '+str(dp(1,i,0)))
#        print('R: '+str(dp(1,i,1)))
#        print('\n')


import sys
input=sys.stdin.readline

t=int(input())
for _ in range(t):
    n=int(input())
    s=input()
    s=s[:n]

    #only RRR and LLL is not allowed
    start=-1
    for i in range(1,n):
        if s[i]!=s[i-1]:
            start=i
            break
    if start==-1: #all the same
        print((n+1)//3)
    else:
        seqLen=0
        cnt=0
        i=start
        for j in range(n+1):
            if s[i]!=s[i-1]: #seq break
#                print('broken')
#                print('i:{} seqLen:{}'.format(i,seqLen))
                cnt+=seqLen//3
                seqLen=1
            else:
                seqLen+=1
            i+=1
            if i==n:
                i=0
        print(cnt)


import sys
input=sys.stdin.readline

t=int(input())
for _ in range(t):
    n=int(input())
    s=input()
    s=s[:n]

    #only RRR and LLL is not allowed
    start=-1
    for i in range(1,n):
        if s[i]!=s[i-1]:
            start=i
            break
    if start==-1: #all the same
        print((n+1)//3)
    else:
        seqLen=1
        cnt=0
        i=start
        for j in range(n+1):
            if s[i]!=s[i-1]: #seq break
#                print('broken')
#                print('i:{} seqLen:{}'.format(i,seqLen))
                cnt+=seqLen//3
                seqLen=1
            else:
                seqLen+=1
            i+=1
            if i==n:
                i=0
        print(cnt)


from functools import lru_cache

t=int(input())
for _ in range(t):
    n=int(input())
    s=input()

    def helper(i,st): #number of moves to make replace s with st up to idx i (end of st matches s[i])
        cnt=0
        stI=0
        for j in range(i-len(st)+1,i+1):
            if s[j]!=st[stI]:
                cnt+=1
            stI+=1
        return cnt
    #t=0 for LL start
    #t=1 for LR
    #t=2 for RL
    #t=3 for RR
    @lru_cache(None)
    def dp(t,i,a): #a is 0 for 'L' and 1 for 'R'
        if i==0 and a==0:
            if t in (2,3):return float('inf')
            else:return helper(0,'L')
        if i==0 and a==1:
            if t in (0,1):return float('inf')
            else:return helper(0,'R')
        if i==1 and a==0:
            if t in (1,3):return float('inf')
            else:
                if t==0:return helper(1,'LL')
                elif t==2:return helper(1,'RL')
        if i==1 and a==1:
            if t in (0,2):return float('inf')
            else:
                if t==1:return helper(1,'LR')
                elif t==3:return helper(1,'RR')

        if i==n-1 and a==0 and t==0: return float('inf') #cant use L|LL
        if i==n-1 and a==0 and t==1: return dp(t,i-1,1)+helper(i,'L') #must be RL/L to avoid LL/L
        if i==n-1 and a==1 and t==3: return float('inf') #cant use R|RR
        if i==n-1 and a==1 and t==2: return dp(t,i-1,0)+helper(i,'R') #must be LR/R to avoid RR/R

        if a==0:
            res=min(dp(t,i-1,1)+helper(i,'L'), #end with 'RL'
                    dp(t,i-2,1)+helper(i,'LL')) #end with 'RLL'
        else: #a==1
            res=min(dp(t,i-1,0)+helper(i,'R'), #end with 'LR'
                    dp(t,i-2,0)+helper(i,'RR')) #end with 'LRR'
        return res

    for t in range(4):
        for i in range(n):
            for a in range(2):
                dp(t,i,a)

    res=float('inf')
    for t in range(4):
        for a in range(2):
            res=min(res,dp(t,n-1,a))
#            print(t,a,res)
    print(res)

#    for i in range(n):
#        print('i: '+str(i))
#        print('L: '+str(dp(1,i,0)))
#        print('R: '+str(dp(1,i,1)))
#        print('\n')


for t in range(int(input())):
    n=int(input())
    s=[]
    r=input()
    r=r+r[0]
    add=0
    sub=0
    h=0
    for i in range(n+1):
        if(r[i]=="L"):
            add+=1
            if(sub!=0):
                s.append(sub)
                h+=1
            sub=0

        elif(r[i]=="R"):
            sub+=1
            if(add!=0):
                s.append(add)
                h+=1
            add=0
    if(add!=0):
        s.append(add)
    else:
        s.append(sub)
    add=0
    p=0
    for i in range(h+1):
        if(s[i]>=3):
            d=s[i]//5
            r=s[i]%5
            if(d>=1):
                p=s[i]-5
                dd=p//3
                rr=p%3
                if(rr!=0):
                    dd+=1
                add+=dd+1
            else:
                add+=1

    if(h%2==0):
        if(s[0]%3==0 and s[h]%3==0):
            add-=1
    print(add)


for t in range(int(input())):
    n=int(input())
    s=input()
    s=s+'k'
    a=[]
    add=1
    count=0
    for i in range(n):
        if(s[i]==s[i+1]):
            add+=1
        else:
            a.append(add)
            count+=1
            add=1
    if(count%2==1 and count!=1):
        a[-1]+=a[0]
        count-=1
        a.pop(0)
    if(count==1):
        if(a[0]%3==0):
            print(a[0]//3)
        else:
            print((a[0]//3)+1)
        continue
    plus=0
    for i in range(count):
        plus+=a[i]//3
    print(plus)


for t in range(int(input())):
    n=int(input())
    s=[]
    r=input()
    for i in range(n):
        s.append(r[i])
    add=0
    for i in range(n-1):
         if(s[i]==s[i+1]==s[(i+2)%(n)]=="L"):
             add+=1
             s[i+1]="R"
         elif(s[i]==s[i+1]==s[(i+2)%(n)]=="R"):
             add+=1
             s[i+1]="L"
    print(add)


for t in range(int(input())):
    n=int(input())
    s=[]
    r=input()
    r=r+r[0]
    add=0
    sub=0
    h=0
    for i in range(n+1):
        if(r[i]=="L"):
            add+=1
            if(sub!=0):
                s.append(sub)
                h+=1
            sub=0

        elif(r[i]=="R"):
            sub+=1
            if(add!=0):
                s.append(add)
                h+=1
            add=0
    if(add!=0):
        s.append(add)
    else:
        s.append(sub)
    add=0
    p=0
    for i in range(h+1):
        if(s[i]>=3):
            d=s[i]//5
            r=s[i]%5
            if(d>=1):
                p=s[i]-5
                dd=p//3
                rr=p%3
                if(rr!=0):
                    dd+=1
                add+=dd+1
            else:
                add+=1

    if(h%2==0):
        if(s[0]%3==0 and s[h]%3==0 and h!=0):
            add-=1
    print(add)


for t in range(int(input())):
    n=int(input())
    s=[]
    r=input()
    r=r+r[0]
    add=0
    sub=0
    h=0
    for i in range(n+1):
        if(r[i]=="L"):
            add+=1
            if(sub!=0):
                s.append(sub)
                h+=1
            sub=0

        elif(r[i]=="R"):
            sub+=1
            if(add!=0):
                s.append(add)
                h+=1
            add=0
    if(add!=0):
        s.append(add)
    else:
        s.append(sub)
    add=0
    for i in range(h+1):
        if(s[i]>=3):
            d=s[i]//5
            r=s[i]%5
            if(d>=1):
                s[i]-=5
                dd=s[i]//3
                rr=s[i]%3
                if(rr!=0):
                    dd+=1
                add+=dd+1
            else:
                add+=1

    print(add)


for t in range(int(input())):
    n=int(input())
    s=[]
    r=input()
    for i in range(n):
        s.append(r[i])
    add=0
    for i in range(n-1):
         if(s[i]==s[i+1]==s[(i+2)%(n)]=="L"):
             add+=1
             s[i+2]="R"
         elif(s[i]==s[i+1]==s[(i+2)%(n)]=="R"):
             add+=1
             s[i+2]="L"
    print(add)


for _ in range(int(input())):
    n = int(input())
    s = input()
    block = [[s[0], 1]]
    for i in range(1, n):
        if s[i] == block[-1][0]:
            block[-1][1] += 1
        else:
            block.append([s[i], 1])
    ans = 0
    if len(block) == 1:
        ans = block[0][1] // 3 + (block[0][1] % 3 != 0 and block[0][1] > 2)
        print(ans)
        continue
    if block[-1][0] == block[0][0] and len(block) > 1:
        block[0][1] += block[-1][1]
        block.pop(-1)
    for i in block:
        ans += (i[1] // 3 + (i[1] % 3 == 2 and i[1] > 2))
    print(ans)


for _ in range(int(input())):
    n = int(input())
    s = input()
    block = [[s[0], 1]]
    for i in range(1, n):
        if s[i] == block[-1][0]:
            block[-1][1] += 1
        else:
            block.append([s[i], 1])
    ans = 0
    if len(block) == 1:
        ans = block[0][1] // 3 + (block[0][1] % 3 != 0 and block[0][1] > 2)
        print(ans)
        continue
    if block[-1][0] == block[0][0] and len(block) > 1:
        block[0][1] += block[-1][1]
        block.pop(-1)
    for i in block:
        ans += (i[1] // 3)
    print(ans)


for _ in range(int(input())):
    n = int(input())
    s = input()
    block = [[s[0], 1]]
    for i in range(1, n):
        if s[i] == block[-1][0]:
            block[-1][1] += 1
        else:
            block.append([s[i], 1])
    if block[-1][0] == block[0][0] and len(block) > 1:
        block[0][1] += block[-1][1]
        block.pop(-1)
    ans = 0
    for i in block:

        ans += (i[1] // 3 + (i[1] % 3 == 2 and i[1] > 2))
    print(ans)


for _ in range(int(input())):
    n = int(input())
    s = input()
    block = [[s[0], 1]]
    for i in range(1, n):
        if s[i] == block[-1][0]:
            block[-1][1] += 1
        else:
            block.append([s[i], 1])
    ans = 0
    if len(block) == 1:
        ans = block[0][1] // 3 + (block[0][1] % 3 != 0)
        print(ans)
        continue
    if block[-1][0] == block[0][0] and len(block) > 1:
        block[0][1] += block[-1][1]
        block.pop(-1)
    for i in block:
        ans += (i[1] // 3 + (i[1] % 3 == 2 and i[1] > 2))
    print(ans)


for _ in range(int(input())):
    n=int(input())
    s=input()
    char=s[0]
    i=0
    j=0
    while j<n and s[j]==char:j+=1
    while i>j-n and s[i-1]==char:i-=1
    n+=i
    ans=0
    while i<n:
        ans+=(j-i-1)//2
        i=j
        while j<n and s[j]==s[i]:j+=1
    print(ans)


for _ in range(int(input())):
    n=int(input())
    s=input()
    i=0
    j=0
    while j<n and s[j]==s[0]:j+=1
    while i>j-n and s[i-1]==s[0]:i-=1
    n+=i
    ans=0
    test=2
    for asd in s:
        if asd!=s[0]:test=0
    while i<n:
        ans+=(j-i+test)//3
        i=j
        while j<n and s[j]==s[i]:j+=1
    print(ans)


for _ in range(int(input())):
    n=int(input())
    s=input()
    i=0
    j=0
    while j<n and s[j]==s[0]:j+=1
    while i>j-n and s[i-1]==s[0]:i-=1
    n+=i
    ans=0
    test=0
    while i<n:
        ans+=(j-i)//3
        i=j
        while j<n and s[j]==s[i]:j+=1
    ans+=1
    for asd in s:
        if asd!=s[0]:
            ans-=1
            break
    print(ans)


for _ in range(int(input())):
    n=int(input())
    s=input()
    i=0
    j=0
    while j<n and s[j]==s[0]:j+=1
    while i>j-n and s[i-1]==s[0]:i-=1
    n+=i
    ans=0
    test=1
    for asd in s:
        if asd!=s[0]:test=0
    while i<n:
        ans+=(j-i+test)//3
        i=j
        while j<n and s[j]==s[i]:j+=1
    print(ans)


t=int(input())
for i in range(t):
    n=int(input())
    s=input()
    b=[]
    for j in s:
        b.append(j)
    if len(set(b))==1:
        print((n+2)//3)
    else:
        curr = b[0]
        count = 1
        j = 1
        c = []
        while (j < n):
            if b[j] == b[j - 1]:
                count += 1
            else:
                c.append(count)
                count = 1
            curr = b[j]
            j += 1
        if b[0] == b[-1]:
            c[0] += c[-1]
            c[-1] = 0
        ans = 0
        for j in c:
            ans += (j // 3)
        print(ans)


t=int(input())
for i in range(t):
    n=int(input())
    s=input()
    b=[]
    for j in s:
        b.append(j)
    j=0
    ans=0
    while(j<n):
        p=b[j%n]
        q=b[(j+1)%n]
        r=b[(j+2)%n]
        if p==q and p==r:
            if b[(j+1)%n]=="R":
                b[(j+1)%n]="L"
            else:
                b[(j + 1) % n] = "R"
            ans+=1
        j+=1
    print(ans)


t=int(input())
for i in range(t):
    n=int(input())
    s=input()
    b=[]
    for j in s:
        b.append(j)
    if len(set(b))==1:
        print((n+2)//3)
    else:
        curr = b[0]
        count = 1
        j = 1
        c = []
        while (j < n):
            if b[j] == curr:
                count += 1
            else:
                c.append(count)
                count = 1
            curr = b[j]
            j += 1
        c.append(count)
        if b[0] == b[-1]:
            c[0] += c[-1]
            c[-1] = 0
        ans = 0
        for j in c:
            ans += (j // 3)
        print(ans)


import sys
input=sys.stdin.buffer.readline
inin=lambda: int(input())
inar=lambda: list(map(int,input().split()))
inst=lambda: input().decode().rstrip('\n\r')
INF=float('inf')

from math import ceil
for _t_ in range(inin()):
    n=inin()
    s=list(inst())
    if len(set(s))==1:
        print(ceil(n//3))
        continue
    else:
        i=0
        while(s[i]==s[-1]):
            s.append(s[i])
            i+=1
        s=s[i:]
        ans=0
        cnt=0
        prev=' '
        #print(s)
        for i in s:
            #print(i,prev)
            if i==prev:
                cnt+=1
            else:
                ans+=(cnt//3)
                cnt=1
                prev=i
        ans+=cnt//3
        print(ans)


import sys
input=sys.stdin.buffer.readline
inin=lambda: int(input())
inar=lambda: list(map(int,input().split()))
inst=lambda: input().decode().rstrip('\n\r')
INF=float('inf')
#from collections import deque as que, defaultdict as vector, Counter
#from bisect import bisect
#from heapq import heapify, heappush as hpush, heappop as hpop
'''from types import GeneratorType
def recursive(f, stack=[]):
    def wrappedfunc(*args, **kwargs):
        if stack:
            return f(*args, **kwargs)
        else:
            to = f(*args, **kwargs)
            while True:
                if type(to) is GeneratorType:
                    stack.append(to)
                    to = next(to)
                else:
                    stack.pop()
                    if not stack:
                        break
                    to = stack[-1].send(to)
            return to
    return wrappedfunc'''

_T_=inin()
for _t_ in range(_T_):
    n=inin()
    s=inst()
    attacked_by=[[] for i in range(n)]
    attacking_to=[[] for i in range(n)]
    for i in range(n):
        if s[i]=='L':
            attacking_to[i].append((i-1)%n)
            attacked_by[(i-1)%n].append(i)
        else:
            attacking_to[i].append((i+1)%n)
            attacked_by[(i+1)%n].append(i)
    #print(attacked_by)
    #print(attacking_to)
    cnt=0
    for i in range(n):
        if len(attacking_to[i])==len(attacked_by[i])==1 and attacking_to[i][0]!=attacked_by[i][0]:
            cnt+=1
            bechara=attacking_to[i][0]
            maza_lootne_wala=attacked_by[i][0]
            attacked_by[bechara].remove(i)
            attacking_to[i].remove(bechara)
            attacking_to[i].append(maza_lootne_wala)
            attacked_by[maza_lootne_wala].append(i)
    print(cnt)


import sys
input=sys.stdin.buffer.readline
inin=lambda: int(input())
inar=lambda: list(map(int,input().split()))
inst=lambda: input().decode().rstrip('\n\r')

for _t_ in range(inin()):
    n=inin()
    s=list(inst())
    if len(set(s))==1:
        print((n+1)//3)
        continue
    else:
        i=0
        while(s[i]==s[-1]):
            s.append(s[i])
            i+=1
        s=s[i:]
        ans=0
        cnt=0
        prev=' '
        #print(s)
        for i in s:
            #print(i,prev)
            if i==prev:
                cnt+=1
            else:
                ans+=(cnt//3)
                cnt=1
                prev=i
        ans+=cnt//3
        print(ans)


import sys
input=sys.stdin.buffer.readline
inin=lambda: int(input())
inar=lambda: list(map(int,input().split()))
inst=lambda: input().decode().rstrip('\n\r')
from math import ceil
for _t_ in range(inin()):
    n=inin()
    s=list(inst())
    if len(set(s))==1:
        print(ceil(n/3))
        continue
    else:
        i=0
        while(s[i]==s[-1]):
            s.append(s[i])
            i+=1
        s=s[i:]
        ans=0
        cnt=0
        prev=' '
        #print(s)
        for i in s:
            #print(i,prev)
            if i==prev:
                cnt+=1
            else:
                ans+=(cnt//3)
                cnt=1
                prev=i
        ans+=cnt//3
        print(ans)


import sys
input=sys.stdin.buffer.readline
inin=lambda: int(input())
inar=lambda: list(map(int,input().split()))
inst=lambda: input().decode().rstrip('\n\r')
INF=float('inf')
#from collections import deque as que, defaultdict as vector, Counter
#from bisect import bisect
#from heapq import heapify, heappush as hpush, heappop as hpop
'''from types import GeneratorType
def recursive(f, stack=[]):
    def wrappedfunc(*args, **kwargs):
        if stack:
            return f(*args, **kwargs)
        else:
            to = f(*args, **kwargs)
            while True:
                if type(to) is GeneratorType:
                    stack.append(to)
                    to = next(to)
                else:
                    stack.pop()
                    if not stack:
                        break
                    to = stack[-1].send(to)
            return to
    return wrappedfunc'''

_T_=inin()
for _t_ in range(_T_):
    n=inin()
    s=inst()
    attacked_by=[[] for i in range(n)]
    attacking_to=[[] for i in range(n)]
    for i in range(n):
        if s[i]=='L':
            attacking_to[i].append((i-1)%n)
            attacked_by[(i-1)%n].append(i)
        else:
            attacking_to[i].append((i+1)%n)
            attacked_by[(i+1)%n].append(i)
    #print(attacked_by)
    #print(attacking_to)
    cnt=0
    for i in range(n):
        if len(attacking_to[i])==len(attacked_by[i])==1 and attacking_to[i][0]!=attacked_by[i][0]:
            cnt+=1
            bechara=attacking_to[i][0]
            maza_lootne_wala=attacked_by[i][0]
            attacked_by[bechara].remove(i)
            attacking_to[i].remove(bechara)
            attacking_to[i].append(maza_lootne_wala)
            attacked_by[maza_lootne_wala].append(i)
    print(min(cnt,n-cnt))


import sys
input=sys.stdin.buffer.readline
inin=lambda: int(input())
inar=lambda: list(map(int,input().split()))
inst=lambda: input().decode().rstrip('\n\r')
for _t_ in range(inin()):
    n=inin()
    s=list(inst())
    if len(set(s))==1:
        print((n-1)//3 + 1)
        continue
    else:
        i=0
        while(s[i]==s[-1]):
            s.append(s[i])
            i+=1
        s=s[i:]
        ans=0
        cnt=0
        prev=' '
        #print(s)
        for i in s:
            #print(i,prev)
            if i==prev:
                cnt+=1
            else:
                ans+=(cnt//3)
                cnt=1
                prev=i
        ans+=cnt//3
        print(ans)


import sys
input=sys.stdin.buffer.readline
inin=lambda: int(input())
inar=lambda: list(map(int,input().split()))
inst=lambda: input().decode().rstrip('\n\r')

for _t_ in range(inin()):
    n=inin()
    s=list(inst())
    if len(set(s))==1:
        print(n//3 + 1)
        continue
    else:
        i=0
        while(s[i]==s[-1]):
            s.append(s[i])
            i+=1
        s=s[i:]
        ans=0
        cnt=0
        prev=' '
        #print(s)
        for i in s:
            #print(i,prev)
            if i==prev:
                cnt+=1
            else:
                ans+=(cnt//3)
                cnt=1
                prev=i
        ans+=cnt//3
        print(ans)


import sys
input=sys.stdin.buffer.readline
inin=lambda: int(input())
inar=lambda: list(map(int,input().split()))
inst=lambda: input().decode().rstrip('\n\r')
INF=float('inf')
#from collections import deque as que, defaultdict as vector, Counter
#from bisect import bisect
#from heapq import heapify, heappush as hpush, heappop as hpop
'''from types import GeneratorType
def recursive(f, stack=[]):
    def wrappedfunc(*args, **kwargs):
        if stack:
            return f(*args, **kwargs)
        else:
            to = f(*args, **kwargs)
            while True:
                if type(to) is GeneratorType:
                    stack.append(to)
                    to = next(to)
                else:
                    stack.pop()
                    if not stack:
                        break
                    to = stack[-1].send(to)
            return to
    return wrappedfunc'''

'''_T_=inin()
for _t_ in range(_T_):
    n=inin()
    s=inst()
    attacked_by=[[] for i in range(n)]
    attacking_to=[[] for i in range(n)]
    for i in range(n):
        if s[i]=='L':
            attacking_to[i].append((i-1)%n)
            attacked_by[(i-1)%n].append(i)
        else:
            attacking_to[i].append((i+1)%n)
            attacked_by[(i+1)%n].append(i)
    #print(attacked_by)
    #print(attacking_to)
    cnt=0
    for i in range(n):
        if len(attacking_to[i])==len(attacked_by[i])==1 and attacking_to[i][0]!=attacked_by[i][0]:
            cnt+=1
            bechara=attacking_to[i][0]
            maza_lootne_wala=attacked_by[i][0]
            attacked_by[bechara].remove(i)
            attacking_to[i].remove(bechara)
            attacking_to[i].append(maza_lootne_wala)
            attacked_by[maza_lootne_wala].append(i)
    print(min(cnt,n-cnt))'''
'''
from math import log2
n=inin()
bb=1
arr=[]
for i in range(n):
    for j in range(n):
        print(bb,end=' ')
        arr.append((i+1,j+1))
        bb<<=1
    print()
    #sys.stdout.flush()
q=inin()
for _ in range(q):
    k=inin()
    x,y=1,1
    idx=0
    pointer=2
    print(1,1)
    while(pointer<k):
        if (k&pointer):
            print(*arr[int(log2(pointer))])
        pointer<<=1
'''

for _t_ in range(inin()):
    n=inin()
    s=list(inst())
    if len(set(s))==1:
        print((n-1)//3 + 1)
        continue
    else:
        i=0
        while(s[i]==s[-1]):
            s.append(s[i])
            i+=1
        s=s[i:]
        ans=0
        cnt=0
        prev=' '
        #print(s)
        for i in s:
            #print(i,prev)
            if i==prev:
                cnt+=1
            else:
                ans+=(cnt//3)
                cnt=1
                prev=i
        ans+=cnt//3
        print(ans)


for test in range(int(input())):
    n, s = int(input()), input()
    t = [1]
    for i in range(1, n):
        if s[i] == s[i - 1]:
            ++t[-1]
        else:
            t.append(0)
    if 1 == len(t):
        print((t[0] + 2) // 3)
        continue
    if len(t) % 2:
        t[0] += t.pop(-1)
    print(sum([x // 3 for x in t))


for test in range(int(input())):
    n, s = int(input()), input()
    t = [1]
    for i in range(1, n):
        if s[i] == s[i - 1]:
            ++t[-1]
        else:
            t.append(0)
    if 1 == len(t):
        print((t[0] + 2) // 3)
        continue
    if len(t) % 2:
        t[0] += t.pop(-1)
    print(sum([x // 3 for x in t]))


for test in range(int(input())):
    n, s = int(input()), input()
    t = [1]
    for i in range(1, n):
        if s[i] == s[i - 1]:
            t[-1] += 1
        else:
            t.append(0)
    if 1 == len(t):
        print((t[0] + 2) // 3)
        continue
    if len(t) % 2:
        t[0] += t.pop(-1)
    print(sum([x // 3 for x in t]))


for test in range(int(input())):
    n, s = int(input()), input()
    t = [1]
    for i in range(1, n):
        if s[i] == s[i - 1]:
            t[-1] += 1
        else:
            t.append(1)
    if 1 == len(t):
        print((t[0] + 2) // 3)
        continue
    if len(t) % 2:
        t[0] += t.pop(-1)
    print(sum([x // 3 for x in t]))