#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,popcnt,tune=native")
#pragma GCC target("avx,avx2")
const int MAX_NUM = 1e9 + 33;
void solve();
signed main() {
  long long t;
  cin >> t;
  while (t--) solve();
}
void solve() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  s += s;
  char first = s[0];
  int st = -1;
  for (int i = 1; i < n; i++) {
    if (s[i] != s[0]) {
      st = i;
      break;
    }
  }
  if (st == -1) {
    cout << ceil(double(n) / 3) << "\n";
    return;
  }
  long long ans = 0;
  char prev = first;
  int p_length = 0;
  for (int i = st; i < n + st; i++) {
    if (s[i] != prev) {
      if (p_length) ans += ceil(double(p_length + 1) / 3) - 1;
      prev = s[i];
      p_length = 1;
      continue;
    }
    p_length++;
  }
  ans += ceil(double(p_length + 1) / 3) - 1;
  cout << ans << "\n";
}
