Problem:
Problem B: e-Market

The city of Hakodate recently established a commodity exchange market. To participate in the market, each dealer transmits through the Internet an order consisting of his or her name, the type of the order (buy or sell), the name of the commodity, and the quoted price.

In this market a deal can be made only if the price of a sell order is lower than or equal to the price of a buy order. The price of the deal is the mean of the prices of the buy and sell orders, where the mean price is rounded downward to the nearest integer. To exclude dishonest deals, no deal is made between a pair of sell and buy orders from the same dealer. The system of the market maintains the list of orders for which a deal has not been made and processes a new order in the following manner.

For a new sell order, a deal is made with the buy order with the highest price in the list satisfying the conditions. If there is more than one buy order with the same price, the deal is made with the earliest of them.

For a new buy order, a deal is made with the sell order with the lowest price in the list satisfying the conditions. If there is more than one sell order with the same price, the deal is made with the earliest of them.

The market opens at 7:00 and closes at 22:00 everyday. When the market closes, all the remaining orders are cancelled. To keep complete record of the market, the system of the market saves all the orders it received everyday.

The manager of the market asked the system administrator to make a program which reports the activity of the market. The report must contain two kinds of information. For each commodity the report must contain informationon the lowest, the average and the highest prices of successful deals. For each dealer, the report must contain information on the amounts the dealer paid and received for commodities.

Input

The input contains several data sets. Each data set represents the record of the market on one day. The first line of each data set contains an integer n (n < 1000) which is the number of orders in the record. Each line of the record describes an order, consisting of the name of the dealer, the type of the order, the name of the commodity, and the quoted price. They are separated by a single space character.

The name of a dealer consists of capital alphabetical letters and is less than 10 characters in length. The type of an order is indicated by a string, "BUY" or "SELL". The name of a commodity is a single capital letter. The quoted price is a positive integer less than 1000.

The orders in a record are arranged according to time when they were received and the first line of the record corresponds to the oldest order.

The end of the input is indicated by a line containing a zero.

Output

