using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {


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

            int datasenum = int.Parse(Console.ReadLine());


            int result = 1;

            for (int i = 0; i < datasenum; i++)
            {
                var red = new Red();
                var bule = new Bule();
                var green = new Green();

                string[] cardnum = Console.ReadLine().Split(' ');

                string[] cardcolor = Console.ReadLine().Split(' ');

                //???????????????????????????????????????
                for (int j = 0; j < cardnum.Length; j++)
                {

                    switch (cardcolor[j])
                    {

                        case "R":
                            red.SetNum(int.Parse(cardnum[j]));
                            break;


                        case "G":
                            green.SetNum(int.Parse(cardnum[j]));
                            break;


                        case "B":
                            bule.SetNum(int.Parse(cardnum[j]));
                            break;

                    }

                }

                if (red.CheckWin() && bule.CheckWin() && green.CheckWin()) result = 1;
                else result = 0;

                //???????????????????????????Add??????
                resultlist.Add(result);

            }

            //????????????
            foreach(var output in resultlist)
            {
                Console.WriteLine(output);
            }


        }

    }



    class Red
    {
        #region ??????????????°(?????£???????????°??????????¨???°???????´??????????)
        int one = 0;
        int two = 0;
        int therr = 0;
        int four = 0;
        int five = 0;
        int six = 0;
        int seven = 0;
        int eight = 0;
        int nine = 0;
        #endregion

        enum CardNum { one = 1, two, therr, four, five, six, seven, eight, nine}


        //?????°?????°?????¨??¢??£????????????????????°???????????????????????????????????????????????????
        public void SetNum(int i)
        {

            switch (i)
            {
                case (int)CardNum.one:
                    this.one += 1;
                    break;

                case (int)CardNum.two:
                    this.two += 1;
                    break;

                case (int)CardNum.therr:
                    this.therr += 1;
                    break;

                case (int)CardNum.four:
                    this.four += 1;
                    break;

                case (int)CardNum.five:
                    this.five += 1;
                    break;

                case (int)CardNum.six:
                    this.six += 1;
                    break;

                case (int)CardNum.seven:
                    this.seven += 1;
                    break;

                case (int)CardNum.eight:
                    this.eight += 1;
                    break;

                case (int)CardNum.nine:
                    this.nine += 1;
                    break;

            }
        }

        //???????????¶????????????????????????????????????????????????
        public bool CheckWin()
        {
            bool result = true;

            //??????????????°?????????????´????????????????
            List<int> cardlist = new List<int>();


            cardlist.Add(this.one);
            cardlist.Add(this.two);
            cardlist.Add(this.therr);
            cardlist.Add(this.four);
            cardlist.Add(this.five);
            cardlist.Add(this.six);
            cardlist.Add(this.seven);
            cardlist.Add(this.eight);
            cardlist.Add(this.nine);


            //?????£??????????????????????????°???????´????????????°
            int totalnum = 0;

            //?????¢?????°???????´????????????°
            int peanum = 0;

            //????????£???????????¢????????°????????´??????????\???¨?????°
            int serialtemp = 0;


            //??£?????????????????????????????????
            int serialcounter = 0;

            //????????£??????????????????????????°(3??§????????????????????´??????false)
            //????????¢?????°(???????????§?????£?????¨??????????????¨????????????????????´??????false)
            //??¢??????????????¢??????????????????????????????
            for (int i = 0; i < 9; i++)
            {
                totalnum += cardlist[i];

                //????????????????????????????????´??????1?????¢
                if (cardlist[i] == 3)
                {
                    peanum += 1;
                    continue;
                }


                if (cardlist[i] == 4)
                {
                    peanum += 1;
                }

                //??£??????????????????
                if (0 != cardlist[i])
                {
                    serialcounter++;

                    //???????????¢????????°????????´????????????
                    if (serialtemp == 0) serialtemp = cardlist[i];
                    else if (serialtemp > cardlist[i]) serialtemp = 1;

                    if (serialcounter == 3)
                    {
                        //??£???????????¢???1or2????????§?????£?????????????????°???????????????2??????2?????¢???????????????
                        peanum += serialtemp;

                        //??£?????????????????°???????????°????????´???????¬??????£?????????????????????????????????
                        if(serialtemp < cardlist[i])
                        {
                            serialcounter = 1;
                            continue;
                        }

                        serialcounter = 0;
                        serialtemp = 0;
                    }
                }
                else
                {
                    serialcounter = 0;
                }
            
            }


            if (totalnum % 3 != 0) result = false;

            if (totalnum / 3 > peanum) result = false;

            //????????????1??????????????´??????true
            if (totalnum == 0) result = true;                 
        
            return result;
        }


        //??????????????°????????????????????¨??????????????????

    }


    class Green
    {
        #region ??????????????°(?????£???????????°??????????¨???°???????´??????????)
        int one = 0;
        int two = 0;
        int therr = 0;
        int four = 0;
        int five = 0;
        int six = 0;
        int seven = 0;
        int eight = 0;
        int nine = 0;
        #endregion

        enum CardNum { one = 1, two, therr, four, five, six, seven, eight, nine }


        public void SetNum(int i)
        {

            switch (i)
            {
                case (int)CardNum.one:
                    this.one += 1;
                    break;

                case (int)CardNum.two:
                    this.two += 1;
                    break;

                case (int)CardNum.therr:
                    this.therr += 1;
                    break;

                case (int)CardNum.four:
                    this.four += 1;
                    break;

                case (int)CardNum.five:
                    this.five += 1;
                    break;

                case (int)CardNum.six:
                    this.six += 1;
                    break;

                case (int)CardNum.seven:
                    this.seven += 1;
                    break;

                case (int)CardNum.eight:
                    this.eight += 1;
                    break;

                case (int)CardNum.nine:
                    this.nine += 1;
                    break;

            }
        }

        public bool CheckWin()
        {
            bool result = true;

            //??????????????°?????????????´????????????????
            List<int> cardlist = new List<int>();


            cardlist.Add(this.one);
            cardlist.Add(this.two);
            cardlist.Add(this.therr);
            cardlist.Add(this.four);
            cardlist.Add(this.five);
            cardlist.Add(this.six);
            cardlist.Add(this.seven);
            cardlist.Add(this.eight);
            cardlist.Add(this.nine);


            //?????£??????????????????????????°???????´????????????°
            int totalnum = 0;

            //?????¢?????°???????´????????????°
            int peanum = 0;

            //????????£???????????¢????????°????????´??????????\???¨?????°
            int serialtemp = 0;


            //??£?????????????????????????????????
            int serialcounter = 0;

            //????????£??????????????????????????°(3??§????????????????????´??????false)
            //????????¢?????°(???????????§?????£?????¨??????????????¨????????????????????´??????false)
            //??¢??????????????¢??????????????????????????????
            for (int i = 0; i < 9; i++)
            {
                totalnum += cardlist[i];

                //????????????????????????????????´??????1?????¢
                if (cardlist[i] == 3)
                {
                    peanum += 1;
                    continue;
                }


                if (cardlist[i] == 4)
                {
                    peanum += 1;
                }

                //??£??????????????????
                if (0 != cardlist[i])
                {
                    serialcounter++;

                    //???????????¢????????°????????´????????????
                    if (serialtemp == 0) serialtemp = cardlist[i];
                    else if (serialtemp > cardlist[i]) serialtemp = 1;

                    if (serialcounter == 3)
                    {
                        //??£???????????¢???1or2????????§?????£?????????????????°???????????????2??????2?????¢???????????????
                        peanum += serialtemp;

                        //??£?????????????????°???????????°????????´???????¬??????£?????????????????????????????????
                        if (serialtemp < cardlist[i])
                        {
                            serialcounter = 1;
                            continue;
                        }

                        serialcounter = 0;
                        serialtemp = 0;
                    }
                }
                else
                {
                    serialcounter = 0;
                }

            }


            if (totalnum % 3 != 0) result = false;

            if (totalnum / 3 > peanum) result = false;

            //????????????1??????????????´??????true
            if (totalnum == 0) result = true;

            return result;
        }

    }


    class Bule
    {
        #region ??????????????°(?????£???????????°??????????¨???°???????´??????????)
        int one = 0;
        int two = 0;
        int therr = 0;
        int four = 0;
        int five = 0;
        int six = 0;
        int seven = 0;
        int eight = 0;
        int nine = 0;
        #endregion

        enum CardNum { one = 1, two, therr, four, five, six, seven, eight, nine }


        public void SetNum(int i)
        {

            switch (i)
            {
                case (int)CardNum.one:
                    this.one += 1;
                    break;

                case (int)CardNum.two:
                    this.two += 1;
                    break;

                case (int)CardNum.therr:
                    this.therr += 1;
                    break;

                case (int)CardNum.four:
                    this.four += 1;
                    break;

                case (int)CardNum.five:
                    this.five += 1;
                    break;

                case (int)CardNum.six:
                    this.six += 1;
                    break;

                case (int)CardNum.seven:
                    this.seven += 1;
                    break;

                case (int)CardNum.eight:
                    this.eight += 1;
                    break;

                case (int)CardNum.nine:
                    this.nine += 1;
                    break;

            }
        }


        public bool CheckWin()
        {
            bool result = true;

            //??????????????°?????????????´????????????????
            List<int> cardlist = new List<int>();


            cardlist.Add(this.one);
            cardlist.Add(this.two);
            cardlist.Add(this.therr);
            cardlist.Add(this.four);
            cardlist.Add(this.five);
            cardlist.Add(this.six);
            cardlist.Add(this.seven);
            cardlist.Add(this.eight);
            cardlist.Add(this.nine);


            //?????£??????????????????????????°???????´????????????°
            int totalnum = 0;

            //?????¢?????°???????´????????????°
            int peanum = 0;

            //????????£???????????¢????????°????????´??????????\???¨?????°
            int serialtemp = 0;


            //??£?????????????????????????????????
            int serialcounter = 0;

            //????????£??????????????????????????°(3??§????????????????????´??????false)
            //????????¢?????°(???????????§?????£?????¨??????????????¨????????????????????´??????false)
            //??¢??????????????¢??????????????????????????????
            for (int i = 0; i < 9; i++)
            {
                totalnum += cardlist[i];

                //????????????????????????????????´??????1?????¢
                if (cardlist[i] == 3)
                {
                    peanum += 1;
                    continue;
                }


                if (cardlist[i] == 4)
                {
                    peanum += 1;
                }

                //??£??????????????????
                if (0 != cardlist[i])
                {
                    serialcounter++;

                    //???????????¢????????°????????´????????????
                    if (serialtemp == 0) serialtemp = cardlist[i];
                    else if (serialtemp > cardlist[i]) serialtemp = 1;

                    if (serialcounter == 3)
                    {
                        //??£???????????¢???1or2????????§?????£?????????????????°???????????????2??????2?????¢???????????????
                        peanum += serialtemp;

                        //??£?????????????????°???????????°????????´???????¬??????£?????????????????????????????????
                        if (serialtemp < cardlist[i])
                        {
                            serialcounter = 1;
                            continue;
                        }

                        serialcounter = 0;
                        serialtemp = 0;
                    }
                }
                else
                {
                    serialcounter = 0;
                }

            }


            if (totalnum % 3 != 0) result = false;

            if (totalnum / 3 > peanum) result = false;

            //????????????1??????????????´??????true
            if (totalnum == 0) result = true;

            return result;
        }


    }
}

Measured memory_bytes: 9364