Problem: Professor Random, known for his research on randomized algorithms, is now conducting an experiment on biased dice. His experiment consists of dropping a number of dice onto a plane, one after another from a fixed position above the plane. The dice fall onto the plane or dice already there, without rotating, and may roll and fall according to their property. Then he observes and records the status of the stack formed on the plane, specifically, how many times each number appears on the faces visible from above. All the dice have the same size and their face numbering is identical, which we show in Figure C-1.

<image>

Figure C-1: Numbering of a die

The dice have very special properties, as in the following.

(1) Ordinary dice can roll in four directions, but the dice used in this experiment never roll in the directions of faces 1, 2 and 3; they can only roll in the directions of faces 4, 5 and 6. In the situation shown in Figure C-2, our die can only roll to one of two directions.

<image>

Figure C-2: An ordinary die and a biased die

(2) The die can only roll when it will fall down after rolling, as shown in Figure C-3. When multiple possibilities exist, the die rolls towards the face with the largest number among those directions it can roll to.

<image>

Figure C-3: A die can roll only when it can fall

(3) When a die rolls, it rolls exactly 90 degrees, and then falls straight down until its bottom face touches another die or the plane, as in the case [B] or [C] of Figure C-4.

(4) After rolling and falling, the die repeatedly does so according to the rules (1) to (3) above.

<image>

Figure C-4: Example stacking of biased dice

For example, when we drop four dice all in the same orientation, 6 at the top and 4 at the front, then a stack will be formed as shown in Figure C-4.

<image>

Figure C-5: Example records

After forming the stack, we count the numbers of faces with 1 through 6 visible from above and record them. For example, in the left case of Figure C-5, the record will be "0 2 1 0 0 0", and in the right case, "0 1 1 0 0 1".

Input

The input consists of several datasets each in the following format.

> n
>  t1  f1
>  t2  f2
>  ...
>  tn  fn
>

Here, n (1 ≤ n ≤ 100) is an integer and is the number of the dice to be dropped. ti and fi (1 ≤ ti, fi ≤ 6) are two integers separated by a space and represent the numbers on the top and the front faces of the i-th die, when it is released, respectively.

The end of the input is indicated by a line containing a single zero.

Output

For each dataset, output six integers separated by a space. The integers represent the numbers of faces with 1 through 6 correspondingly, visible from above. There may not be any other characters in the output.

Sample Input


4
6 4
6 4
6 4
6 4
2
5 3
5 3
8
4 2
4 2
4 2
4 2
4 2
4 2
4 2
4 2
6
4 6
1 5
2 3
5 3
2 4
4 2
0


Output for the Sample Input


0 1 1 0 0 1
1 0 0 0 1 0
1 2 2 1 0 0
2 3 0 0 0 0






Example

Input

4
6 4
6 4
6 4
6 4
2
5 3
5 3
8
4 2
4 2
4 2
4 2
4 2
4 2
4 2
4 2
6
4 6
1 5
2 3
5 3
2 4
4 2
0


Output

0 1 1 0 0 1
1 0 0 0 1 0
1 2 2 1 0 0
2 3 0 0 0 0

Solution (in PYTHON3):

Solution:
class RollDir:
    def __init__(self, d, w):
        self.d = d
        self.w = w

    def __str__(self):
        return "Roll dir: {}, weight: {}".format(self.d, self.w)

    @classmethod
    def create(cls, d, w):
        if w == 6 or w == 5 or w == 4:
            return cls(d, w)
        else:
            return None

class Dice:
    def __init__(self, t, f):
        self.top = t
        self.bot = 7 - t

        if t == 1:
            arr = [2, 3, 5, 4]
        elif t == 6:
            arr = [5, 3, 2, 4]
        elif t == 2:
            arr = [3, 1, 4, 6]
        elif t == 5:
            arr = [3, 6, 4, 1]
        elif t == 3:
            arr = [6, 5, 1, 2]
        elif t == 4:
            arr = [1, 5, 6, 2]

        idx = arr.index(f)
        self.front = arr[idx]
        self.right = arr[(idx + 1) % 4]
        self.back = arr[(idx + 2) % 4]
        self.left = arr[(idx + 3) % 4]

    def roll(self, direction):
        if direction == 'N':
            temp = self.top
            self.top = self.front
            self.front = self.bot
            self.bot = self.back
            self.back = temp
        elif direction == 'E':
            temp = self.top
            self.top = self.left
            self.left = self.bot
            self.bot = self.right
            self.right = temp
        elif direction == 'W':
            temp = self.top
            self.top = self.right
            self.right = self.bot
            self.bot = self.left
            self.left = temp
        elif direction == 'S':
            temp = self.top
            self.top = self.back
            self.back = self.bot
            self.bot = self.front
            self.front = temp

    def getRollDir(self):
        arr = []
        roll = RollDir.create('S', self.front)
        if roll != None:
            arr.append(roll)
        roll = RollDir.create('E', self.right)
        if roll != None:
            arr.append(roll)
        roll = RollDir.create('N', self.back)
        if roll != None:
            arr.append(roll)
        roll = RollDir.create('W', self.left)
        if roll != None:
            arr.append(roll)

        return sorted(arr, key=lambda x: x.w ,reverse=True)
        
    def __str__(self):
        return "Top: {}   Front: {}   Left: {}   Right: {}" \
            .format(self.top, self.front, self.left, self.right)

class Cell:
    def __init__(self):
        self.height = 0
        self.val = None

class Grid:
    def __init__(self):
        self.cells = {}

    def drop(self, dice, x, y):
        if (x, y) not in self.cells:
            self.cells[ (x, y) ] = Cell()
        cell = self.cells[ (x, y) ]

        diceRollDirs = dice.getRollDir()
        gridRollDirs = self.getRollableDir(x, y)

        didRoll = False
        for roll in diceRollDirs:
            if roll.d in gridRollDirs:
                direction = roll.d
                dice.roll(direction)
                rollCoord = self.getRollCoord(x, y, direction)
                self.drop(dice, rollCoord[0], rollCoord[1])
                didRoll = True
                break
        
        if not didRoll:
            cell.height += 1
            cell.val = dice.top

    def getRollableDir(self, x, y):
        if (x, y) not in self.cells:
            self.cells[(x, y)] = Cell()
        cell = self.cells[(x, y)]

        arr = []
        if cell.height == 0:
            return []
        if (x-1, y) not in self.cells or self.cells[(x-1, y)].height < cell.height:
            arr.append('N')
        if (x, y+1) not in self.cells or self.cells[(x, y+1)].height < cell.height:
            arr.append('E')
        if (x, y-1) not in self.cells or self.cells[(x, y-1)].height < cell.height:
            arr.append('W')
        if (x+1, y) not in self.cells or self.cells[(x+1, y)].height < cell.height:
            arr.append('S')
        return arr

    def getRollCoord(self, x, y, d):
        if d == 'N':
            return (x-1, y)
        elif d == 'E':
            return (x, y+1)
        elif d == 'W':
            return (x, y-1)
        elif d == 'S':
            return (x+1, y)

    def countVals(self):
        count = [0, 0, 0, 0, 0, 0]
        for coord in self.cells:
            cell = self.cells[coord]
            count[ cell.val - 1 ] += 1
        return count


if __name__ == '__main__':
    while True:
        N = int(input())
        if N == 0:
            break

        grid = Grid()
        for _ in range(N):
            t, f = [ int(x) for x in list(filter(lambda x: x != '', \
                input().strip().split(' '))) ]
            dice = Dice(t, f)
            grid.drop(dice, 0, 0)

        print(" ".join(list(map(str, grid.countVals()))))