import scala.io.StdIn.{readInt,readLine}
import scala.collection.mutable.ListBuffer

object Main {
  def isWinner(a:Array[(Int,String)]) = {
    def scan(a:List[(Int,String)]):Int = {
      a match {
        case Nil => 1
        case hd1::hd2::hd3::tail if hd1._2!=hd2._2 || hd2._2!=hd3._2 => 0
        case hd1::hd2::hd3::tail if(hd1._1==hd2._1 && hd2._1==hd3._1) => scan(tail)
        case hd::tail => {
          val buf = new ListBuffer[(Int,String)]
          var (x1,x2) = (false,false)
          for(i<-tail) {
            if(i._1==(hd._1+1) && !x1) { x1 = true }
            else if(i._1==(hd._1+2) && !x2) { x2 = true }
            else buf += i
          }
          if(x1 && x2) scan(buf.toList)
          else 0
        }
      }
    }
    scan(a.toList)
  }

  def main(args:Array[String]) = {
    val t = readInt
    for(i<-1 to t) {
      val a = (readLine.split(" ").map(_.toInt) zip readLine.split(" ")) .sortWith{ (x,y)=>
        if(x._2<y._2) true
        else if(x._2==y._2) {
          x._1<y._1
        } else false
      }
      println(isWinner(a))
    }
  }
}