#include<cstdio>

int main() {
	bool a = false, b = false, c = false;
	int score = 0;
	char event[10];
	int d;
	scanf_s("%d", &d, 10);
	for (int i = 0; i<d; i++) {
		score = 0;
		a = b = c = false;
		int out = 0;
		while (1) {
			scanf_s("%s", &event, 10);
			if (event[1] == 'I') {
				if (c)score++;
				c = b;
				b = a;
				a = true;
			}
			else if (event[1] == 'O') {
				score++;
				if (a)score++;
				if (b)score++;
				if (c)score++;
				a = b = c = false;
			}
			else out++;
			if (out == 3) {
				printf("%d\n", score);
				break;
			}
		}
	}
}
