using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Lummy
    {
        public static void Main(string[] args)
        {
            string strLine = Console.ReadLine();
            int intDateLength = int.Parse(strLine);
            int[ , ] intNum = new int[intDateLength, 9];
            string[ , ] strMark = new string[intDateLength, 9];
            for (int first = 0; first < intDateLength; first++)
            {
                strLine = Console.ReadLine();
                string[] strNumArr = strLine.Split(' ');
                for (int second = 0; second < strNumArr.Length; second++)
                {
                    intNum[first, second] = int.Parse(strNumArr[second]);
                }
                strLine = Console.ReadLine();
                string[] strTempMark = strLine.Split(' ');
                for (int second = 0; second < strTempMark.Length; second++)
                {
                    strMark[first, second] = strTempMark[second];
                }

            }

            //TestPrint(intDateLength, intNum, strMark);

            List<int> listCount = new List<int>();

            for (int first = 0; first < intDateLength; first++)
            {

                //ソート
                for (int second = 0; second < 9; second++)
                {
                    for (int third = 8; second < third; third--)
                    {
                        if (intNum[first, third] < intNum[first, second])
                        {
                            int intTemp = intNum[first, third];
                            string strTemp = strMark[first, third];
                            intNum[first, third] = intNum[first, second];
                            strMark[first, third] = strMark[first, second];
                            intNum[first, second] = intTemp;
                            strMark[first, second] = strTemp;
                        }
                    }
                }

                //TestPrint(intDateLength, intNum, strMark);

                int intCount = 0;
                List<int> listNGIndex = new List<int>();

                //判定
                for (int second = 0; second < 7; second++)
                {
                    bool bl2Roop = NGJuge(second, listNGIndex);
                    if (bl2Roop)
                    {
                        for (int third = second + 1; third < 8; third++)
                        {
                            bool bl3Roop = NGNextJuge(second, third, listNGIndex);
                            if (bl3Roop)
                            {
                                for (int forth = third + 1; forth < 9; forth++)
                                {
                                    //TestNG(listNGIndex);
                                    bool bl4Roop = NGAfterNextJuge(second, third, forth, listNGIndex);
                                    if (bl4Roop)
                                    {
                                        if (strMark[first, second].Equals(strMark[first, third]) &&
                                            strMark[first, second].Equals(strMark[first, forth]))
                                        {
                                            if (intNum[first, second] == intNum[first, third] &&
                                            intNum[first, second] == intNum[first, forth])
                                            {
                                                intCount++;
                                                listNGIndex.Add(second);
                                                listNGIndex.Add(third);
                                                listNGIndex.Add(forth);
                                                //TestNG(listNGIndex);
                                            }
                                            else if (intNum[first, second] == intNum[first, third] - 1 &&
                                                intNum[first, second] == intNum[first, forth] - 2)
                                            {
                                                intCount++;
                                                listNGIndex.Add(second);
                                                listNGIndex.Add(third);
                                                listNGIndex.Add(forth);
                                                //TestNG(listNGIndex);
                                            }
                                        }
                                        

                                    }
                                }
                            }
                            
                        }
                    }
                    
                }

                if (intCount == 3)
                {
                    listCount.Add(1);
                }
                else
                {
                    listCount.Add(0);
                }


            }
            foreach (int ans in listCount)
            {
                Console.WriteLine(ans);
            }
            Console.ReadKey();
        }

        private static bool NGAfterNextJuge(int second, int third, int forth, List<int> listNGIndex)
        {
            bool blTrue = true;
            foreach (int intNG in listNGIndex)
            {
                if (intNG == second || intNG == third || intNG == forth)
                {
                    blTrue = false;
                }
                
            }
            return blTrue;
        }

        private static bool NGNextJuge(int second, int third, List<int> listNGIndex)
        {
            bool blTrue = true;
            foreach (int intNG in listNGIndex)
            {
                if (intNG == second || intNG == third)
                {
                    blTrue = false;
                }
            }
            return blTrue;
        }

        public static bool NGJuge(int intIndex, List<int> listNGNum)
        {
            bool blTrue = true;

            foreach (int intNG in listNGNum)
            {
                if (intIndex == intNG)
                {
                    blTrue = false;
                    break;
                }
            }
            return blTrue;
        }

        public static void TestPrint(int intDateLength, int[ , ] intNum, string[ , ] strMark)
        {
            for (int first = 0; first < intDateLength; first++)
            {
                for (int second = 0; second < 9; second++)
                {
                    Console.Write(intNum[first, second]);
                    Console.Write(" ");
                }
                Console.WriteLine();
                for (int second = 0; second < 9; second++)
                {
                    Console.Write(strMark[first, second]);
                    Console.Write(" ");
                }
                Console.WriteLine();
            }
        }

        public static void TestNG(List<int> list)
        {
            foreach (int print in list)
            {
                Console.Write(print);
            }
            Console.WriteLine();
        }

    }

    
}



Measured memory_bytes: 15940