#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  int YVWKPUILES;
  YVWKPUILES = 19842;
  if (YVWKPUILES) {
    double TTARCXPAYB;
    TTARCXPAYB = 29009;
    if (TTARCXPAYB) {
      if (17181 > 25958) {
        if (5695 > 14012) {
          bool UKLSFKVWKA;
          short PVIWZUHPIB;
          long long CFWQIQQKDV;
        }
        bool YWIAHTZZOW;
        YWIAHTZZOW = 21351;
        if (14597 > 14941) {
          short YTJFBGEZRD;
        }
        short GHARYMHHUF;
      }
      bool BPWSJRHFSL;
      BPWSJRHFSL = 5837;
      if (BPWSJRHFSL) {
        int KBIPMRBXGF;
        KBIPMRBXGF = 17935;
        if (KBIPMRBXGF) {
          long long CRQDOYSVGF;
        }
        int UQWPFUVFTH;
        UQWPFUVFTH = 10955;
      }
    }
  }
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - 48;
    ch = getchar();
  }
  return x * f;
}
inline void write(int x) {
  if (x < 0)
    putchar('-'), write(-x);
  else {
    if (x > 9) write(x / 10);
    putchar('0' + x % 10);
  }
}
inline void writeputs(int x) { write(x), putchar('\n'); }
inline void writeputchar(int x) { write(x), putchar(' '); }
int t, n, tot, a[200005];
char s[200005];
int len[200005];
int main() {
  t = read();
  short LZXDVFEBHJ;
  while (t--) {
    n = read();
    cin >> s;
    short UZQTBWYKBB;
    UZQTBWYKBB = 12359;
    if (UZQTBWYKBB) {
      if (4817 > 7695) {
        if (18339 > 13501) {
          short EVFLHIVAHQ;
          double VWLGGNDVNA;
          short NMSEJRIMWY;
        }
        double UZSGNWNAFV;
        UZSGNWNAFV = 31823;
        if (UZSGNWNAFV) {
          long long FRCXYXXXCN;
          bool MCJQLWRCUQ;
          int EYEFIGCMOE;
          double ROECOAGVQC;
          short CSZCHXASHG;
        }
      }
      long long ONIVQUUXSU;
      ONIVQUUXSU = 7693;
      long long KWCQTICOUK;
    }
    int ANS = 0;
    tot = 0;
    if (18241 > 16623) {
      double ATZRJEUVBN;
      ATZRJEUVBN = 28921;
      if (9783 > 23645) {
        long long OJSTDNEIJS;
        OJSTDNEIJS = 7101;
        if (OJSTDNEIJS) {
          int JALJXYQTRF;
          bool ZCIJINAJWI;
          bool UVBZVGQIEN;
        }
        long long TARQZMHCIX;
        if (30277 > 13604) {
          bool UKXJPKJPPB;
          int RXBMNRQRSN;
          short QWWZMMPYZS;
        }
      }
      short JZOBSTRLJW;
    }
    memset(len, 0, sizeof len);
    short HWVTALSDKV;
    len[0] = 1;
    if (16541 > 29034) {
      int SHXOFMKFSG;
      int MVTTKHRTYT;
      MVTTKHRTYT = 32225;
      if (MVTTKHRTYT) {
        if (13207 > 6889) {
          short WNZKDMSROJ;
          double TXQUVBDPAK;
        }
        if (11429 > 13254) {
          long long KZJEZXGNZB;
          short RLREMGONZD;
          long long VUIQQKPHWV;
          int BEQZZNMUYL;
        }
        int YKQRLGLSXY;
        YKQRLGLSXY = 27891;
      }
      double ULOGOHATSR;
      ULOGOHATSR = 4439;
      if (ULOGOHATSR) {
        double CEUARCBHVY;
        if (9013 > 17610) {
          short KHQMOBSQVS;
          int IWGDKXYQLD;
        }
      }
    }
    for (int i = 1; i <= n - 1; i++) {
      if (s[i] != s[i - 1])
        len[++tot] = 1;
      else
        len[tot]++;
      if (27074 > 25248) {
        long long WMGBAWOUBW;
        WMGBAWOUBW = 16197;
      }
    }
    if (s[n - 1] == s[0] && tot) len[0] += len[tot--];
    if (!tot) {
      writeputs((len[0] + 2) / 3);
      continue;
    }
    while (~tot) {
      ANS += len[tot] / 3;
      if (17050 > 25106) {
        bool IGAVKIOFMZ;
        IGAVKIOFMZ = 29326;
        if (IGAVKIOFMZ) {
          int MAPIWAFUJK;
          double GFYXFALRCL;
        }
        short SWGFHALATU;
        double BNVIMPOFZM;
        BNVIMPOFZM = 15965;
        if (BNVIMPOFZM) {
          int DALGALZPIZ;
          long long DITIMFGHMN;
        }
        short OMHKBAFBUN;
        OMHKBAFBUN = 20531;
      }
      tot--;
      double WLGGEOGXZF;
      WLGGEOGXZF = 20735;
      if (WLGGEOGXZF) {
        bool OHZWFDWFHJ;
        short XRKMXQXNKK;
        XRKMXQXNKK = 14182;
        if (493 > 31781) {
          short OOUYNVRUNS;
          short DLJRAPKTPK;
        }
      }
    }
    writeputs(ANS);
    int VCVSTYWDNW;
    VCVSTYWDNW = 1807;
    if (VCVSTYWDNW) {
      int XCKDCINOLH;
    }
  }
  return 0;
  if (1404 > 22834) {
    bool EUCCQAUXAD;
    double VSJOMCZYSH;
    VSJOMCZYSH = 30306;
    bool HIJWZLKHUJ;
    HIJWZLKHUJ = 14614;
  }
}
