#include <bits/stdc++.h>
using namespace std;
inline int incr(int);
int main() {
  int t;
  scanf("%d", &t);
  for (int i = 0; i < t; i++) {
    int n;
    int ans = 0;
    char s[200009];
    scanf("%d", &n);
    scanf("%s", s);
    int a = 0;
    int b = n - 1;
    while (a < (n - 1) && s[a] == s[a + 1]) {
      a++;
    }
    a++;
    while (b > 0 && s[b] == s[b - 1]) {
      b--;
    }
    b--;
    if (s[0] == s[n - 1]) {
      if (a < n) {
        ans += incr(a + n - b - 1);
      } else {
        ans = (n + 2) / 3;
      }
    } else {
      ans += incr(a);
      ans += incr(n - b - 1);
    }
    while (a < b) {
      int aa = a;
      while (a < (n - 1) && s[a] == s[a + 1]) {
        a++;
      }
      a++;
      ans += incr(a - aa);
      if (a < b) {
        int bb = b;
        while (b > 0 && s[b] == s[b - 1]) {
          b--;
        }
        b--;
        ans += incr(bb - b);
      }
    }
    cout << ans << endl;
  }
}
inline int incr(int x) { return x / 3; }
