#include <bits/stdc++.h>
using namespace std;
void oj() {}
void solve() {
  long long n;
  string s;
  cin >> n >> s;
  long long i;
  for (i = 1; i < n; i++)
    if (s[i] == s[i - 1])
      continue;
    else
      break;
  if (i == n) {
    cout << (n + 2) / 3 << endl;
    return;
  }
  long long pos;
  for (pos = n - 1; true; pos--)
    if (s[pos] != s[0]) break;
  long long ans = (i + n - 1 - pos) / 3;
  long long cnt = 1;
  for (; i <= pos; i++) {
    if (s[i] == s[i - 1])
      cnt++;
    else {
      ans += cnt / 3;
      cnt = 1;
    }
  }
  ans += cnt / 3;
  cout << ans << endl;
}
signed main() {
  oj();
  ios::sync_with_stdio(0);
  cin.tie(0);
  ;
  long long t;
  cin >> t;
  while (t--) solve();
  return 0;
}
