#include <bits/stdc++.h>
using namespace std;
char s[400050];
void work() {
  int n;
  scanf("%d%s", &n, s + 1);
  bool flag = 1;
  for (int i = 2; i <= n && (flag = (s[i] == s[i - 1])); i++)
    ;
  if (flag) return printf("%.0lf\n", ceil(1.0 * n / 3)), void();
  char* a;
  if (s[1] == s[n]) {
    a = s + 2;
    for (; a <= s + n; a++)
      if (*a != a[-1]) break;
    memcpy(s + n + 1, s + 1, (a - s - 1) * sizeof(char));
  } else
    a = s + 1;
  int ans = 0;
  a[n] = 0;
  for (int i = 1, j = 1; i <= n; i++) {
    if (a[i] == a[i - 1])
      j++;
    else
      ans += j / 3, j = 1;
  }
  printf("%d\n", ans);
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) work(), void();
  return 0;
}
