#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    string s;
    cin >> s;
    long long flag = 1;
    for (long long i = 1; i < n; i++) {
      if (s[i] != s[i - 1]) flag = 0;
    }
    if (flag) {
      long long ans = (n / 3) + ((n % 3) != 0);
      cout << ans << '\n';
      continue;
    }
    long long cnt = 1;
    vector<long long> v;
    for (long long i = 1; i < n; i++) {
      if (s[i] == s[i - 1])
        cnt++;
      else {
        v.push_back(cnt);
        cnt = 1;
      }
    }
    if (s[0] == s[n - 1])
      v[0] += cnt;
    else
      v.push_back(cnt);
    long long ans = 0;
    for (long long it : v) ans += (it / 3);
    cout << ans << '\n';
  }
}
