#875. MS and the Food Store Ft. Mahjong

MS and the Food Store Ft. Mahjong

Background

 MS is a very NB teammate of Legendary Grandmaster DiDiDi. There are endless stories about how they won the championship in various Grand Prix.

 One day after they get the championship in GP of Zhijiang, MS goes to Zhijiang food street to find something to eat tonight.

 After visiting every food shop in the street, he found a ”Jansou” owned by fuko_ibuki in the corner, where one could play mahjong.

 There are 3434 different tiles in Majong, which can be divided into four suits: Manzu , Souzu , Pinzu , and Jihai . Manzu, Souzu, and Pinzu each have 9 kinds of tiles, and Jihai only has 7 kinds of tiles.

 Here are the pictures of tiles, each row represents a suit - Manzu, Souzu, Pinzu, and Jihai. image To simplify the expression, we use **number ** + suit to represent a tile. For example, ”1m” stands for Wanzu of 1, ”4s” stands for Souzu of 4, ”7p” stands for Pinzu of 7, and Jihai are numbered in the order of the picture above, such as ”4z” for North Wind.

 Mahjong starts with what we call a ”hand” of 1313 tiles, and with each turn, the player draws a new tile and discards one.

 A complete hand has 1414 tiles, consisting of seven different Toitsu or four Mentsu plus a Toitsu, where Toitsu refers to two tiles of the same suit with the same number, and Mentsu includes Shuntsu and Koutsu. Shuntsuis three consecutive cards of the same suit (not including Jihai, and cannot be cycled), and Koutsuis three cards of the same suit with the same number.

 For example, 1m2m3m1m2m3m is Shuntsu (abbreviated as 123m123m), 1z2z3z1z2z3z and 8m9m1m are not Shuntsu, 1m1m1m1m1m1m is Koutsu, and 1z1z1z1z1z1z is also Koutsu. And 123m789m111s789s99p123m789m 111s 789s 99p makes a complete hand.

 We call a hand Tenpai , that is, it has 1313 tiles, and a complete hand can be achieved by drawing one tile.

 For example, 123m789m111s78s99p123m 789m 111s 78s 99p can achieve complete hand by drawing 6p6p or 9p9p (or awaiting 6p6p and 9p9p in short). So this hand is Tenpai.

 In the process of learning mahjong, MS learns the concept of X-Shanten , in which ”X”represents the minimum number of rounds for a player to draw and discard tiles to achieve Tenpai. Since there are two ways to achieve the complete hand, we choose the one that is closer to the two types.

 For example, 13p567p245s77s11z3z13p 567p 245s 77s 11z 3z can draw 2p2p and discard 3z3z to become 123p567p245s77s11z123p 567p 245s 77s 11z, and draw 7s7s and discard 2s2s to become 123p567p45s777s11z123p 567p 45s 777s 11z, which is Tenpai to awaiting 3s3s and 6s6s, so this hand (13p567p245s77s11z3z13p 567p 245s 77s 11z 3z) is 22-Shanten.

 Since 77 pairs can also achieve the complete hand, the maximum number of ”x” is 66 (because we can change 66 tiles to pairs to achieve the Tenpai), otherwise, 147m258s369p1234z147m 258s 369p 1234z is ”8-Shanten”.

 we define Taatsu as 22 tiles awaiting one to achieve a Mentsu, such as 46s,45s,46s, 45s, and 55s,55s, and each tile could only be contained in one Taatsu. A simple way to calculate the ”x-Shanten” is x=8x = 8 - (Mentsu * 22 + Taatsu), but be careful in the case when you have 66 or more Taatsu.

 fuko_ibuki gives MS n different hands, and MS needs to tell her the hand is X-Shanten. To simplify the input, we use xxxx mm xxxx ss xxxx pp xxxx zz to give a hand (or leave out if there is no tile of the suit), like 123m456m123s789s44z123m 456m 123s 789s 44z would be represented as 123456m123789s44z123456m123789s44z.

Input

The first line contains an integer T(T5000)T ( T ≤ 5000), denoting the number of test cases. For each test case, the only line contains a ”hand” described above.

Output

For each test case, print one integer n in one line, denoting the x of ”x-Shanten”.

Samples

2
24577s13567p113z
33446m88s5599p12z
2
1