#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
using namespace std;
const int N = 4e5 + 10;
const int M = 2e6 + 10;
const long long mod = 998244353;
inline int read() {
  int num = 0, neg = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-') neg = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    num = (num << 3) + (num << 1) + c - '0';
    c = getchar();
  }
  return num * neg;
}
int n, m;
vector<int> g[N];
int t[N];
int num[N];
int a[N];
char s[M];
int f[N][2];
int sum[N];
int u;
int main() {
  int T = read();
  while (T--) {
    u = 0;
    n = read();
    u++;
    scanf("%s", s + 1);
    u++;
    for (int i = 1; i <= n; i++) f[i][0] = f[i][1] = 1e9;
    int res1 = 0, res2 = 0;
    for (int i = 1; i <= n; i++) {
      if (s[i] == 'L')
        res1 = 1;
      else
        res2 = 1;
    }
    int ans = 0;
    if (!res1 || !res2) {
      cout << (n + 2) / 3 << '\n';
      continue;
    }
    if (res1 && res2) {
      int sum = 0, pos = n;
      if (s[1] == s[n]) {
        if (s[pos] == 'L')
          f[pos][1]++;
        else
          f[pos][0]++;
        ++sum;
        while (s[pos - 1] == s[pos]) --pos, ++sum, u++;
      }
      int ret = n;
      u++;
      if (f[pos][1] != f[pos][0]) u--;
      if (pos < n) ret = pos - 1;
      for (int i = 1; i <= ret; i++) {
        ++sum;
        if (s[i] == 'L')
          f[i][0]--;
        else
          f[i][1]++;
        if (f[i][0] < f[i][1]) u--;
        if (u < -1e9) {
          n++;
        }
        if (s[i] != s[i + 1]) ans += sum / 3, sum = 0;
      }
      cout << ans << '\n';
    }
  }
}
