🏡 index : github.com/captn3m0/codechef.git

---
{"category_name":"easy","problem_code":"EATTWICE","problem_name":"Eat Twice","problemComponents":{"constraints":"","constraintsState":false,"subtasks":"","subtasksState":false,"inputFormat":"","inputFormatState":false,"outputFormat":"","outputFormatState":false,"sampleTestCases":{"0":{"id":1,"input":"2\n3 6\n5 7\n1 9\n2 5\n3 7\n5 8\n2 5\n5 10","output":"16\n15","explanation":"**Example case 1:** The optimal solution is to try dishes $1$ and $2$.\n\n**Example case 2:** The optimal solution is to try dishes $2$ and $3$.","isDeleted":false}}},"video_editorial_url":"https://youtu.be/gHNOZmjxadU","languages_supported":{"0":"CPP14","1":"C","2":"JAVA","3":"PYTH 3.6","4":"PYTH","5":"PYP3","6":"CS2","7":"ADA","8":"PYPY","9":"TEXT","10":"PAS fpc","11":"NODEJS","12":"RUBY","13":"PHP","14":"GO","15":"HASK","16":"TCL","17":"PERL","18":"SCALA","19":"LUA","20":"kotlin","21":"BASH","22":"JS","23":"LISP sbcl","24":"rust","25":"PAS gpc","26":"BF","27":"CLOJ","28":"R","29":"D","30":"CAML","31":"FORT","32":"ASM","33":"swift","34":"FS","35":"WSPC","36":"LISP clisp","37":"SCM guile","38":"PERL6","39":"ERL","40":"CLPS","41":"ICK","42":"NICE","43":"PRLG","44":"ICON","45":"COB","46":"SCM chicken","47":"PIKE","48":"SCM qobi","49":"ST","50":"NEM"},"max_timelimit":1,"source_sizelimit":50000,"problem_author":"kingofnumbers","problem_tester":"","date_added":"19-06-2019","tags":{"0":"cook","1":"cook107","2":"kingofnumbers"},"problem_difficulty_level":"Easy","best_tag":"","editorial_url":"https://discuss.codechef.com/problems/EATTWICE","time":{"view_start_date":1561314600,"submit_start_date":1561314600,"visible_start_date":1561314600,"end_date":1735669800},"is_direct_submittable":false,"problemDiscussURL":"https://discuss.codechef.com/search?q=EATTWICE","is_proctored":false,"visitedContests":{},"layout":"problem"}
---
### Read problems statements in [Hindi](https://www.codechef.com/download/translated/COOK107/hindi/EATTWICE.pdf), [Mandarin Chinese](https://www.codechef.com/download/translated/COOK107/mandarin/EATTWICE.pdf), [Russian](https://www.codechef.com/download/translated/COOK107/russian/EATTWICE.pdf), [Vietnamese](https://www.codechef.com/download/translated/COOK107/vietnamese/EATTWICE.pdf), and [Bengali](https://www.codechef.com/download/translated/COOK107/bengali/EATTWICE.pdf) as well.

Hasan has recently heard about Chef's restaurant, which serves the tastiest dishes. The restaurant has published a list of $N$ dishes (numbered $1$ through $N$) that will be served in the next $M$ days. For each valid $i$, the $i$-th dish will be served only on the $D_i$-th day. Hasan has investigated their tastiness and he knows that for each valid $i$, the $i$-th dish has tastiness $V_i$.

Hasan's budget is only large enough to try two dishes. He wants to choose these two dishes in such a way that their total (summed up) tastiness is as large as possible. However, he cannot try 2 dishes on the same day.

Help Hasan and calculate the maximum possible total tastiness of the dishes he should try.

### Input
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $M$.
- $N$ lines follow. For each valid $i$, the $i$-th of these lines contains two space-separated integers $D_i$ and $V_i$.

### Output
For each test case, print a single line containing one integer — the maximum total tastiness.

### Constraints 
- $1 \le T \le 1,000$
- $2 \le N, M \le 10^5$
- $1 \le D_i \le M$ for each valid $i$
- $1 \le V_i \le 10^9$ for each valid $i$
- there are at least two dishes that are served on different days
- the sum of $N$ over all test cases does not exceed $10^6$
- the sum of $M$ over all test cases does not exceed $10^6$

### Example Input
```
2
3 6
5 7
1 9
2 5
3 7
5 8
2 5
5 10
```

### Example Output
```
16
15
```

### Explanation
**Example case 1:** The optimal solution is to try dishes $1$ and $2$.

**Example case 2:** The optimal solution is to try dishes $2$ and $3$.

<aside style='background: #f8f8f8;padding: 10px 15px;'><div>All submissions for this problem are available.</div></aside>