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 datasetnum = int.Parse(Console.ReadLine());

            int result = 1;

            for (int i = 0; i < datasetnum; i++)
            {
                //????????????????????????????????????
                List<int> redlist = new List<int>();
                List<int> bulelist = new List<int>();
                List<int> greenlist = new List<int>();


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

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

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

                        case "R":
                            redlist.Add(int.Parse(cardnum[j]));
                            break;


                        case "G":
                            greenlist.Add(int.Parse(cardnum[j]));
                            break;


                        case "B":
                            bulelist.Add(int.Parse(cardnum[j]));
                            break;

                    }
                }

                //??????????????????????????????????????????
                var red = new Red(redlist);
                var bule = new Bule(bulelist);
                var green = new Green(greenlist);


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

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

            }

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

        }

    }



    class Red
    {
        #region ??????????????°(?????£???????????°??????????¨???°???????´??????????)
        int[] card = new int[9];
        #endregion


        #region ?????????????????????
        public Red(List<int> setlist)
        {
            for (int i = 0; i < 9; i++)
            {
                var match = setlist.FindAll(x => x == i + 1);

                card[i] = match.Count;
            }
        }
        #endregion

        public bool redresult()
        {
            var checkresult = new CardControl();
            return checkresult.CheckWin(this.card);
        }

    }


    class Green
    {
        #region ??????????????°(?????£???????????°??????????¨???°???????´??????????)
        int[] card = new int[9];
        #endregion


        #region ?????????????????????
        public Green(List<int> setlist)
        {
            for (int i = 0; i < 9; i++)
            {
                var match = setlist.FindAll(x => x == i + 1);

                card[i] = match.Count;
            }
        }
        #endregion

        public bool redresult()
        {
            var checkresult = new CardControl();
            return checkresult.CheckWin(this.card);
        }

    }



    class Bule
    {
        #region ??????????????°(?????£???????????°??????????¨???°???????´??????????)
        int[] card = new int[9];
        #endregion

        #region ?????????????????????
        public Bule(List<int> setlist)
        {
            for (int i = 0; i < 9; i++)
            {
                var match = setlist.FindAll(x => x == i + 1);

                card[i] = match.Count;
            }
        }
        #endregion

        public bool redresult()
        {
            var checkresult = new CardControl();
            return checkresult.CheckWin(this.card);
        }

    }



    class CardControl
    {
        /// <summary>
        /// ???????????¶????????´?????????????????????
        /// </summary>
        /// <param name="card"></param>
        /// <returns></returns>
        public bool CheckWin(int[] card)
        {
            bool result = true;

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

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

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


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

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

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


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

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

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

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

                        //??£?????????????????°???????????°????????´???????¬??????£?????????????????????????????????
                        if (serialtemp < card[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: 9208