Omkar is playing his favorite pixelated video game, Bed Wars! In Bed Wars, there are $n$ players arranged in a circle, so that for all $j$ such that $2 \leq j \leq n$, player $j - 1$ is to the left of the player $j$, and player $j$ is to the right of player $j - 1$. Additionally, player $n$ is to the left of player $1$, and player $1$ is to the right of player $n$.

Currently, each player is attacking either the player to their left or the player to their right. This means that each player is currently being attacked by either $0$, $1$, or $2$ other players. A key element of Bed Wars strategy is that if a player is being attacked by exactly $1$ other player, then they should logically attack that player in response. If instead a player is being attacked by $0$ or $2$ other players, then Bed Wars strategy says that the player can logically attack either of the adjacent players.

Unfortunately, it might be that some players in this game are not following Bed Wars strategy correctly. Omkar is aware of whom each player is currently attacking, and he can talk to any amount of the $n$ players in the game to make them instead attack another player  — i. e. if they are currently attacking the player to their left, Omkar can convince them to instead attack the player to their right; if they are currently attacking the player to their right, Omkar can convince them to instead attack the player to their left. 

Omkar would like all players to be acting logically. Calculate the minimum amount of players that Omkar needs to talk to so that after all players he talked to (if any) have changed which player they are attacking, all players are acting logically according to Bed Wars strategy.


-----Input-----

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The descriptions of the test cases follows.

The first line of each test case contains one integer $n$ ($3 \leq n \leq 2 \cdot 10^5$)  — the amount of players (and therefore beds) in this game of Bed Wars.

The second line of each test case contains a string $s$ of length $n$. The $j$-th character of $s$ is equal to L if the $j$-th player is attacking the player to their left, and R if the $j$-th player is attacking the player to their right.

It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.


-----Output-----

For each test case, output one integer: the minimum number of players Omkar needs to talk to to make it so that all players are acting logically according to Bed Wars strategy.

It can be proven that it is always possible for Omkar to achieve this under the given constraints.


-----Example-----
Input
5
4
RLRL
6
LRRRRL
8
RLLRRRLL
12
LLLLRRLRRRLL
5
RRRRR

Output
0
1
1
3
2



-----Note-----

In the first test case, players $1$ and $2$ are attacking each other, and players $3$ and $4$ are attacking each other. Each player is being attacked by exactly $1$ other player, and each player is attacking the player that is attacking them, so all players are already being logical according to Bed Wars strategy and Omkar does not need to talk to any of them, making the answer $0$.

In the second test case, not every player acts logically: for example, player $3$ is attacked only by player $2$, but doesn't attack him in response. Omkar can talk to player $3$ to convert the attack arrangement to LRLRRL, in which you can see that all players are being logical according to Bed Wars strategy, making the answer $1$.

Difficulty: MEDIUM_HARD
Tags: Dynamic programming

Greedy algorithms
Skill types: Dynamic programming

Greedy algorithms

Starter code:

Solutions:
def translate_to_segments(string):
	firstchar = string[0]
	prevchar = string[0]
	segments = []
	for char in string:
		if len(segments) == 0 or char != prevchar:
			segments.append(0)
			prevchar = char
		segments[-1] = segments[-1] + 1
	if len(segments) > 1 and firstchar == prevchar:
		segments[0] = segments[0] + segments[-1]
		segments[-1] = 0
	return segments

def count_flips(segments):
	if len(segments) == 1:
		return 1 + (segments[0] - 1) // 3
	elif len(segments) == 0:
		return 0
	else:
		summ = 0
		for seg in segments:
			summ = summ + seg // 3
		return summ
total = int(input())
for index in range(2 * total):
	line = input()
	if index % 2 == 0:
		pass
	else:
		segments = translate_to_segments(line)
		flips = count_flips(segments)
		print(flips)


kase = int(input().strip())
while kase > 0:
	kase = kase - 1
	n = int(input().strip())
	s = [str(si) for si in input().strip()]
	slen = len(s)
	ans = 0
	if len(list({}.fromkeys(s).keys())) == 1:
		print((slen - 1) // 3 + 1)
		continue
	for (i, si) in enumerate(s):
		u = i
		v = (i + 1) % slen
		if s[u] is 'R' and s[v] is 'L':
			cnt_r = 0
			while s[u] == 'R':
				cnt_r = cnt_r + 1
				u = (u + slen - 1) % slen
			cnt_l = 0
			while s[v] == 'L':
				cnt_l = cnt_l + 1
				v = (v + 1) % slen
			ans = ans + cnt_l // 3 + cnt_r // 3
	print(ans)


import math
T = int(input())
for _ in range(T):
	N = int(input())
	S = input()
	B = [[-1, -1, -1] for i in range(N)]
	for i in range(N):
		if S[i] == 'L':
			B[i][0] = i - 1
			if i == 0:
				B[i][0] = N - 1
		else:
			B[i][0] = (i + 1) % N
		b = B[i][0]
		if B[b][1] == -1:
			B[b][1] = i
		else:
			B[b][2] = i
	ans = 0
	C = [0 for i in range(N)]
	for i in range(N):
		val = B[i]
		if val[1] != -1 and val[2] == -1 and (val[0] != val[1]):
			C[i] = 1
	flag = -1
	for i in range(N):
		if C[i] == 0:
			flag = i
			break
	if flag == -1:
		ans = math.ceil(N / 3)
	else:
		D = []
		for i in range(N):
			D.append(C[(flag + i) % N])
		D.append(0)
		for i in range(1, N + 1):
			if D[i] == 1:
				D[i] = D[i] + D[i - 1]
			if D[i] == 0:
				ans = ans + math.ceil(D[i - 1] / 3)
	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 = 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)


import sys
input = sys.stdin.readline
from math import ceil
inf = int(10000000000.0)
t = int(input())
for _ in range(t):
	n = int(input())
	lis = list(input().strip())
	p = [0] * n
	for i in range(0, n):
		if lis[(i - 1) % n % n] != lis[i % n] or lis[i % n] != lis[(i + 1) % n]:
			p[i] = 1
		else:
			p[i] = 0
	ans = []
	i = 0
	for j in range(n):
		if p[j] == 1:
			i = j
			break
	r = i
	while i < n + r:
		if p[i % n] == 0:
			v = i
			while i < n + r and p[i % n] == 0:
				i += 1
			vv = i - 1
			ans.append((v, vv))
		i += 1
	val = 0
	for i in ans:
		l = i[1] - i[0] + 1
		if l in (1, 2, 3):
			val += 1
		else:
			val += ceil(l / 3)
	print(val)


