#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int x;
    cin >> x;
    string s;
    cin >> s;
    bool uniform = true;
    for (int i = 1; i < s.size(); i++) {
      if (s[i] != s[i - 1]) {
        uniform = false;
        break;
      }
    }
    if (uniform) {
      cout << (s.size() + 2) / 3 << endl;
      continue;
    }
    int pref = 0;
    while (s[pref] == s[s.size() - 1]) {
      pref++;
    }
    string ns = s.substr(pref, s.size() - pref) + s.substr(0, pref);
    int ans = 0;
    int l = 0;
    for (int r = 0; r < ns.size(); r++) {
      if (r < s.size() - 1 && ns[r] == ns[r + 1]) continue;
      ans += (r - l + 1) / 3;
      l = r + 1;
    }
    cout << ans << endl;
  }
}
