Galactic Import
Difficulty: Hard

With the introduction of the new ThrustoZoom gigadimensional drive, it has become possible for HyperCommodities, the import/export conglomerate from New Jersey, to begin trading with even the most remote galaxies in the universe. HyperCommodities wants to import goods from some of the galaxies in the Plural Z sector. Planets within these galaxies export valuable products and raw materials like vacuuseal, transparent aluminum, digraphite, and quantum steel. Preliminary reports have revealed the following facts:

* Each galaxy contains at least one and at most 26 planets. Each planet within a galaxy is  identified by a unique letter from A to Z.
* Each planet specializes in the production and export of one good. Different planets within the same galaxy export different goods.
* Some pairs of planets are connected by hyperspace shipping lines. If planets A and B are connected, they can trade goods freely. If planet C is connected to B but not to A, then A and C can still trade goods with each other through B, but B keeps 5% of the shipment as a shipping fee. (Thus A only receives 95% of what C shipped, and C receives only 95% of what A shipped.) In general, any two planets can trade goods as long as they are connected by some set of shipping lines, but each intermediate planet along the shipping route keeps 5% of what it shipped (which is not necessarily equal to 5% of the original shipment).
* At least one planet in each galaxy is willing to open a ThrustoZoom shipping line to Earth. A ThrustoZoom line is the same as any other shipping line within the galaxy, as far as business is concerned. For example, if planet K opens a ThrustoZoom line to Earth, then the Earth can trade goods freely with K, or it can trade goods with any planet connected to K, subject to the usual shipping fees.

HyperCommodities has assigned a relative value (a positive real number less than 10) to each planet's chief export. The higher the number, the more valuable the product. More valuable products can be resold with a higher profit margin in domestic markets. The problem is to determine which planet has the most valuable export when shipping fees are taken into account.

**Input Format:**
The consists of one or more galaxy descriptions. Each galaxy description begins with a line containing an integer N which specifies the number of planets in the galaxy. The next N lines contain descriptions of each planet, which consist of:

1. The letter used to represent the planet.
2. A space.
3. The relative value of the planet's export, in the form d.dd.
4. A space.
5. A string containing letters and/or the character `'*'`; a letter indicates a shipping line to that planet, and a`'*'` indicates a willingness to open a ThrustoZoom shipping line to Earth.

**Output Format:**
For each galaxy description, output a single line which reads "Import from P" where P is the letter of the planet with the most valuable export, once shipping fees have been taken into account. (If more than one planet have the same most valuable export value then output the plant which is alphabetically first).

A **sample input** is shown here:

	1  
	F 0.81 *  
	5  
	E 0.01 *A  
	D 0.01 A*  
	C 0.01 *A  
	A 1.00 EDCB  
	B 0.01 A*  
	10  
	S 2.23 Q*  
	A 9.76 C  
	K 5.88 MI  
	E 7.54 GC  
	M 5.01 OK  
	G 7.43 IE  
	I 6.09 KG
	C 8.42 EA
	O 4.55 QM
	Q 3.21 SO

The following **output** should be produced from the above sample input:

	Import from F
	Import from A
	Import from A

Input Format:

Output Format:

Sample Input:

Sample Output:

Python 3 starter code:


Reference solution (C++):
	//When the input is from a file and the output is to a file. (Logic remains the same.)
    #include <stdio.h>
	#include <string.h>
	#include <math.h>

	#define INPUT_FILE_NAME  "import.in"
	#define OUTPUT_FILE_NAME "import.out"

	#define N         27
	#define INFINITY 100
	#define EARTH     26

	int distance[N][N];
	double value[N];

	FILE *infile, *outfile;


	void Floyd_Warshall (void)
	{
  		register int i, j, k;

  	for (k = 0; k < N; ++k)
    	for (i = 0; i < N; ++i)
      	for (j = 0; j < N; ++j)
        {
          int distance_ikj = distance[i][k] + distance[k][j];
          if (distance_ikj < distance[i][j])
            distance[i][j] = distance_ikj;
        }
	}


	void add_edge (char A, char B)
	{
  		int i = (A == '*') ? EARTH : A-'A';
  		int j = (B == '*') ? EARTH : B-'A';
  
  		distance[i][j] = 1;
  		distance[j][i] = 1;
	}


	void initialize (void)
	{
  		int i, j;

	  for (i = 0; i < N; ++i)
    	for (j = 0; j < N; ++j)
      		distance[i][j] = INFINITY;

 	 for (i = 0; i < N; ++i)
    	value[i] = 0.0;
	}


	double import_value (int planet)
	{
  		if (distance[EARTH][planet] < INFINITY)
    		return
      	pow (0.95, distance[EARTH][planet] - 1) * value[planet];
  	else
    	return 0.0;
	}


	void find_best ()
	{
  		double best = 0.0;
  	int i, planet = 0;

  	for (i = 0; i < EARTH; ++i)
    {
      double v = import_value (i);
      if (v > best)
        {
          best   = v;
          planet = i;
        }
    }

	  fprintf (outfile, "Import from %c\n", planet + 'A');
	}
      
	
	void do_galaxy (void)
	{	
	  int i, j, planets;
  
	  initialize ();	
	  fscanf (infile, "%d\n", &planets);

	  for (i = 0; i < planets; ++i)
    {
      double v;
      char p, neighbor[N];
      
      fscanf (infile, "%c %lf %s\n", &p, &v, neighbor);

      value[p-'A'] = v;
      for (j = 0; j < strlen (neighbor); ++j)
        add_edge (p, neighbor[j]);
    }

	  Floyd_Warshall ();
	  find_best ();	
	}
  
  

	void main (void)
	{
	  infile  = fopen (INPUT_FILE_NAME, "rt");
	  outfile = fopen (OUTPUT_FILE_NAME, "wt");

	  while (! feof (infile)) do_galaxy ();

	  fclose (infile);
	  fclose (outfile);
	}