#include <bits/stdc++.h>
using namespace std;
int main() {
  int test;
  cin >> test;
  while (test--) {
    int n;
    cin >> n;
    string s;
    cin >> s;
    int moves = 0;
    int i = 0;
    while (s[0] == s[i] && i < n) i++;
    if (i == n) {
      moves = (n - 1) / 3 + 1;
    } else {
      int stop = i;
      int counter = 1;
      i++;
      while (i != stop) {
        if (i > 0 && s[i] == s[i - 1] || i == 0 && s[i] == s[n - 1])
          counter++;
        else {
          moves += counter / 3;
          counter = 1;
        }
        if (i < n - 1)
          i++;
        else
          i = 0;
      }
      if (counter != 1) moves += counter / 3;
    }
    cout << moves << endl;
  }
}
