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

/// <summary>
/// AOJ RUMMY????????°??????
/// </summary>
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[] cardColors = Console.ReadLine().Split(' ');

            //?????????????????????????????????????????????(??\??????????????§?????????????????????????????´????????????????????£????????????)
            List<Card> cards = GetCardList(cardNums, cardColors);

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

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

            //?????????????????????????????¨??????????¢????
            Console.WriteLine(CheckUsedCard(cards) ? "1" : "0");
        }
    }

    /// <summary>
    /// ???????????????????????????????????????????????????????????????????????????
    /// </summary>
    /// <param name="cardNums"></param>
    /// <param name="cardColors"></param>
    /// <returns></returns>
    private static List<Card> GetCardList(int[] cardNums, string[] cardColors)
    {
        List<Card> cards = new List<Card>();

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

        return cards;
    }

    /// <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)
    {
        //checkCount=0???????????£????????§??????
        //checkCount=1??????????????????????????§??????
        for (int checkCount = 0; checkCount < 2; checkCount++)
        {
            //??\??????3???for??????????????????????????????????????????????????????
            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 (checkCount == 0)
                                {
                                    //??£????????§?????????????±?????????????????????????????????£???????????£????????????
                                    if (IsContinuityNumber(cards[i], cards[j], cards[k]))
                                    {
                                        SetCardUsed(cards[i], cards[j], cards[k]);
                                    }
                                }
                                else
                                {
                                    //?????????????????§?????????????±????3??????????????????????????????????????£????????????
                                    if (IsSameNumber(cards[i], cards[j], cards[k]))
                                    {
                                        SetCardUsed(cards[i], cards[j], cards[k]);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    /// <summary>
    /// ??????????????????????????¨????????????????????????
    /// </summary>
    /// <param name="card1"></param>
    /// <param name="card2"></param>
    /// <param name="card3"></param>
    private static void SetCardUsed(Card card1, Card card2, Card card3)
    {
        card1.isUsed = true;
        card2.isUsed = true;
        card3.isUsed = true;
    }

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

        if (card1.number == card2.number - 1 && card1.number == card3.number - 2)
            isContinuityNumber = true;

        return isContinuityNumber;
    }

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

        if (card1.number == card2.number && card1.number == card3.number)
            isSameNumber = true;

        return isSameNumber;
    }

    /// <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: 9256