/* TU Darmstadt Programming Contest 2001
 * Problem: Cocktails
 * Author : Joerg Treschau <treschau@informatik.tu-darmstadt.de>
 * Comment: 1 Euro - ca. 2000 Lira
 */

#include <stdio.h>

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;


vector<string> cocktailList;
vector<string> ingredientList;

int mixCount;

/**
 */
class Ingredient
{
	public:
		string mName;
		int mAmount;
		int mPrice;

	friend istream& operator >> (istream& stream, Ingredient& ingredient)
	{
		stream >> ingredient.mName >> ingredient.mAmount >> ingredient.mPrice;
		return stream;
	}

	friend ostream& operator << (ostream& stream, const Ingredient& ingredient)
	{
		stream << ingredient.mName << " "
			<< ingredient.mAmount << " "
			<< ingredient.mPrice;
		return stream;
	}
};

/**
 */
class CocktailIngredient
{
	public:
		string mName;
		int mAmount;

	friend istream& operator >> (istream& stream, CocktailIngredient& ingredient)
	{
		stream >> ingredient.mName >> ingredient.mAmount;
		return stream;
	}

	friend ostream& operator << (ostream& stream, const CocktailIngredient& ingredient)
	{
		stream << ingredient.mName << " " << ingredient.mAmount;
		return stream;
	}
};

/**
 */
class Cocktail
{
	public:
		Cocktail(void) : mPrice(0), mMixed(0)
		{
		}

		bool mixable(map<string,Ingredient>& ingredients) const
		{
			for(vector<CocktailIngredient>::const_iterator ci=mIngredients.begin(); ci!=mIngredients.end(); ci++)
			{
				// check if ingredient is available
				if(find(ingredientList.begin(), ingredientList.end(), ci->mName) == ingredientList.end())
				{
					// cerr << "ingredient " << ci->mName << " not available" << endl;
					return false;
				}

				if(ci->mAmount > ingredients[ci->mName].mAmount)
				{
					//cerr << *ci << " not available: " << ingredients[ci->mName].mAmount << endl;
					return false;
				}
			}
			return true;
		}

		void mix(map<string,Ingredient>& ingredients)
		{
			for(vector<CocktailIngredient>::const_iterator ci=mIngredients.begin(); ci!=mIngredients.end(); ci++)
			{
				ingredients[ci->mName].mAmount -= ci->mAmount;
			}
			mMixed++;
			mixCount++;
		}

		string mName;
		vector<CocktailIngredient> mIngredients;
		int mPrice;
		int mMixed;

	friend bool operator < (const Cocktail& c1, const Cocktail& c2)
	{
		if(c1.mMixed == c2.mMixed)
		{
			vector<string>::iterator pos1 = find(cocktailList.begin(), cocktailList.end(), c1.mName);
			vector<string>::iterator pos2 = find(cocktailList.begin(), cocktailList.end(), c2.mName);
			return pos1 < pos2;
		}
		return c1.mMixed > c2.mMixed;
	}

	friend istream& operator >> (istream& stream, Cocktail& cocktail)
	{
		int ic;
		stream >> cocktail.mName >> ic;
		CocktailIngredient ingredient;
		for(int i=0; i<ic; i++)
		{
			stream >> ingredient;
			cocktail.mIngredients.push_back(ingredient);
		}
		return stream;
	}

	friend ostream& operator << (ostream& stream, const Cocktail& cocktail)
	{
		stream << cocktail.mName << " " << cocktail.mIngredients.size() << "\n";
		for(vector<CocktailIngredient>::const_iterator ci=cocktail.mIngredients.begin(); ci!=cocktail.mIngredients.end(); ci++)
		{
			stream << *ci;
			if(ci != cocktail.mIngredients.end()-1)
				stream << " ";
		}
		return stream;
	}
};


/**
 */
