#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

class Commodity
{
public:
    int total, num, high, low;
    Commodity(){
        total = num = 0;
        high = INT_MIN;
        low = INT_MAX;
    }
    void add(int price){
        total += price;
        ++ num;
        high = max(high, price);
        low = min(low, price);
    }
};

class Dealer
{
public:
    int sell, buy;
    Dealer(){
        buy = sell = 0;
    }
};

int main()
{
    for(;;){
        int n;
        cin >> n;
        if(n == 0)
            return 0;

        list<pair<string, pair<string, int> > > buy, sell;
        map<string, Commodity> commodity;
        map<string, Dealer> dealer;

        while(--n >= 0){
            string nameD, type, nameC;
            int price;
            cin >> nameD >> type >> nameC >> price;
            dealer[nameD];

            if(type == "SELL"){
                list<pair<string, pair<string, int> > >::iterator it, maxIt;
                string maxNameD;
                int maxPrice = INT_MIN;
                for(it=buy.begin(); it!=buy.end(); ++it){
                    if(nameD != it->first && nameC == it->second.first && it->second.second >= price && it->second.second > maxPrice){
                        maxNameD = it->first;
                        maxPrice = it->second.second;
                        maxIt = it;
                    }
                }
                if(maxPrice != INT_MIN){
                    price = (price + maxPrice) / 2;
                    dealer[nameD].sell += price;
                    dealer[maxNameD].buy += price;
                    commodity[nameC].add(price);
                    buy.erase(maxIt);
                }else{
                    sell.push_back(make_pair(nameD, make_pair(nameC, price)));
                }
            }else{
                list<pair<string, pair<string, int> > >::iterator it, minIt;
                string minNameD;
                int minPrice = INT_MAX;
                for(it=sell.begin(); it!=sell.end(); ++it){
                    if(nameD != it->first && nameC == it->second.first && it->second.second <= price && it->second.second < minPrice){
                        minNameD = it->first;
                        minPrice = it->second.second;
                        minIt = it;
                    }
                }
                if(minPrice != INT_MAX){
                    price = (price + minPrice) / 2;
                    dealer[nameD].buy += price;
                    dealer[minNameD].sell += price;
                    commodity[nameC].add(price);
                    sell.erase(minIt);
                }else{
                    buy.push_back(make_pair(nameD, make_pair(nameC, price)));
                }
            }
        }

        map<string, Commodity>::iterator it1;
        for(it1=commodity.begin(); it1!=commodity.end(); ++it1){
            cout << it1->first << ' ' << it1->second.low << ' '
                 << (it1->second.total / it1->second.num) << ' ' << (it1->second.high) << endl;
        }
        cout << "--" << endl;

        map<string, Dealer>::iterator it2;
        for(it2=dealer.begin(); it2!=dealer.end(); ++it2){
            cout << it2->first << ' ' << it2->second.buy << ' ' << it2->second.sell << endl;
        }
        cout << "----------" << endl;
    }
}

Measured memory_bytes: 0