Basically Speaking
Difficulty: Hard

The Really Neato Calculator Company, Inc. has recently hired your team to help design their Super Neato Model I calculator. As a computer scientist you suggested to the company that it would be neato if this new calculator could convert among number bases. The company thought this was a stupendous idea and has asked your team to come up with the prototype program for doing base conversion. The project manager of the Super Neato Model I calculator has informed you that the calculator will have the following neato features:

* It will have a 7-digital display.
* Its buttons will include the capital letters A through F in addition to the digits 0 through 9.
* It will support bases 2 through 16.

**Input Format:**  
The input for your prototype program will consist of one base conversion per line. There will be three numbers per line. The first number will be the number in the base you are converting from. The second number is the base you are converting from. The third number is the base you are converting to. There will be one or more blanks surrounding (on either side of) the numbers. There are several lines of input and your program should continue to read until the input given is `*`. 

**Output Format:**  
The output will only be the converted number as it would appear on the display of the calculator. The number should be right justified in the 7-digit display. If the number is too large to appear on the display, then print `ERROR` right justified in the display.

**Sample Input:**

	1111000  2 10  
	1111000  2 16  
	2102101  3 10  
	2102101  3 15  
	  12312  4  2  
	     1A 15  2  
	1234567 10 16  
	   ABCD 16 15  
       *

**Expected Output:**

	    120
	     78
	   1765
	    7CA
	  ERROR
	  11001
	 12D687
	   D071

Input Format:

Output Format:

Sample Input:

Sample Output:

Python 3 starter code:


Reference solution (C++):
	//When the input is taken from a data file and output is printed to a data file.
    #include <iostream.h>
	#include <iomanip.h>
	#include <fstream.h>
	#include <string.h>

	const int A_10 = 55;
	const int ZERO = 48;
	const int ASIZE = 8;
	const int WSIZE = 7;
	const int SIZE_1 = 6;

	long int power (int, int);
	long int convert_to_10 (int, char *);
	int      convert_to_base (int, long int, char *);

	long int power (int base, int raise)
	 {
	  long int prod = 1;
	  int      i;

	  for (i = 0; i < raise; i++)
	   prod *= base;

	  return prod;
	 }

	long int convert_to_10 (int base, char * cnum)
	 {
	  int      len = strlen (cnum) - 1;
	  int      i;
	  int      raise = 0;
	  int      num;
	  long int base10 = 0;

	  for (i = len; i >= 0 && cnum[i] != ' '; i--)
	   {
    if (cnum[i] >= '0' && cnum[i] <= '9')
     num = cnum[i] - ZERO;
    else
     num = cnum[i] - A_10;

    base10 += num * power (base, raise);

    raise++;
	   }

	  return base10;
	 }

	int convert_to_base (int base, long int number, char * cnum)
	 {
	  int  pos = SIZE_1;
	  int  valid = 1;
	  int  remain;
	  char chremain;

	  strcpy (cnum, "       ");

	  while (number > 0 && valid)
	   {
    remain = number % base;
    number /= base;

    if (remain < 10)
     chremain = remain + ZERO;
    else
     chremain = remain + A_10;

    if (pos >= 0)
     {
      cnum[pos] = chremain;
      pos--;
     }
    else
     valid = 0;
	   }

	  return valid;
	 }

	void main ()
	 {
	  char     old_cnum[ASIZE];
	  char     new_cnum[ASIZE];
	  int      base_from;
	  int      base_to;
	  int      valid;
	  long int number;
	  fstream  in_file;
	  ofstream out_file;

	  in_file.open ("base.in", ios::in);
	  out_file.open ("base.out");

	  in_file >> ws >> old_cnum >> base_from >> base_to;

	  while (!in_file.eof ())
	   {
    number = convert_to_10 (base_from, old_cnum);
    valid = convert_to_base (base_to, number, new_cnum);

    if (valid)
     out_file << setw (WSIZE) << new_cnum << endl;
    else
     out_file << setw (WSIZE) << "ERROR" << endl;

    in_file >> ws >> old_cnum >> base_from >> base_to;
	   }

	  in_file.close ();
	  out_file.close ();
	 }