#include <bits/stdc++.h>
using namespace std;
const int maxn = 411000;
char s[maxn];
int main() {
  int t, n;
  cin >> t;
  while (t--) {
    cin >> n;
    cin >> s + 1;
    int ans = 0;
    bool flag = true;
    for (int i = 2; i <= n; i++)
      if (s[i] != s[i - 1]) flag = false;
    if (flag) {
      cout << (n + 2) / 3 << '\n';
    } else {
      int st = 1;
      while (s[n] == s[st]) {
        n++;
        s[n] = s[st];
        st++;
      }
      int c = 1;
      for (int i = st; i <= n; i++) {
        if (s[i] != s[i - 1]) {
          ans += (c / 3);
          c = 1;
        } else
          c++;
      }
      cout << ans + (c / 3) << '\n';
    }
  }
  return 0;
}
