#t548. 旗鼓相当的对手(加强版)

旗鼓相当的对手(加强版)

Well-Matched Rivals (Hard Version)

Problem Description

NN students took the final exam and we have each student's information: name (a string of at most 88 characters, no spaces), and scores in Chinese, Math, and English (natural numbers not exceeding 150150).

A pair of students (i,j)(i,j) is considered "well-matched rivals" if:

  • The difference in each subject's score is at most 55;
  • The difference in total score is at most 1010.

Now we want to know which students are "well-matched rivals"; output their names.

All names are given in lexicographic order. The output should also list all rival pairs in lexicographic order:

  1. Within each pair, the first name's lexicographic order should be smaller than the second name's;
  2. If the first names of two pairs differ, output the pair whose first name is lexicographically smaller first;
  3. If the first names are the same but the second names differ, output the pair whose second name is lexicographically smaller first.

Input Format

The first line contains a positive integer NN, the number of students.

The following NN lines each start with a string (the student's name), followed by 33 natural numbers (Chinese, Math, English scores). The string and the integers are separated by spaces.

Output Format

Output several lines, each containing 22 space-separated strings representing one pair of well-matched rivals.

Output in the order described above.

Sample Input

3
fafa 90 90 90
lxl 95 85 90
senpai 100 80 91

Sample Output

fafa lxl
lxl senpai

Sample Explanation

There are 33 students:

  • fafa's Chinese, Math, and English scores are 90,90,9090, 90, 90, total 270270;
  • lxl's Chinese, Math, and English scores are 95,85,9095, 85, 90, total 270270;
  • senpai's Chinese, Math, and English scores are 100,80,91100, 80, 91, total 271271.

For fafa and lxl:

  • The three subject differences are 5,5,05, 5, 0, all ≤5\le 5;
  • The total-score difference is 00, ≤10\le 10. So they are well-matched rivals.

For fafa and senpai:

  • The Chinese difference is 1010, >5> 5. So they are not well-matched rivals.

For lxl and senpai:

  • The three subject differences are 5,5,15, 5, 1, all ≤5\le 5;
  • The total-score difference is 11, ≤10\le 10. So they are well-matched rivals.

Therefore, outputting these two pairs in lexicographic order gives the two lines in the sample output.

Constraints

1≤N≤10001 \le N \le 1000

Names have length at most 88; Chinese, Math, and English scores are natural numbers not exceeding 150150.