#include <bits/stdc++.h>
using namespace std;
const int mn = INT_MIN;
const int mx = INT_MAX;
const long long int bmod = 2305843009213693951;
const long long int mod = 1e9 + 9;
double poi = 3.141592653589793238463;
double absd(double v) { return (v >= 0) ? v : -v; }
void prt(long long int n) {
  while (n) {
    cout << (n & 1);
    n = n >> 1;
  }
  cout << '\n';
}
long long int pw(long long int a, long long int p, long long int m = mod) {
  long long int res = 1;
  while (p > 0) {
    if (p & 1) res = (res * a) % m;
    a = (a * a) % m;
    p >>= 1;
  }
  return res;
}
long long int gcd(long long int a, long long int b) {
  return (b == 0) ? a : gcd(b, a % b);
}
const int sz = (2e5) + 10;
int jada = 2e9;
vector<int> vst;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  long long int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    string s;
    cin >> s;
    vst.assign(n, 0);
    int curr = 0;
    int flg = 0;
    int ans = 0, val = 0;
    while (curr < 3 * n) {
      int c = curr % n;
      int p = (curr - 1 + n) % n;
      int nxt = (curr + 1) % n;
      if (!flg) {
        flg = (s[p] != s[c]);
      }
      if (flg) {
        vst[c] = 1;
        while (s[nxt] == s[c] && !vst[nxt]) {
          vst[nxt] = 1;
          c = (c + 1) % n;
          nxt = (c + 1) % n;
          val++;
          curr++;
        };
        ans += (val + 1) / 3;
        val = 0;
      }
      curr++;
    }
    if (!flg) ans = (n / 3) + (n % 3 != 0);
    cout << ans << '\n';
  }
}
