Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. Each test is supposed to run simultaneously in several places, and the ranklists will be merged immediately after the test. Now it is your job to write a program to correctly merge all the ranklists and generate the final rank.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive number  (), the number of test locations. Then  ranklists follow, each starts with a line containing a positive integer  (), the number of testees, and then  lines containing the registration number (a 13-digit number) and the total score of each testee. All the numbers in a line are separated by a space.

Output Specification:

For each test case, first print in one line the total number of testees. Then print the final ranklist in the following format:

registration_number final_rank location_number local_rank

The locations are numbered from 1 to . The output must be sorted in nondecreasing order of the final ranks. The testees with the same score must have the same rank, and the output must be sorted in nondecreasing order of their registration numbers.

Sample Input:

2
5
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
4
1234567890013 65
1234567890011 25
1234567890014 100
1234567890012 85

Sample Output:

9
1234567890005 1 1 1
1234567890014 1 2 1
1234567890001 3 1 2
1234567890003 3 1 2
1234567890004 5 1 4
1234567890012 5 2 2
1234567890002 7 1 5
1234567890013 8 2 3
1234567890011 9 2 4

#include <iostream>
#include <string>
#include <algorithm>
#include<math.h>
#include<queue>
#include<vector>
using namespace std;

struct testee {
    string registration_number;
    int score;
    int location_number;
    int final_rank;
    int local_rank;
}testees[30010],local_testees[310];

bool cmp(testee a, testee b) {//组内排序
    if (a.score != b.score)return a.score > b.score;//分数不同,则大分在前
    else return a.registration_number < b.registration_number;//分数相同,则小序号在前
}

void get_local_rank(testee a[],int k) {//获取组内排名
    a[1].local_rank = 1;
    for (int i = 2; i <= k; i++) {
        if (a[i].score != a[i - 1].score) {
            a[i].local_rank = i;
        }
        else {
            a[i].local_rank = a[i - 1].local_rank;
        }
    } 
}

void get_final_rank(testee a[], int k) {//获取最终排名
    a[1].final_rank = 1;
    for (int i = 2; i <= k; i++) {
        if (a[i].score != a[i - 1].score) {
            a[i].final_rank = i;
        }
        else {
            a[i].final_rank = a[i - 1].final_rank;
        }
    }
}

int main() {
    int N;//几组测试
    int K;//每组数据量
    cin >> N;
    int location = 0;
    int testee_num = 1;
    while (location < N) {
        location++;//记录现在为第几组数据
        int rank = 1;
        cin >> K;
        for (int i = 1; i<= K; i++) {
            cin >> local_testees[i].registration_number;
            cin >> local_testees[i].score;
            local_testees[i].location_number = location;
        }
        sort(local_testees + 1, local_testees + K + 1, cmp);
        get_local_rank(local_testees, K);
        for (int i = 1; i <= K; i++,testee_num++) {//将没组数据放到同一个列表中
            testees[testee_num] = local_testees[i];
        }
    }
    sort(testees + 1, testees + testee_num, cmp);
    get_final_rank(testees, testee_num - 1);
    cout << testee_num - 1 << endl;//for最后一轮循环已经++
    for (int i = 1; i <= testee_num - 1; i++) {
        cout << testees[i].registration_number << ' ' << testees[i].final_rank << ' ' << testees[i].location_number << ' ' << testees[i].local_rank << endl;
    }
}