#include <bits/stdc++.h>
using namespace std;
int t, n;
int a[200005];
int c[200005][2][2];
int f[200005][2][2];
char ch;
void inp() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf(" %c", &ch);
    if (ch == 'L')
      a[i] = 0;
    else
      a[i] = 1;
    for (int j = 0; j < 2; j++) {
      for (int k = 0; k < 2; k++) c[i][j][k] = false;
    }
  }
}
void init() {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 2; j++) {
      for (int k = 0; k < 2; k++) c[i][j][k] = false;
    }
  }
}
int tryy(int i, int m, int r) {
  if (i == n - 1) {
    if (r != a[0]) return n;
    int cnt = 0;
    if (m == 1) cnt++;
    if (a[1] == 0) cnt++;
    if (cnt == 2 || cnt == 0) return 0;
    if (a[0] == 1 && a[1] == 0) return 0;
    if (m == 1 && a[0] == 0) return 0;
    return n;
  }
  if (c[i][m][r]) return f[i][m][r];
  int tmp = n;
  if (m == 0) {
    if (r == 0) {
      tmp = min(tmp, tryy(i + 1, r, 1) + 1 - a[(i + 2) % n]);
    } else if (r == 1) {
      tmp = min(tmp, tryy(i + 1, r, 1) + 1 - a[(i + 2) % n]);
      tmp = min(tmp, tryy(i + 1, r, 0) + (0 != a[(i + 2) % n]));
    }
  } else if (m == 1) {
    if (r == 0) {
      tmp = min(tmp, tryy(i + 1, r, 1) + 1 - a[(i + 2) % n]);
      tmp = min(tmp, tryy(i + 1, r, 0) + (0 != a[(i + 2) % n]));
    } else if (r == 1) {
      tmp = min(tmp, tryy(i + 1, r, 0) + (0 != a[(i + 2) % n]));
    }
  }
  c[i][m][r] = true;
  f[i][m][r] = tmp;
  return tmp;
}
void proc() {
  a[0] = a[0];
  int ans = tryy(0, a[0], a[1]);
  init();
  a[1] = 1 - a[1];
  ans = min(ans, tryy(0, a[0], a[1]) + 1);
  a[0] = 1 - a[0];
  init();
  a[1] = 1 - a[1];
  ans = min(ans, tryy(0, a[0], a[1]) + 1);
  init();
  a[1] = 1 - a[1];
  ans = min(ans, tryy(0, a[0], a[1]) + 2);
  printf("%d\n", ans);
}
int main() {
  ios_base::sync_with_stdio(0);
  scanf("%d", &t);
  while (t--) {
    inp();
    proc();
  }
  return 0;
}
