object Main extends App {

  override def main(args: Array[String]): Unit = {
    Iterator.continually(dataSet).takeWhile(!_.isEmpty)
      .foreach(list => println(solve(list)))
  }

  def solve(list: List[Dice]): String = {
    val arr = Array.ofDim[Dice](30, 30, 31)

    val dropX = 15
    val dropY = 15
    val dropH = 30

    list.foreach(dice => {
      arr(dropX)(dropY)(dropH) = dice
      drop(arr, dropX, dropY, dropH)
//      println("?????? " + collect(arr))
    })

    collect(arr)
  }

  def getValue(arr: Array[Array[Array[Dice]]], x: Int, y: Int, h: Int): Int = {
    if (isEmpty(arr, x, y, h)) 0
    else if (!isEmpty(arr, x, y, h + 1)) getValue(arr, x, y, h + 1)
    else arr(x)(y)(h).top
  }

  def collect(arr: Array[Array[Array[Dice]]]): String = {
    val res = Array(0, 0, 0, 0, 0, 0, 0)
    val values = for (x <- (0 until arr.length); y <- (0 until arr.length)) yield {
      getValue(arr, x, y, 0)
    }
    values.foreach(top => res(top) += 1)

    res.takeRight(6).mkString(" ")
  }

  def isEmpty(arr: Array[Array[Array[Dice]]], x: Int, y: Int, h: Int): Boolean = {
    h >= 0 && arr(x)(y)(h) == null
  }

  def drop(arr: Array[Array[Array[Dice]]], x: Int, y: Int, h: Int): Unit = {
    val ll = (1 to h).takeWhile(h2 => isEmpty(arr, x, y, h - h2)).map(h-_).toList
    if(ll.isEmpty ) return 
    val hh = ll.min
   
    if (h != hh) {
      arr(x)(y)(hh) = arr(x)(y)(h);
      arr(x)(y)(h) = null;
      die(arr, x, y, hh)
    }
  }

  def die(arr: Array[Array[Array[Dice]]], x: Int, y: Int, h: Int): Unit = {
    val canEast = if (isEmpty(arr, x + 1, y, h - 1)) arr(x)(y)(h).canEast else 0
    val canWest = if (isEmpty(arr, x - 1, y, h - 1)) arr(x)(y)(h).canWest else 0
    val canSouth = if (isEmpty(arr, x, y - 1, h - 1)) arr(x)(y)(h).canSouth else 0
    val canNorth = if (isEmpty(arr, x, y + 1, h - 1)) arr(x)(y)(h).canNorth else 0

    val max = List(canEast, canWest, canSouth, canNorth).max
    if (max > 0) {

      if (canEast == max) {
        arr(x + 1)(y)(h) = arr(x)(y)(h).toEast
        arr(x)(y)(h) = null
        drop(arr, x + 1, y, h )
      }
      if (canWest == max) {
        arr(x - 1)(y)(h) = arr(x)(y)(h).toWest
        arr(x)(y)(h) = null
        drop(arr, x - 1, y, h )
      }
      if (canSouth == max) {
        arr(x)(y - 1)(h ) = arr(x)(y)(h).toSouth
        arr(x)(y)(h) = null
        drop(arr, x, y - 1, h )
      }
      if (canNorth == max) {
        arr(x)(y + 1)(h ) = arr(x)(y)(h).toNorth
        arr(x)(y)(h) = null
        drop(arr, x, y + 1, h)
      }
    }

  }

  def dataSet: List[Dice] = {
    val n = io.StdIn.readLine.toInt
    if (n == 0) {
      return List.empty
    }

    (1 to n).map(_ => io.StdIn.readLine.split(' '))
      .map(ts => new Dice(ts(0).toInt, ts(1).toInt)).toList
  }

  case class Dice(top: Int, south: Int) {
    val buttom = 7 - top
    val north = 7 - south

    val east = (top, south) match {
      case (1, 3) => 5
      case (3, 6) => 5
      case (6, 4) => 5
      case (4, 1) => 5
      case (3, 1) => 2
      case (6, 3) => 2
      case (4, 6) => 2
      case (1, 4) => 2
      case (1, 5) => 4
      case (5, 6) => 4
      case (6, 2) => 4
      case (2, 1) => 4
      case (5, 1) => 3
      case (6, 5) => 3
      case (2, 6) => 3
      case (1, 2) => 3
      case (3, 5) => 1
      case (5, 4) => 1
      case (4, 2) => 1
      case (2, 3) => 1
      case (5, 3) => 6
      case (4, 5) => 6
      case (2, 4) => 6
      case (3, 2) => 6  
    }

    val west = 7 - east

    def toEast: Dice = new Dice(west, south)
    def toWest: Dice = new Dice(east, south)
    def toNorth: Dice = new Dice(south, buttom)
    def toSouth: Dice = new Dice(north, top)

    def canEast: Int = if (east >= 4) east else 0
    def canWest: Int = if (west >= 4) west else 0
    def canNorth: Int = if (north >= 4) north else 0
    def canSouth: Int = if (south >= 4) south else 0

  }

}

Measured memory_bytes: 80436