class Hand
  def initialize
    @card = Hash.new
  end
  def add(c, n)
    @card[c] = Array.new(13, 0) unless @card.key?(c)
    @card[c][n] += 1
  end
  def score()
  end
end

def count(card)
  return 0 if card.inject(:+) % 3 != 0
  res = 0
  for i in 1..9 do
    if card[i] > 0 && card[i + 1] > 0 && card[i + 2] > 0
      new = card
      new[i] -= 1
      new[i + 1] -= 1
      new[i + 2] -= 1
      res = [res, 1 + count(new)].max
    end
    if card[i] >= 3
      new = card
      new[i] -= 3
      res = [res, 1 + count(new)].max
    end
  end
  res
end

T = gets.to_i
T.times do
  hand = Hash.new
  n = gets.split.map(&:to_i)
  c = gets.split
  for i in 0...9 do
    hand[c[i]] = Array.new(13, 0) unless hand.key?(c[i])
    hand[c[i]][n[i]] += 1
  end
  total = 0
  hand.each do |color, card|
    total += count(card)
  end
  puts total == 3 ? "1" : "0"
end