n   = gets.to_i
ans = []

def check(ary)
  for i in 0 .. 8
    if ary[i] < 0
      break
    elsif ary[i] == 4
      ary[i]      = 0
      ary[i + 1] -= 1
      ary[i + 2] -= 1
    elsif ary[i] == 3
      ary[i]      = 0
    elsif ary[i] > 0
      ary[i + 1] -= ary[i]
      ary[i + 2] -= ary[i]
      ary[i]      = 0
    end
  end

  if ary.uniq == [0]
    return true
  else
    return false
  end
end

n.times do
  num   = gets.split.map(&:to_i)
  color = gets.chomp.split

  aryR = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
  aryG = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
  aryB = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

  9.times do |i|
    eval("ary" + color[i])[num[i] - 1] += 1
  end

  if check(aryR) && check(aryG) && check(aryB)
    ans << 1
  else
    ans <<  0
  end
end

ans.each do |a|
  puts a
end

