#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 2;
long long inf = 1ll << 60;
void solve() {
  long long n, cnt = 1, ans = 0;
  vector<long long> v;
  string s;
  cin >> n >> s;
  for (long long i = 1; i < n; i++) {
    if (s[i] == s[i - 1])
      cnt++;
    else
      v.push_back(cnt), cnt = 1;
  }
  if (cnt == n) {
    v.push_back(cnt + 2);
  } else if (s[n - 1] == s[0])
    v[0] += cnt;
  else
    v.push_back(cnt);
  for (long long i : v) ans += i / 3;
  cout << ans << '\n';
}
int main() {
  long long t;
  cin >> t;
  while (t--) solve();
}
