{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "dico  = {\"ZERO\":0,\n",
    "        \"ONE\":1,\n",
    "        \"TWO\":2,\n",
    "        \"THREE\":3,\n",
    "        \"FOUR\":4,\n",
    "        \"FIVE\":5,\n",
    "        \"SIX\":6,\n",
    "        \"SEVEN\":7,\n",
    "        \"EIGHT\":8,\n",
    "        \"NINE\":9,}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def solve(line):\n",
    "    res = \"\"\n",
    "    if(\"Z\" in line):\n",
    "        cpt_0 = line.count(\"Z\")\n",
    "        for i in range(cpt_0):\n",
    "            res+=\"0\"\n",
    "        line = line.replace(\"Z\",\"\",cpt_0)\n",
    "        line = line.replace(\"E\",\"\",cpt_0)\n",
    "        line = line.replace(\"R\",\"\",cpt_0)\n",
    "        line = line.replace(\"O\",\"\",cpt_0)\n",
    "    if(\"W\" in line):\n",
    "        cpt_0 = line.count(\"W\")\n",
    "        for i in range(cpt_0):\n",
    "            res+=\"2\"\n",
    "        line = line.replace(\"W\",\"\",cpt_0)\n",
    "        line = line.replace(\"T\",\"\",cpt_0)\n",
    "        line = line.replace(\"O\",\"\",cpt_0)\n",
    "    if(\"X\" in line):\n",
    "        cpt_0 = line.count(\"X\")\n",
    "        for i in range(cpt_0):\n",
    "            res+=\"6\"\n",
    "        line = line.replace(\"S\",\"\",cpt_0)\n",
    "        line = line.replace(\"I\",\"\",cpt_0)\n",
    "        line = line.replace(\"X\",\"\",cpt_0)\n",
    "    if(\"U\" in line):\n",
    "        cpt_0 = line.count(\"U\")\n",
    "        for i in range(cpt_0):\n",
    "            res+=\"4\"\n",
    "        line = line.replace(\"U\",\"\",cpt_0)\n",
    "        line = line.replace(\"F\",\"\",cpt_0)\n",
    "        line = line.replace(\"R\",\"\",cpt_0)\n",
    "        line = line.replace(\"O\",\"\",cpt_0)\n",
    "    if(\"G\" in line):\n",
    "        cpt_0 = line.count(\"G\")\n",
    "        for i in range(cpt_0):\n",
    "            res+=\"8\"\n",
    "        line = line.replace(\"H\",\"\",cpt_0)\n",
    "        line = line.replace(\"E\",\"\",cpt_0)\n",
    "        line = line.replace(\"I\",\"\",cpt_0)\n",
    "        line = line.replace(\"G\",\"\",cpt_0)\n",
    "        line = line.replace(\"T\",\"\",cpt_0)\n",
    "    if(\"S\" in line):\n",
    "        cpt_0 = line.count(\"S\")\n",
    "        for i in range(cpt_0):\n",
    "            res+=\"7\"\n",
    "        line = line.replace(\"S\",\"\",cpt_0)\n",
    "        line = line.replace(\"E\",\"\",cpt_0*2)\n",
    "        line = line.replace(\"V\",\"\",cpt_0)\n",
    "        line = line.replace(\"N\",\"\",cpt_0)\n",
    "    if(\"O\" in line):\n",
    "        cpt_0 = line.count(\"O\")\n",
    "        for i in range(cpt_0):\n",
    "            res+=\"1\"\n",
    "        line = line.replace(\"O\",\"\",cpt_0)\n",
    "        line = line.replace(\"N\",\"\",cpt_0)\n",
    "        line = line.replace(\"E\",\"\",cpt_0)\n",
    "    if(\"F\" in line):\n",
    "        cpt_0 = line.count(\"F\")\n",
    "        for i in range(cpt_0):\n",
    "            res+=\"5\"\n",
    "        line = line.replace(\"F\",\"\",cpt_0)\n",
    "        line = line.replace(\"I\",\"\",cpt_0)\n",
    "        line = line.replace(\"V\",\"\",cpt_0)\n",
    "        line = line.replace(\"E\",\"\",cpt_0)\n",
    "    if(\"I\" in line):\n",
    "        cpt_0 = line.count(\"I\")\n",
    "        for i in range(cpt_0):\n",
    "            res+=\"9\"\n",
    "        line = line.replace(\"N\",\"\",cpt_0*2)\n",
    "        line = line.replace(\"I\",\"\",cpt_0)\n",
    "        line = line.replace(\"E\",\"\",cpt_0)\n",
    "    if(\"T\" in line):\n",
    "        cpt_0 = line.count(\"T\")\n",
    "        for i in range(cpt_0):\n",
    "            res+=\"3\"\n",
    "        line = line.replace(\"T\",\"\",cpt_0)\n",
    "        line = line.replace(\"H\",\"\",cpt_0)\n",
    "        line = line.replace(\"T\",\"\",cpt_0)\n",
    "        line = line.replace(\"E\",\"\",cpt_0*2)\n",
    "    bla=[]\n",
    "    for i in res:\n",
    "        bla.append(int(i))\n",
    "    bla.sort()\n",
    "    res = \"\"\n",
    "    for i in bla:\n",
    "        res+=str(i)\n",
    "    return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "input_file = 'A-small-attempt5.in'\n",
    "output_file = 'out.out'\n",
    "f = open(input_file, 'r')\n",
    "output = open(output_file, 'w')\n",
    "n_lines = int(f.readline().strip())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "for num_line, line in enumerate(f):\n",
    "    line = line.strip()\n",
    "    res = solve(line)\n",
    "    output.writelines(\"Case #\"+str(num_line+1)+\": \"+str(res)+\"\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "f.close()\n",
    "output.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