from sys import stdin
input = stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	s = input().strip()
	if s == 'R' * n or s == 'L' * n:
		print((n + 2) // 3)
	else:
		bckt = []
		end = n - 1
		ct = 0
		while end >= 0 and s[end] == s[0]:
			ct += 1
			end -= 1
		start = 0
		while start < n and s[start] == s[-1]:
			ct += 1
			start += 1
		bckt.append(ct)
		while start <= end:
			j = start + 1
			while j <= end and s[j] == s[start]:
				j += 1
			bckt.append(j - start)
			start = j
		ans = 0
		for i in bckt:
			ans += i // 3
		print(ans)


import math
t = int(input())
for j in range(t):
	n = int(input())
	s = input()
	set_s = set(s)
	count = 1
	ans = 0
	if len(set_s) > 1:
		if s[0] == s[-1]:
			a = s.index('L')
			b = s.index('R')
			if a > b:
				s = s[a:] + s[b:a]
			else:
				s = s[b:] + s[a:b]
		for i in range(1, n):
			if s[i - 1] == s[i]:
				count += 1
			else:
				ans += count // 3
				count = 1
			if i == n - 1:
				ans += count // 3
		print(ans)
	else:
		print(math.ceil(n / 3))


tc = int(input())
for t in range(tc):
	n = int(input())
	s = str(input())
	l = []
	l.append(0)
	for i in range(n - 1):
		l[-1] += 1
		if s[i] != s[i + 1]:
			l.append(0)
	l[-1] += 1
	ll = len(l)
	if ll > 1:
		if s[0] == s[-1]:
			l[0] += l[-1]
			ll -= 1
		ans = 0
		for i in range(ll):
			ans += l[i] // 3
		print(ans)
	else:
		print((n + 2) // 3)


import sys, math as mt
import heapq as hp
import collections as cc
import bisect as bi
import math as mt
import itertools as it
I = lambda : list(map(int, input().split()))
for tc in range(int(input())):
	(n,) = I()
	s = input().strip()
	run = []
	cur = s[0]
	lenn = 0
	for i in s:
		if i == cur:
			lenn += 1
		else:
			run.append(lenn)
			cur = i
			lenn = 1
	if lenn < n and s[0] == s[-1]:
		run[0] += lenn
	else:
		run.append(lenn)
	ans = 0
	if len(run) == 1 and n >= 3:
		ans += 1
		run[0] -= 1
	for i in run:
		ans += i // 3
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(input())
	ans = float('INF')
	for i in range(n):
		if l[i] != l[i - 1]:
			count = 0
			now = 0
			c = l[i]
			for j in range(n):
				if l[(i + j) % n] == c:
					now += 1
				else:
					count += now // 3
					now = 1
					c = l[(i + j) % n]
			count += now // 3
			ans = min(ans, count)
			break
	if ans == float('INF'):
		print((n + 2) // 3)
	else:
		print(ans)


from math import ceil

def main():
	for _ in range(int(input())):
		size = int(input())
		array = list(input())
		ans = 0
		count = 0
		while len(array) and array[0] == array[-1]:
			count += 1
			array.pop()
		if len(array) == 0:
			if count <= 2:
				print(0)
			else:
				print((count + 2) // 3)
			continue
		array.append('$')
		for i in range(len(array) - 1):
			count += 1
			if array[i] != array[i + 1]:
				ans += count // 3
				count = 0
		print(ans)
main()


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


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):
		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
	memo = [[[-1 for _ in range(2)] for __ in range(n)] for ___ in range(4)]

	@bootstrap
	def dp(t, i, a):
		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')
				elif 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')
				elif 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')
			elif i == n - 1 and a == 0 and (t == 1):
				memo[t][i][a] = (yield dp(t, i - 1, 1)) + helper(i, 'L')
			elif i == n - 1 and a == 1 and (t == 3):
				memo[t][i][a] = float('inf')
			elif i == n - 1 and a == 1 and (t == 2):
				memo[t][i][a] = (yield dp(t, i - 1, 0)) + helper(i, 'R')
			elif a == 0:
				memo[t][i][a] = min((yield dp(t, i - 1, 1)) + helper(i, 'L'), (yield dp(t, i - 2, 1)) + helper(i, 'LL'))
			else:
				memo[t][i][a] = min((yield dp(t, i - 1, 0)) + helper(i, 'R'), (yield dp(t, i - 2, 0)) + helper(i, 'RR'))
		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(res)


for _ in range(int(input())):
	n = int(input())
	s = input()
	sl = list(s)
	cnt = 0
	while sl and sl[0] == sl[-1]:
		cnt += 1
		sl.pop()
	if len(set(s)) == 1:
		if n <= 3:
			print(n // 3)
		else:
			print((n + 2) // 3)
	else:
		ans = 0
		sl.append('$')
		for i in range(len(sl) - 1):
			cnt += 1
			if sl[i] != sl[i + 1]:
				ans += cnt // 3
				cnt = 0
		print(ans)


t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(input())
	ans = 0
	cnt = 0
	while len(a):
		if a[0] != a[-1]:
			break
		cnt += 1
		a.pop(-1)
	if len(a) == 0:
		if cnt <= 2:
			print(0)
		elif cnt == 3:
			print(1)
		else:
			print(int((cnt + 2) / 3))
		continue
	a.append('$')
	for i in range(len(a) - 1):
		cnt += 1
		if a[i] != a[i + 1]:
			ans += int(cnt / 3)
			cnt = 0
	print(ans)


from collections import defaultdict as dd
import math
import sys
import heapq
import copy
input = sys.stdin.readline

def nn():
	return int(input())

def li():
	return list(input())

def mi():
	return list(map(int, input().split()))

def lm():
	return list(map(int, input().split()))

def solve():
	n = nn()
	s = input()
	streaks = []
	st = 1
	for i in range(n - 1):
		if s[i] == s[i + 1]:
			st += 1
		else:
			streaks.append(st)
			st = 1
	if s[0] == s[n - 1] and len(streaks) > 0:
		streaks[0] += st
	else:
		streaks.append(st)
	moves = 0
	if len(streaks) == 1:
		moves = (streaks[0] - 1) // 3 + 1
	else:
		for num in streaks:
			moves += num // 3
	print(moves)
q = nn()
for _ in range(q):
	solve()


def ne(c):
	if c == 'L':
		return 'R'
	return 'L'
for _ in range(int(input())):
	n = int(input())
	s = input()
	ans = 0
	if s[0] * n == s:
		ans += 1
		s = ne(s[0]) + s[0] * (n - 1)
	if s[0] == s[-1]:
		i = 0
		while s[i] == s[0]:
			i += 1
		s = s[i:] + s[:i]
	lst = '#'
	cnt = 0
	for i in range(n):
		if lst != s[i]:
			lst = s[i]
			ans += cnt // 3
			cnt = 1
		else:
			cnt += 1
	ans += cnt // 3
	print(ans)


from itertools import *
for s in [*open(0)][2::2]:
	a = [len([*g]) for (k, g) in groupby(s[:-1])]
	a[0] += len(a) % 2 and (a[1:] and a.pop() or 2)
	print(sum((x // 3 for x in a)))


t = int(input())
for t in range(t):
	n = int(input())
	a = [int(x == 'R') for x in input()]
	fl = 1
	for i in range(1, n):
		if a[i] != a[0]:
			fl = 0
			break
	if fl:
		if n < 3:
			print(0)
		elif n % 3 == 0:
			print(round(n / 3))
		else:
			print(n // 3 + 1)
	else:
		a = a[i:len(a)] + a[0:i]
		dat = [[a[0]]]
		for i in range(1, n):
			if dat[-1][-1] == a[i]:
				dat[-1].append(a[i])
			else:
				dat.append([a[i]])
		lns = [len(d) for d in dat]
		q = 0
		for ln in lns:
			if ln < 3:
				continue
			elif ln % 3 == 0:
				q += round(ln / 3)
			else:
				q += int(ln / 3)
		print(q)


from sys import stdin, stdout

def INPUT():
	return list((int(i) for i in stdin.readline().split()))

def inp():
	return stdin.readline()

def out(x):
	return stdout.write(x)
import math
import random
for _ in range(int(input())):
	n = int(input())
	A = list(input())
	if len(set(A)) == 1:
		print((n + 2) // 3)
		continue
	cur = 1
	x = []
	for i in range(n):
		if A[i] == A[(i + 1) % n]:
			cur += 1
		else:
			x.append(cur)
			cur = 1
	if A[0] == A[-1]:
		x[0] += cur - 1
	ans = 0
	for i in x:
		ans += i // 3
	print(ans)


def tc():
	n = int(input())
	s = input()
	segs = []
	cur = s[0]
	seg = 1
	for ch in s[1:]:
		if ch == cur:
			seg += 1
		else:
			segs.append(seg)
			seg = 1
			cur = ch
	if s[0] == s[-1] and segs:
		segs[0] += seg
	else:
		segs.append(seg)
	ans = 0
	if len(segs) == 1:
		ans = (segs[0] - 1) // 3 + 1
	else:
		for seg in segs:
			if seg >= 3:
				ans += seg // 3
	print(ans)
T = int(input())
for _ in range(T):
	tc()


import sys

def input():
	return sys.stdin.readline().rstrip()

def input_split():
	return [int(i) for i in input().split()]
import random

def sol(arr):
	n = len(arr)
	if n == 3:
		if sum(arr) == 3 or sum(arr) == 0:
			ans = 1
		else:
			ans = 0
	elif n == 4:
		if sum(arr) == 3 or sum(arr) == 1:
			ans = 1
		elif sum(arr) == 4 or sum(arr) == 0:
			ans = 2
		else:
			ans = 0
	else:
		ans = 0
		for i in range(n):
			(x, y, z) = (arr[(i - 1) % n], arr[i], arr[(i + 1) % n])
			if x == y == z:
				ans += 1
				(l, r) = (arr[(i - 2) % n], arr[(i + 2) % n])
				if l == r:
					arr[i] = 1 - arr[i]
				elif l == x:
					arr[(i - 1) % n] = 1 - arr[(i - 1) % n]
				elif r == x:
					arr[(i + 1) % n] = 1 - arr[(i + 1) % n]
				else:
					raise Exception('shouldnt have come here')
	return ans

def is_correct(arr):
	for i in range(n):
		(x, y, z) = (arr[(i - 1) % n], arr[i], arr[(i + 1) % n])
		if x == y == z:
			return False
	return True

def brute(arr):
	costs = []
	for i in range(int(2 ** n)):
		barr = arr.copy()
		cost = 0
		for j in range(n):
			if i // 2 ** j % int(2) == 1:
				barr[j] = 1 - barr[j]
				cost += 1
		if is_correct(barr):
			costs.append(cost)
	return min(costs)
testCases = int(input())
answers = []
for _ in range(testCases):
	n = int(input())
	s = input()
	arr = [1 if i == 'L' else 0 for i in s]
	poss = []
	for i in range(5):
		poss.append(sol(arr[i:] + arr[:i]))
	answers.append(min(poss))
print(*answers, sep='\n')


import sys
input = sys.stdin.readline
for f in range(int(input())):
	n = int(input())
	s = input()
	s = s[0:n]
	mn = n
	for i in range(4):
		mins = [n] * 4
		mins[i] = 0
		if i < 2 and s[0] == 'L':
			mins[i] = 1
		if i > 1 and s[0] == 'R':
			mins[i] = 1
		for j in range(1, n):
			nmins = [n] * 4
			nmins[0] = min(mins[2], mins[3])
			nmins[1] = mins[0]
			nmins[2] = mins[3]
			nmins[3] = min(mins[0], mins[1])
			if s[j] == 'L':
				nmins[0] += 1
				nmins[1] += 1
			else:
				nmins[2] += 1
				nmins[3] += 1
			for k in range(4):
				mins[k] = nmins[k]
		if i == 0:
			mn = min(mn, mins[2], mins[3])
		if i == 1:
			mn = min(mn, mins[0])
		if i == 2:
			mn = min(mn, mins[3])
		if i == 3:
			mn = min(mn, mins[0], mins[1])
	print(mn)


import sys
sys.setrecursionlimit(10 ** 5)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep='\n')

def II():
	return int(sys.stdin.readline())

def MI():
	return map(int, sys.stdin.readline().split())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def SI():
	return sys.stdin.readline()[:-1]
for _ in range(II()):
	n = II()
	s = SI()
	cR = s.count('R')
	if cR == n or cR == 0:
		if n < 3:
			ans = 0
		elif n == 3:
			ans = 1
		else:
			ans = (n + 2) // 3
		print(ans)
		continue
	s *= 2
	si = -1
	for i in range(1, n):
		if s[i - 1] != s[i]:
			si = i
			break
	cnt = 1
	ans = 0
	for i in range(si, si + n):
		if s[i] != s[i + 1]:
			ans += cnt // 3
			cnt = 1
		else:
			cnt += 1
	print(ans)


T = int(input())
for _ in range(0, T):
	n = int(input())
	s = input()
	L = []
	c = 1
	for i in range(1, len(s)):
		if s[i] != s[i - 1]:
			L.append(c)
			c = 1
		else:
			c += 1
	L.append(c)
	if len(L) > 1:
		if s[-1] == s[0]:
			L[0] += L[-1]
			L = L[:len(L) - 1]
		ans = 0
		for i in range(0, len(L)):
			tt = L[i] // 3
			ans += tt
		print(ans)
	else:
		ans = L[0] // 3
		if L[0] % 3 != 0:
			ans += 1
		print(ans)


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


for _ in range(int(input())):
	n = int(input())
	a = input()
	b = [False] * n
	ans = 0
	for i in range(n):
		if a[i - 1] == 'L' and a[(i + 1) % n] == 'R':
			b[i] = True
		elif a[i] == 'R' and a[(i + 1) % n] == 'L':
			b[i] = True
		elif a[i] == 'L' and a[i - 1] == 'R':
			b[i] = True
	c = 0
	s = 0
	if b[0] == False and b[-1] == False:
		for i in range(n):
			if s == 0:
				if b[i] == False:
					c += 1
				else:
					s = c
					c = 0
			elif b[i] == False:
				c += 1
			else:
				ans += c // 3 + 1 * (c % 3 != 0)
				c = 0
		s = c + s
		ans += s // 3 + 1 * (s % 3 != 0)
		print(ans)
	else:
		for i in range(n):
			if b[i] == False:
				c += 1
			else:
				ans += c // 3 + 1 * (c % 3 != 0)
				c = 0
		ans += c // 3 + 1 * (c % 3 != 0)
		print(ans)


t = int(input())
for ii in range(t):
	n = int(input())
	s = list(input())
	count = 0
	diff = None
	for i in range(n):
		if s[i] != s[0]:
			diff = i
			break
	if diff is None:
		count = n // 3
		if n % 3 > 0:
			count += 1
		print(count)
	else:
		sameC = 0
		letter = s[diff]
		i = 0
		while i < n:
			index = (diff + i) % n
			while i < n and s[index] == letter:
				sameC += 1
				i += 1
				index = (diff + i) % n
			count += sameC // 3
			sameC = 0
			letter = s[index]
		print(count)


t = int(input())
for _ in range(t):
	n = int(input())
	s = list(input())
	if s[-1] == s[0]:
		i = n - 1
		while i > 0 and s[i] == s[0]:
			i -= 1
		if i != 0:
			s = s[i + 1:] + s[:i + 1]
	ans = 0
	for i in range(n):
		if s[i] == 'R':
			if not (s[(i + 1) % n] == 'L' or s[(i + 2) % n] == 'L'):
				ans += 1
				if s[(i + 3) % n] == 'R':
					s[(i + 2) % n] = 'L'
		elif not (s[(i - 1) % n] == 'R' or s[(i - 2) % n] == 'R'):
			ans += 1
			s[i] = 'R'
	print(ans)


for _ in range(int(input())):
	n = int(input())
	s = input()
	l = []
	i = 0
	while i < n:
		ct = 0
		while i < n and s[i] == 'L':
			ct += 1
			i += 1
		if ct > 0:
			l.append(ct)
		ct = 0
		while i < n and s[i] == 'R':
			ct += 1
			i += 1
		if ct > 0:
			l.append(ct)
	if s[0] == s[-1]:
		l[0] += l[-1]
		l.pop()
	if len(l) < 2:
		print((n + 2) // 3)
	else:
		ct = 0
		for i in l:
			ct += i // 3
		print(ct)


import math
for i in range(int(input())):
	n = int(input())
	arr = list(input())
	ans = 0
	j = 0
	z = 0
	for i in range(n - 1, -1, -1):
		if arr[i] != arr[0]:
			z = i
			break
	if z == 0:
		print(math.ceil(n / 3))
	else:
		br = arr[z + 1:]
		arr = br + arr[:z + 1]
		ct = 1
		for i in range(1, n):
			if arr[i] != arr[i - 1]:
				ans += ct // 3
				ct = 0
			ct += 1
		ans += ct // 3
		print(ans)


for _ in range(int(input())):
	n = int(input())
	l = str(input())
	l1 = []
	ans = 0
	count = 1
	t = -1
	for i in range(1, n):
		if l[i] == l[i - 1]:
			count += 1
		else:
			break
	if l[0] == l[-1]:
		count += 1
		for j in range(n - 1, i, -1):
			if l[j - 1] == l[j]:
				count += 1
			else:
				break
		l1.append(count)
	else:
		j = n
		l1.append(count)
	count = 1
	if l1[0] == n + 1:
		if n % 3 == 0:
			print(n // 3)
		else:
			print(n // 3 + 1)
		continue
	for k in range(i + 1, j):
		if l[k] == l[k - 1]:
			count += 1
		else:
			l1.append(count)
			count = 1
	l1.append(count)
	for i in l1:
		ans += i // 3
	print(ans)


import math as mt
import sys
import bisect
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	partl = []
	partr = []
	ind = -1
	(cntl, cntr) = (0, 0)
	(indl, indr) = (n, n)
	for i in range(n - 1, -1, -1):
		if s[i] != 'L':
			break
		cntl += 1
		indl -= 1
	for i in range(n - 1, -1, -1):
		if s[i] != 'R':
			break
		cntr += 1
		indr -= 1
	ans = 0
	for i in range(indl):
		if s[i] == 'L':
			cntl += 1
		else:
			ans += cntl // 3
			cntl = 0
	if cntl == n:
		ans += mt.ceil(cntl / 3)
	else:
		ans += cntl // 3
	for i in range(indr):
		if s[i] == 'R':
			cntr += 1
		else:
			ans += cntr // 3
			cntr = 0
	if cntr == n:
		ans += mt.ceil(cntr / 3)
	else:
		ans += cntr // 3
	print(ans)


tc = int(input())
while tc:
	n = int(input())
	s = input()
	if s.count('L') == n or s.count('R') == n:
		ans = (n + 2) // 3
		print(ans)
	else:
		i = 0
		tmp = []
		while i < n:
			j = i
			cnt = 0
			while j < n and s[j] == s[i]:
				j += 1
				cnt += 1
			tmp.append(cnt)
			i = j
		l = len(tmp)
		if s[n - 1] == s[0]:
			tmp[0] += tmp[l - 1]
			tmp.pop()
		ans = 0
		for t in tmp:
			ans += t // 3
		print(ans)
	tc -= 1


from math import inf
import sys
input = sys.stdin.readline

def print(val):
	sys.stdout.write(str(val) + '\n')

def prog():
	for _ in range(int(input())):
		n = int(input())
		s = input().strip()
		dp = [[[0] * 4 for i in range(n - 1)] for i in range(4)]
		dp[0][0][0] = (s[0] != 'L') + (s[n - 1] != 'L')
		for i in range(4):
			if i != 0:
				dp[0][0][i] = inf
		dp[1][0][2] = (s[0] != 'L') + (s[n - 1] != 'R')
		for i in range(4):
			if i != 2:
				dp[1][0][i] = inf
		dp[2][0][1] = (s[0] != 'R') + (s[n - 1] != 'L')
		for i in range(4):
			if i != 1:
				dp[2][0][i] = inf
		dp[3][0][3] = (s[0] != 'R') + (s[n - 1] != 'R')
		for i in range(4):
			if i != 3:
				dp[3][0][i] = inf
		for i in range(4):
			for j in range(1, n - 1):
				dp[i][j][0] = ('L' != s[j]) + dp[i][j - 1][2]
				dp[i][j][3] = ('R' != s[j]) + dp[i][j - 1][1]
				dp[i][j][1] = ('R' != s[j]) + min(dp[i][j - 1][0], dp[i][j - 1][2])
				dp[i][j][2] = ('L' != s[j]) + min(dp[i][j - 1][1], dp[i][j - 1][3])
		mn = min(dp[0][n - 2][3], dp[0][n - 2][1], dp[1][n - 2][2], dp[1][n - 2][1], dp[1][n - 2][0], dp[2][n - 2][3], dp[2][n - 2][2], dp[2][n - 2][1], dp[3][n - 2][2], dp[3][n - 2][0])
		print(mn)
prog()


from sys import stdin, stdout
from math import gcd, sqrt, factorial
from collections import deque, defaultdict
input = stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\n')
L = lambda : list(R())
P = lambda x: stdout.write(x)
lcm = lambda x, y: x * y // gcd(x, y)
hg = lambda x, y: (y + x - 1) // x * x
pw = lambda x: 0 if x == 1 else 1 + pw(x // 2)
chk = lambda x: chk(x // 2) if not x % 2 else True if x == 1 else False
sm = lambda x: (x ** 2 + x) // 2
N = 10 ** 9 + 7
for _ in range(I()):
	n = I()
	s = S()
	a = []
	for i in range(n):
		if s[(i - 1) % n] == 'R' and s[(i + 1) % n] == 'L' or (s[(i - 1) % n] == 'L' and s[(i + 1) % n] == 'R') or (s[(i - 1) % n] == 'R' and s[i] == 'L') or (s[i] == 'R' and s[(i + 1) % n] == 'L'):
			a += (0,)
		else:
			a += (1,)
	ans = 0
	val = []
	ind = -1
	f = -1
	for i in range(n):
		if ind == -1:
			if not a[i]:
				ind = i
				f = i
		elif not a[i]:
			ans += (i - ind - 1 + 2) // 3
			ind = i
	if ind == -1:
		print((n + 2) // 3)
	else:
		print(ans + (n - ind - 1 + f + 2) // 3)


for s in [*open(0)][2::2]:
	f = [1]
	for (b, a) in zip(s, s[1:-1]):
		if a == b:
			f[-1] += 1
		else:
			f.append(1)
	if s[0] == s[-2] and len(f) > 1:
		f[0] += f.pop()
	f[0] += 2 * (len(f) < 2)
	print(sum((i // 3 for i in f)))


def find_cost_of_reordering(directions):
	assert len(directions) >= 3
	if len(set(directions)) == 1:
		return 1 + (len(directions) - 1) // 3
	chains_lengths = _extract_lengths_of_chains_of_same_directions(directions)
	cost = sum((chain_length // 3 for chain_length in chains_lengths if chain_length >= 3))
	return cost
_DIRECTION_SAME = 'S'
_DIRECTION_CHANGED = 'C'

def _extract_lengths_of_chains_of_same_directions(directions):
	if len(set(directions)) == 1:
		return [len(directions)]
	directions_shifted_left = directions[1:] + directions[0]
	directions_diffs = ''.join((_DIRECTION_SAME if curr_dir == next_dir else _DIRECTION_CHANGED for (curr_dir, next_dir) in zip(directions, directions_shifted_left)))
	first_changed_i = directions_diffs.index(_DIRECTION_CHANGED)
	directions_diffs = directions_diffs[first_changed_i + 1:] + directions_diffs[:first_changed_i + 1]
	assert directions_diffs[-1] == _DIRECTION_CHANGED
	directions_diffs = directions_diffs[:-1]
	stability_chains = directions_diffs.split(_DIRECTION_CHANGED)
	chains_lengths = [len(stability_chain) + 1 for stability_chain in stability_chains]
	return chains_lengths

def main():
	t = int(input())
	for i in range(t):
		n = int(input())
		directions = input()
		result = find_cost_of_reordering(directions)
		print(result)
main()


from collections import deque
import sys

def input():
	return sys.stdin.readline().rstrip()

def slv():
	n = int(input())
	G = [[] for i in range(n)]
	IN = [0] * n
	S = input()
	if S == 'L' * n or S == 'R' * n:
		print((n + 2) // 3)
		return
	for i in range(n):
		(l, r) = (i - 1, (i + 1) % n)
		if S[i] == 'R':
			G[i].append(r)
			IN[r] += 1
		else:
			G[i].append(l)
			IN[l] += 1
	ans = 0
	for i in range(n):
		if IN[i] == 0:
			l = 0
			p = i
			while IN[p] != 2:
				p = G[p][0]
				l += 1
			ans += (l + 1) // 3
	print(ans)
	return

def main():
	t = int(input())
	for _ in range(t):
		slv()
	return
main()


import math
import collections
from sys import stdin, stdout, setrecursionlimit
from bisect import bisect_left as bsl
from bisect import bisect_right as bsr
import heapq as hq
setrecursionlimit(2 ** 20)
t = 1
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	s = stdin.readline().strip('\n')
	ans = 0
	prv = s[0]
	l = []
	d = {}
	cnt = 0
	for i in range(n):
		d[s[i]] = 1
		if s[i] == prv:
			cnt += 1
		else:
			l.append(cnt)
			cnt = 1
		prv = s[i]
	l.append(cnt)
	if s[0] == s[-1]:
		sz = len(l) - 1
		l[0] += l[-1]
	else:
		sz = len(l)
	for i in range(sz):
		ans += l[i] // 3
	if len(d) == 1:
		print(1 + (n - 1) // 3)
		continue
	print(ans)


for _ in range(int(input())):
	n = int(input())
	s = str(input())
	a = []
	t = s[0]
	c = 1
	for i in s[1:]:
		if i == t:
			c += 1
		else:
			a.append(c)
			c = 1
		t = i
	a.append(c)
	if s[0] == s[-1] and len(a) > 1:
		a[0] += a[-1]
		del a[-1]
	ans = 0
	if len(a) == 1:
		print(a[0] // 3 + int(bool(a[0] % 3)))
	else:
		for i in a:
			ans += i // 3
		print(ans)


def find_cost_of_reordering(directions):
	assert len(directions) >= 3
	directions_n = len(directions)
	get_direction = lambda index: directions[index % directions_n]
	if len(set(directions)) == 1:
		return 1 + (directions_n - 1) // 3
	i = 0
	while get_direction(i) == get_direction(i - 1):
		i += 1
	directions = directions[i:] + directions[:i]
	cost = 0
	chain_length = 1
	for i in range(1, directions_n):
		if get_direction(i) == get_direction(i - 1):
			chain_length += 1
		else:
			cost += chain_length // 3
			chain_length = 1
	cost += chain_length // 3
	return cost

def main():
	t = int(input())
	for i in range(t):
		n = int(input())
		directions = input()
		result = find_cost_of_reordering(directions)
		print(result)
main()


import math
import random
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
import sys
import threading
from collections import defaultdict
threading.stack_size(10 ** 8)
mod = 10 ** 9 + 7
mod1 = 998244353
import os
import sys
from io import BytesIO, IOBase
sys.setrecursionlimit(300000)
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')

class TreeNode:

	def __init__(self, k, v):
		self.key = k
		self.value = v
		self.left = None
		self.right = None
		self.parent = None
		self.height = 1
		self.num_left = 1
		self.num_total = 1

class AvlTree:

	def __init__(self):
		self._tree = None

	def add(self, k, v):
		if not self._tree:
			self._tree = TreeNode(k, v)
			return
		node = self._add(k, v)
		if node:
			self._rebalance(node)

	def _add(self, k, v):
		node = self._tree
		while node:
			if k < node.key:
				if node.left:
					node = node.left
				else:
					node.left = TreeNode(k, v)
					node.left.parent = node
					return node.left
			elif node.key < k:
				if node.right:
					node = node.right
				else:
					node.right = TreeNode(k, v)
					node.right.parent = node
					return node.right
			else:
				node.value = v
				return

	@staticmethod
	def get_height(x):
		return x.height if x else 0

	@staticmethod
	def get_num_total(x):
		return x.num_total if x else 0

	def _rebalance(self, node):
		n = node
		while n:
			lh = self.get_height(n.left)
			rh = self.get_height(n.right)
			n.height = max(lh, rh) + 1
			balance_factor = lh - rh
			n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right)
			n.num_left = 1 + self.get_num_total(n.left)
			if balance_factor > 1:
				if self.get_height(n.left.left) < self.get_height(n.left.right):
					self._rotate_left(n.left)
				self._rotate_right(n)
			elif balance_factor < -1:
				if self.get_height(n.right.right) < self.get_height(n.right.left):
					self._rotate_right(n.right)
				self._rotate_left(n)
			else:
				n = n.parent

	def _remove_one(self, node):
		replacement = node.left or node.right
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = replacement
			else:
				node.parent.right = replacement
			replacement.parent = node.parent
			node.parent = None
		else:
			self._tree = replacement
			replacement.parent = None
		node.left = None
		node.right = None
		node.parent = None
		self._rebalance(replacement)

	def _remove_leaf(self, node):
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = None
			else:
				node.parent.right = None
			self._rebalance(node.parent)
		else:
			self._tree = None
		node.parent = None
		node.left = None
		node.right = None

	def remove(self, k):
		node = self._get_node(k)
		if not node:
			return
		if AvlTree._is_leaf(node):
			self._remove_leaf(node)
			return
		if node.left and node.right:
			nxt = AvlTree._get_next(node)
			node.key = nxt.key
			node.value = nxt.value
			if self._is_leaf(nxt):
				self._remove_leaf(nxt)
			else:
				self._remove_one(nxt)
			self._rebalance(node)
		else:
			self._remove_one(node)

	def get(self, k):
		node = self._get_node(k)
		return node.value if node else -1

	def _get_node(self, k):
		if not self._tree:
			return None
		node = self._tree
		while node:
			if k < node.key:
				node = node.left
			elif node.key < k:
				node = node.right
			else:
				return node
		return None

	def get_at(self, pos):
		x = pos + 1
		node = self._tree
		while node:
			if x < node.num_left:
				node = node.left
			elif node.num_left < x:
				x -= node.num_left
				node = node.right
			else:
				return (node.key, node.value)
		raise IndexError('Out of ranges')

	@staticmethod
	def _is_left(node):
		return node.parent.left and node.parent.left == node

	@staticmethod
	def _is_leaf(node):
		return node.left is None and node.right is None

	def _rotate_right(self, node):
		if not node.parent:
			self._tree = node.left
			node.left.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.left
			node.left.parent = node.parent
		else:
			node.parent.right = node.left
			node.left.parent = node.parent
		bk = node.left.right
		node.left.right = node
		node.parent = node.left
		node.left = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	def _rotate_left(self, node):
		if not node.parent:
			self._tree = node.right
			node.right.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.right
			node.right.parent = node.parent
		else:
			node.parent.right = node.right
			node.right.parent = node.parent
		bk = node.right.left
		node.right.left = node
		node.parent = node.right
		node.right = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	@staticmethod
	def _get_next(node):
		if not node.right:
			return node.parent
		n = node.right
		while n.left:
			n = n.left
		return n

class SegmentTree1:

	def __init__(self, data, default='z', func=lambda a, b: min(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: a + b):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD

def powm(a, n, m):
	if a == 1 or n == 0:
		return 1
	if n % 2 == 0:
		s = powm(a, n // 2, m)
		return s * s % m
	else:
		return a * powm(a, n - 1, m) % m

def sort_list(list1, list2):
	zipped_pairs = zip(list2, list1)
	z = [x for (_, x) in sorted(zipped_pairs)]
	return z

def product(l):
	por = 1
	for i in range(len(l)):
		por *= l[i]
	return por

def binarySearchCount(arr, n, key):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = int((right + left) / 2)
		if arr[mid] < key:
			count = mid + 1
			left = mid + 1
		else:
			right = mid - 1
	return count

def countdig(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

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

def countGreater(arr, n, k):
	l = 0
	r = n - 1
	leftGreater = n
	while l <= r:
		m = int(l + (r - l) / 2)
		if arr[m] >= k:
			leftGreater = m
			r = m - 1
		else:
			l = m + 1
	return n - leftGreater
for ik in range(int(input())):
	n = int(input())
	s = list(input())
	c = 1
	t = n - 1
	ans = 0
	cou = s.count('R')
	if cou == n or cou == 0:
		print(math.ceil(n / 3))
		continue
	while s[t] == s[0]:
		c += 1
		t -= 1
	for i in range(1, t + 1):
		if s[i] == s[i - 1]:
			c += 1
		else:
			ans += c // 3
			c = 1
	if c > 0:
		ans += c // 3
	print(ans)


for _ in range(int(input())):
	n = int(input())
	s = input()
	l = [0]
	for i in range(n - 1):
		l[-1] += 1
		if s[i] != s[i + 1]:
			l.append(0)
	l[-1] += 1
	if len(l) == 1:
		print((n + 2) // 3)
	else:
		if s[0] == s[-1]:
			l[0] += l.pop()
		print(sum((i // 3 for i in l)))


import sys
input = sys.stdin.readline
(inp, ip) = (lambda : int(input()), lambda : [int(w) for w in input().split()])
for _ in range(inp()):
	n = inp()
	s = input().strip()
	prev = -1
	x = []
	for i in range(n):
		if s[i] != prev:
			x.append(0)
		x[-1] += 1
		prev = s[i]
	ans = 0
	if len(x) == 1:
		ans += (x[0] - 1) // 3 + 1
	else:
		if s[0] == s[-1] and len(x) > 1:
			t = x.pop()
			x[0] += t
		for i in x:
			ans += (i - 3) // 3 + 1
	print(ans)


def f(a):
	if len(a) == 0:
		return 0
	(r, l) = (0, 0)
	for i in a:
		if i == 'L':
			l += 1
		else:
			r += 1
	return max(0, (l + 1) // 3) + max(0, (r + 1) // 3)
for _ in range(int(input())):
	n = int(input())
	s = input()
	t = 0
	if n == 2:
		print(0)
		continue
	for i in s:
		t += i == 'L'
	if t % n == 0:
		print((n + 2) // 3)
		continue
	a = []
	for i in s:
		a.append(i)
	s = -1
	for i in range(n + 1):
		if a[i % n] == 'R' and a[(i + 1) % n] == 'L':
			a[i % n] = 'Q'
			a[(i + 1) % n] = 'Q'
			s = (i + 2) % n
	new = [[]]
	now = 0
	while a[s] != 'W':
		if a[s] == 'L' or a[s] == 'R':
			new[now].append(a[s])
		else:
			now += 1
			new.append([])
		a[s] = 'W'
		s += 1
		s %= n
	ans = 0
	for i in new:
		ans += f(i)
	print(ans)


t = int(input())
anss = []
for _ in range(t):
	n = int(input())
	l = list(input())
	currl = l[0]
	cnts = []
	curr_cnt = 1
	for i in l[1:]:
		if i == currl:
			curr_cnt += 1
		else:
			currl = i
			cnts.append(curr_cnt)
			curr_cnt = 1
	cnts.append(curr_cnt)
	if len(cnts) == 1:
		ans = (cnts[0] + 2) // 3
	else:
		if len(cnts) % 2 == 1 and len(cnts) != 1:
			cnts = [cnts[0] + cnts[-1]] + cnts[1:-1]
		ans = 0
		for i in cnts:
			if i >= 3:
				ans += i // 3
	anss.append(ans)
for ans in anss:
	print(ans)


test = int(input())
for _ in range(test):
	n = int(input())
	arr = list(input())
	ans = 0
	i = 0
	freq = []
	while i < n:
		f = 1
		i += 1
		while i < n and arr[i] == arr[i - 1]:
			f += 1
			i += 1
		freq.append(f)
	if len(freq) == 1:
		print((n - 1) // 3 + 1)
	else:
		if len(freq) % 2:
			s = freq.pop()
			freq[0] += s
		res = 0
		for i in range(len(freq)):
			v = freq[i] // 3
			res += v
		print(res)


for _ in range(int(input())):
	n = int(input())
	s = input()
	if len(set(s)) == 1:
		print((n + 2) // 3)
		continue
	cur = 1
	arr = []
	for i in range(n):
		if s[i] == s[(i + 1) % n]:
			cur += 1
		else:
			arr.append(cur)
			cur = 1
	if s[-1] == s[0]:
		arr[0] += cur - 1
	res = 0
	for e in arr:
		res += e // 3
	print(res)


from collections import *
import sys

def ri():
	return int(input())

def rl():
	return list(map(int, input().split()))
t = ri()
for _ in range(t):
	n = ri()
	aa = input()
	groups = []
	start = 0
	current = 1
	while start < n:
		for i in range(start + 1, n):
			if aa[i] == aa[start]:
				current += 1
				if i == n - 1:
					groups.append(current)
					start = n
					break
			else:
				groups.append(current)
				start = i
				current = 1
				if start == n - 1:
					groups.append(current)
					start = n
				break
	if len(groups) == 1:
		print((n + 2) // 3)
	else:
		if aa[0] == aa[-1]:
			last = groups.pop()
			groups[0] += last
		ans = 0
		for g in groups:
			ans += g // 3
		print(ans)


for _ in range(int(input())):
	n = int(input())
	s = input()
	if len(set(s)) == 1:
		print((len(s) + 2) // 3)
		continue
	counter = 0
	kos = len(s) - 1
	if s[0] == s[-1]:
		if s[0] == 'R':
			kos = s.rfind('L')
			s = 'R' * (len(s) - kos - 1) + s[:kos + 1]
		else:
			kos = s.rfind('R')
			s = 'L' * (len(s) - kos - 1) + s[:kos + 1]
	while s != '':
		r = s.find('R')
		l = s.find('L')
		if r == -1 or l == -1:
			counter += len(s) // 3
			s = ''
		else:
			counter += max(r, l) // 3
			s = s[max(r, l):]
	print(counter)


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


from sys import stdin, stdout

def shift(c_a):
	c = c_a[0]
	for i in range(n - 1):
		c_a[i] = c_a[i + 1]
	c_a[n - 1] = c

def omkar_and_bed_wars(n, s):
	c_a = list(s)
	r1 = cal_dp(n, c_a)
	shift(c_a)
	r2 = cal_dp(n, c_a)
	shift(c_a)
	r3 = cal_dp(n, c_a)
	shift(c_a)
	r4 = cal_dp(n, c_a)
	return min(r1, r2, r3, r4)

def cal_dp(n, c_a):
	dp = [10 ** 9] * n
	for i in range(1, n):
		rl = get_cnt(i - 1, c_a, 'RL')
		if i > 1:
			rl += dp[i - 2]
		rrl = 10 ** 9
		rll = 10 ** 9
		if i > 1:
			rrl = get_cnt(i - 2, c_a, 'RRL')
			rll = get_cnt(i - 2, c_a, 'RLL')
			if i > 2:
				rrl += dp[i - 3]
				rll += dp[i - 3]
		rrll = 10 ** 9
		if i > 2:
			rrll = get_cnt(i - 3, c_a, 'RRLL')
			if i > 3:
				rrll += dp[i - 4]
		dp[i] = min(rl, rrl, rll, rrll)
	return dp[n - 1]

def get_cnt(idx, c_a, s):
	cnt = 0
	for i in range(len(s)):
		if c_a[idx + i] != s[i]:
			cnt += 1
	return cnt
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	s = stdin.readline().strip()
	stdout.write(str(omkar_and_bed_wars(n, s)) + '\n')


import sys

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 19
MOD = 10 ** 9 + 7
for _ in range(INT()):
	N = INT()
	S = input()
	ans = INF
	dp = list2d(4, N + 1, INF)
	dp[0][2] = S[:2].count('R')
	for i in range(2, N):
		dp[1][i + 1] = min(dp[1][i + 1], dp[0][i] + (S[i] != 'R'))
		dp[2][i + 1] = min(dp[2][i + 1], dp[3][i] + (S[i] != 'L'))
		dp[2][i + 1] = min(dp[2][i + 1], dp[1][i] + (S[i] != 'L'))
		dp[3][i + 1] = min(dp[3][i + 1], dp[1][i] + (S[i] != 'R'))
		dp[1][i + 1] = min(dp[1][i + 1], dp[2][i] + (S[i] != 'R'))
		dp[0][i + 1] = min(dp[0][i + 1], dp[2][i] + (S[i] != 'L'))
	ans = min(ans, dp[1][N], dp[3][N])
	dp = list2d(4, N + 1, INF)
	dp[1][2] = (S[0] != 'L') + (S[1] != 'R')
	for i in range(2, N):
		dp[1][i + 1] = min(dp[1][i + 1], dp[0][i] + (S[i] != 'R'))
		dp[2][i + 1] = min(dp[2][i + 1], dp[3][i] + (S[i] != 'L'))
		dp[2][i + 1] = min(dp[2][i + 1], dp[1][i] + (S[i] != 'L'))
		dp[3][i + 1] = min(dp[3][i + 1], dp[1][i] + (S[i] != 'R'))
		dp[1][i + 1] = min(dp[1][i + 1], dp[2][i] + (S[i] != 'R'))
		dp[0][i + 1] = min(dp[0][i + 1], dp[2][i] + (S[i] != 'L'))
	ans = min(ans, dp[1][N], dp[2][N], dp[3][N])
	dp = list2d(4, N + 1, INF)
	dp[2][2] = (S[0] != 'R') + (S[1] != 'L')
	for i in range(2, N):
		dp[1][i + 1] = min(dp[1][i + 1], dp[0][i] + (S[i] != 'R'))
		dp[2][i + 1] = min(dp[2][i + 1], dp[3][i] + (S[i] != 'L'))
		dp[2][i + 1] = min(dp[2][i + 1], dp[1][i] + (S[i] != 'L'))
		dp[3][i + 1] = min(dp[3][i + 1], dp[1][i] + (S[i] != 'R'))
		dp[1][i + 1] = min(dp[1][i + 1], dp[2][i] + (S[i] != 'R'))
		dp[0][i + 1] = min(dp[0][i + 1], dp[2][i] + (S[i] != 'L'))
	ans = min(ans, dp[0][N], dp[1][N], dp[2][N])
	dp = list2d(4, N + 1, INF)
	dp[3][2] = S[:2].count('L')
	for i in range(2, N):
		dp[1][i + 1] = min(dp[1][i + 1], dp[0][i] + (S[i] != 'R'))
		dp[2][i + 1] = min(dp[2][i + 1], dp[3][i] + (S[i] != 'L'))
		dp[2][i + 1] = min(dp[2][i + 1], dp[1][i] + (S[i] != 'L'))
		dp[3][i + 1] = min(dp[3][i + 1], dp[1][i] + (S[i] != 'R'))
		dp[1][i + 1] = min(dp[1][i + 1], dp[2][i] + (S[i] != 'R'))
		dp[0][i + 1] = min(dp[0][i + 1], dp[2][i] + (S[i] != 'L'))
	ans = min(ans, dp[0][N], dp[2][N])
	print(ans)


import sys
import math
from math import *
from collections import Counter, defaultdict

def main():
	n = int(input())
	s = input()
	ans = []
	i = 0
	while i < n:
		cnt = 0
		while i < n:
			if s[i] == 'R':
				break
			cnt += 1
			i += 1
		if cnt != 0:
			ans.append(cnt)
		cnt = 0
		while i < n:
			if s[i] == 'L':
				break
			cnt += 1
			i += 1
		if cnt != 0:
			ans.append(cnt)
	if s[0] == s[-1] and len(ans) > 1:
		ans[0] += ans[-1]
		ans.pop()
	elif len(ans) == 1:
		ans[0] += 2
	val = 0
	for i in ans:
		if i >= 3:
			val += i // 3
	print(val)
for i in range(int(input())):
	main()


for _ in range(int(input())):
	n = int(input())
	a = list(input())
	ans = 0
	if len(set(a)) == 1:
		print(1 + (n - 1) // 3)
	elif 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)


t = int(input())
for i in range(t):
	n = int(input())
	l = input()
	p = 0
	b = False
	for i in range(1, n):
		if l[i] != l[i - 1]:
			p = i
			b = True
			break
	if b:
		l = l[p:] + l[:p] + '.'
		t = 0
		r = 0
		for i in range(1, n + 1):
			r += 1
			if l[i] != l[i - 1]:
				t += r // 3
				r = 0
		print(t)
	else:
		print((n - 1) // 3 + 1)


import sys
import math
t = int(input())
i = 0
for line in sys.stdin:
	if i == 0:
		n = int(line)
	else:
		s = line[:-1]
		if s[0] != s[n - 1]:
			cur = s[0]
			amount = 0
			changes = 0
			for j in range(len(s)):
				if s[j] == cur:
					amount += 1
				else:
					cur = s[j]
					changes += amount // 3
					amount = 1
			changes += amount // 3
		else:
			cur = s[0]
			amount = 0
			changes = 0
			start = 0
			while start < len(s) and s[start] == cur:
				start += 1
			if start != len(s):
				cur = s[start]
				for j in range(start, len(s)):
					if s[j] == cur:
						amount += 1
					else:
						cur = s[j]
						changes += amount // 3
						amount = 1
				changes += (amount + start) // 3
			else:
				changes += math.ceil(start / 3)
		print(changes)
	i = 1 - i


import sys

class DOmkarAndBedWars:

	def solve(self, tc=0):
		for _ in range(int(input())):
			n = int(input())
			s = list(input().strip())
			ans = 0
			op = {'R': 'L', 'L': 'R'}
			if s == ['R'] * n or s == ['L'] * n:
				print((n + 2) // 3)
				continue
			sizes = []
			(l, r) = (0, 0)
			while s[r] == s[0]:
				r += 1
			while s[l] == s[0]:
				l -= 1
			sizes.append(r - l - 1)
			l = n + l
			cur = r
			while cur <= l:
				while r <= l and s[cur] == s[r]:
					r += 1
				sizes.append(r - cur)
				cur = r
			print(sum((length // 3 for length in sizes)))
solver = DOmkarAndBedWars()
input = sys.stdin.readline
solver.solve()


def solve(s):
	ans = 0
	if s.count(1) in (len(s), 0):
		s[0] = 1 - s[0]
		ans += 1
	start = 0
	groups = []
	for i in range(len(s) + 1):
		if i == len(s) or s[i] != s[start]:
			groups.append(i - start)
			start = i
	if len(s) > 1 and s[0] == s[-1]:
		groups[0] += groups[-1]
		groups.pop()
	for group in groups:
		ans += group // 3
	return ans
for _ in range(int(input())):
	_ = int(input())
	s = input()
	s = [1 if ch == 'R' else 0 for ch in s]
	print(solve(s))


t = int(input())
for case in range(t):
	n = int(input())
	s = list(map(str, list(input())))
	counter = 0
	l = 0
	r = n - 1
	if s[0] == s[n - 1]:
		for i in range(n):
			if s[i] != s[0]:
				break
			l += 1
		if l != n:
			for i in range(n):
				if s[n - 1 - i] != s[0]:
					break
				r -= 1
		if l == n:
			counter += (n + 2) // 3
		else:
			counter += (l + n - 1 - r) // 3
	blockLength = 0
	if l != n:
		prev = s[l]
	for i in range(l, r + 1):
		if s[i] == prev:
			blockLength += 1
		else:
			counter += blockLength // 3
			blockLength = 1
		prev = s[i]
	counter += blockLength // 3
	print(counter)


import math
for _ in range(int(input())):
	n = int(input())
	s = input()
	l = list(s)
	cnt = s.count('R')
	if cnt == 0 or cnt == n:
		print(math.ceil(n / 3))
	else:
		(start, end, c) = (0, n - 1, 0)
		prev = s[0]
		while start < n and s[start] == prev:
			start += 1
			c += 1
		while end >= 0 and s[end] == prev:
			end -= 1
			c += 1
		ans = math.floor(c / 3)
		prev = 'R'
		c = 0
		for i in range(start, end + 1):
			if s[i] == prev:
				c += 1
			else:
				ans += math.floor(c / 3)
				c = 1
				prev = s[i]
		ans += math.floor(c / 3)
		print(ans)


for _ in range(int(input())):
	n = int(input())
	s = input()
	count = 0
	while s[0] == s[-1] and count < n:
		s = s[1:] + s[0]
		count += 1
	if count >= n:
		print((n + 2) // 3)
	else:
		count = 1
		out = 0
		for i in range(n - 1):
			if s[i] == s[i + 1]:
				count += 1
			else:
				out += count // 3
				count = 1
		out += count // 3
		print(out)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	s = input().strip()
	lengths = []
	curr = s[0]
	currLen = 0
	for c in s:
		if c == curr:
			currLen += 1
		else:
			lengths.append(currLen)
			currLen = 1
			curr = c
	if currLen < n and s[0] == s[-1]:
		lengths[0] += currLen
	else:
		lengths.append(currLen)
	out = 0
	if len(lengths) == 1 and n >= 3:
		out = 1
		lengths[0] -= 1
		lengths.append(1)
	for v in lengths:
		out += v // 3
	print(out)


import math
t = int(input())
for q in range(t):
	n = int(input())
	s = list(input())
	a = s[0]
	start = -1
	for i in range(n):
		if s[i] != a:
			start = i
			break
	if start == -1:
		print((n - 1) // 3 + 1)
		continue
	c = 0
	v = []
	for i in range(n):
		if s[(start + i) % n] == a:
			c += 1
		else:
			v.append(c)
			a = s[(start + i) % n]
			c = 1
	v.append(c)
	sum = 0
	for i in v:
		sum += i // 3
	print(sum)


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)


import math
t = int(input().split()[0])
for case in range(t):
	n = int(input().split()[0])
	s = input().split()[0]
	pt = -1
	for i in range(n):
		if s[i] == 'R' and s[(i + 1) % n] == 'L':
			pt = (i + 1) % n
			break
	if pt == -1:
		print((n + 2) // 3)
		continue
	curr = (pt + 1) % n
	streak = 'N'
	ct = 0
	ans = 0
	while curr != pt:
		if streak == 'N':
			streak = s[curr]
			ct = 1
		elif streak == 'R':
			if s[curr] == 'L':
				streak = 'N'
				ans += ct // 3
			else:
				ct += 1
		elif s[curr] == 'R':
			ans += (ct + 1) // 3
			streak = 'R'
			ct = 1
		else:
			ct += 1
		curr = (curr + 1) % n
	ans += ct // 3
	print(ans)


import math

def func():
	n = int(input())
	s = input()
	end = n - 1
	while s[end] == s[0] and end >= 0:
		end -= 1
	s = s[end + 1:] + s[:end + 1]
	if end == -1:
		x = 0
		if n % 3 != 0:
			x = 1
		return x + n // 3
	(r, l, ans) = (0, 0, 0)
	for i in range(n):
		if s[i] == 'R':
			r += 1
			ans += int(l // 3)
			l = 0
		else:
			l += 1
			ans += int(r // 3)
			r = 0
	ans += int(l // 3) + int(r // 3)
	return ans
t = int(input())
while t > 0:
	t -= 1
	print(func())


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')
import math
for j in range(int(input())):
	n = int(input())
	vals = [k for k in input()]
	first = 0
	setty = set(vals)
	for s in range(n):
		if vals[s] != vals[(s - 1) % n]:
			first = s
			break
	chains = []
	c = []
	for s in range(first, first + n):
		if vals[s % n] == vals[(s - 1) % n]:
			if len(c) == 0 and len(setty) == 2:
				c.append(vals[(s - 1) % n])
			c.append(vals[s % n])
		else:
			if len(c) > 0:
				chains.append(c)
			c = [vals[s % n]]
	if len(chains) > 0:
		if chains[-1] != c:
			chains.append(c)
	else:
		chains.append(c)
	ans = 0
	for s in range(len(chains)):
		if len(chains) >= 2:
			ans += len(chains[s]) // 3
		else:
			ans += math.ceil(len(chains[s]) / 3)
	print(ans)


import math
for _ in range(int(input())):
	n = int(input())
	s = [i for i in input()]
	arr = []
	a = s[0]
	count = 0
	for i in s:
		if i == a:
			count += 1
		else:
			arr.append(count)
			a = i
			count = 1
	arr.append(count)
	if s[0] == s[-1] and len(arr) != 1:
		a = arr.pop()
		arr[0] += a
	ans = 0
	for i in arr:
		if i <= 2:
			continue
		else:
			ans += i // 3
	if len(arr) != 1:
		print(ans)
	else:
		print(math.ceil(arr[0] / 3))


import collections
import math
t = int(input())
for ti in range(t):
	n = int(input())
	state = input()
	counter = collections.Counter(state)
	if len(counter) == 1:
		print(math.ceil(n / 3))
		continue
	answer = 0
	i = 0
	num_of_row = 0
	end_idx = n
	while i < end_idx:
		if i == 0 and state[0] == state[-1]:
			j = n - 1
			while state[j] == state[0]:
				j -= 1
				num_of_row += 1
			end_idx = j + 1
		num_of_row += 1
		if i == end_idx - 1 or state[i] != state[i + 1]:
			answer += num_of_row // 3
			num_of_row = 0
		i += 1
	print(answer)


import sys, bisect, string, math, time, functools, random, fractions
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
from itertools import permutations, combinations, groupby
rep = range
R = range

def Golf():
	(n, *t) = map(int, open(0).read().split())

def I():
	return int(input())

def S_():
	return input()

def IS():
	return input().split()

def LS():
	return [i for i in input().split()]

def MI():
	return map(int, input().split())

def LI():
	return [int(i) for i in input().split()]

def LI_():
	return [int(i) - 1 for i in input().split()]

def NI(n):
	return [int(input()) for i in range(n)]

def NI_(n):
	return [int(input()) - 1 for i in range(n)]

def StoLI():
	return [ord(i) - 97 for i in input()]

def ItoS(n):
	return chr(n + 97)

def LtoS(ls):
	return ''.join([chr(i + 97) for i in ls])

def RA():
	return map(int, open(0).read().split())

def RLI(n=8, a=1, b=10):
	return [random.randint(a, b) for i in range(n)]

def RI(a=1, b=10):
	return random.randint(a, b)

def Rtest(T):
	(case, err) = (0, 0)
	for i in range(T):
		inp = INP()
		(a1, ls) = naive(*inp)
		a2 = solve(*inp)
		if a1 != a2:
			print((a1, a2), inp)
			err += 1
		case += 1
	print('Tested', case, 'case with', err, 'errors')

def GI(V, E, ls=None, Directed=False, index=1):
	org_inp = []
	g = [[] for i in range(V)]
	FromStdin = True if ls == None else False
	for i in range(E):
		if FromStdin:
			inp = LI()
			org_inp.append(inp)
		else:
			inp = ls[i]
		if len(inp) == 2:
			(a, b) = inp
			c = 1
		else:
			(a, b, c) = inp
		if index == 1:
			a -= 1
			b -= 1
		aa = (a, c)
		bb = (b, c)
		g[a].append(bb)
		if not Directed:
			g[b].append(aa)
	return (g, org_inp)

def GGI(h, w, search=None, replacement_of_found='.', mp_def={'#': 1, '.': 0}, boundary=1):
	mp = [boundary] * (w + 2)
	found = {}
	for i in R(h):
		s = input()
		for char in search:
			if char in s:
				found[char] = (i + 1) * (w + 2) + s.index(char) + 1
				mp_def[char] = mp_def[replacement_of_found]
		mp += [boundary] + [mp_def[j] for j in s] + [boundary]
	mp += [boundary] * (w + 2)
	return (h + 2, w + 2, mp, found)

def TI(n):
	return GI(n, n - 1)

def accum(ls):
	rt = [0]
	for i in ls:
		rt += [rt[-1] + i]
	return rt

def bit_combination(n, base=2):
	rt = []
	for tb in R(base ** n):
		s = [tb // base ** bt % base for bt in R(n)]
		rt += [s]
	return rt

def gcd(x, y):
	if y == 0:
		return x
	if x % y == 0:
		return y
	while x % y != 0:
		(x, y) = (y, x % y)
	return y

def YN(x):
	print(['NO', 'YES'][x])

def Yn(x):
	print(['No', 'Yes'][x])

def show(*inp, end='\n'):
	if show_flg:
		print(*inp, end=end)
mo = 10 ** 9 + 7
inf = float('inf')
FourNb = [(-1, 0), (1, 0), (0, 1), (0, -1)]
EightNb = [(-1, 0), (1, 0), (0, 1), (0, -1), (1, 1), (-1, -1), (1, -1), (-1, 1)]
compas = dict(zip('WENS', FourNb))
cursol = dict(zip('LRUD', FourNb))
l_alp = string.ascii_lowercase
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
input = lambda : sys.stdin.readline().rstrip()
show_flg = False
show_flg = True
ans = 0
for _ in range(I()):
	n = I()
	a = [1 if i == 'R' else 0 for i in input()]
	x = a.count(1)
	ans = 0
	if a[0] == 0:
		for i in range(n):
			if a[i] == 1:
				a = a[i:] + a[:i]
				break
	else:
		for i in range(n)[::-1]:
			if a[i] == 0:
				a = a[i + 1:] + a[:i + 1]
				break
	if x == n:
		a[-1] = 0
		ans = 1
	elif x == 0:
		a[0] = 1
		ans = 1
	for (k, d) in groupby(a):
		nd = len(list(d))
		ans += nd // 3
	print(ans)


for _ in range(int(input())):
	n = int(input())
	s = input()
	arr = []
	if s[0] == s[1] and s[1] == s[-1]:
		arr.append(1)
	else:
		arr.append(0)
	for i in range(1, n - 1):
		if s[i - 1] == s[i] and s[i] == s[i + 1]:
			arr.append(1)
		else:
			arr.append(0)
	if s[n - 2] == s[n - 1] and s[n - 1] == s[0]:
		arr.append(1)
	else:
		arr.append(0)
	special = False
	if arr[0] == 1 and arr[-1] == 1:
		special = True
	new = []
	sum = 0
	for i in arr:
		if i == 1:
			sum += 1
		elif sum != 0:
			new.append(sum)
			sum = 0
	if sum != 0:
		new.append(sum)
		sum = 0
	if special == True and len(new) != 1:
		new[0] += new.pop()
	ans = 0
	for i in new:
		if i % 3 == 0:
			ans += i // 3
		else:
			ans += i // 3 + 1
	print(ans)


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
from collections import deque
t = int(input())
for _ in range(t):
	n = int(input())
	string = input()
	L = string.count('L')
	R = n - L
	if not R or not L:
		print((n + 2) // 3)
		continue
	dq = deque([x for x in string])
	while dq[0] == dq[-1]:
		x = dq.popleft()
		dq.append(x)
	if dq[0] == 'L':
		dq.popleft()
		dq.pop()
	s = ''.join([x for x in dq])
	arr = s.split('RL')
	ans = 0
	for item in arr:
		if len(item) >= 2:
			l = item.count('L')
			r = item.count('R')
			ans += (l + 1) // 3
			ans += (r + 1) // 3
	print(ans)


from math import ceil
from math import floor
for case in range(int(input())):
	n = int(input())
	s = input()
	if all((s[i] == 'L' for i in range(n))) or all((s[i] == 'R' for i in range(n))):
		print(ceil(n / 3))
	else:
		index1 = -1
		index2 = 1
		first = s[0]
		chains = []
		while s[index1] == first:
			index1 -= 1
		index1 += 1
		while s[index2] == first:
			index2 += 1
		chains.append(floor((index1 * -1 + index2) / 3))
		buffer = s[index2]
		counts = 1
		for j in range(index2 + 1, n + index1):
			if s[j] == buffer:
				counts += 1
				if j == n + index1 - 1:
					chains.append(floor(counts / 3))
			else:
				chains.append(floor(counts / 3))
				buffer = s[j]
				counts = 1
		print(sum(chains))


import math
for _ in range(int(input())):
	n = int(input())
	s = str(input())
	count = []
	temp = 1
	for i in range(1, len(s)):
		if s[i] == s[i - 1]:
			temp += 1
		else:
			count.append(temp)
			temp = 1
	count.append(temp)
	if s[0] == s[-1] and len(count) != 1:
		count[0] = count[0] + count[-1]
		count.pop()
	ans = 0
	if count[0] == n:
		ans = int(math.ceil(n / 3))
	else:
		for i in count:
			ans += i // 3
	print(ans)


import math
for _ in range(int(input())):
	n = int(input())
	s = input()
	p = []
	for i in range(n):
		if i == 0:
			l = s[-1]
			r = s[i + 1]
		elif i == n - 1:
			l = s[i - 1]
			r = s[0]
		else:
			l = s[i - 1]
			r = s[i + 1]
		if l == 'L' and r == 'L':
			if s[i] == 'R':
				p.append(0)
			else:
				p.append(1)
		elif l == 'R' and r == 'R':
			if s[i] == 'L':
				p.append(0)
			else:
				p.append(1)
		else:
			p.append(0)
	k = []
	c = 0
	if p[0] == 1:
		c += 1
	for i in range(1, n):
		if p[i] == 1:
			c += 1
		else:
			if c != 0:
				k.append(c)
			c = 0
	if p[-1] == 1 and c != 0:
		if k and p[0] == 1:
			k[0] += c
		else:
			k.append(c)
	a = 0
	for i in k:
		a += math.ceil(i / 3)
	print(a)


from sys import stdin

def inp():
	return stdin.buffer.readline().rstrip().decode('utf8')

def itg():
	return int(stdin.buffer.readline())

def mpint():
	return map(int, stdin.buffer.readline().split())

def check_idx(x):
	if x == -1:
		return n - 1
	if x == n:
		return 0
	return x

def is_good(x):
	left = s[check_idx(x - 1)]
	right = s[check_idx(x + 1)]
	if s[x] == 'L':
		return left == 'R' or right == 'R'
	return left == 'L' or right == 'L'
from math import ceil
for case in range(1, itg() + 1):
	n = itg()
	s = list(inp())
	ans = 0
	is_bad = set((i for i in range(n) if not is_good(i)))
	if not is_bad:
		print(0)
		continue
	while is_bad:
		start = temp = is_bad.pop()
		count = 1
		while is_bad:
			temp = check_idx(temp + 1)
			if temp in is_bad:
				is_bad.remove(temp)
				count += 1
			else:
				break
		temp = start
		while is_bad:
			temp = check_idx(temp - 1)
			if temp in is_bad:
				is_bad.remove(temp)
				count += 1
			else:
				break
		count = ceil(count / 3)
		ans += count
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	arr = []
	cnt = 1
	for i in range(1, n):
		if s[i] == s[i - 1]:
			cnt += 1
		else:
			arr.append(cnt)
			cnt = 1
	arr.append(cnt)
	if s[0] == s[-1]:
		if len(arr) != 1:
			x = arr[0] + arr[-1]
			arr.pop(0)
			arr.pop()
			arr.append(x)
	ans = 0
	if len(arr) == 1:
		ans = arr[0] // 3
		if arr[0] % 3 != 0:
			ans += 1
	else:
		for i in arr:
			ans += i // 3
	print(ans)


from sys import stdin, stdout
import math
from itertools import permutations, combinations
from collections import defaultdict
from bisect import bisect_left
from bisect import bisect_right

def L():
	return list(map(int, stdin.readline().split()))

def In():
	return map(int, stdin.readline().split())

def I():
	return int(stdin.readline())
P = 1000000007

def main():
	for t in range(I()):
		n = I()
		s = list(input())
		c = -1
		for i in range(n - 1):
			if s[i] != s[i + 1]:
				c = i
				break
		if c == -1:
			print(-(-n // 3))
			continue
		st = 1
		ans = 0
		for i in range(c, c + n):
			if s[i % n] == s[(i + 1) % n]:
				st += 1
			else:
				ans += st // 3
				st = 1
		ans += st // 3
		print(ans)
main()


for _ in range(int(input())):
	n = int(input())
	s = input()
	f = 0
	if s[0] == s[-1]:
		f = 1
	p = 0
	c = s[0]
	b = []
	x1 = 0
	ans = 0
	for i in range(n):
		if s[i] == c:
			p += 1
		else:
			b.append(p)
			p = 1
			c = s[i]
		if i == n - 1:
			b.append(p)
	if len(b) == 1:
		ans += b[0] // 3 + (b[0] % 3 != 0)
	else:
		if f:
			t = 0
			if len(b) > 1:
				t = b.pop()
			b[0] += t
		for i in range(len(b)):
			ans += b[i] // 3
	print(ans)


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')

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(RL())

def N():
	return int(input())

def comb(n, m):
	return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0

def perm(n, m):
	return factorial(n) // factorial(n - m) if n >= m else 0

def mdis(x1, y1, x2, y2):
	return abs(x1 - x2) + abs(y1 - y2)

def toord(c):
	return ord(c) - ord('a')

def lcm(a, b):
	return a * b // gcd(a, b)
mod = 998244353
INF = float('inf')
from math import factorial, sqrt, ceil, floor, gcd
from collections import Counter, defaultdict, deque
from heapq import heapify, heappop, heappush
from itertools import groupby

def main():
	for _ in range(N()):
		n = N()
		s = input()
		arr = [len(list(i)) for (_, i) in groupby(s)]
		if len(arr) == 1:
			print((arr[0] + 2) // 3)
			continue
		res = 0
		if s[0] == s[-1]:
			arr[0] += arr[-1]
			arr.pop()
		for i in arr:
			if i > 2:
				res += i // 3
		print(res)
main()


for _ in range(int(input())):
	n = int(input())
	a = list(str(input()))
	ans = 0
	flag = 0
	c = 1
	for i in range(1, n):
		if a[i] != a[i - 1]:
			flag = 1
			break
	if i == n - 1 and flag == 0:
		if a[0] == 'R':
			a[0] = 'L'
		else:
			a[0] = 'R'
		ans = 1
	else:
		a = a[i:n] + a[:i]
	for i in range(1, n):
		if a[i] == a[i - 1]:
			c += 1
		else:
			ans += c // 3
			c = 1
	print(ans + c // 3)


T = int(input())
for t in range(T):
	n = int(input())
	s = list(input())
	all_same = True
	for i in range(1, len(s)):
		if s[i] != s[0]:
			all_same = False
			break
	if all_same:
		print((len(s) + 2) // 3)
		continue
	arr = []
	cnt = 1
	last = 0
	for i in range(1, len(s)):
		if s[last] == s[i]:
			cnt += 1
		else:
			arr.append(cnt)
			cnt = 1
			last = i
	if s[0] == s[n - 1]:
		arr[0] += cnt
	else:
		arr.append(cnt)
	ans = 0
	for a in arr:
		ans += a // 3
	print(ans)


import sys

def rs():
	return sys.stdin.readline().rstrip()

def ri():
	return int(sys.stdin.readline())

def ria():
	return list(map(int, sys.stdin.readline().split()))

def ws(s):
	sys.stdout.write(s)
	sys.stdout.write('\n')

def wi(n):
	sys.stdout.write(str(n))
	sys.stdout.write('\n')

def wia(a, sep=' '):
	sys.stdout.write(sep.join([str(x) for x in a]))
	sys.stdout.write('\n')
from collections import Counter

def solve(n, s):
	c = Counter(s)
	if len(c) == 1:
		return (n + 2) // 3
	if s[0] == s[-1]:
		cnt = 0
		while s[cnt] == s[-1]:
			s.append(s[cnt])
			cnt += 1
		s = s[cnt:]
	ans = 0
	i = 0
	while i < n:
		j = i + 1
		while j < n and s[j] == s[i]:
			j += 1
		ans += (j - i) // 3
		i = j
	return ans

def solve1(n, s):
	ans = 0
	for i in range(1, n):
		cnt = 0
		nx = (i + 1) % n
		if s[i - 1] == 1:
			cnt += 1
		if s[nx] == -1:
			cnt += 1
		if cnt == 1 and (not (s[i - 1] == 1 and s[i] == -1)) and (not (s[nx] == -1 and s[i] == 1)):
			ans += 1
			s[i] = -s[i]
	return ans

def ok(n, s):
	for i in range(n):
		if s[i] == s[i - 1] and s[i] == s[(i + 1) % n]:
			return False
	return True

def solve_naive(n, s):
	s = [si for si in s]
	res = n
	for mask in range(2 ** n):
		ss = s.copy()
		cnt = 0
		for bit in range(n):
			if mask & 1 << bit > 0:
				cnt += 1
				ss[bit] = -ss[bit]
		if ok(n, ss):
			res = min(res, cnt)
	return res

def gen():
	for n in range(3, 8):
		wi(n)
		for lls in range(n + 1):
			s = 'L' * lls + 'R' * (n - lls)
			s = [si for si in s]
			res1 = solve(n, s)
			res2 = solve_naive(n, s)
			if res1 != res2:
				wia(s)
				wia([res1, res2])
				solve_naive(n, s)
				return

def main():
	for _ in range(ri()):
		n = ri()
		s = rs()
		s = [si for si in s]
		wi(solve(n, s))
main()


import math

def BedWars(n, s):
	if 'L' not in s or 'R' not in s:
		if n < 3:
			return 0
		return math.ceil(n / 3)
	res = 0
	i = 0
	j = n - 1
	if s[0] == s[-1]:
		(left, right) = (None, None)
		while i < n and s[i] == s[0]:
			left = i
			i += 1
		while j >= 0 and s[j] == s[0]:
			right = j
			j -= 1
		temp = left + 1 + n - right
		res = res + temp // 3
	ch = None
	index = -1
	vac = True
	ptr = i
	while ptr <= j:
		if vac:
			ch = s[ptr]
			vac = False
			index = ptr
			ptr += 1
		elif s[ptr] == ch:
			ptr += 1
			if ptr > j:
				res += (ptr - index) // 3
				break
		else:
			temp = ptr - index
			res += temp // 3
			vac = True
	return res
t = int(input())
for y in range(t):
	n = int(input())
	s = input()
	print(BedWars(n, s))


import math
for _ in range(int(input())):
	am = int(input())
	arr = list(input())
	ok = [True] * am
	for i in range(am):
		c = 0
		if arr[(i - 1) % am] == 'R':
			c += 1
		if arr[(i + 1) % am] == 'L':
			c += 1
		if c == 1:
			if arr[(i - 1) % am] == 'R':
				if arr[i] == 'R':
					ok[i] = False
			elif arr[i] == 'L':
				ok[i] = False
	f = 0
	t = 0
	for i in range(am):
		if ok[i]:
			t += 1
		else:
			f += 1
	if f == 0:
		print(0)
		continue
	if f == am:
		print(math.ceil(am / 3))
		continue
	tot = 0
	fi = 0
	li = 0
	for i in range(am):
		if ok[i]:
			break
		else:
			fi += 1
	for i in range(am - 1, -1, -1):
		if ok[i]:
			break
		else:
			li += 1
	tNow = 0
	fl = False
	anss = []
	for i in range(fi, am - li):
		if fl:
			if ok[i]:
				anss.append(tNow)
				tNow = 0
				fl = False
			else:
				tNow += 1
		elif not ok[i]:
			tNow = 1
			fl = True
	anss.append(fi + li)
	for i in range(len(anss)):
		tot += math.ceil(anss[i] / 3)
	print(tot)


import time, math as mt, bisect, sys
from sys import stdin, stdout
from collections import deque
from fractions import Fraction
from collections import Counter
from collections import OrderedDict
pi = 3.141592653589793

def II():
	return int(stdin.readline())

def IO():
	return stdin.readline()

def IP():
	return map(int, stdin.readline().split())

def L():
	return list(map(int, stdin.readline().split()))

def P(x):
	return stdout.write(str(x) + '\n')

def PI(x, y):
	return stdout.write(str(x) + ' ' + str(y) + '\n')

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

def gcd(a, b):
	if a == 0:
		return b
	elif b == 0:
		return a
	if a > b:
		return gcd(a % b, b)
	else:
		return gcd(a, b % a)

def readTree():
	v = int(input())
	adj = [set() for i in range(v + 1)]
	for i in range(v - 1):
		(u1, u2) = In()
		adj[u1].add(u2)
		adj[u2].add(u1)
	return (adj, v)

def bfs(adj, v):
	visited = [False] * (v + 1)
	q = deque()
	while q:
		pass

def sieve():
	li = [True] * 1000001
	(li[0], li[1]) = (False, False)
	for i in range(2, len(li), 1):
		if li[i] == True:
			for j in range(i * i, len(li), i):
				li[j] = False
	prime = []
	for i in range(1000001):
		if li[i] == True:
			prime.append(i)
	return prime

def setBit(n):
	count = 0
	while n != 0:
		n = n & n - 1
		count += 1
	return count
mx = 10 ** 7
spf = [mx] * (mx + 1)

def SPF():
	spf[1] = 1
	for i in range(2, mx + 1):
		if spf[i] == mx:
			spf[i] = i
			for j in range(i * i, mx + 1, i):
				if i < spf[j]:
					spf[j] = i
	return
mod = 10 ** 9 + 7

def solve():
	n = II()
	s = input()
	d = {}
	li = []
	for ele in s:
		d[ele] = d.get(ele, 0) + 1
		if len(li) > 0 and li[-1][0] == ele:
			li[-1][1] = li[-1][1] + 1
		else:
			li.append([ele, 1])
	if len(d) == 1:
		print(mt.ceil(n / 3))
		return
	if li[0][0] == li[-1][0]:
		li[0][1] += li[-1][1]
		li.pop()
	ans = 0
	for i in range(len(li)):
		ans = ans + li[i][1] // 3
	P(ans)
	return
t = II()
for i in range(t):
	solve()


from math import ceil, floor

def solve():
	n = int(input())
	s = input()
	if len(set(s)) == 1:
		print(ceil(n / 3)) if n > 2 else print(0)
		return
	c = s[0]
	(idx, start) = (0, 0)
	for i in range(1, n):
		if s[i] != c:
			(idx, start) = (i, i)
			break
	lst = []
	while True:
		length = 1
		while s[(idx + 1) % n] == s[idx % n]:
			length += 1
			idx = (idx + 1) % n
		idx = (idx + 1) % n
		lst.append(length)
		if idx == start:
			break
	ans = 0
	for x in lst:
		ans += floor(x / 3)
	print(ans)
for _ in range(int(input())):
	solve()


from itertools import groupby
for _ in range(int(input())):
	n = int(input())
	s = input()
	ans = 0
	if len(set(list(s))) == 1:
		if n % 3 == 0:
			ans = n // 3
		else:
			ans = n // 3 + 1
	else:
		c = 0
		if s[0] == s[n - 1]:
			for i in range(n):
				if s[i] == s[n - 1]:
					c += 1
				else:
					break
			s = s[c:] + s[-n:c]
		for (num, grp) in groupby(s):
			l = len(''.join(grp))
			ans += l // 3
	print('%d\n' % ans)


import sys, io, os
BUFSIZE = 8192

class FastIO(io.IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = io.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(io.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')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
T = int(input())
INF = 10 ** 18
(L, LL, R, RR) = range(4)
(L_LL, LL_LL, R_LL, RR_LL, L_LR, LL_LR, R_LR, RR_LR) = range(8)
(L_RL, LL_RL, R_RL, RR_RL, L_RR, LL_RR, R_RR, RR_RR) = range(8, 16)
for _ in range(T):
	N = int(input())
	S = input()
	dp = [0] * 16
	if S[0] == 'L':
		for i in range(8, 16):
			dp[i] += 1
	else:
		for i in range(8):
			dp[i] += 1
	if S[1] == 'L':
		for i in [4, 5, 6, 7, 12, 13, 14, 15]:
			dp[i] += 1
	else:
		for i in [0, 1, 2, 3, 8, 9, 10, 11]:
			dp[i] += 1
	for i in [0, 2, 3, 4, 5, 7, 9, 10, 11, 12, 13, 14]:
		dp[i] = INF
	for c in S[2:]:
		dp_new = [0] * 16
		for g in range(0, 16, 4):
			if c == 'L':
				dp_new[g + L] = min(dp[g + R], dp[g + RR])
				dp_new[g + LL] = dp[g + L]
				dp_new[g + R] = min(dp[g + L], dp[g + LL]) + 1
				dp_new[g + RR] = dp[g + R] + 1
			else:
				dp_new[g + L] = min(dp[g + R], dp[g + RR]) + 1
				dp_new[g + LL] = dp[g + L] + 1
				dp_new[g + R] = min(dp[g + L], dp[g + LL])
				dp_new[g + RR] = dp[g + R]
		dp = dp_new
	ans = min((dp[i] for i in [R_LL, RR_LL, L_LR, R_LR, RR_LR, L_RL, LL_RL, R_RL, L_RR, LL_RR]))
	print(ans)


import os
import heapq
import sys, threading
import math
import bisect
import operator
from collections import defaultdict
sys.setrecursionlimit(10 ** 5)
from io import BytesIO, IOBase

def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)

def power(x, p, m):
	res = 1
	while p:
		if p & 1:
			res = res * x % m
		x = x * x % m
		p >>= 1
	return res

def inar():
	return [int(k) for k in input().split()]

def lcm(num1, num2):
	return num1 * num2 // gcd(num1, num2)

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		arr = input()
		lis = []
		take = 1
		for i in range(1, n):
			if arr[i] == arr[i - 1]:
				take += 1
			else:
				lis.append(take)
				take = 1
		lis.append(take)
		if len(lis) > 1:
			if arr[0] == arr[-1] and len(lis) > 1:
				el1 = lis[0]
				el2 = lis[-1]
				lis.pop(0)
				lis.pop(-1)
				lis.append(el1 + el2)
			ans = 0
			for i in range(len(lis)):
				ans += lis[i] // 3
			print(ans)
		else:
			ans = 0
			ans += lis[0] // 3
			if lis[0] % 3 > 0:
				ans += 1
			print(ans)
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')
main()


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


t = int(input())
for i in range(t):
	n = int(input())
	a = input()
	ans = 0
	f = False
	for j in range(n):
		if a[j - 1] != a[j]:
			f = True
			k = j
	if f:
		k = (k + 1) % n
		c = 1
		for j in range(k, n):
			if a[j] == a[j - 1]:
				c += 1
			else:
				ans += c // 3
				c = 1
		for j in range(k):
			if a[j] == a[j - 1]:
				c += 1
			else:
				ans += c // 3
				c = 1
	else:
		ans += (n + 2) // 3
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	s = list(s)
	i = 0
	count = 0
	p = []
	total = 0
	k = False
	while i < n:
		count = 1
		j = 0
		if s[i] == 'L':
			j = i + 1
			while j < n and s[j] == 'L':
				count += 1
				j += 1
			p.append([count, 'L'])
		else:
			j = i + 1
			while j < n and s[j] == 'R':
				count += 1
				j += 1
			p.append([count, 'R'])
		i = j
	m = len(p)
	if p[0][1] == p[m - 1][1] and len(p) > 1:
		p[0][0] = p[0][0] + p[m - 1][0]
		p.pop(m - 1)
	if len(p) > 1:
		for i in p:
			if i[0] // 3:
				total += i[0] // 3
	elif p[0][0] >= 3:
		if p[0][0] % 3:
			total += p[0][0] // 3 + 1
		else:
			total += p[0][0] // 3
	print(total)


from math import ceil
t = int(input())
for test in range(t):
	n = int(input())
	s = list(input())
	blocks = [[s[0], 1]]
	for i in range(1, n):
		if s[i] == blocks[-1][0]:
			blocks[-1][1] += 1
		else:
			blocks.append([s[i], 1])
	i = 0
	ans = 0
	if len(blocks) == 1:
		ans = ceil(blocks[0][1] / 3)
	else:
		while i < len(blocks):
			if i == 0 and blocks[i][0] == blocks[-1][0]:
				blocks[i][1] += blocks[-1][1]
				del blocks[-1]
			ans += blocks[i][1] // 3
			i += 1
	print(ans)


import sys
pprint = lambda s: print(' '.join(map(lambda x: str(x), s)))
input = lambda : sys.stdin.readline().strip()
ipnut = input
for _ in range(int(input())):
	n = int(input())
	a = list(input())
	ans = 0
	if len(set(a)) == 1:
		ans = 1
		s = a[0]
		if s == 'L':
			s = 'R'
		else:
			s = 'L'
		a[0] = s
	if a[0] == a[-1]:
		s = a[0]
		if s == 'L':
			s = 'R'
		else:
			s = 'L'
		ind = a.index(s)
		a = a[ind:] + a[:ind]
	aaa = [[a[0]]]
	for i in range(1, n):
		if aaa[-1][0] == a[i]:
			aaa[-1].append(a[i])
		else:
			aaa.append([a[i]])
	for i in aaa:
		ans += len(i) // 3
	print(ans)


from sys import stdin
input = stdin.readline

def A():
	t = int(input())
	for _ in range(t):
		n = int(input())
		a = list(map(int, input().split()))
		if len(set(a)) == 1:
			print(n)
		else:
			print(1)

def B():
	t = int(input())
	for _ in range(t):
		(n, k) = map(int, input().split())
		a = list(map(int, input().split()))
		d = max(a)
		for i in range(n):
			a[i] = d - a[i]
		if k % 2 == 1:
			print(*a)
		d = max(a)
		for i in range(n):
			a[i] = d - a[i]
		if k % 2 == 0:
			print(*a)

def C():
	t = int(input())
	for _ in range(t):
		n = int(input())
		a = list(map(int, input().split()))
		ops = 0
		for i in range(n - 1, 0, -1):
			if a[i] < a[i - 1]:
				ops += a[i - 1] - a[i]
		print(ops)

def D():
	from collections import deque
	t = int(input())
	for _ in range(t):
		n = int(input())
		s = [bool(ord(x) - ord('L')) for x in input().rstrip()]
		curr = s[0]
		d = deque()
		for i in range(1, n):
			if s[i] != curr:
				curr = s[i]
				d.append(i)
		tot = 0
		for i in range(1, len(d)):
			tot += (d[i] - d[i - 1]) // 3
		if len(d) == 0:
			tot = (n + 2) // 3
		elif s[-1] == s[0]:
			tot += (d[0] + (n - d[-1])) // 3
		else:
			tot += d[0] // 3
			tot += (n - d[-1]) // 3
		print(tot)
D()


for i1 in range(int(input())):
	n = int(input())
	s = input()
	s = s.replace('\r', '')
	c1 = 0
	c2 = 0
	ans = 0
	dict = {}
	for i in s:
		if i not in dict:
			dict[i] = 1
		else:
			dict[i] += 1
	if len(dict) == 1:
		if n / 3 == n // 3:
			print(n // 3)
		else:
			print(n // 3 + 1)
		continue
	temp = s[0]
	i = 0
	while True:
		if s[i] != temp:
			break
		i += 1
	if s[0] == s[-1]:
		t = s[i:]
		rev = s[:i]
		t = t + rev
		s = t
	if s[0] == 'R':
		flag = 1
	else:
		flag = 0
	for i in s:
		if i == 'R':
			if flag == 0:
				c1 = 0
			c1 += 1
			if c1 == 3 and flag == 1:
				ans += 1
				c1 = 0
			flag = 1
		else:
			if flag == 1:
				c2 = 0
			c2 += 1
			if c2 == 3 and flag == 0:
				ans += 1
				c2 = 0
			flag = 0
	print(ans)


a = int(input())
for i in range(a):
	s = int(input())
	t = input()
	z = []
	for j in range(len(t)):
		z.append(t[j])
	ans = []
	count = 1
	for i in range(1, len(z)):
		if z[i] == z[i - 1]:
			count += 1
		else:
			ans.append(count)
			count = 1
	ans.append(count)
	if len(ans) == 1 and ans[0] < 3:
		print(0)
		continue
	if len(ans) == 1:
		print((ans[0] + 2) // 3)
		continue
	if z[-1] == z[0]:
		ans[0] += ans[-1]
		ans.pop()
		total = 0
		for i in range(len(ans)):
			total += ans[i] // 3
		print(total)
		continue
	else:
		total = 0
		for i in range(len(ans)):
			total += ans[i] // 3
		print(total)
		continue


def min_Change(bed, str_len):
	current_cnt = [0]
	for i in range(str_len - 1):
		current_cnt[-1] += 1
		if bed[i] != bed[i + 1]:
			current_cnt.append(0)
	current_cnt[-1] += 1
	if len(current_cnt) == 1:
		return (str_len + 2) // 3
	else:
		if bed[0] == bed[-1]:
			current_cnt[0] += current_cnt.pop()
		return sum((n // 3 for n in current_cnt))
ans = list()
for _ in range(int(input())):
	str_len = int(input())
	bed = input()
	ans.append(min_Change(bed, str_len))
for a in ans:
	print(a, end='\n')


import math
import sys
input = sys.stdin.readline
t = int(input())
for i in range(t):
	n = int(input())
	s = [i for i in input() if i != '\n']
	if len(set(s)) == 1:
		print(math.ceil(n / 3))
	else:
		start = 0
		for i in range(n - 1, -1, -1):
			if s[i] != s[0]:
				start = (i + 1) % n
				break
		s = s[start:] + s[:start]
		(ans, count) = (0, 1)
		for i in range(1, n):
			if s[i] == s[i - 1]:
				count += 1
			else:
				count = 1
			if count == 3:
				count = 0
				ans += 1
		print(ans)


import os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
t = int(input())
LNum = 76
RNum = 82
for _ in range(t):
	n = int(input())
	s = input()
	segCount = []
	cnt = 0
	for i in range(1, n):
		if s[i] != s[i - 1]:
			segCount.append(i - cnt)
			cnt = i
	if not segCount:
		segCount.append(n - cnt)
	elif s[0] == s[n - 1]:
		segCount[0] += n - cnt
	else:
		segCount.append(n - cnt)
	ans = 0
	for elem in segCount:
		ans += elem // 3
	if len(segCount) >= 2:
		print(ans)
	else:
		print((segCount[0] + 2) // 3)


def ints():
	return map(int, input().split())
t = int(input())
for _ in range(t):
	n = int(input())
	graph = {i: 0 for i in range(n)}
	s = input()
	for i in range(n):
		if s[i] == 'L':
			graph[i] = (i - 1) % n
		else:
			graph[i] = (i + 1) % n
	vis = set()
	for i in range(n):
		if graph[graph[i]] == i:
			vis.add(i)
			vis.add(graph[i])
	ans = 0
	start = set(range(n))
	for i in range(n):
		if graph[i] in start:
			start.remove(graph[i])
	for v in start:
		curr = 1
		currv = v
		while graph[currv] not in vis:
			curr += 1
			currv = graph[currv]
		ans += (curr + 1) // 3
	if len(set(s)) == 1:
		print(1 + (n - 1) // 3)
	else:
		print(ans)


for _ in range(int(input())):
	n = int(input())
	st = input()
	if st.count('L') == n or st.count('R') == n:
		print(1 + (n - 1) // 3)
	else:
		l = n
		count = 0
		while st[n - 1] == st[0]:
			n -= 1
			count += 1
		st = st[0] * count + st[:n]
		ans = 0
		c = 1
		for i in range(1, l):
			if st[i] == st[i - 1]:
				c += 1
			else:
				ans += c // 3
				c = 1
		ans += c // 3
		print(ans)


import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
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')
ALPHA = 'abcdefghijklmnopqrstuvwxyz'
M = 10 ** 9 + 7
EPS = 1e-06

def value():
	return tuple(map(int, input().split()))

def array():
	return [int(i) for i in input().split()]

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]
for _ in range(Int()):
	n = Int()
	s = input()
	n = len(s)
	if len(set(s)) == 1:
		ans = 1 + (n - 1) // 3
		print(ans)
		continue
	l = 1
	c = 1
	while l < n and s[l] == s[l - 1]:
		l += 1
		c += 1
	r = n - 1
	ans = 0
	if s[-1] == s[0]:
		c += 1
		r -= 1
		while r >= 0 and s[r] == s[r + 1]:
			c += 1
			r -= 1
	ans += c // 3
	r += 1
	while l < r:
		c = 1
		l += 1
		while l < r and s[l] == s[l - 1]:
			c += 1
			l += 1
		ans += c // 3
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(input())
	l = 0
	r = 0
	t = 0
	j = 0
	if a[0] == 'L' and a[-1] == 'L' and ('R' in a):
		j = a.index('R')
	elif a[0] == 'R' and a[-1] == 'R' and ('L' in a):
		j = a.index('L')
	for i in range(j, n):
		if a[i] == 'L':
			t += r // 3
			l += 1
			r = 0
		else:
			t += l // 3
			r += 1
			l = 0
	if l == n or r == n:
		if n % 3 == 0:
			t += n // 3
		else:
			t += n // 3 + 1
	elif l > 0:
		t += (j + l) // 3
	else:
		t += (j + r) // 3
	print(t)


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')
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 = ' '
		for i in s:
			if i == prev:
				cnt += 1
			else:
				ans += cnt // 3
				cnt = 1
				prev = i
		ans += cnt // 3
		print(ans)


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


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from itertools import *
import bisect
from heapq import *
from math import *
from copy import *
from collections import deque
from collections import Counter as counter
from itertools import combinations as comb
from itertools import permutations as permutate
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
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')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func
mod = 1000000007

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

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

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def solve():
	t = int(inp())
	for _ in range(t):
		n = int(inp())
		s = inp()
		curr = s[0]
		curr_l = 1
		ar = []
		for i in range(1, n):
			if s[i] == curr:
				curr_l += 1
			else:
				ar.append(curr_l)
				curr = s[i]
				curr_l = 1
		if curr == s[0] and ar:
			ar[0] += curr_l
		else:
			ar.append(curr_l)
		if len(ar) == 1:
			print((n + 2) // 3)
		else:
			ans = 0
			for i in ar:
				ans += i // 3
			print(ans)
solve()


a = int(input())
import math

def nott(a):
	if a == 1:
		return 0
	else:
		return 1
for i in range(0, a):
	b = int(input())
	s = input()
	arr = []
	for i in s:
		if i == 'L':
			arr.append(1)
		else:
			arr.append(0)
	brr = []
	piv = arr[0]
	count = 0
	for i in range(0, len(arr)):
		if arr[i] == piv:
			count = count + 1
		else:
			piv = nott(piv)
			brr.append(count)
			count = 1
	brr.append(count)
	if piv == arr[0]:
		c = brr[0]
		brr[0] = 0
		brr[len(brr) - 1] = brr[len(brr) - 1] + c
	ans = 0
	for i in brr:
		ans = ans + i // 3
	if len(brr) == 1:
		print(math.ceil(brr[0] / 3))
	else:
		print(ans)


(z, zz) = (input, lambda : list(map(int, z().split())))
(szz, dgraphs, mod) = (lambda : sorted(zz()), {}, 10 ** 9 + 7)
from string import *
from collections import *
from queue import *
from sys import *
from collections import *
from math import *
from heapq import *
from itertools import *
from bisect import *
from collections import Counter as cc
from math import factorial as f
from bisect import bisect as bs
from bisect import bisect_left as bsl
from itertools import accumulate as ac

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)

def prime(x):
	p = ceil(x ** 0.5) + 1
	for i in range(2, p):
		if x % i == 0 and x != 2:
			return 0
	return 1

def dfs(u, visit, graph):
	visit[u] = True
	for i in graph[u]:
		if not visit[i]:
			dfs(i, visit, graph)
num = int(z())
for i in range(num):
	n = int(z())
	l = z()
	find = -1
	for i in range(n - 1):
		if l[i] != l[i + 1]:
			find = i + 1
			break
	if find == -1:
		print(ceil(n / 3))
		continue
	cnt = 1
	res = 0
	for i in range(find, find + n):
		if l[i % n] == l[(i + 1) % n]:
			cnt += 1
		else:
			res += cnt // 3
			cnt = 1
	print(res)


import sys
T = int(sys.stdin.readline().strip())
for t in range(0, T):
	n = int(sys.stdin.readline().strip())
	s = sys.stdin.readline().strip()
	ans = 0
	c = 1
	L = []
	for i in range(1, n):
		if s[i] == s[i - 1]:
			c = c + 1
		else:
			L.append(c)
			c = 1
	if len(L) == 0:
		if c <= 2:
			print(0)
		else:
			print((c + 2) // 3)
	else:
		if len(L) % 2 == 0:
			L[0] = L[0] + c
		else:
			L.append(c)
		for l in L:
			ans = ans + l // 3
		print(ans)


for t in range(int(input())):
	n = int(input())
	s = input()
	if len(list(set(list(s)))) == 1:
		print((n + 2) // 3)
	else:
		a = list(s)
		c = 0
		ans = []
		temp = []
		i = 1
		temp.append(a[0])
		while i < len(a):
			if a[i] == a[i - 1]:
				temp.append(a[i])
			else:
				if a[i] == 'L':
					ans.append(temp)
				else:
					ans.append(temp)
				temp = []
				temp.append(a[i])
			i += 1
		for i in range(len(ans)):
			if len(ans[i]) >= 3:
				c += len(ans[i]) // 3
		if len(temp) >= 3:
			c += len(temp) // 3
		if set(ans[0]) == set(temp):
			if len(temp) > 1:
				if len(ans[0]) % 3 != 0 and len(temp) % 3 != 0:
					if len(ans[0]) < 3 or len(temp) < 3:
						c += 1
						if len(ans[0]) == 1 and len(temp) > 3 and (len(temp) % 3 != 2):
							c -= 1
					elif len(ans[0]) > 3 and len(temp) > 3:
						arr = []
						x = len(ans[0]) % 3
						arr.append(x)
						x = len(temp) % 3
						arr.append(x)
						arr.sort()
						if arr == [1, 2] or arr == [2, 2]:
							c += 1
			elif len(ans[0]) >= 2 and len(ans[0]) % 3 == 2:
				c += 1
		print(c)


import math
t = int(input())
for i in range(t):
	n = int(input())
	s = list(input())
	if s == [s[0]] * n:
		print(math.ceil(n / 3))
	else:
		series = [1]
		for j in range(1, n):
			if s[j] == s[j - 1]:
				series[-1] += 1
			else:
				series.append(1)
		if s[0] == s[-1]:
			series[0] += series[-1]
			series.pop()
		ans = 0
		for j in series:
			ans += math.floor(j / 3)
		print(ans)


import sys
import math
import collections
import operator as op
from collections import deque
from math import gcd, inf, sqrt, pi, cos, sin, ceil, log2
from bisect import bisect_right, bisect_left
from functools import reduce
from sys import stdin, stdout, setrecursionlimit
setrecursionlimit(2 ** 20)

def factorial(n):
	if n == 0:
		return 1
	return n * factorial(n - 1)

def ncr(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer // denom

def prime_factors(n):
	i = 2
	factors = []
	while i * i <= n:
		if n % i:
			i += 1
		else:
			n //= i
			factors.append(i)
	if n > 1:
		factors.append(n)
	return list(factors)

def sumDigits(no):
	return 0 if no == 0 else int(no % 10) + sumDigits(int(no / 10))
MOD = 1000000007
PMOD = 998244353
N = 10 ** 5
T = 1
T = int(stdin.readline())
for _ in range(T):
	n = int(stdin.readline())
	s = list(stdin.readline().strip('\n'))
	pref = s[0]
	arr = []
	d = {}
	(cnt, res) = (0, 0)
	for i in range(n):
		d[s[i]] = 1
		if s[i] == pref:
			cnt += 1
		else:
			arr.append(cnt)
			cnt = 1
		pref = s[i]
	arr.append(cnt)
	if s[0] != s[-1]:
		arr_n = len(arr)
	else:
		arr_n = len(arr) - 1
		arr[0] += arr[-1]
	for i in range(arr_n):
		res += arr[i] // 3
	if len(d) == 1:
		print(1 + (n - 1) // 3)
	else:
		print(res)


T = int(input())
for case in range(T):
	i = int(input())
	s = input()
	if len(set(list(s))) == 1:
		if len(s) == 1 or len(s) == 2:
			print(1)
		else:
			print((len(s) + 2) // 3)
		continue
	else:
		while s[0] == s[-1]:
			s = s[1:] + s[0]
	ls = [len(x) for x in s.split('R') if len(x) != 0]
	rs = [len(x) for x in s.split('L') if len(x) != 0]
	c = 0
	for e in ls:
		c += e // 3
	for e in rs:
		c += e // 3
	print(c)


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


def answer(n, A):
	ans = 0
	count = 0
	index = -1
	flag = 0
	for i in range(n - 1, -1, -1):
		if A[i] == A[0]:
			flag = 1
			count += 1
		else:
			index = i
			break
	if index == -1:
		if n > 2:
			ans = n // 3
			if n % 3 != 0:
				ans += 1
	else:
		count += 1
		for i in range(1, index + 1):
			if A[i] == A[i - 1]:
				count += 1
			else:
				ans += count // 3
				count = 1
		ans += count // 3
	return ans
t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	print(answer(n, s))


import math as mt

def tran(s, n):
	an = 0
	cou = []
	c = 1
	for i in range(n - 1):
		if s[i + 1] == s[i]:
			c += 1
		else:
			cou.append(c)
			c = 1
	cou.append(c)
	if s[-1] == s[0] and len(cou) > 1:
		p = cou.pop(-1)
		cou[0] += p
	for i in range(len(cou)):
		if cou[i] == 1 or cou[i] == 2:
			pass
		else:
			an = an + mt.ceil((cou[i] - 2) / 3)
	if len(cou) == 1:
		return 1 + mt.ceil((cou[0] - 3) / 3)
	else:
		return an
t = int(input())
ans = []
for i in range(t):
	n = int(input())
	s = input()
	r = tran(s, n)
	ans.append(r)
for i in ans:
	print(i)


import sys
readline = sys.stdin.readline
T = int(readline())
Ans = [None] * T
INF = 10 ** 9 + 7
for qu in range(T):
	N = int(readline())
	A = [1 if s == 'R' else 0 for s in readline().strip()]
	ans = INF
	for _ in range(4):
		dp = [0, INF, INF, INF, INF]
		for a in A:
			dp2 = [INF] * 5
			if a == 0:
				dp2[0] = min(dp[1], dp[2], dp[3], dp[4])
				dp2[1] = dp[0] + 1
				dp2[2] = dp[1]
				dp2[3] = dp[1] + 1
				dp2[4] = dp[3]
			else:
				dp2[0] = min(dp[1], dp[2], dp[3], dp[4]) + 1
				dp2[1] = dp[0]
				dp2[2] = dp[1] + 1
				dp2[3] = dp[1]
				dp2[4] = dp[3] + 1
			dp = dp2[:]
		ans = min(ans, dp[0])
		A = [A[-1]] + A[:-1]
	Ans[qu] = ans
print('\n'.join(map(str, Ans)))


import math
for _ in range(int(input())):
	n = int(input())
	S = input()
	s = None
	for i in range(n):
		if S[i] != S[i - 1]:
			s = i
			break
	ans = 0
	if s == None:
		ans = math.ceil(n / 3)
	else:
		cur = s
		pre = S[cur]
		cnt = 1
		while (cur + 1) % n != s:
			cur = (cur + 1) % n
			if S[cur] == pre:
				cnt += 1
			else:
				ans += 0 if cnt < 3 else int(cnt / 3)
				cnt = 1
			pre = S[cur]
		ans += int(cnt / 3)
	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]:
					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)
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')
main()


def main():
	for _ in range(int(input())):
		n = int(input())
		a = list(map(str, input()))
		if len(set(a)) == 1:
			if n % 3 == 0:
				print(n // 3)
			else:
				print(n // 3 + 1)
		else:
			while True:
				if a[0] == a[n - 1]:
					a.append(a[0])
					a.remove(a[0])
				else:
					break
			s = 0
			c = 1
			for i in range(1, n):
				if a[i] == a[i - 1]:
					if i == n - 1:
						c = c + 1
						s = s + c // 3
						break
					else:
						c = c + 1
				else:
					s = s + c // 3
					c = 1
			print(s)
main()


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

def main():
	t = int(input())
	for case in range(t):
		n = int(input())
		s = input()
		l = s
		if len(set(s)) == 1:
			if n % 3 == 0:
				print(n // 3)
			else:
				print(n // 3 + 1)
			continue
		vis = [0 for i in range(n)]
		ans = 0
		for i in range(n):
			if vis[i] == 1:
				continue
			vis[i] = 1
			ct = 1
			j = i + 1
			while True:
				j %= n
				if l[j] == l[i] and vis[j] == 0:
					ct += 1
					vis[j] = 1
				else:
					break
				j += 1
			j = i - 1
			while True:
				j = (j + n) % n
				if l[j] == l[i] and vis[j] == 0:
					ct += 1
					vis[j] = 1
				else:
					break
				j -= 1
			ans += ct // 3
		print(ans)
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')
main()


import math
t = int(input())
for i in range(t):
	n = int(input())
	a = list(input().strip())
	l = 0
	r = 0
	c = []
	for j in range(n):
		if a[j] == 'R':
			r = r + 1
			if l >= 1:
				c.append(l)
				l = 0
		else:
			l = l + 1
			if r >= 1:
				c.append(r)
				r = 0
	if l > 0:
		c.append(l)
	elif r > 0:
		c.append(r)
	if a[0] == a[-1] and len(c) > 1:
		c[0] = c[0] + c[-1]
		del c[-1]
	if len(c) == 1:
		if c[0] >= 3:
			print(math.ceil(c[0] / 3))
		else:
			print(0)
	else:
		an = 0
		for v in c:
			an = an + v // 3
		print(an)


from sys import stdin
from collections import deque
mod = 10 ** 9 + 7
import sys
import random
sys.setrecursionlimit(10 ** 6)
from queue import PriorityQueue
from collections import Counter as cc
from bisect import bisect_right
from bisect import bisect_left
from collections import defaultdict
from math import sqrt, factorial, gcd, log2, inf, ceil
import heapq
input = lambda : sys.stdin.readline().rstrip()
from sys import stdin, stdout
from heapq import heapify, heappush, heappop
from itertools import permutations
from math import factorial as f

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p
import sys

def bfs(n):
	que = [n]
	boo[n] = True
	ans = 0
	while que:
		z = que.pop(0)
		ans += 1
		for i in hash[z]:
			if not boo[i]:
				boo[i] = True
				que.append(i)
	return ans
t = int(input())
ha = []
for _ in range(t):
	n = int(input())
	l = list(input())
	ans = 0
	if len(set(l)) == 1:
		print(ceil(n / 3))
		continue
	hash = defaultdict(list)
	ha = defaultdict(bool)
	for i in range(n):
		if l[i] == l[(i + 1) % n]:
			hash[i].append((i + 1) % n)
			ha[(i + 1) % n] = True
	boo = defaultdict(bool)
	for i in range(n):
		if not boo[i] and (not ha[i]):
			z = bfs(i)
			ans += z // 3
	print(ans)


import math

def main():
	for case in range(int(input())):
		n = int(input())
		s = input()
		if s.count('L') == n or s.count('R') == n:
			print(math.ceil(n / 3))
			continue
		if s[0] == s[-1]:
			j = 1
			while s[j] == s[0]:
				j += 1
			t = s[j:] + s[:j]
			s = t
		ans = 0
		temp = []
		i = 0
		while i < n:
			cur = s[i]
			l = 1
			while i + 1 < n and s[i + 1] == cur:
				l += 1
				i += 1
			temp.append(l)
			i += 1
		for x in temp:
			ans += x // 3
		print(ans)
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

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

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			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')
main()


t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	c = 0
	a = s[0]
	if s[0] != s[n - 1]:
		d = 1
		for i in s[1:]:
			if i == a:
				d = d + 1
			else:
				c = c + d // 3
				d = 1
			a = i
		c = c + d // 3
	else:
		b = 0
		e = d = 1
		for i in s[1:]:
			if i == a:
				if b == 0:
					e = e + 1
				else:
					d = d + 1
			else:
				if b == 0:
					b = 1
				c = c + d // 3
				d = 1
			a = i
		if b == 0:
			d = d + 1
		e = e + d
		c = c + e // 3
	print(c)


from collections import defaultdict
for t in range(int(input())):
	n = int(input())
	s = [str(i) for i in input()]
	p = [''] * n
	d = defaultdict(list)
	if s == ['R'] * n or s == ['L'] * n:
		total = n // 3
		if n % 3 != 0:
			total += 1
		print(total)
	else:
		for i in range(n):
			if i < n - 1:
				if s[i] == 'R' and s[i + 1] == 'L':
					s[i] = '*'
					s[i + 1] = '*'
					div = i
			elif s[i] == 'R' and s[0] == 'L':
				s[i] = '*'
				s[0] = '*'
				div = i
		for i in range(n):
			p[i] = s[(div + i) % n]
		p += ['0']
		i = 0
		while i < n:
			base = i
			while p[i] == p[i + 1]:
				i += 1
			d[p[i]].append([base, i])
			i += 1
			if i == n:
				break
		l_num = 0
		r_num = 0
		if 'L' in d:
			for elem in d['L']:
				l_num += (elem[1] - elem[0] + 1) // 3 + max((elem[1] - elem[0] + 1) % 3 - 1, 0)
		if 'R' in d:
			for elem in d['R']:
				r_num += (elem[1] - elem[0] + 1) // 3 + max((elem[1] - elem[0] + 1) % 3 - 1, 0)
		ans = l_num + r_num
		print(ans)


for _ in range(int(input())):
	n = int(input())
	t = input()
	i = 0
	if len(set(t)) > 1:
		while t[-i - 1] == t[0]:
			i += 1
		if i:
			t = t[0] * i + t[:-i]
		i = 0
		ops = 0
		while i < len(t):
			j = 1
			while i + j < len(t) and t[i + j] == t[i]:
				j += 1
			ops += j // 3
			i += j
	else:
		ops = (len(t) + 2) // 3
	print(ops)


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
	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)


def find_cost_of_reordering(directions):
	directions_n = len(directions)
	if 'RRR' not in directions * 2:
		directions = ''.join(('R' if direction == 'L' else 'L' for direction in directions))
	directions_2x = directions * 2
	if 'RRR' not in directions_2x:
		result = 0
	elif directions_n == 3:
		result = 1
	else:
		start_index = directions_2x.find('RRR') % len(directions)
		result = min((_find_answer_with_group_start(directions, index % len(directions)) for index in (start_index, start_index + 1, start_index + 2)))
		pre_start_index = (start_index - 1) % len(directions)
		if directions[pre_start_index] == 'R':
			result = min(result, _find_answer_with_group_start(directions, pre_start_index))
	return result

def _find_answer_with_group_start(directions, border_index):
	directions = directions[border_index:] + directions[:border_index]
	directions_n = len(directions)
	min_costs = [float('inf')] * directions_n
	min_costs[0] = 0
	final_min_cost = float('inf')
	for left_group_border in range(directions_n):
		for piece_length in (2, 3, 4):
			if left_group_border + piece_length > directions_n:
				continue
			next_group_start = (left_group_border + piece_length) % directions_n
			right_group_border = (next_group_start - 1) % directions_n
			possible_new_cost = _find_cost_changing_from(directions, left_group_border, right_group_border) + min_costs[left_group_border]
			if next_group_start == 0:
				final_min_cost = min(final_min_cost, possible_new_cost)
				continue
			min_costs[next_group_start] = min(min_costs[next_group_start], possible_new_cost)
	return final_min_cost

def _find_cost_changing_from(directions, group_first, group_last):
	curr_piece = directions[group_first:group_last + 1]
	piece_length = len(curr_piece)
	if piece_length == 2:
		(p1, p2) = curr_piece
		cost = int(p1 != 'R') + int(p2 != 'L')
	elif piece_length == 3:
		(p1, p2, p3) = curr_piece
		cost_1 = int(p1 != 'R') + int(p2 != 'R') + int(p3 != 'L')
		cost_2 = int(p1 != 'R') + int(p2 != 'L') + int(p3 != 'L')
		cost = min(cost_1, cost_2)
	else:
		assert piece_length == 4
		(p1, p2, p3, p4) = curr_piece
		cost = int(p1 != 'R') + int(p2 != 'R') + int(p3 != 'L') + int(p4 != 'L')
	return cost

def main():
	t = int(input())
	for i in range(t):
		n = int(input())
		directions = input()
		result = find_cost_of_reordering(directions)
		print(result)
main()


from itertools import *
for s in [*open(0)][2::2]:
	a = [len([*g]) for (k, g) in groupby(s[:-1])]
	if len(a) % 2:
		a[0] += a.pop() if a[1:] else 2
	print(sum((x // 3 for x in a)))


import sys

def minp():
	return sys.stdin.readline().strip()

def mint():
	return int(minp())

def mints():
	return map(int, minp().split())

def check(a, b, c):
	if int(a == 1) + int(c == 0) == 1:
		if a == 1:
			return b == 0
		if c == 0:
			return b == 1
	return True
good = [False] * 8
for a in range(2):
	for b in range(2):
		for c in range(2):
			good[a * 4 + b * 2 + c] = check(a, b, c)

def solve():
	n = mint()
	s = [1 if i == 'R' else 0 for i in minp()]
	s.append(s[0])
	s.append(s[1])
	INF = int(1000000000.0)
	dp = [[INF] * (n + 3) for i in range(4)]
	res = INF
	for a in range(2):
		for b in range(2):
			for z in range(4):
				dp[z][2] = INF
			dp[a * 2 + b][2] = 0
			for i in range(2, n + 2):
				for aa in range(2):
					for bb in range(2):
						d = INF
						for cc in range(2):
							dd = dp[cc * 2 + aa][i]
							if good[cc * 4 + aa * 2 + bb]:
								d = min(d, dd)
						dp[aa * 2 + bb][i + 1] = d + (s[i] ^ bb)
			res = min(res, dp[a * 2 + b][n + 2])
	print(res)
for i in range(mint()):
	solve()


t = int(input())
for i in range(t):
	n = int(input())
	string = input()
	j = 0
	ans = 0
	while j < n - 1 and string[j] == string[j + 1]:
		j += 1
	if j != n - 1:
		string1 = string[0:j + 1]
		string2 = string[j + 1:n]
		string = string2 + string1
		j = 0
		while j < n - 1:
			s = 0
			while j < n - 1 and string[j] == string[j + 1]:
				j += 1
				s += 1
			ans += (s + 1) // 3
			j += 1
	else:
		ans = (n + 2) // 3
	print(ans)


for _ in range(int(input())):
	n = int(input())
	s = input()
	count = 0
	i = n - 1
	if s[0] == s[-1]:
		while i > -1 and s[0] == s[i]:
			count += 1
			i -= 1
	if i == -1:
		if n % 3 == 0:
			print(n // 3)
		else:
			print(n // 3 + 1)
		continue
	end = i
	i = 0
	arr = []
	count += 1
	while i < end:
		if s[i] == s[i + 1]:
			count += 1
		else:
			arr.append(count)
			count = 1
		i += 1
	arr.append(count)
	ans = 0
	for i in arr:
		ans += i // 3
	print(ans)


for _ in range(int(input())):
	n = int(input())
	s = input()
	t1 = t2 = 0
	curr = s[0]
	for i in s:
		if i == curr:
			t1 += 1
		else:
			break
	curr = s[-1]
	for i in s[::-1]:
		if i == curr:
			t2 += 1
		else:
			break
	if t1 == n:
		print((n + 2) // 3)
		continue
	s = s + 'E'
	temp = ans = out = 0
	curr = s[0]
	for i in range(n):
		if curr == s[i]:
			temp += 1
		if s[i + 1] != curr:
			ans += temp // 3
			curr = s[i + 1]
			temp = 0
	if s[0] == s[-2]:
		out = (t1 + t2) // 3 - t1 // 3 - t2 // 3
	print(ans + out)


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]))


import sys
import random
from fractions import Fraction
from math import *

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def finput():
	return float(input())

def tinput():
	return input().split()

def linput():
	return list(input())

def rinput():
	return map(int, tinput())

def fiinput():
	return map(float, tinput())

def flinput():
	return list(fiinput())

def rlinput():
	return list(map(int, input().split()))

def trinput():
	return tuple(rinput())

def srlinput():
	return sorted(list(map(int, input().split())))

def NOYES(fl):
	if fl:
		print('NO')
	else:
		print('YES')

def YESNO(fl):
	if fl:
		print('YES')
	else:
		print('NO')

def main():
	n = iinput()
	q = linput()
	res = len(set(q)) == 1
	if res:
		q[0] = ['L', 'R'][(['L', 'R'].index(q[0]) + 1) % 2]
	if q[0] == q[-1]:
		e = q.index(['L', 'R'][(['L', 'R'].index(q[0]) + 1) % 2])
		q = q[e:] + q[:e]
	w = [[q[0]]]
	for i in range(1, n):
		if w[-1][0] == q[i]:
			w[-1].append(q[i])
		else:
			w.append([q[i]])
	print(res + sum([len(i) // 3 for i in w]))
for TESTING in range(iinput()):
	main()


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)


for _ in range(int(input())):
	n = int(input())
	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)
	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
	print(count)


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')

class union_find:

	def __init__(self, n):
		self.n = n
		self.rank = [0] * n
		self.parent = [int(j) for j in range(n)]

	def union(self, i, j):
		i = self.find(i)
		j = self.find(j)
		if self.rank[i] == self.rank[j]:
			self.parent[i] = j
			self.rank[j] += 1
		elif self.rank[i] > self.rank[j]:
			self.parent[j] = i
		else:
			self.parent[i] = j

	def find(self, i):
		temp = i
		if self.parent[temp] != temp:
			self.parent[temp] = self.find(self.parent[temp])
		return self.parent[temp]
from math import log2, ceil
from collections import deque, Counter as CC, defaultdict as dd
MOD = int(1000000000.0 + 7)
from copy import deepcopy

def main():
	for t in range(int(input())):
		n = int(input())
		s = input()
		ans = 0
		if s[0] == s[-1]:
			for i in range(n):
				if s[i] == s[0]:
					pass
				else:
					s = s[i:] + s[:i]
					break
			else:
				ans = (len(s) + 2) // 3
		if ans == 0:
			tmp = 0
			ans = 0
			last = -1
			for i in range(len(s)):
				if s[i] == last:
					tmp += 1
				else:
					last = s[i]
					ans += tmp // 3
					tmp = 1
			ans += tmp // 3
		print(ans)
main()


import math
n = int(input())
for t in range(n):
	g = int(input())
	l = list(input())
	c = 0
	for i in range(g - 1):
		if l[i] == l[i + 1]:
			c = c + 1
	if c == g - 1:
		print(math.ceil(g / 3))
	else:
		k = []
		d = 1
		l.append('#')
		for i in range(g):
			if l[i] == l[i + 1]:
				d = d + 1
			else:
				k.append(d)
				d = 1
		if l[0] == l[-2]:
			k[0] = k[-1] + k[0]
			k[-1] = 0
		e = 0
		for i in range(len(k)):
			e = e + int(k[i] / 3)
		print(e)


for _ in range(int(input())):
	n = int(input())
	a = input()
	r = 0
	l = 0
	b = []
	for i in range(n):
		if a[i % n] == 'R':
			if l != 0:
				b.append(l)
			r += 1
			l = 0
		else:
			if r != 0:
				b.append(r)
			l += 1
			r = 0
	if len(b) == 0:
		b.append(0)
	if a[0] == a[-1]:
		b[0] += r + l
	else:
		b.append(r + l)
	ans = 0
	for i in b:
		ans += i // 3
	if a.count('R') == n or a.count('L') == n:
		print((n + 2) // 3)
	else:
		print(ans)


import itertools
from queue import Queue
import copy
import random

def check(arr):
	n = len(arr)
	for i in range(n):
		left = arr[(i - 1) % n]
		right = arr[(i + 1) % n]
		curr = arr[i]
		if not (left == 'L' and right == 'L' and (curr == 'R') or (left == 'L' and right == 'R' and (curr in ['L', 'R'])) or (left == 'R' and right == 'L' and (curr in ['L', 'R'])) or (left == 'R' and right == 'R' and (curr == 'L'))):
			return False
	return True

def brute(n, arr):
	q = Queue()
	q.put((0, arr))
	while not q.empty():
		(numOps, curr) = q.get()
		if check(curr):
			return numOps
		for i in range(n):
			nextArr = copy.copy(curr)
			if curr[i] == 'L':
				nextArr[i] = 'R'
			else:
				nextArr[i] = 'L'
			q.put((numOps + 1, nextArr))
	assert False

def fast(n, arr):
	result = 0
	start = 0
	for i in range(n + 1):
		if arr[i % n] != arr[(i + n - 1) % n]:
			start = i
			break
	for i in range(start, start + n):
		window = []
		for p in range(5):
			window.append(arr[(i + p) % n])
		if all((num == 'L' for num in window)) or all((num == 'R' for num in window)):
			result += 1
			if arr[(i + 2) % n] == 'L':
				arr[(i + 2) % n] = 'R'
			else:
				arr[(i + 2) % n] = 'L'
		window = []
		for p in range(3):
			window.append(arr[(i + p) % n])
		if all((num == 'L' for num in window)) or all((num == 'R' for num in window)):
			result += 1
			if arr[(i + 1) % n] == 'L':
				arr[(i + 1) % n] = 'R'
			else:
				arr[(i + 1) % n] = 'L'
	return result

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		arr = [c for c in input()]
		result = fast(n, arr)
		print(result)

def randomTest():
	t = 10000
	for i in range(1, t + 1):
		n = random.randint(5, 20)
		arr = [random.choice(['L', 'R']) for _ in range(n)]
		print(i, ''.join(arr))
		b = brute(n, arr)
		f = fast(n, arr[:])
		if b != f:
			print('Broken!')
			print(''.join(arr), b, f)
			return

def test():
	S = 'LLLLLLRRL'
	arr = [c for c in S]
	n = len(arr)
	b = brute(n, arr)
	f = fast(n, arr[:])
	print(S, b, f)
main()


from sys import stdin
tt = int(stdin.readline())
for loop in range(tt):
	n = int(stdin.readline())
	S = stdin.readline()
	S = S[:-1]
	rnum = 0
	lnum = 0
	if 'L' not in S or 'R' not in S:
		print((n + 2) // 3)
		continue
	for i in range(n - 1):
		if S[i] != S[i + 1]:
			S = S[i + 1:] + S[:i + 1]
			break
	ans = 0
	for i in S:
		if i == 'R':
			rnum += 1
			ans += lnum // 3
			lnum = 0
		else:
			lnum += 1
			ans += rnum // 3
			rnum = 0
	if lnum >= 3:
		ans += lnum // 3
	if rnum >= 3:
		ans += rnum // 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)


from sys import stdin, stdout
import math
from collections import Counter, deque
L = lambda : list(map(int, stdin.readline().strip().split()))
M = lambda : map(int, stdin.readline().strip().split())
I = lambda : int(stdin.readline().strip())
S = lambda : stdin.readline().strip()
C = lambda : stdin.readline().strip().split()
mod = 1000000007

def pl(a):
	print(' '.join(list(map(str, a))))

def solve():
	n = I()
	s = S()
	d = Counter(s)
	if d['L'] == n or d['R'] == n:
		print((n + 2) // 3)
		return
	ans = 0
	c = 1
	a = []
	for i in range(n):
		if s[i] == s[(i + 1) % n]:
			c += 1
		else:
			a += [c]
			c = 1
	if s[-1] == s[0]:
		a[0] += c - 1
	for e in a:
		ans += e // 3
	print(ans)
for _ in range(I()):
	solve()


from sys import stdin
g = lambda : stdin.readline().strip()
gl = lambda : g().split()
gil = lambda : [int(var) for var in gl()]
gfl = lambda : [float(var) for var in gl()]
gcl = lambda : list(g())
gbs = lambda : [int(var) for var in g()]
mod = int(1000000000.0) + 7
inf = float('inf')
(t,) = gil()
for _ in range(t):
	(n,) = gil()
	s = g()
	(arr, prev) = ([1], s[0])
	for i in range(1, n):
		if s[i] == prev:
			arr[-1] += 1
		else:
			arr.append(1)
			prev = s[i]
	if len(arr) == 1:
		print((n - 1) // 3 + 1)
	else:
		if len(arr) & 1:
			arr[0] += arr.pop()
		ans = 0
		for a in arr:
			ans += a // 3
		print(ans)


import math
for _ in range(int(input())):
	n = int(input())
	s = input()
	ans = 0
	if len(set(s)) == 1:
		ans = math.ceil(n / 3)
	else:
		if s[0] == s[-1] == 'L':
			a = s.split('R')
			b = s.split('L')
			a[0] = a[0] + a[-1]
			a.pop(-1)
		elif s[0] == s[-1] == 'R':
			a = s.split('L')
			b = s.split('R')
			a[0] = a[0] + a[-1]
			a.pop(-1)
		else:
			a = s.split('R')
			b = s.split('L')
		a = [len(x) // 3 for x in a if len(x) >= 3]
		b = [len(x) // 3 for x in b if len(x) >= 3]
		ans += sum(a) + sum(b)
	print(ans)


import math
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	in_a_row = [1]
	for i in range(1, len(s)):
		if s[i] == s[i - 1]:
			in_a_row[-1] += 1
		else:
			in_a_row.append(1)
	if len(in_a_row) == 1:
		print(math.ceil(in_a_row[0] / 3))
		continue
	elif len(in_a_row) % 2:
		in_a_row[0] += in_a_row.pop()
	discussions = 0
	for num_consecutive in in_a_row:
		if num_consecutive > 2:
			discussions += num_consecutive // 3
	print(discussions)


from sys import stdin, gettrace
if gettrace():
	inputi = input
else:

	def input():
		return next(stdin)[:-1]

	def inputi():
		return stdin.buffer.readline()

def solve():
	n = int(input())
	ss = list(input())
	res = 0
	if all((s == ss[0] for s in ss)):
		print((n + 2) // 3)
		return
	st = 0
	while ss[st] == ss[-1]:
		st += 1
	cur = ss[-1]
	count = 0
	for s in ss[st:] + ss[:st]:
		if s != cur:
			res += count // 3
			count = 0
			cur = s
		count += 1
	res += count // 3
	print(res)

def main():
	t = int(input())
	for _ in range(t):
		solve()
main()


def solve():
	n = int(input())
	arr = list(input())
	ans = 0
	items = []
	curr = []
	curr_val = arr[0]
	for x in arr:
		if x != curr_val:
			curr_val = x
			items.append(curr)
			curr = [x]
		else:
			curr.append(x)
	if len(curr) > 0:
		items.append(curr)
	if len(items) > 1 and items[0][0] == items[-1][0]:
		items[0].extend(items[-1])
		items.pop(-1)
	if len(items) == 1:
		print((len(items[0]) + 2) // 3)
		return
	for x in items:
		ans += len(x) // 3
	print(ans)
for _ in range(int(input())):
	solve()


for _ in range(int(input())):
	x = int(input())
	s = input()
	if 'R' in s and 'L' in s:
		A = []
		if s[0] == 'R':
			A.append([1, 1])
		else:
			A.append([2, 1])
		for i in range(1, len(s)):
			if A[-1][0] == 1:
				if s[i] == 'R':
					A[-1][1] += 1
				else:
					A.append([2, 1])
			elif s[i] == 'L':
				A[-1][1] += 1
			else:
				A.append([1, 1])
		if A[0][0] == A[-1][0]:
			A[0][1] += A[-1][1]
			A.pop()
		Ans = 0
		for i in range(len(A)):
			Ans += A[i][1] // 3
		print(Ans)
	else:
		print((len(s) + 2) // 3)


from collections import deque
from collections import OrderedDict
import math
import sys
import os
import threading
import bisect
import operator
import heapq
from atexit import register
from io import BytesIO
import io
input = lambda : sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	n = int(input())
	s = input()
	dp = [1] * (n + 3)
	for i in range(1, n):
		if s[i] == s[i - 1]:
			dp[i] = dp[i - 1] + 1
	isCircle = False
	if s[0] == s[n - 1]:
		if s.count('R') != 0 and s.count('L') != 0:
			dp[n] = 0
			for i in range(n - 1):
				if s[i] == s[n - 1]:
					dp[n] += 1
					dp[i] = 1
				else:
					break
			for i in range(n - 1, 0, -1):
				if s[i] == s[0]:
					dp[n] += 1
					dp[i] = 1
				else:
					break
		else:
			if n > 2:
				print(n // 3 + (1 if n % 3 != 0 else 0))
			else:
				print(0)
			continue
	lisAnswer = []
	for i in range(1, n + 1):
		if dp[i] > 2 and dp[i + 1] == 1:
			lisAnswer.append(dp[i])
	answer = 0
	for i in range(len(lisAnswer)):
		answer += lisAnswer[i] // 3
	print(answer)
sys.exit(0)

class Person:

	def __init__(self, name, age):
		self.name = name
		self.age = age
p1 = Person('heelo', 27)
print(help(Person))
age = 26
name = 'Swaroop'
print('Возрас {} -- {} лет'.format(name, age))
print(help(object))


import sys
import math
from collections import defaultdict, Counter
t = int(input())
for i in range(t):
	n = int(input())
	s = list(input())
	j = n - 1
	while s[j] == s[0] and j >= 0:
		j -= 1
	if j == -1:
		print(int(math.ceil(n / 3)))
		continue
	s = s[j + 1:] + s[:j + 1]
	ans = 0
	prev = 1
	for j in range(1, n):
		if s[j] == s[j - 1]:
			prev += 1
		else:
			ans += prev // 3
			prev = 1
	ans += prev // 3
	print(ans)


n = int(input())
for i in range(n):
	k = int(input())
	s = input()
	l = [1]
	c = 0
	su = 0
	flag = 0
	if len(s) == 1:
		print(1)
		continue
	for j in range(k - 1):
		if s[j] == s[j + 1]:
			l[-1] += 1
		else:
			l.append(1)
	if s[0] == s[-1] and ('L' in s and 'R' in s):
		l[0] += l.pop()
	elif 'L' not in s or 'R' not in s:
		su = (l[0] + 2) // 3
		print(su)
		continue
	for j in l:
		su += j // 3
	print(su)


from math import *
from collections import *
from random import *
from decimal import Decimal
from heapq import *
from bisect import *
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 5)

def lis():
	return list(map(int, input().split()))

def ma():
	return map(int, input().split())

def inp():
	return int(input())

def st1():
	return input().rstrip('\n')
t = inp()
while t:
	t -= 1
	n = inp()
	s = st1()
	i = 0
	j = n - 1
	re = 0
	res = []
	if s[0] == s[-1]:
		co = 0
		while i < n:
			if s[i] == s[0]:
				i += 1
				co += 1
				continue
			break
		while j > i:
			if s[j] == s[0]:
				j -= 1
				co += 1
				continue
			break
		res.append([co, s[0]])
	try:
		p = s[i]
	except:
		pass
	co = 0
	for k in range(i, j + 1):
		if s[k] == p:
			co += 1
			continue
		else:
			res.append([co, p])
			co = 1
			p = s[k]
	if co:
		res.append([co, s[0]])
	re = 0
	if len(res) == 1:
		print(ceil(res[0][0] / 3))
		continue
	for i in range(len(res)):
		if res[i][0] > 2:
			x = res[i][0]
			x -= 1
			re += (x + 1) // 3
	print(re)


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 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)


t = int(input())

def ceildiv(a, b):
	return (a + b - 1) // b
for case in range(t):
	n = int(input())
	s = input()
	if all((x == s[0] for x in s)):
		print(ceildiv(n, 3))
	else:
		if s[0] == s[-1]:
			q = s.index('L' if s[0] == 'R' else 'R')
			s = s[q:] + s[:q]
		c = 0
		out = 0
		prev = None
		for x in s:
			if x == prev:
				c += 1
			else:
				out += c // 3
				c = 1
			prev = x
		if c:
			out += c // 3
		print(out)


t = int(input())
for tc in range(t):
	n = int(input())
	s = input()
	blocks = {}
	blocks[0] = [1, s[0]]
	p = s[0]
	curr = 0
	for i in range(1, n):
		if s[i] == p:
			blocks[curr][0] += 1
		else:
			curr += 1
			p = s[i]
			blocks[curr] = [1, p]
	arr = []
	for i in range(len(blocks)):
		arr.append(blocks[i])
	if len(arr) == 1:
		print((arr[0][0] - 4) // 3 + 2)
		continue
	if arr[0][1] == arr[-1][1]:
		arr[0][0] += arr[-1][0]
		del arr[-1]
	cnt = 0
	for i in arr:
		cnt += i[0] // 3
	print(cnt)


for _ in range(int(input())):
	n = int(input())
	s = input()
	if len(set(list(s))) == 1:
		print(1 + (n - 1) // 3)
	else:
		t = s[0]
		c = 0
		x = 0
		y = 0
		for i in range(n):
			if s[i] == t:
				c += 1
			else:
				x = i
				break
		for i in range(n - 1, -1, -1):
			if s[i] == t:
				c += 1
			else:
				y = i
				break
		ans = c // 3
		c = 1
		for i in range(x + 1, y + 1):
			if s[i] == s[i - 1]:
				c += 1
			else:
				ans += c // 3
				c = 1
		ans += c // 3
		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
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
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])))


import sys

def main():
	t = Iint()
	for _ in range(t):
		n = Iint()
		s = Istr()
		debug(s)
		if s == 'R' * n or s == 'L' * n:
			ans = (n + 2) // 3
			print(ans)
			continue
		if s[0] == s[-1]:
			debug('check')
			c = s[0]
			s = s.lstrip(c)
			s += c * (n - len(s))
		debug(s)
		R_same = 0
		L_same = 0
		prev = ''
		ans = 0
		for (i, si) in en(s):
			if si == 'R' and prev in ('R', ''):
				R_same += 1
			elif si == 'L' and prev in ('L', ''):
				L_same += 1
			elif si == 'R' and prev == 'L':
				debug('L', L_same, c=3)
				ans += L_same // 3
				L_same = 0
				R_same = 1
			elif si == 'L' and prev == 'R':
				debug('R', R_same, c=3)
				ans += R_same // 3
				R_same = 0
				L_same = 1
			prev = si
		ans += R_same // 3 + L_same // 3
		print(ans)
en = enumerate
FIO = 10

def debug(*args, c=6):
	pass
BUFSIZE = 8 * 1024

def Istr():
	return input()

def Iint():
	return int(input())

def Ilist():
	return list(map(int, input().split()))

def Imap():
	return map(int, input().split())

def Plist(li, s=' '):
	print(s.join(map(str, li)))
if not FIO:
	if __name__ == '__main__':
		main()
		exit()
else:
	from io import BytesIO, IOBase
	import os

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))

def input():
	return sys.stdin.readline().rstrip('\r\n')
main()


import io
import os
from collections import Counter, defaultdict, deque

def solve(N, S):
	if 'R' not in S or 'L' not in S:
		return 1 + (N - 1) // 3
	S = deque(S)
	while S[0] == S[-1]:
		S.rotate(1)

	def makeRepeat(arr):
		repeat = [[arr[0], 0]]
		for x in arr:
			if x == repeat[-1][0]:
				repeat[-1][1] += 1
			else:
				repeat.append([x, 1])
		return repeat
	repeats = makeRepeat(S)
	ans = 0
	for (c, counts) in repeats:
		ans += counts // 3
	return ans
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
TC = int(input())
for tc in range(1, TC + 1):
	(N,) = [int(x) for x in input().split()]
	S = input().decode().rstrip()
	ans = solve(N, S)
	print(ans)


t = int(input())

def diff(s, t):
	return sum((1 for (a, b) in zip(s, t) if a != b))
for _ in range(t):
	n = int(input())
	s = input()
	best = n + 5
	for _ in range(4):
		cc = [n + 2] * (n + 1)
		cc[0] = 0
		for i in range(2, n + 1):
			cc[i] = min(cc[i], cc[i - 2] + diff('RL', s[i - 2:i]))
			if i >= 3:
				cc[i] = min(cc[i], cc[i - 3] + diff('RRL', s[i - 3:i]))
				cc[i] = min(cc[i], cc[i - 3] + diff('RLL', s[i - 3:i]))
			if i >= 4:
				cc[i] = min(cc[i], cc[i - 4] + diff('RRLL', s[i - 4:i]))
		best = min(cc[n], best)
		s = s[1:] + s[0]
	print(best)


def solve():
	n = int(input())
	lst = list(input())
	if len(set(lst)) == 1:
		print(n // 3 + (n % 3 != 0))
		return 0
	for i in range(n - 1):
		if lst[i] != lst[i + 1]:
			q = i + 1
			break
	lst = [lst[i] for i in range(q, n)] + [lst[i] for i in range(q)]
	rcnt = 0
	lcnt = 0
	ans = 0
	for i in lst:
		if i == 'R':
			rcnt += 1
			lcnt = 0
		else:
			lcnt += 1
			rcnt = 0
		if rcnt == 3:
			rcnt = 0
			ans += 1
		if lcnt == 3:
			lcnt = 0
			ans += 1
	print(ans)
for i in range(int(input())):
	solve()


for _ in range(int(input())):
	n = int(input())
	s = input()
	se = set(s)
	ans = 0
	if len(se) == 1:
		n += 2
		print(n // 3)
	else:
		for i in range(n):
			if s[i] != s[i - 1]:
				break
		j = i + 1
		count = 1
		while j != i:
			if s[j] == s[j - 1]:
				count += 1
			else:
				count = 1
			if count == 3:
				count = 0
				ans += 1
			j += 1
			j %= n
		print(ans)


T = int(input())
for _ in range(T):
	n = int(input())
	s = input()
	ans = 0
	if len(set(s)) == 1:
		s = ('R' if s[0] == 'L' else 'L') + s[1:]
		ans += 1
	i = n - 1
	while s[i] == s[0]:
		i -= 1
	s = s[i + 1:] + s[:i + 1]
	rs = [len(x) // 3 for x in s.split('L')]
	ls = [len(x) // 3 for x in s.split('R')]
	ans += sum(rs) + sum(ls)
	print(ans)


def fact(x):
	if x == 0:
		return 1
	else:
		return x * fact(x - 1)

def lower_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] >= num:
			answer = middle
			end = middle - 1
		else:
			start = middle + 1
	return answer

def upper_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] <= num:
			answer = middle
			start = middle + 1
		else:
			end = middle - 1
	return answer

def abs(x):
	return x if x >= 0 else -x

def binary_search(li, val, lb, ub):
	ans = 0
	while lb <= ub:
		mid = (lb + ub) // 2
		if li[mid] > val:
			ub = mid - 1
		elif val > li[mid]:
			lb = mid + 1
		else:
			ans = 1
			break
	return ans

def kadane(x):
	sum_so_far = 0
	current_sum = 0
	for i in x:
		current_sum += i
		if current_sum < 0:
			current_sum = 0
		else:
			sum_so_far = mpos(sum_so_far, current_sum)
	return sum_so_far

def pref(li):
	pref_sum = [0]
	for i in li:
		pref_sum.append(pref_sum[-1] + i)
	return pref_sum
from math import *
for _ in range(int(input())):
	n = int(input())
	a = input()
	st = a[0]
	ind_st = 0
	for i in range(n - 1, -1, -1):
		if a[i] == st:
			ind_st = i
		else:
			break
	a += a
	ans = []
	count_similar = 0
	curr = a[ind_st]
	for i in range(ind_st, ind_st + n):
		if a[i] == curr:
			count_similar += 1
		else:
			curr = a[i]
			ans.append(count_similar)
			count_similar = 1
	ans.append(count_similar)
	final = 0
	for i in ans:
		final += i // 3 if i != n else (i + 2) // 3
	print(final)


for _ in range(int(input())):
	n = int(input())
	s = list(input())
	ans = 0
	m = 1
	if s.count('R') == n or s.count('L') == n:
		ans = (n + 2) // 3
	else:
		while s[-1] == s[0]:
			s.append(s.pop(0))
		s.append('A')
		for i in range(len(s) - 1):
			if s[i] == s[i + 1]:
				m += 1
			else:
				ans += m // 3
				m = 1
	print(ans)


t = int(input())
for _ in range(t):
	ans = 0
	n = int(input())
	mas = []
	dop = 1
	s = []
	for __ in input():
		s.append(__)
	for i in range(n - 1):
		if s[i] == s[i + 1]:
			dop += 1
		else:
			mas.append(dop)
			dop = 1
	mas.append(dop)
	if s[-1] == s[0] and len(mas) > 1:
		mas[0] += mas[-1]
		mas = mas[:-1]
	if len(mas) == 1:
		if mas[0] % 3 == 0:
			ans += mas[0] // 3
		else:
			ans += mas[0] // 3 + 1
	else:
		for j in range(len(mas)):
			ans += mas[j] // 3
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(input())
	c = 0
	while a and a[-1] == a[0]:
		a.pop()
		c += 1
	if len(a) == 0:
		if c <= 2:
			print(0)
		elif c == 3:
			print(1)
		else:
			print((c + 2) // 3)
	else:
		a += ['$']
		ans = 0
		for i in range(len(a) - 1):
			c += 1
			if a[i] != a[i + 1]:
				ans += c // 3
				c = 0
		print(ans)


import sys
input = sys.stdin.readline
t = int(input())
ss = ['LLL', 'LLR', 'LRL', 'LRR', 'RLL', 'RLR', 'RRL', 'RRR']
ok = ['LLR', 'LRL', 'LRR', 'RLL', 'RLR', 'RRL']
D = {}
for s1 in ss:
	for s2 in ss:
		c = 0
		for i in range(3):
			if s1[i] != s2[i]:
				c += 1
		D[s1, s2] = c
for _ in range(t):
	x = int(input())
	vl = input().rstrip()
	v_best = 10 ** 8
	for kk in range(3):
		l = vl[kk:] + vl[:kk]
		B = {}
		init = l[:3]
		for s in ok:
			B[s] = D[s, init]
			if s[0] != init[0] or s[1] != init[1]:
				B[s] += 10 ** 8
		for i in range(1, x - 2):
			sub = l[i:i + 3]
			G = dict(B)
			for key in ok:
				best = 10 ** 8
				c = 0
				if sub[2] != key[2]:
					c += 1
				for sub_key in ok:
					if sub_key[1:] == key[:2]:
						best = min(best, B[sub_key])
				G[key] = c + best
			B = dict(G)
		ans1 = 10 ** 8
		for key in B:
			if key[1:] + init[0] in ok and key[2:] + init[:2] in ok:
				ans1 = min(ans1, B[key])
		v_best = min(v_best, ans1)
	print(v_best)


N = int(input())
for _ in range(N):
	n = int(input())
	w = input()
	res = 0
	rc = w.count('R')
	if rc == n or rc == 0:
		if n < 3:
			print(0)
		elif n == 3:
			print(1)
		else:
			print(2 + (n - 4) // 3)
		continue
	i2c = dict()
	i = 0
	while w[i] == w[i + 1]:
		i += 1
	i += 1
	steps = 0
	while steps < n:
		j = i
		while j < n and w[j] == w[i]:
			j = (j + 1) % n
		d = j - i if j > i else j + n - i
		steps += d
		i = j
		if d not in i2c:
			i2c[d] = 1
		else:
			i2c[d] += 1
	for (i, c) in i2c.items():
		res += c * (i // 3)
	print(res)


for _ in range(int(input())):
	n = int(input())
	s = input()
	island = [0 for i in range(n + 1)]
	act = 0
	island[0] = 1
	for i in range(1, n):
		if s[i] != s[i - 1]:
			act += 1
		island[act] += 1
	if act == 0:
		if n % 3 == 0:
			print(n // 3)
		else:
			print(n // 3 + 1)
	else:
		if act % 2 == 0:
			island[0] += island[act]
			act -= 1
		ans = 0
		for i in range(act + 1):
			ans += island[i] // 3
		print(ans)


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
import collections
from itertools import permutations
from collections import defaultdict
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')
for _ in range(int(input())):
	n = int(input())
	s = input()
	if n <= 2:
		print(0)
	else:
		s = list(s)
		if s.count('L') == 0 or s.count('R') == 0:
			print((n + 2) // 3)
		else:
			a = list()
			curr = 1
			for i in range(1, n):
				if s[i] == s[i - 1]:
					curr += 1
				else:
					a.append(curr)
					curr = 1
			if s[0] == s[-1]:
				a[0] += curr
			else:
				a.append(curr)
			c = 0
			for i in a:
				c += i // 3
			print(c)


import math
t = int(input())
for __ in range(t):
	n = int(input())
	seq = input()
	if len(set(seq)) == 1:
		print(math.ceil(n / 3))
		continue
	l_lens = [len(seg) for seg in seq.split('R')]
	r_lens = [len(seg) for seg in seq.split('L')]
	if seq[0] == seq[-1] == 'R':
		r_lens[0] += r_lens.pop()
	if seq[0] == seq[-1] == 'L':
		l_lens[0] += l_lens.pop()
	total_cost = sum([x // 3 for x in l_lens]) + sum([x // 3 for x in r_lens])
	print(total_cost)


from sys import stdout, stdin
(_input, _print) = (stdin.readline, stdout.write)
(_int, _range, _str, _enumerate) = (int, range, str, enumerate)

def solution():
	for _ in _range(_int(_input())):
		n = _int(_input())
		arr = _input().rstrip('\n')
		if arr[-1] == arr[0]:
			k = 0
			while arr[0] == arr[-(k + 1)] and k + 1 < n:
				k += 1
			arr = arr[-k:] + arr[:-k]
		current = 0
		ans = 0
		for (i, v) in _enumerate(arr):
			if v != arr[current]:
				ans += (i - current) // 3
				current = i
		else:
			ans += (n - current) // 3
			if current == 0 and (n - current) % 3 > 0:
				ans += 1
		print(ans)
solution()


from sys import exit, stdin, stderr

def rl():
	return [int(w) for w in stdin.readline().split()]
(T,) = rl()
for _ in range(T):
	(n,) = rl()
	s = stdin.readline().rstrip()
	l = 1
	while l < n and s[l] == s[0]:
		l += 1
	if l == n:
		print((n + 2) // 3)
		continue
	r = n
	while s[r - 1] == s[0]:
		r -= 1
	y = (n - r + l) // 3
	k = l
	for i in range(l, r):
		if s[i] != s[k]:
			y += (i - k) // 3
			k = i
	y += (r - k) // 3
	print(y)


import sys
LI = lambda : list(map(int, sys.stdin.readline().strip('\n').split()))
MI = lambda : map(int, sys.stdin.readline().strip('\n').split())
SI = lambda : sys.stdin.readline().strip('\n')
II = lambda : int(sys.stdin.readline().strip('\n'))
for _ in range(II()):
	n = II()
	a = SI()
	rc = a.count('R')
	if rc == 0 or rc == n:
		print(n // 3 + (1 if n % 3 else 0))
		continue
	i = a.index('L')
	while a[i] == 'L':
		i = i + 1 if i < n - 1 else 0
	(ans, c, j) = (0, 1, i + 1 if i < n - 1 else 0)
	while j != i:
		if a[j] == a[j - 1 if j else n - 1]:
			c += 1
		else:
			ans += c // 3
			c = 1
		j = j + 1 if j < n - 1 else 0
	ans += c // 3
	print(ans)


import os
import sys
from io import BytesIO, IOBase

def main():
	for _ in range(int(input())):
		n = int(input())
		s = input()
		lengths = []
		curr = s[0]
		currlen = 0
		for c in s:
			if c == curr:
				currlen += 1
			else:
				lengths.append(currlen)
				currlen = 1
				curr = c
		if currlen < n and s[0] == s[-1]:
			lengths[0] += currlen
		else:
			lengths.append(currlen)
		out = 0
		if len(lengths) == 1 and n >= 3:
			out = 1
			lengths[0] -= 1
			lengths.append(1)
		for v in lengths:
			out += v // 3
		print(out)
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')
main()


t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	count = []
	last = 0
	c = 1
	for i in range(1, n):
		if s[last] == s[i]:
			c += 1
		else:
			last = i
			count.append(c)
			c = 1
	count.append(c)
	if len(count) == 1:
		print((count[0] + 2) // 3)
	else:
		if s[0] == s[-1]:
			if s[0] == 'L':
				count[-1] += count[0]
				del count[0]
			else:
				count[0] += count[-1]
				del count[-1]
		changes = 0
		for i in range(len(count)):
			changes += count[i] // 3
		print(changes)


import sys
input = lambda : sys.stdin.readline().rstrip()
T = int(input())
for _ in range(T):
	N = int(input())
	S = input()
	X = []
	prev = S[0]
	c = 0
	for (i, s) in enumerate(S):
		if s == prev:
			c += 1
		else:
			X.append(c)
			prev = s
			c = 1
	if not X:
		X.append(N + 2)
	elif prev == S[0]:
		X[0] += c
	else:
		X.append(c)
	print(sum([x // 3 for x in X]))


from fractions import Fraction
import bisect
import os
import io
from collections import Counter
import bisect
from collections import defaultdict
import math
import random
import heapq
from math import sqrt
import sys
from functools import reduce, cmp_to_key
from collections import deque
import threading
from itertools import combinations
from io import BytesIO, IOBase
from itertools import accumulate
from queue import Queue

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def tinput():
	return input().split()

def rinput():
	return map(int, tinput())

def rlinput():
	return list(rinput())
mod = int(1000000000.0) + 7

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))
t = 1
t = iinput()
for _ in range(t):
	n = iinput()
	s = list(input())
	if n == 1 or n == 2:
		print(0)
	else:
		cnt = s.count('L')
		if cnt == n or cnt == 0:
			print(1 + (n - 1) // 3)
			continue
		ans = 0
		i = 1
		temp = 1
		while i < n and s[i] == s[i - 1]:
			i += 1
			temp += 1
		j = n - 1
		while j > i and s[j] == s[0]:
			j -= 1
			temp += 1
		ans = temp // 3
		while i < j:
			temp = 1
			while i < j and s[i] == s[i + 1]:
				temp += 1
				i += 1
			i += 1
			ans += temp // 3
		print(ans)


for t in range(int(input())):
	n = int(input())
	f = list(input())
	ans = 0
	fir = 0
	sem = 1
	for i in range(1, n):
		if f[i] == f[i - 1]:
			sem += 1
		elif fir == 0:
			fir = sem
			sem = 1
		else:
			ans += sem // 3
			sem = 1
	if sem == n and n % 3 != 0:
		ans += 1
	if f[0] == f[-1]:
		ans += (sem + fir) // 3
	else:
		ans += sem // 3
		ans += fir // 3
	print(ans)


import math, sys, bisect, heapq
from collections import defaultdict, Counter, deque
from itertools import groupby, accumulate
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
ilele = lambda : map(int, input().split())
alele = lambda : list(map(int, input().split()))

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def Y(c):
	print(['NO', 'YES'][c])

def y(c):
	print(['no', 'yes'][c])

def Yy(c):
	print(['No', 'Yes'][c])
for _ in range(int(input())):
	N = int(input())
	S = input()
	if len(set(S)) == 1:
		print(math.ceil(N / 3))
	else:
		G = [list(j) for (i, j) in groupby(S)]
		if G[0][0] == G[-1][0]:
			G[0] += G.pop()
		print(sum([len(i) // 3 for i in G]))


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)


t = int(input())
for _ in range(t):
	n = int(input())
	st = input()
	li = []
	if st[0] == 'L':
		bo = True
	else:
		bo = False
	cur = 0
	for i in st:
		if bo:
			if i == 'L':
				cur += 1
			else:
				li.append(cur)
				cur = 1
				bo = not bo
		elif i == 'R':
			cur += 1
		else:
			li.append(cur)
			cur = 1
			bo = not bo
	if st[0] == st[-1] and len(li) > 0:
		li[0] += cur
	else:
		li.append(cur)
	s = 0
	if len(li) == 1:
		print(1 + (li[0] - 1) // 3)
		continue
	for i in li:
		s += i // 3
	print(s)


import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log
from collections import defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
from collections import Counter
inp = lambda : int(input())
strng = lambda : input().strip()
jn = lambda x, l: x.join(map(str, l))
strl = lambda : list(input().strip())
mul = lambda : map(int, input().strip().split())
mulf = lambda : map(float, input().strip().split())
seq = lambda : list(map(int, input().strip().split()))
ceil = lambda x: int(x) if x == int(x) else int(x) + 1
ceildiv = lambda x, d: x // d if x % d == 0 else x // d + 1
flush = lambda : stdout.flush()
stdstr = lambda : stdin.readline()
stdint = lambda : int(stdin.readline())
stdpr = lambda x: stdout.write(str(x))
stdarr = lambda : map(int, stdstr().split())
mod = 1000000007
for _ in range(stdint()):
	n = stdint()
	s = input()
	if len(set(s)) == 1:
		print(math.ceil(n / 3))
	else:
		same = []
		(r, last) = (0, 0)
		for i in range(n):
			if s[i] == s[last]:
				r += 1
			else:
				same.append(r)
				last = i
				r = 1
		if s[-1] != s[0]:
			same.append(r)
		else:
			ne = same[0] + r
			del same[0]
			same.append(ne)
		res = 0
		for i in same:
			res += i // 3
		print(res)


t = int(input())
while t:
	t -= 1
	n = int(input())
	s = input()
	j = -1
	for i in range(n):
		if s[i] != s[n - 1]:
			j = i
			break
	if j == -1:
		print((n + 2) // 3)
		continue
	ans = 0
	last = j
	while j < n:
		k = j
		while k + 1 < n and s[k + 1] == s[j]:
			k += 1
		if k == n - 1:
			k += last
		ans += (k - j + 1) // 3
		j = k + 1
	print(ans)


import os
import sys
from io import BytesIO, IOBase
import heapq as h
from bisect import bisect_left, bisect_right
from types import GeneratorType
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		import os
		self.os = os
		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 = self.os.read(self._fd, max(self.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 = self.os.read(self._fd, max(self.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:
			self.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')
from collections import defaultdict as dd, deque as dq
import math, string

def getInts():
	return [int(s) for s in input().split()]

def getInt():
	return int(input())

def getStrs():
	return [s for s in input().split()]

def getStr():
	return input()

def listStr():
	return list(input())
MOD = 10 ** 9 + 7

def solve():
	N = getInt()
	S = getStr()
	seqs = []
	curr = 0
	for j in range(N):
		if j == 0:
			curr += 1
			continue
		if S[j] == S[j - 1]:
			curr += 1
		else:
			seqs.append(curr)
			curr = 1
	seqs.append(curr)
	if len(seqs) == 1:
		seqs[0] += 2
	if len(seqs) > 1 and S[0] == S[-1]:
		seqs[0] += seqs[-1]
		seqs.pop()
	ans = 0
	for seq in seqs:
		ans += seq // 3
	return ans
for _ in range(getInt()):
	print(solve())


import math
T = int(input())
for _ in range(T):
	n = int(input())
	s = input()
	cnt = []
	typ = []
	lr = [0, 0]
	if s[0] == 'L':
		lr[0] += 1
		flag = 0
	else:
		lr[1] += 1
		flag = 1
	for i in range(1, n):
		if s[i] != s[i - 1]:
			cnt.append(lr[flag])
			typ.append(flag)
			lr[flag] = 0
			flag = 1 - flag
			lr[flag] += 1
		else:
			lr[flag] += 1
	cnt.append(lr[flag])
	typ.append(flag)
	if len(cnt) == 1:
		ans = math.ceil(n / 3)
	else:
		for i in range(len(cnt)):
			if i == 0:
				ll = -1
				rr = 1
			elif i == len(cnt) - 1:
				ll = i - 1
				rr = 0
			else:
				ll = i - 1
				rr = i + 1
			if typ[i] != typ[ll]:
				cnt[i] -= 1
			if typ[i] != typ[rr]:
				cnt[i] = max(0, cnt[i] - 1)
		if typ[0] == typ[-1]:
			cnt[0] += cnt[-1]
			cnt.pop()
			typ.pop()
		ans = 0
		for i in range(len(cnt)):
			ans += math.ceil(cnt[i] / 3)
	print(ans)


def calc_one(n):
	frac = n / 3
	if frac % 1 == 0:
		return int(frac)
	return int(frac + 1)

def calc_ops(L):
	frac = (L - 2) / 3
	if frac % 1 == 0:
		return int(frac)
	return int(frac + 1)

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		s = input()
		l = []
		c = 1
		for i in range(1, n):
			if s[i] == s[i - 1]:
				c += 1
			else:
				l.append(c)
				c = 1
		if s[0] == s[-1]:
			if len(l) == 0:
				l.append(c)
			else:
				l[0] += c
		else:
			l.append(c)
		if len(l) == 1:
			print(calc_one(n))
		else:
			total = 0
			for j in l:
				total += calc_ops(j)
			print(total)
main()


for _ in range(int(input())):
	n = int(input())
	s = input()
	s = [_ for _ 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
	h = n
	if l != 0:
		h = (l + n) % n
	i += 1
	while i < h:
		r = i
		while r < h and s[r] == s[(r + 1) % n]:
			r += 1
		res += (r - i + 1) // 3
		i = r + 1
	print(res)


from sys import stdin
input = lambda : stdin.readline().rstrip()
for _ in range(int(input())):
	n = int(input())
	s = input()
	ans = 0
	f = [1]
	for i in range(1, n):
		if s[i] == s[i - 1]:
			f[-1] += 1
		else:
			f.append(1)
	if s[0] == s[-1] and len(f) > 1:
		f[0] += f[-1]
		f.pop()
	if len(f) == 1:
		f[0] += 2
	for i in f:
		ans += i // 3
	print(ans)


from math import ceil

def same(s):
	if s == 'R' * len(s) or s == 'L' * len(s):
		return True
	return False
for _ in range(int(input())):
	n = int(input())
	s = input()
	s1 = ''
	if same(s):
		print(ceil(n / 3))
		continue
	f = n - 1
	for i in range(n - 1, -1, -1):
		if s[0] == s[i]:
			f -= 1
		else:
			break
	for i in range(f + 1, n):
		s1 += s[i]
	for i in range(0, f + 1):
		s1 += s[i]
	i = 0
	cnt = 0
	while i < n:
		s2 = s1[i:i + 3]
		if s2 == 'LLL' or s2 == 'RRR':
			cnt += 1
			i += 3
		else:
			i += 1
	print(cnt)


import sys
t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	c = 1
	g = [1]
	for j in range(1, n):
		if s[j] == s[j - 1]:
			g[c - 1] = g[c - 1] + 1
		else:
			g.append(1)
			c = c + 1
	if c == 1:
		print((g[0] + 2) // 3)
	else:
		if c % 2 == 1:
			g[0] = g[0] + g[c - 1]
			c = c - 1
		sum = 0
		for j in range(c):
			sum = sum + g[j] // 3
		print(sum)


t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	lengths = []
	curr = s[0]
	currLen = 0
	for c in s:
		if c == curr:
			currLen += 1
		else:
			lengths.append(currLen)
			currLen = 1
			curr = c
	if currLen < n and s[0] == s[-1]:
		lengths[0] += currLen
	else:
		lengths.append(currLen)
	out = 0
	if len(lengths) == 1 and n >= 3:
		print((n + 2) // 3)
	else:
		for v in lengths:
			out += v // 3
		print(out)


import sys
from collections import defaultdict
t = 1
t = int(input())
for i in range(t):
	n = int(input())
	a = list(sys.stdin.readline().strip().split())
	op = 0
	a = list(a[0])
	if 'L' in a and 'R' not in a:
		op = 1
		a[0] = 'R'
	elif 'R' in a and 'L' not in a:
		op = 1
		a[0] = 'L'
	else:
		xx = 0
	x = a[-1]
	j = 0
	while j < n and a[j] == x:
		j += 1
	a = a[j:] + a[0:j]
	c = 1
	for j in range(1, n):
		if a[j] == a[j - 1]:
			c += 1
		else:
			c = 1
		if c > 0 and c % 3 == 0:
			op += 1
	print(op)


def op(rr, ll, rl, lr, s):
	for i in range(2, len(s)):
		rrn = lr + (s[i] == 'L')
		lln = rl + (s[i] == 'R')
		rln = min(rr, lr) + (s[i] == 'R')
		lrn = min(ll, rl) + (s[i] == 'L')
		rr = rrn
		ll = lln
		rl = rln
		lr = lrn
	return (rr, ll, rl, lr)
for t in range(int(input())):
	n = int(input())
	s = list(input())
	cc = 10000000
	rl = (s[0] == 'L') + (s[1] == 'R')
	(rr, ll, rl, lr) = op(cc, cc, rl, cc, s)
	ans = min(ll, rl, lr)
	lr = (s[0] == 'R') + (s[1] == 'L')
	(rr, ll, rl, lr) = op(cc, cc, cc, lr, s)
	ans = min(ans, rr, rl, lr)
	rr = (s[0] == 'L') + (s[1] == 'L')
	(rr, ll, rl, lr) = op(rr, cc, cc, cc, s)
	ans = min(ans, ll, rl)
	ll = (s[0] == 'R') + (s[1] == 'R')
	(rr, ll, rl, lr) = op(cc, ll, cc, cc, s)
	print(min(ans, rr, lr))


import math
import sys
import itertools
import random
import bisect
import heapq
import collections

def lcm(x, y):
	return x * y // math.gcd(x, y)

def hcf(x, y):
	return math.gcd(x, y)

def primes(n):
	prime = [True for _ in range(0, n + 1)]
	res = []
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[p] = False
		p += 1
	for i in range(0, n + 1):
		if prime[i] == True:
			res.append(i)
	return res

def primeFactors(n):
	res = []
	while n % 2 == 0:
		res.append(2)
		n //= 2
	for i in range(3, math.sqrt(n) + 1, 2):
		while n % i == 0:
			res.append(i)
			n //= i
	if n > 2:
		res.append(n)
	return res
for _ in range(0, int(input())):
	n = int(input())
	orig = list(input())
	s = [k for k in orig]
	res2 = 0
	curr = 1
	starting_idx = 0
	for i in range(0, n):
		if s[i] != s[(i + 1) % n]:
			starting_idx = (i + 1) % n
			break
	if starting_idx == 0:
		print(int(math.ceil(n / 3)))
		continue
	curr_idx = starting_idx
	while True:
		if s[curr_idx % n] == s[(curr_idx + 1) % n]:
			curr += 1
		else:
			res2 += curr // 3
			curr = 1
		curr_idx = (curr_idx + 1) % n
		if curr_idx == starting_idx:
			break
	res2 += curr // 3
	print(res2)


import sys
input = sys.stdin.readline

def compress(string):
	n = len(string)
	(begin, cnt) = (0, 0)
	ans = []
	if n == 0:
		return ans
	for end in range(n + 1):
		if end == n or string[begin] != string[end]:
			ans.append((string[begin], cnt))
			(begin, cnt) = (end, 1)
		else:
			cnt += 1
	return ans
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()[:-1]
	if n == 2:
		print(1)
		continue
	if s.count('R') == 0 or s.count('L') == 0:
		print((n + 2) // 3)
		continue
	ans = 0
	for i in range(n - 1):
		if s[i] == 'L' and s[i + 1] == 'R':
			s = s[i + 1:] + s[:i + 1]
			break
	comp = compress(s)
	for (_, cnt) in comp:
		ans += cnt // 3
	print(ans)


import math as mt
for _ in range(int(input())):
	n = int(input())
	s = input()
	ans = []
	cnt = 1
	for i in range(1, n):
		if s[i - 1] == s[i]:
			cnt += 1
			continue
		ans += [[cnt, s[i - 1]]]
		cnt = 1
	ans += [[cnt, s[n - 1]]]
	if ans[len(ans) - 1][1] == ans[0][1] and len(ans) > 1:
		ans[0][0] += ans[len(ans) - 1][0]
		ans.pop()
	if len(ans) == 1:
		print(mt.ceil(ans[0][0] / 3))
	else:
		print(sum((ans[i][0] // 3 for i in range(len(ans)))))


import sys, math
import io, os
from bisect import bisect_left as bl, bisect_right as br, insort
from heapq import heapify, heappush, heappop
from collections import defaultdict as dd, deque, Counter

def data():
	return sys.stdin.readline().strip()

def mdata():
	return list(map(int, data().split()))

def outl(var):
	sys.stdout.write(' '.join(map(str, var)) + '\n')

def out(var):
	sys.stdout.write(str(var) + '\n')
from decimal import Decimal
from fractions import Fraction
INF = float('inf')
mod = int(1000000000.0) + 7
for t in range(int(data())):
	n = int(data())
	s = data()
	k = s[0]
	l = []
	cnt = 1
	for i in range(1, n):
		if s[i] == s[i - 1]:
			cnt += 1
		else:
			l.append(cnt)
			cnt = 1
	for ind in range(n):
		if s[ind] == s[-1]:
			cnt += 1
		else:
			break
	l.append(cnt)
	if cnt > n:
		ans = n // 3
		if n % 3 != 0:
			ans += 1
		out(ans)
	elif ind > 0:
		ans = 0
		for i in l[1:]:
			ans += i // 3
		out(ans)
	else:
		ans = 0
		for i in l:
			ans += i // 3
		out(ans)


import os, sys
from io import BytesIO, IOBase

def main():
	for _ in range(int(input())):
		n = int(input())
		s = list(input())
		state2 = {'RR': -1, 'LR': -1, 'LL': -1, 'RL': -1}
		ans = 10 ** 10
		for x in state2:
			state = state2.copy()
			state[x] = (x[0] != s[0]) + (x[1] != s[1])
			for i in range(2, n):
				state1 = state.copy()
				if state['RR'] != -1 or state['LR'] != -1:
					state1['RL'] = min(10 ** 10 if state['RR'] == -1 else state['RR'], 10 ** 10 if state['LR'] == -1 else state['LR']) + (s[i] != 'L')
				else:
					state1['RL'] = -1
				state1['RR'] = -1 if state['LR'] == -1 else state['LR'] + (s[i] != 'R')
				if state['RL'] != -1 or state['LL'] != -1:
					state1['LR'] = min(10 ** 10 if state['LL'] == -1 else state['LL'], 10 ** 10 if state['RL'] == -1 else state['RL']) + (s[i] != 'R')
				else:
					state1['LR'] = -1
				state1['LL'] = -1 if state['RL'] == -1 else state['RL'] + (s[i] != 'L')
				state = state1.copy()
			mini = 10 ** 10
			for d in state:
				y = d[1] + x[0]
				if state[d] != -1 and (d == 'LR' or d == 'RL' or d[1] != x[0]) and (y == 'LR' or y == 'RL' or y[1] != x[1]):
					mini = min(mini, state[d])
			ans = min(ans, mini)
		print(ans)
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')
main()


for _ in range(int(input())):
	n = int(input())
	s = input()
	l = []
	x = 0
	f = 0
	for i in s:
		if i == 'L':
			if f == 0:
				x += 1
			else:
				f = 0
				if x > 0:
					l.append(x)
				x = 1
		elif f == 1:
			x += 1
		else:
			f = 1
			if x > 0:
				l.append(x)
			x = 1
	if x > 0:
		l.append(x)
	if len(s) > 0:
		if s[0] == s[-1]:
			if len(l) != 1:
				x = l.pop(-1)
				l[0] += x
	ans = 0
	for i in l:
		ans += i // 3
	if len(l) == 1 and l[0] > 3:
		ans = 1 + (l[0] - 1) // 3
	print(ans)


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)


import math
import bisect
import copy
import time
from collections import Counter
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)


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


t = int(input())

def rle(s):
	nums = []
	(tmp, count, ans) = (s[0], 1, '')
	for i in range(1, len(s)):
		if tmp == s[i]:
			count += 1
		else:
			nums.append(count)
			tmp = s[i]
			count = 1
	nums.append(count)
	if s[-1] == s[0] and len(nums) >= 2:
		nums[0] += nums[-1]
		nums.pop(-1)
	elif len(nums) == 1:
		nums[0] += 2
	return nums

def calc(nums):
	return sum((nu // 3 for nu in nums))
for _ in range(t):
	n = input()
	s = input()
	rs = rle(s)
	print(calc(rs))


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
import math

def gift():
	for _ in range(t):
		n = int(input())
		lst = list(input())
		ans = 0
		leftP = []
		rightP = []
		for i in range(n):
			if i == 0:
				left = lst[-1]
			else:
				left = lst[i - 1]
			if i == n - 1:
				right = lst[0]
			else:
				right = lst[i + 1]
			if left == 'L':
				if right == 'L':
					if lst[i] != 'R':
						ans += 1
						leftP.append(i)
			elif right == 'R':
				if lst[i] != 'L':
					ans += 1
					rightP.append(i)

			def checkPart(param, n):
				if not param:
					return 0
				res = 0
				consectlen = 1
				(s, e) = (0, len(param) - 1)
				if e == s:
					return 1
				tempe = e
				checked = False
				while s < e and s < tempe:
					if param[s] == 0 and param[e] == n - 1 and (not checked):
						if param[tempe] - 1 == param[tempe - 1]:
							tempe -= 1
							consectlen += 1
						else:
							tempe -= 1
							e = tempe
							consectlen += 1
							checked = True
					elif param[s] + 1 == param[s + 1]:
						s += 1
						consectlen += 1
					else:
						res += math.ceil(consectlen / 3)
						s += 1
						consectlen = 1
				res += math.ceil(consectlen / 3)
				return res
		yield (checkPart(leftP, n) + checkPart(rightP, n))
t = int(input())
ans = gift()
print(*ans, sep='\n')


from itertools import groupby

def solve():
	n = int(input())
	s = input()
	if all((i == s[0] for i in s)):
		print((n + 2) // 3)
		return
	if s[0] == s[-1]:
		if s[0] == 'R':
			idx = s.index('L')
			s = s[idx:] + 'R' * idx
		else:
			idx = s.index('R')
			s = s[idx:] + 'L' * idx
	res = 0
	for (_, v) in groupby(s):
		sz = len(list(v))
		res += sz // 3
	print(res)
for _ in range(int(input())):
	solve()


from math import ceil
for _ in range(int(input())):
	n = int(input())
	s = list(input())
	c = 0
	while n > 0 and s[0] == s[-1]:
		c += 1
		s.pop()
		n -= 1
	if n == 0:
		print(int(ceil(c / 3)))
	else:
		ans = 0
		for i in range(n):
			c += 1
			if i == n - 1 or s[i] != s[i + 1]:
				ans += c // 3
				c = 0
		print(ans)


test_cases = int(input())
for test_case in range(test_cases):
	n = int(input())
	s = input()
	ans = 0
	count = 1
	c = []
	for i in range(len(s) - 1):
		if s[i] == s[i + 1]:
			count += 1
		else:
			c.append(count)
			count = 1
	c.append(count)
	if s[0] == s[-1] and c[0] != n:
		c[0] = c[0] + c[-1]
		c.pop()
	if c[0] != n:
		for i in c:
			ans += i // 3
	else:
		ans += (c[0] + 2) // 3
	print(ans)


import os
import heapq
import sys
import math
import operator
from collections import defaultdict, deque
from io import BytesIO, IOBase

def inpt():
	return [int(k) for k in input().split()]

def main():
	for _ in range(int(input())):
		n = int(input())
		s = input()
		ar = list(s)
		br = list(s)
		ans = 0
		ct = 1
		ok = True
		for i in range(n - 2, -1, -1):
			if ar[i] != ar[i + 1]:
				ok = False
				prev = ar[i + 1]
			else:
				ct += 1
			if not ok:
				for j in range(i + 1):
					if prev != ar[j]:
						prev = ar[j]
						if ct > 1:
							ans += ct // 3
						ct = 1
					else:
						ct += 1
				if ct > 1:
					ans += ct // 3
				ct = 0
				break
		if ct > 1:
			ans = ct // 3 + (1 if ct % 3 else 0)
		print(ans)
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')
main()


from sys import stdin, stdout
from collections import Counter

def find2(arr, N, start, S, cnt):
	k = 0
	if start == [] and ('L' not in S or 'R' not in S):
		return -(-N // 3)
	for i in start:
		a = arr[i]
		b = arr[a]
		c = arr[b]
		d = arr[c]
		if b == i:
			continue
		if c == a:
			continue
		if d == b:
			cnt[b] -= 1
			cnt[i] += 1
			arr[a] = i
		else:
			cnt[c] -= 1
			cnt[a] += 1
			arr[b] = a
		if cnt[c] == 0:
			start.append(c)
		if cnt[b] == 0:
			start.append(b)
		k += 1
	return k

def find(S, N):
	arr = [None for _ in range(N)]
	for i in range(len(S)):
		if S[i] == 'L':
			arr[i] = (i - 1) % N
		if S[i] == 'R':
			arr[i] = (i + 1) % N
	cnt = Counter(arr)
	start = []
	for i in range(len(S)):
		if cnt[i] == 0:
			start.append(i)
	return find2(arr, N, start, S, cnt)

def main():
	for _ in range(int(stdin.readline())):
		N = int(stdin.readline())
		S = stdin.readline().strip()
		print(find(S, len(S)))
main()


import sys
import heapq, functools, collections
import math, random
from collections import Counter, defaultdict

def solve(arr):
	console('----- solving ------')
	console(arr)
	minres = 10 ** 9
	for i in range(min(4, len(arr))):
		minres = min(minres, solve2(arr[i:] + arr[:i]))
	return minres

def diff(given, desired):
	return sum([a != b for (a, b) in zip(given, desired)])

def solve2(arr):
	console(arr)
	arr = list(arr)
	dp = [0] + [10 ** 9 for _ in arr]
	for i in range(len(arr)):
		if i + 4 <= len(arr):
			dp[i + 4] = min(dp[i + 4], dp[i] + diff('RRLL', arr[i:i + 4]))
		if i + 3 <= len(arr):
			dp[i + 3] = min(dp[i + 3], dp[i] + diff('RLL', arr[i:i + 3]))
			dp[i + 3] = min(dp[i + 3], dp[i] + diff('RRL', arr[i:i + 3]))
		if i + 2 <= len(arr):
			dp[i + 2] = min(dp[i + 2], dp[i] + diff('RL', arr[i:i + 2]))
	return dp[-1]

def console(*args):
	print('\x1b[36m', *args, '\x1b[0m', file=sys.stderr)
	return
lines = sys.stdin.readlines()
for case_num in range(int(lines[0])):
	strr = lines[2 + 2 * case_num].strip()
	res = solve(strr)
	print(res)


def main():
	(n,) = map(int, input().split(' '))
	s = list(input())
	ss = set(s)
	if len(ss) == 1:
		return (len(s) - 1) // 3 + 1
	while s[0] == s[-1]:
		s = s[1:] + s[:1]
	z = 0
	pred = s[0]
	count = 1
	for i in range(1, n):
		if s[i] == pred:
			count += 1
		else:
			z += count // 3
			count = 1
			pred = s[i]
	z += count // 3
	return z
for _ in range(int(input())):
	print(main())


t = int(input())
for i in range(t):
	n = int(input())
	a = input()
	j = 0
	ans = 0
	while j < n - 1 and a[j] == a[j + 1]:
		j += 1
	if j != n - 1:
		a1 = a[0:j + 1]
		a2 = a[j + 1:n]
		a = a2 + a1
		j = 0
		while j < n - 1:
			s = 0
			while j < n - 1 and a[j] == a[j + 1]:
				j += 1
				s += 1
			ans += (s + 1) // 3
			j += 1
	else:
		ans = (n + 2) // 3
	print(ans)


t = int(input())
while t != 0:
	input()
	s = input()
	i = 0
	mod_s = ''
	b = 0
	for (i, c) in enumerate(s):
		if c != s[-1]:
			b = i
			break
	s = s[b:] + s[0:b]
	last = s[0]
	f = 1
	ans = 0
	for c in s[1:]:
		if c == last:
			f += 1
		else:
			ans += f // 3
			last = c
			f = 1
	ans += f // 3
	if f == len(s) and len(s) % 3 != 0:
		ans += 1
	print(ans)
	t -= 1


for nt in range(int(input())):
	n = int(input())
	s = input()
	if 'L' not in s or 'R' not in s:
		if n <= 2:
			print(0)
		else:
			print((n + 2) // 3)
		continue
	g = []
	curr = s[0]
	count = 1
	for i in range(1, n):
		if s[i] == curr:
			count += 1
		else:
			curr = s[i]
			g.append(count)
			count = 1
	g.append(count)
	if s[0] != s[-1]:
		ans = 0
		for i in g:
			ans += i // 3
		print(ans)
	else:
		g[0] += g[-1]
		g[-1] = 0
		ans = 0
		for i in g:
			ans += i // 3
		print(ans)


from math import ceil
for _ in range(int(input())):
	n = int(input())
	s = list(input())
	l = [0 for _ in range(n)]
	boolList = [True for _ in range(n)]
	comp = {'R': 'L', 'L': 'R'}
	for i in range(n):
		if s[i] == 'R':
			l[(i + 1) % n] += 1
		else:
			l[(i - 1) % n] += 1
	for i in range(n):
		if l[i] == 1:
			val = -1 if s[i] == 'L' else 1
			if s[(i + val) % n] != comp[s[i]]:
				boolList[i] = False
	(first, tot, curr) = (None, 0, 0)
	for i in range(n):
		if boolList[i] == True:
			if first == None:
				first = i
			tot += ceil(curr / 3)
			curr = 0
		elif first != None:
			curr += 1
	if first == None:
		tot = ceil(n / 3)
	else:
		tot += ceil((curr + first) / 3)
	print(tot)


import math
for _ in range(int(input())):
	length = int(input())
	n = length
	s = input()
	first = s[0]
	i = 0
	if n <= 2:
		print(s)
		continue
	while i != -length and s[i - 1] == first:
		i -= 1
		n -= 1
	if i == -length:
		print(math.ceil(length / 3))
		continue
	sum = 0
	while i < n:
		count = 0
		first = s[i]
		while i < n and s[i] == first:
			count += 1
			i += 1
		sum += count // 3
	print(sum)


import math
T = int(input())
for i in range(T):
	n = int(input())
	a = list(input())
	if a.count(a[0]) == n:
		print(1 + (n - 1) // 3)
		continue
	good = ['RRL', 'RLL', 'LRR', 'LLR', 'RLR', 'LRL']
	ans = 0
	cv = a[0]
	c = 0
	f = 1
	fc = 0
	for i in range(n):
		if a[i] == cv:
			c += 1
		else:
			if not f:
				ans += c // 3
			else:
				f = 0
				fc = c
			c = 1
			cv = a[i]
	if a[0] == a[-1]:
		ans += (fc + c) // 3
	else:
		ans += c // 3
		ans += fc // 3
	print(ans)


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')

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())

def print_list(l):
	print(' '.join(map(str, l)))
for _ in range(N()):
	n = N()
	s = input()
	if len(set(s)) == 1:
		print(1 + (n - 1) // 3)
	else:
		data = []
		tmp = 1
		for i in range(1, n):
			if s[i] != s[i - 1]:
				data.append(tmp)
				tmp = 1
			else:
				tmp += 1
		if s[0] == s[-1]:
			data[0] += tmp
		else:
			data.append(tmp)
		res = 0
		for i in data:
			res += i // 3
		print(res)


import sys

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 19
MOD = 10 ** 9 + 7

def RLE(data):
	from itertools import groupby
	return [[x, len(list(grp))] for (x, grp) in groupby(data)]
for _ in range(INT()):
	N = INT()
	S = input()
	rle = RLE(S)
	if len(rle) == 1:
		ans = ceil(N, 3)
		print(ans)
		continue
	if rle[0][0] == rle[-1][0]:
		rle[0][1] += rle[-1][1]
		rle.pop()
	ans = 0
	for (k, v) in rle:
		ans += v // 3
	print(ans)


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

	def helper(i, st):
		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
	memo = [[[-1 for _ in range(2)] for __ in range(n)] for ___ in range(4)]

	def dp(t, i, a):
		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')
				elif 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')
				elif 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')
			elif i == n - 1 and a == 0 and (t == 1):
				memo[t][i][a] = dp(t, i - 1, 1) + helper(i, 'L')
			elif i == n - 1 and a == 1 and (t == 3):
				memo[t][i][a] = float('inf')
			elif i == n - 1 and a == 1 and (t == 2):
				memo[t][i][a] = dp(t, i - 1, 0) + helper(i, 'R')
			elif a == 0:
				memo[t][i][a] = min(dp(t, i - 1, 1) + helper(i, 'L'), dp(t, i - 2, 1) + helper(i, 'LL'))
			else:
				memo[t][i][a] = min(dp(t, i - 1, 0) + helper(i, 'R'), dp(t, i - 2, 0) + helper(i, 'RR'))
		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(res)


from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	n = int(input())
	s = input()[:-1]
	x = 0
	c = 0
	ans = 0
	if s[0] == 'L':
		s = s.strip('L')
		if not s:
			print((n + 2) // 3)
			continue
	else:
		s = s.strip('R')
		if not s:
			print((n + 2) // 3)
			continue
	ans += (n - len(s)) // 3
	for i in s:
		if c:
			if i == 'L':
				x += 1
			else:
				ans += x // 3
				c ^= 1
				x = 1
		elif i == 'R':
			x += 1
		else:
			ans += x // 3
			c ^= 1
			x = 1
	ans += x // 3
	print(ans)


import sys
input = sys.stdin.readline
t = int(input())
for tests in range(t):
	n = int(input())
	S = input().strip()
	if n == 3:
		if S[0] == S[1] == S[2]:
			print(1)
		else:
			print(0)
		continue
	if 'LR' in S:
		ind = S.index('LR')
		S = S[ind + 1:] + S[:ind + 1]
	S += 'LLLL'
	DP = [1 << 30] * (n + 6)
	DP[0] = 0
	for i in range(n):
		if S[i] == 'R' and S[i + 1] == 'R' and (S[i + 2] == 'R') and (S[i + 3] == 'R'):
			DP[i + 2] = min(DP[i + 2], DP[i] + 1)
			DP[i + 3] = min(DP[i + 3], DP[i] + 1)
			DP[i + 4] = min(DP[i + 4], DP[i] + 2)
		elif S[i] == 'R' and S[i + 1] == 'R' and (S[i + 2] == 'R') and (S[i + 3] == 'L'):
			DP[i + 2] = min(DP[i + 2], DP[i] + 1)
			DP[i + 3] = min(DP[i + 3], DP[i] + 1)
			DP[i + 4] = min(DP[i + 4], DP[i] + 1)
		elif S[i] == 'R' and S[i + 1] == 'R' and (S[i + 2] == 'L') and (S[i + 3] == 'R'):
			DP[i + 2] = min(DP[i + 2], DP[i] + 1)
			DP[i + 3] = min(DP[i + 3], DP[i])
			DP[i + 4] = min(DP[i + 4], DP[i] + 1)
		elif S[i] == 'R' and S[i + 1] == 'R' and (S[i + 2] == 'L') and (S[i + 3] == 'L'):
			DP[i + 2] = min(DP[i + 2], DP[i] + 1)
			DP[i + 3] = min(DP[i + 3], DP[i])
			DP[i + 4] = min(DP[i + 4], DP[i])
		elif S[i] == 'R' and S[i + 1] == 'L' and (S[i + 2] == 'R') and (S[i + 3] == 'R'):
			DP[i + 2] = min(DP[i + 2], DP[i])
			DP[i + 3] = min(DP[i + 3], DP[i] + 1)
			DP[i + 4] = min(DP[i + 4], DP[i] + 1)
		elif S[i] == 'R' and S[i + 1] == 'L' and (S[i + 2] == 'R') and (S[i + 3] == 'L'):
			DP[i + 2] = min(DP[i + 2], DP[i])
			DP[i + 3] = min(DP[i + 3], DP[i] + 1)
			DP[i + 4] = min(DP[i + 4], DP[i])
		elif S[i] == 'R' and S[i + 1] == 'L' and (S[i + 2] == 'L') and (S[i + 3] == 'R'):
			DP[i + 2] = min(DP[i + 2], DP[i])
			DP[i + 3] = min(DP[i + 3], DP[i])
			DP[i + 4] = min(DP[i + 4], DP[i] + 2)
		elif S[i] == 'R' and S[i + 1] == 'L' and (S[i + 2] == 'L') and (S[i + 3] == 'L'):
			DP[i + 2] = min(DP[i + 2], DP[i])
			DP[i + 3] = min(DP[i + 3], DP[i])
			DP[i + 4] = min(DP[i + 4], DP[i] + 1)
		elif S[i] == 'L' and S[i + 1] == 'R' and (S[i + 2] == 'R') and (S[i + 3] == 'R'):
			DP[i + 2] = min(DP[i + 2], DP[i] + 2)
			DP[i + 3] = min(DP[i + 3], DP[i] + 2)
			DP[i + 4] = min(DP[i + 4], DP[i] + 3)
		elif S[i] == 'L' and S[i + 1] == 'R' and (S[i + 2] == 'R') and (S[i + 3] == 'L'):
			DP[i + 2] = min(DP[i + 2], DP[i] + 2)
			DP[i + 3] = min(DP[i + 3], DP[i] + 2)
			DP[i + 4] = min(DP[i + 4], DP[i] + 2)
		elif S[i] == 'L' and S[i + 1] == 'R' and (S[i + 2] == 'L') and (S[i + 3] == 'R'):
			DP[i + 2] = min(DP[i + 2], DP[i] + 2)
			DP[i + 3] = min(DP[i + 3], DP[i] + 1)
			DP[i + 4] = min(DP[i + 4], DP[i] + 2)
		elif S[i] == 'L' and S[i + 1] == 'R' and (S[i + 2] == 'L') and (S[i + 3] == 'L'):
			DP[i + 2] = min(DP[i + 2], DP[i] + 2)
			DP[i + 3] = min(DP[i + 3], DP[i] + 1)
			DP[i + 4] = min(DP[i + 4], DP[i] + 1)
		elif S[i] == 'L' and S[i + 1] == 'L' and (S[i + 2] == 'R') and (S[i + 3] == 'R'):
			DP[i + 2] = min(DP[i + 2], DP[i] + 1)
			DP[i + 3] = min(DP[i + 3], DP[i] + 2)
			DP[i + 4] = min(DP[i + 4], DP[i] + 2)
		elif S[i] == 'L' and S[i + 1] == 'L' and (S[i + 2] == 'R') and (S[i + 3] == 'L'):
			DP[i + 2] = min(DP[i + 2], DP[i] + 1)
			DP[i + 3] = min(DP[i + 3], DP[i] + 2)
			DP[i + 4] = min(DP[i + 4], DP[i] + 1)
		elif S[i] == 'L' and S[i + 1] == 'L' and (S[i + 2] == 'L') and (S[i + 3] == 'R'):
			DP[i + 2] = min(DP[i + 2], DP[i] + 1)
			DP[i + 3] = min(DP[i + 3], DP[i] + 1)
			DP[i + 4] = min(DP[i + 4], DP[i] + 3)
		elif S[i] == 'L' and S[i + 1] == 'L' and (S[i + 2] == 'L') and (S[i + 3] == 'L'):
			DP[i + 2] = min(DP[i + 2], DP[i] + 1)
			DP[i + 3] = min(DP[i + 3], DP[i] + 1)
			DP[i + 4] = min(DP[i + 4], DP[i] + 2)
	print(DP[n])


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')
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
	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]
	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())
	s = input()
	print(find(s))


def subsolve(a):
	n = len(a)
	dp = [[0] * 4 for _ in range(n + 1)]
	if a[0] == 'R':
		dp[0] = [0, n, n, n]
	else:
		dp[0] = [1, n, n, n]
	for i in range(1, n):
		dp[i][0] = min(dp[i - 1][2], dp[i - 1][3]) + int(a[i] != 'R')
		dp[i][1] = min(dp[i - 1][0], n) + int(a[i] != 'R')
		dp[i][2] = min(dp[i - 1][0], dp[i - 1][1]) + int(a[i] != 'L')
		dp[i][3] = min(dp[i - 1][2], n) + int(a[i] != 'L')
	return min(dp[n - 1][2], dp[n - 1][3], n)

def solve():
	n = int(input())
	a = input()
	a += a
	ans = n
	for i in range(min(n, 4)):
		ans = min(ans, subsolve(a[i:i + n]))
	print(ans)
t = int(input())
for _ in range(t):
	solve()


def main():
	n = int(input())
	s = input()
	ans = 0
	if s == 'L' * n:
		s = s[:-1] + 'R'
		ans += 1
	if s == 'R' * n:
		s = s[:-1] + 'L'
		ans += 1
	i = s.find('LR')
	if i != -1:
		s = s[i + 1:] + s[:i + 1]
	i = 0
	while 0 <= i < n:
		j = s.find('L', i)
		k = s.find('R', j)
		if k == -1:
			k = n
		count1 = j - i
		count2 = k - j
		ans += count1 // 3 + count2 // 3
		i = k
	print(ans)
t = int(input())
for _ in range(t):
	main()


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)


import math
for _ in range(int(input())):
	n = int(input())
	s = input()
	c = 1
	res = 0
	bb = n - 1
	piu = 12
	if s.count('R') == n or s.count('R') == 0:
		print(math.ceil(n / 3))
	else:
		while s[bb] == s[0]:
			c += 1
			bb -= 1
		for i in range(1, bb + 1):
			if s[i] == s[i - 1]:
				c += 1
			else:
				res += c // 3
				c = 1
		if c > 0:
			res += c // 3
		print(res + 12 - piu)


import os
import sys
import math
import heapq
from decimal import *
from io import BytesIO, IOBase
from collections import defaultdict, deque

def r():
	return int(input())

def rm():
	return map(int, input().split())

def rl():
	return list(map(int, input().split()))
for _ in range(r()):
	n = r()
	a = input()
	ans = True
	for i in range(1, n):
		if a[i] != a[i - 1]:
			ans = False
	if ans:
		if n <= 2:
			print(0)
		elif n == 3:
			print(1)
		else:
			print((n + 2) // 3)
	else:
		b = [1]
		for i in range(1, n):
			if a[i] == a[i - 1]:
				b[-1] += 1
			else:
				b.append(1)
		if a[0] == a[-1]:
			b[0] += b[-1]
			b = b[:-1]
		b.sort(reverse=True)
		i = 0
		cnt = 0
		while i < len(b) and b[i] >= 3:
			cnt += b[i] // 3
			i += 1
		print(cnt)


import sys
input = sys.stdin.readline
(T,) = map(int, input().split())
for _ in range(T):
	(N,) = map(int, input().split())
	S = input().strip()
	S += '.'
	R = []
	c = 0
	b = '.'
	for i in range(N + 1):
		if S[i] == b:
			c += 1
		else:
			if b != '.':
				R.append(c)
			c = 1
		b = S[i]
	if len(R) == 1:
		print(1 + (R[0] - 1) // 3)
	else:
		if S[0] == S[N - 1]:
			R[0] += R[-1]
			R.pop()
		rr = 0
		for r in R:
			rr += r // 3
		print(rr)


t = int(input())
for i in range(t):
	n = int(input())
	s = list(input())
	ans = 0
	if len(set(s)) != 1:
		ind = 0
		for i in range(1, n):
			if s[i] != s[i - 1]:
				ind = i
				break
		S = s[ind:] + s[:ind]
		A = [1]
		for i in range(1, n):
			if S[i] != S[i - 1]:
				A.append(1)
			else:
				A[-1] += 1
		for i in range(len(A)):
			ans += A[i] // 3
	else:
		ans = 1
		n -= 1
		ans += n // 3
	print(ans)


import math
t = int(input())
for i in range(t):
	n = int(input())
	s = str(input())
	lis = []
	x = 0
	y = 0
	ind = 0
	for j in range(n):
		if s[j] != s[0]:
			ind = j
			break
	if ind != 0:
		s = s[ind:] + s[:ind]
	for j in range(n):
		if s[j] == 'L':
			if y > 2:
				lis.append(y)
			x += 1
			y = 0
		if s[j] == 'R':
			if x > 2:
				lis.append(x)
			y += 1
			x = 0
	if x > 2:
		lis.append(x)
	if y > 2:
		lis.append(y)
	ans = 0
	l = len(lis)
	if l == 1 and lis[0] == n:
		print(math.ceil(n / 3))
	else:
		for j in range(l):
			ans += math.floor(lis[j] / 3)
		print(ans)