#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0, f = 1;
  char c = getchar();
  while (c != '-' && (c < '0' || c > '9')) c = getchar();
  if (c == '-') f = -1, c = getchar();
  while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  return f * x;
}
const long long N = 2e5 + 7;
const long long M = 3e5 + 7;
const long long MAXN = 2e18 + 7;
const long long Mod = 998244353;
long long _, i, j, k, n, m, p, q, s, T, t, l, r, o, u, v, w, x, y, z, ans, nex,
    num, st, en, sx, sy, tx, ty, len, th, ma, mi, mod, sum, res, flag, cas, ret,
    mid, cnt, now, tmp;
long long a[N], b[N], c[N];
char s1[M], s2[M], ch;
vector<long long> g, h;
string s3;
signed main() {
  ios::sync_with_stdio(false);
  long long T = 1;
  cin >> T;
  while (T--) {
    cin >> n;
    cin >> s1;
    for (i = 1; i < n; i++) {
      if (s1[i - 1] != s1[i]) break;
    }
    if (i == n) {
      printf("%lld\n", 1 + (n - 1) / 3);
    } else {
      ans = 0;
      j = i;
      while (1) {
        for (k = 1; k <= n; k++) {
          if (s1[(j + k) % n] != s1[j]) break;
        }
        ans = ans + k / 3;
        j = (j + k) % n;
        if (j == i) break;
      }
      printf("%lld\n", ans);
    }
  }
  return 0;
}
