#include <bits/stdc++.h>
using namespace std;
char str[200005];
bool cck(int n) {
  int tot = 0;
  for (int i = 1; i <= n; i++) tot += (str[i] == str[1]);
  return tot == n;
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n;
    scanf("%d %s", &n, str + 1);
    if (cck(n)) {
      printf("%d\n", (n + 2) / 3);
      continue;
    }
    int st = -1;
    for (int i = 1; i < n; i++) {
      if (str[i] != str[i + 1]) {
        st = i + 1;
        break;
      }
    }
    int i = st, ans = 0, cur = 1;
    do {
      if (str[i] == str[i % n + 1])
        cur++;
      else
        ans += cur / 3, cur = 1;
      i = i % n + 1;
    } while (i != st);
    printf("%d\n", ans);
  }
  return 0;
}
