#include <bits/stdc++.h>
using namespace std;
vector<long long> a;
long long n, m, k, x, y;
int main() {
  long long t, ans = 0;
  string s;
  cin >> t;
  while (t--) {
    cin >> n >> s;
    long long kr = 0;
    ans = 0;
    while (!s.empty() && s[0] == s.back()) {
      kr++;
      s.pop_back();
    }
    if (s.empty()) {
      if (s.empty()) {
        cout << (kr <= 3 ? kr / 3 : (kr + 2) / 3) << endl;
      }
    } else {
      s.push_back('X');
      for (long long i = 0; i < s.size() - 1; ++i) {
        kr++;
        if (s[i + 1] == s[i]) continue;
        ans += kr / 3;
        kr = 0;
      }
      cout << ans << endl;
    }
  }
  return 0;
};
