#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 4e5 + 5;
char s[maxn];
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int n, pos = -1;
    scanf("%d%s", &n, s + 1);
    for (int i = 2; i <= n; ++i) {
      if (s[i] != s[i - 1]) pos = i;
    }
    if (pos == -1) {
      printf("%d\n", (n + 2) / 3);
    } else {
      int ans = 0;
      for (int i = 1; i <= n; ++i) s[i + n] = s[i];
      for (int i = pos, cnt = 0; i < n + pos; ++i) {
        if (s[i] == s[i + 1])
          cnt++;
        else {
          ans += (cnt + 1) / 3;
          cnt = 0;
        }
      }
      printf("%d\n", ans);
    }
  }
  return 0;
}
