#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, ans = 0, cnt = 0;
    string s;
    cin >> n;
    cin >> s;
    while (s.size() && s[0] == s[n - 1]) {
      ++cnt;
      --n;
      s.pop_back();
    }
    if (s.empty()) {
      if (cnt <= 2) {
        cout << 0 << '\n';
        continue;
      }
      cout << (cnt + 2) / 3 << '\n';
      continue;
    }
    s.push_back('$');
    for (int i = 0; i + 1 < s.size(); ++i) {
      ++cnt;
      if (s[i] != s[i + 1]) {
        ans += cnt / 3;
        cnt = 0;
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
