#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cout << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
void solve() {
  long long int n;
  cin >> n;
  string s;
  cin >> s;
  for (long long int i = 0; i < n; i++) {
    if (s[0] != s[n - 1]) break;
    s += s[0];
    s.erase(s.begin());
  }
  long long int ct = 1;
  long long int ans = 0;
  for (long long int i = 1; i < n; i++) {
    if (s[i] != s[i - 1]) {
      ans += (ct / 3);
      ct = 1;
    } else {
      ct++;
    }
  }
  ans += (ct / 3);
  if (ct == n) {
    ans += (n % 3 != 0);
  }
  cout << ans << '\n';
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
