#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
struct Student{
char id[15];
int score;
int group;
int rank_group;
int rank_tol;
}stu[30010];
bool cmp(Student a, Student b){
if(a.score != b.score) return a.score > b.score;
else return strcmp(a.id, b.id) < 0;
}
int main(int argc, char** argv) {
int n, k = 0;
int tol = 0;
cin >> n;
for(int i = 0; i < n; i++){
cin >> k;
for(int j = 0; j < k; j++){
cin >> stu[tol].id >> stu[tol].score;
stu[tol].group = i+1;
tol++;
}
sort(stu+tol-k, stu+tol, cmp);
stu[tol-k].rank_group = 1;
for(int j = tol-k+1; j < tol; j++){
if(stu[j].score == stu[j-1].score){
stu[j].rank_group = stu[j-1].rank_group ;
}else{
stu[j].rank_group = j-tol+k+1;
}
}
}
sort(stu, stu+tol, cmp);
cout << tol << endl;
stu[0].rank_tol = 1;
for(int i = 0; i < tol; i++){
if(i > 0 && stu[i].score != stu[i-1].score){
stu[i].rank_tol = i + 1;
} else if(i > 0 && stu[i].score == stu[i-1].score){
stu[i].rank_tol = stu[i-1].rank_tol;
}
cout << stu[i].id << " " << stu[i].rank_tol << " " << stu[i].group << " " << stu[i].rank_group << endl;
}
return 0;
}