The output for each data set consists of two parts separated by a line containing two hyphen (`-') characters.

The first part is output for commodities. For each commodity, your program should output the name of the commodity and the lowest, the average and the highest prices of successful deals in one line. The name and the prices in a line should be separated by a space character. The average price is rounded downward to the nearest integer. The output should contain only the commodities for which deals are made and the order of the output must be alphabetic.

The second part is output for dealers. For each dealer, your program should output the name of the dealer, the amounts the dealer paid and received for commodities. The name and the numbers in a line should be separated by a space character. The output should contain all the dealers who transmitted orders. The order of dealers in the output must be lexicographic on their names. The lexicographic order is the order in which words in dictionaries are arranged.

The output for each data set should be followed by a linecontaining ten hyphen (`-') characters.

Sample Input

3
PERLIS SELL A 300
WILKES BUY A 200
HAMMING SELL A 100
4
BACKUS SELL A 10
FLOYD BUY A 20
IVERSON SELL B 30
BACKUS BUY B 40
7
WILKINSON SELL A 500
MCCARTHY BUY C 300
WILKINSON SELL C 200
DIJKSTRA SELL B 100
BACHMAN BUY A 400
DIJKSTRA BUY A 600
WILKINSON SELL A 300
2
ABCD SELL X 10
ABC BUY X 15
2
A SELL M 100
A BUY M 100
0

Output for the Sample Input

A 150 150 150
--
HAMMING 0 150
PERLIS 0 0
WILKES 150 0
----------
A 15 15 15
B 35 35 35
--
BACKUS 35 15
FLOYD 15 0
IVERSON 0 35
----------
A 350 450 550
C 250 250 250
--
BACHMAN 350 0
DIJKSTRA 550 0
MCCARTHY 250 0
WILKINSON 0 1150
----------
X 12 12 12
--
ABC 12 0
ABCD 0 12
----------
--
A 0 0
----------

Solution in JAVA (judge verdict: Accepted)

import java.awt.geom.*;
import java.io.*;
import java.util.*;


public class Main {
	//2041 start
	
	class E implements Comparable<E>{
		char name;
		int min, max, sum, count;
		public E(char name) {
			this.name = name;
			min = 1 << 24;
			max = 0;
			sum = 0;
			count = 0;
		}
		
		private void calc(int num){
			sum += num;
			min = Math.min(min, num);
			max = Math.max(max, num);
			count++;
		}
		public int compareTo(E o) {
			if(this.name < o.name) return -1;
			if(this.name > o.name) return 1;
			return 0;
		}
	}
	
	class D implements Comparable<D>{
		String name;
		int pay, gain;
		public D(String name, int pay, int gain) {
			this.name = name;
			this.pay = pay;
			this.gain = gain;
		}
		public int compareTo(D o) {
			return this.name.compareTo(o.name);
		}
		@Override
		public String toString() {
			return name + " " + pay + " " + gain;
		}
	}
	
	class C{
		String s;
		int digit;
		char goods;
		int value;
		public C(String s, int digit, char goods, int value) {
			this.s = s;
			this.digit = digit;
			this.goods = goods;
			this.value = value;
		}
	}
	
	private void doit(){
		Scanner sc = new Scanner(System.in);
		while(true){
			int n = sc.nextInt();
			if(n == 0)break;
			HashMap<String, D> humlist = new HashMap<String, D>();
			HashMap<Character, E> goodslist = new HashMap<Character, E>();
			ArrayList<C> data = new ArrayList<Main.C>();
			for(int i =0; i < n; i++){
				
				String name = sc.next();
				String str = sc.next();
				int digit = 0;
				if(str.equals("SELL")) digit = 1;
				else digit = -1;
				char goods= sc.next().charAt(0);
				int value = sc.nextInt();
				humlist.put(name, new D(name,0,0));
				goodslist.put(goods, new E(goods));
				data.add(new C(name, digit, goods, value));
			}
			
			ArrayList<C> record = new ArrayList<Main.C>();
			for(int i = 0; i < n;i++){
				C now = data.get(i);
				if(now.digit > 0){
					//sell
					int max = 0;
					int ind = -1;
					for(int j = 0; j < record.size(); j++){
						C nowj = record.get(j);
						if(nowj.digit < 0 && ! now.s.equals(nowj.s) && now.goods == nowj.goods){
							if(nowj.value > max){
								max = nowj.value;
								ind = j;
							}
						}
					}
					if(ind!= -1){
						C buyer = record.get(ind);
						if(buyer.value < now.value){
							record.add(now);
						}
						else{
							int res = (buyer.value + now.value) / 2;
							D a = humlist.get(now.s);
							a.gain = a.gain + res;
							D b = humlist.get(buyer.s);
							b.pay = b.pay + res;
							humlist.put(now.s, a);
							humlist.put(buyer.s, b);
							record.remove(ind);
							E c = goodslist.get(now.goods);
							c.calc(res);
							
						}
					}
					else{
						record.add(now);
					}
				}
				else{
					//buy
					int min = 1 << 24;
					int ind = -1;
					for(int j = 0; j < record.size(); j++){
						C nowj = record.get(j);
						if(nowj.digit > 0 && ! now.s.equals(nowj.s) && now.goods == nowj.goods){
							if(nowj.value < min){
								min = nowj.value;
								ind = j;
							}
						}
					}
					if(ind!= -1){
						C seller = record.get(ind);
						if(now.value < seller.value){
							record.add(now);
						}
						else{
							
							int res = (seller.value + now.value) / 2;
							D a = humlist.get(now.s);
							a.pay = a.pay + res;
							D b = humlist.get(seller.s);
							b.gain = b.gain + res;
							humlist.put(now.s, a);
							humlist.put(seller.s, b);
							record.remove(ind);
							E c = goodslist.get(now.goods);
							c.calc(res);
						}
					}
					else{
						record.add(now);
					}
				}
			}
			
			ArrayList<E> goods = new ArrayList<E>();
			ArrayList<D> hum = new ArrayList<Main.D>();
			for(String key: humlist.keySet()){
				hum.add(humlist.get(key));
			}
			
			for(char key: goodslist.keySet()){
				goods.add(goodslist.get(key));
			}
			Collections.sort(hum);
			Collections.sort(goods);
			
			for(E res: goods){
				if(res.count == 0) continue;
				System.out.println(res.name + " " + res.min + " " + (res.sum / res.count) + " " + res.max);
			}
			System.out.println("--");
			for(D res: hum){
				System.out.println(res);
			}
			System.out.println("----------");
		}
	}

	public static void main(String [] args){
		new Main().doit();
	}
}