void printStatus(map<string,Cocktail>& cocktails, map<string,Ingredient>& ingredients, vector<string>& orders)
{
	cerr << cocktails.size() << endl;
	for(map<string,Cocktail>::iterator it=cocktails.begin(); it!=cocktails.end(); it++)
	{
		cerr << it->second << endl;
	}

	cerr << ingredients.size() << endl;
	for(map<string,Ingredient>::iterator it=ingredients.begin(); it!=ingredients.end(); it++)
	{
		cerr << it->second << endl;
	}

	cerr << orders.size() << endl;
	for(vector<string>::iterator it=orders.begin(); it!=orders.end(); it++)
	{
		cerr << *it << endl;
	}
}

/**
 */
void solve(int run, map<string,Cocktail>& cocktails, map<string,Ingredient>& ingredients, vector<string>& orders)
{
	/*
	printStatus(cocktails, ingredients, orders);
	cerr << endl;
	 */

	// calculate prices
	for(map<string,Cocktail>::iterator it=cocktails.begin(); it!=cocktails.end(); it++)
	{
		int price = 0;
		for(vector<CocktailIngredient>::iterator it2=it->second.mIngredients.begin(); it2!=it->second.mIngredients.end(); it2++)
		{
			price += it2->mAmount * ingredients[it2->mName].mPrice;
		}
		it->second.mPrice = 10000 + price*3;
	}

	// mix cocktails
	string last = "";
	for(vector<string>::iterator it=orders.begin(); it!=orders.end(); it++)
	{
		string order = *it;

		// check if cocktail is in list
		if(find(cocktailList.begin(), cocktailList.end(), order) == cocktailList.end())
		{
			//cerr << "cockail " << order << " not in list" << endl;
			continue;
		}

		//cerr << order << ":" << endl;
		for(unsigned int i=0; i<cocktails.size(); i++)
		{
			//cerr << "check: " << order << endl;
			if(order!=last && cocktails[order].mixable(ingredients))
			{
				cocktails[order].mix(ingredients);
				last = order;
				//cerr << "mixed: " << order << endl;
				break;
			}

			// find next cocktail
			vector<string>::iterator next = find(cocktailList.begin(), cocktailList.end(), order);
			next++;
			if(next == cocktailList.end())
				next = cocktailList.begin();
			order = *next;
		}
	}

	/*
	cerr << endl;
	printStatus(cocktails, ingredients, orders);
	 */

	// build top list
	vector<Cocktail> cv;
	for(map<string,Cocktail>::iterator it=cocktails.begin(); it!=cocktails.end(); it++)
		cv.push_back(it->second);

	sort(cv.begin(), cv.end());

	cout << "Scenario " << run << " top cocktails:\n";
	int t=0;
	for(vector<Cocktail>::iterator it=cv.begin(); it!=cv.end() && t<10; it++, t++)
	{
		if(it->mMixed>0)
		{
			// FG: output # of times mixed instead of percent
			cout << t+1 << " " << it->mName << " "
				<< it->mMixed << " " << it->mPrice << "\n";
		}
		else
			break;
	}
}


int main(void)
{
	int runs = 0;
	cin >> runs;
	for(int r=0; r<runs; r++)
	{
		cocktailList.clear();
		ingredientList.clear();
		mixCount = 0;

		int c = 0;
		map<string,Cocktail> cocktails;
		map<string,Ingredient> ingredients;
		vector<string> orders;

		cin >> c;
		for(int i=0; i<c; i++)
		{
			Cocktail cocktail;
			cin >> cocktail;
			cocktails[cocktail.mName] = cocktail;
			cocktailList.push_back(cocktail.mName);
		}

		cin >> c;
		for(int i=0; i<c; i++)
		{
			Ingredient ingredient;
			cin >> ingredient;
			ingredients[ingredient.mName] = ingredient;
			ingredientList.push_back(ingredient.mName);
		}

		cin >> c;
		for(int i=0; i<c; i++)
		{
			string order;
			cin >> order;
			orders.push_back(order);
		}

		solve(r+1, cocktails, ingredients, orders);
	}
	return 0;
}
