#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)

int time(int a,int b,int c,int d,int e,int m){
	int ret =  a+b+c+d+e;
	rep(i,m-1){
		ret += d+e;
	}
	return ret;
}
bool cmp(const pair<double,string>&a,const pair<double,string>&b){
	if( fabs(a.first-b.first) < 1e-8){
		return a.second < b.second;
	}
	return a.first > b.first;
}
int main(){
	int n;
	
	while(cin >> n , n){
		vector< pair<double,string> > data(n);
		rep(i,n){
			string l; int p,a,b,c,d,e,f,s,m;
			cin >> l >> p >> a >> b >> c >> d >> e >> f >> s >> m;
			data[i].first = (double)(f*s*m-p)/time(a,b,c,d,e,m);
			data[i].second = l;
		}
		sort(data.begin(),data.end(),cmp);
		rep(i,n){
			cout << data[i].second << endl;
		}
		cout << "#" << endl;
	}
}
#include <iostream>
#define N 100
#define WHITE 0
#define BLACK 1
#define INF 10000

using namespace std;

int M[N][N],n,d[N];

void prim(){
  int color[n],p[n];
  for(int i=0;i<n;i++){
    color[i]=WHITE;
    d[i]=INF;
  }
  d[0]=0;
  p[0]=-1;
  while(1){
    int mincost=INF;
    int u;
    for(int i=0;i<n;i++){
      if(color[i]!=BLACK && d[i]<mincost){
        mincost=d[i];
        u=i;
      }
    }
    if(mincost==INF) break;
    //connect u to p[u]
    color[u]=BLACK;
    for(int v=0;v<n;v++){
      if(color[v]!=BLACK && M[u][v]!=-1){
        if(M[u][v]<d[v]){
          d[v]=M[u][v];
          p[v]=u;
        }
      }
    }
  }
}

int main(){
  cin >> n;
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++) cin >> M[i][j];
  }
  prim();
  int sum=0;
  for(int i=0;i<n;i++) sum+=d[i];
  cout << sum << endl;
}