#include <stdio.h>
#include <string.h>

int main ( void ) {
    
    char str[1000], str2[1000], str3[1000], s[7];
    int q, a, b;

    scanf ( "%s %d", str, &q );

    for ( int i = 0; i < q; i++ ) {

        scanf ( "%s", s );

        if ( strcmp ( s, "print" ) == 0 ) {
            scanf ( "%d %d", &a, &b );
            for ( int j = a; j < b + 1; j++ ) {
                printf ( "%c", str[j] );
            }
            printf ( "\n" );
        }

        else if ( strcmp ( s, "reverse" ) == 0 ) {
            scanf ( "%d %d", &a, &b );
            for ( int j = a, k = b; j < b + 1; j++, k-- ) {
                str2[k] = str[j];
            }
            for ( int j = a; j < b + 1; j++ ) {
                str[j] = str2[j];
            }
        }

        else if ( strcmp ( s, "replace" ) == 0 ) {
            scanf ( "%d %d %s", &a, &b, str3 );
            for ( int j = a, k = 0; j < b + 1; j++, k++ ){
                str[j] = str3[k];
            }
        }

    }
    
    return 0;
}

#include <bits/stdc++.h>
using namespace std;

int main(void){
    int N;
    while( cin >> N , N ){
        string L[50];
        int P[50], A[50], B[50], C[50], D[50], E[50], F[50], S[50], M[50];
        vector< pair<double, string> > ans(N);

        for(int i = 0; i < N; i++){
            int time = 0, value = 0;
            cin >> L[i] >> P[i] >> A[i] >> B[i] >> C[i] >> D[i] >> E[i] >> F[i] >> S[i] >> M[i];
            ans[i].second = L[i];
            time = A[i] + B[i] + C[i] + (D[i] + E[i]) * M[i];
            value = M[i] * F[i] * S[i] - P[i];
            ans[i].first = -value / (double)time;
        }

        sort(ans.begin(), ans.end());
        for(int i = 0;  i< N; i++){
            cout << ans[i].second << endl;
        }
        cout << "#" << endl;

    }
    return 0;
}