def shuntsu(x, y)
  if y <= 6
    for z in 0..2
      x[y+z] -= 1
    end
  end
  return x
end

gets.to_i.times{
  h_n = gets.split.map{|i| (i.to_i)-1}
  h_c = gets.split
  r, g, b = [0]*9, [0]*9, [0]*9
  
  for x in 0...9
    case h_c[x]
    when "R"; r[h_n[x]] += 1
    when "G"; g[h_n[x]] += 1
    when "B"; b[h_n[x]] += 1
    end
  end
  
  for x in [r, g, b]
    for y in 0...9
      case x[y]
      when 1
        x = shuntsu(x, y)
      when 2
        x = shuntsu(shuntsu(x, y), y)
      when 3
        x[y] -= 3
      when 4
        x[y] -= 3
        x = shuntsu(x, y)
      end
    end
  end
  
  if [r, g, b] == [[0]*9]*3
    puts 1
  else
    puts 0
  end
}