def goodSet(a)
  colors  = a.map { |x, c| c }
  numbers = a.sort.map { |n, x| n }

  (colors[0] == colors[1] && colors[1] == colors[2]) &&
  ((numbers[0] == numbers[1] && numbers[1] == numbers[2]) ||
  (numbers[2] - numbers[1] == 1 && numbers[1] - numbers[0] == 1))
end

def rummy()
  numbers = gets.split.map(&:to_i)
  colors  = gets.split
  numbersAndColors = (0..8).to_a.map{ |i| [numbers[i], colors[i]] }
  winCon = 0

  numbersAndColors.permutation do |nc|
    winCon = 1 if goodSet(nc[0,3]) && goodSet(nc[3,3]) && goodSet(nc[6,3])
  end

  p winCon
end

if __FILE__ == $0
  gets.to_i.times{ rummy }
end