#include <bits/stdc++.h>
using namespace std;
const long long MX = 5e5 + 1;
const long long LL_MAX = 1e18 + 3;
const long long MOD = 1e9 + 7;
pair<long long, long long> DISP[8] = {{1, 0}, {0, 1},   {-1, 0}, {0, -1},
                                      {1, 1}, {-1, -1}, {1, -1}, {-1, 1}};
long long solve() {
  long long n;
  cin >> n;
  string s;
  cin >> s;
  if (n < 3) return 0;
  for (long long i = 0; i < n - 1; i++) {
    if (s[i] != s[i + 1]) {
      rotate(s.begin(), s.begin() + i + 1, s.end());
      break;
    }
  }
  long long ans = 0, ctr = 1;
  for (long long i = 1; i < n; i++) {
    if (s[i] == s[i - 1])
      ctr++;
    else {
      ans += ctr / 3;
      ctr = 1;
    }
  }
  if (ctr == n)
    return ceil(ctr / 3.0);
  else
    ans += ctr / 3;
  return ans;
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  cin >> t;
  while (t--) cout << solve() << '\n';
  return 0;
}
