using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication2
{
    //RUMMY

    class Program
    {
        /// <summary>
        /// ????????°????????¨????????????????????????
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            int loopCount = int.Parse(Console.ReadLine());
            
            for(int i = 0; i < loopCount; i++)
            {
                int[] cardNums = Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray();

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

                List<Card> cards = new List<Card>();

                for (int cardCount = 0; cardCount < cardNums.Length; cardCount++)
                    cards.Add(new Card(cardNums[cardCount], cardColor[cardCount]));

                //??????????????????????°?????????????????????????
                SortCard(cards);

                //?????£???????????????????????????
                CheckCombinationWin(cards);

                //?????????????????????????????¨??????????¢????
                Console.WriteLine(CheckUsedCard(cards) ? "1" : "0");
            }
        }
        /// <summary>
        /// ??????????????°???????°??????????????????????
        /// </summary>
        private static void SortCard(List<Card> cards)
        {
            for (int i = 0; i < cards.Count - 1; i++)
            {
                // ???????????????????????????????????????
                for (int j = cards.Count - 1; j > i; j--)
                {
                    // ??????????????§????????¨?????????????????\???????????????
                    if (cards[j].number < cards[j - 1].number)
                    {
                        Card card = cards[j];
                        cards[j] = cards[j - 1];
                        cards[j - 1] = card;
                    }
                }
            }
        }

        /// <summary>
        /// ??¨???????????????????????¨?????????????¢????
        /// </summary>
        /// <param name="cards"></param>
        /// <returns></returns>
        private static bool CheckUsedCard(List<Card> cards)
        {
            bool isWin = true;

            foreach (Card card in cards)
                if (!card.isUsed)
                    isWin = false;

            return isWin;
        }

        /// <summary>
        /// ????????£?????????????????????????????????????????£????????????????????¨???????????????
        /// </summary>
        /// <param name="cards"></param>
        private static void CheckCombinationWin(List<Card> cards)
        {
            for(int x = 0; x < 2; x++)
            {
                //??£???
                for (int i = 0; i < cards.Count - 2; i++)
                {
                    for (int j = i + 1; j < cards.Count - 1; j++)
                    {
                        for (int k = j + 1; k < cards.Count; k++)
                        {
                            //????????????????????????????????¨???????¢????
                            if (IsUsedCard(cards[i], cards[j], cards[k]))
                            {
                                //??????????????????????????????????????????????????£????????????
                                if (IsSameColor(cards[i], cards[j], cards[k]))
                                {
                                    if (x == 0)
                                    {
                                        //??£????????§??????
                                        //????±?????????????????????????????????£???????????£????????????
                                        if (cards[i].number == cards[j].number - 1 && cards[i].number == cards[k].number - 2)
                                        {
                                            cards[i].isUsed = true;
                                            cards[j].isUsed = true;
                                            cards[k].isUsed = true;
                                        }
                                    }
                                    else
                                    {
                                        //?????????????????§??????
                                        //????±????3??????????????????????????????????????£????????????
                                        if (cards[i].number == cards[j].number && cards[i].number == cards[k].number)
                                        {
                                            cards[i].isUsed = true;
                                            cards[j].isUsed = true;
                                            cards[k].isUsed = true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        

        /// <summary>
        /// ????????????????????????????????¨???????¢????
        /// </summary>
        /// <param name="card1"></param>
        /// <param name="card2"></param>
        /// <param name="card3"></param>
        /// <returns></returns>
        private static bool IsUsedCard(Card card1, Card card2, Card card3)
        {
            bool isUsed = false;

            if (!card1.isUsed && !card2.isUsed && !card3.isUsed)
                isUsed = true;

            return isUsed;
        }

        /// <summary>
        /// ???????????????????????¨????????????????¢????
        /// </summary>
        /// <param name="card1"></param>
        /// <param name="card2"></param>
        /// <param name="card3"></param>
        /// <returns></returns>
        private static bool IsSameColor(Card card1, Card card2, Card card3)
        {
            bool isSameColor = false;

            if (card1.color == card2.color && card2.color == card3.color)
                isSameColor = true;

            return isSameColor;
        }
    }

    /// <summary>
    /// ??????????????????
    /// </summary>
    public class Card
    {
        /// <summary>
        /// ??????????????°???
        /// </summary>
        public int number { get; set; }
        /// <summary>
        /// ???????????????
        /// </summary>
        public string color { get; set; }
        /// <summary>
        /// ?????¨??????????????°
        /// </summary>
        public bool isUsed { get; set; }

        /// <summary>
        /// ?????????????????????
        /// </summary>
        /// <param name="num">???????????????</param>
        /// <param name="color">????????????</param>
        public Card(int num, string color)
        {
            this.number = num;
            this.color = color;
        }
    }
}

Measured memory_bytes: 9268