B = [ [0, 1, 1], [1, 2, 2], [1, 2, 2], ] T = gets.to_i def build_grid(h, w) grid = [] ly = Rational(h, 3).ceil lx = Rational(w, 3).ceil (ly * 3).times do |y| row = [] (lx * 3).times do |x| row << B[y % 3][x % 3] end grid << row end if h % 3 == 1 grid.pop(2) elsif h % 3 == 2 grid.shift end if w % 3 == 1 grid = grid.map { |row| row.pop(2); row } elsif w % 3 == 2 grid = grid.map { |row| row.shift; row } end grid end T.times do h, w = gets.split.map(&:to_i) grid = build_grid(h, w) puts grid.map { |row| row.join(' ') } end