HZNUOJ

骰子代数

Tags:
Time Limit:  1 s      Memory Limit:   128 MB
Submission:173     AC:29     Score:99.07

Description

有一种鲜为人知的代数系统叫做骰子代数(Dice Algebra)
大概长这样:"2d6 + 12"
下面是严格的定义:
<notation> ::= <term> "+" <notation>
            |  <term> "-" <notation>
            |  <term>
<term> ::= <factor> "*" <term>
        |  <factor> "/" <term>
        |  <factor>
<factor> ::= "(" <notation> ")"
          |  <integer>
          |  <dice>
<integer> ::= <digit> <integer>
           |  <digit>
<digit> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
<dice> ::= <integer> "d" <integer>
        |  "d" <integer>
然而这种代数在计算机内很难被识别,你需要将其转化为计算机能识别的版本。
给你一行骰子代数,你需要做三件事:
  1. 扩展<dice>,例如"3d5"你需要扩展成"([d5] + [d5] + [d5])","1d5"或者"d5"需要扩展成"[d5]",即"adb"要扩展成a个[db]相加,输入保证0<a<=50。
  2. 消除多余的空格和制表符('\t'),只在运算符("+" / "-" / "*" / "/")的两边分别加一个空格。
  3. 在结尾加上' = [Result]'

具体见样例。

——改编自Zhejiang University Programming Contest

Input

第一行一个整数T,表示有T组数据。

每组数据包含一行字符串s(|s|<=200000)。

Output

对每组数据,输出一行转换后的字符串。

Samples

input
3 d6+1 ((2d6) +5)*((12* 3d6)) 2d10 * d100
output
[d6] + 1 = [Result] ((([d6] + [d6])) + 5) * ((12 * ([d6] + [d6] + [d6]))) = [Result] ([d10] + [d10]) * [d100] = [Result]

Author

WEI, Lixin

Source

Zhejiang University Programming Contest