import scala.io.StdIn.{readInt,readLine}

object Main {
  def isWinner(a:Array[(Int,String)]) = {
    var flg = 1
    for(i<-0 to 2) {
      val buf = Array(a(i*3),a(i*3+1),a(i*3+2)).sorted
      if(buf.map(_._2).distinct.length>1) flg = 0
      if(buf(0)._1+1 != buf(1)._1 || buf(1)._1+1 != buf(2)._1) {
        if(buf.map(_._1).distinct.length>1) flg = 0
      }
    }
    flg
  }

  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))
    }
  }
}