Canoe Athletes

No attempts yetTime limit3sMemory limit256 MB

Problem

The International Canoe Sprint Championship (ICSC) is about to open. The official boats certified by the ICSC are the C1, C2, and C4, where "C" stands for canoe and the number is how many paddlers ride in it. Canoe races are held on a straight, lane-divided course over calm water, and international events are split into the 200m, 500m, and 1000m distances.

A sports school plans to enter the ICSC C4 1000m event. The school has four classes with the same number of students each, and it forms a team by picking exactly one athlete from each class. The school's C4 boat performs best when the combined weight of its four athletes is as close as possible to a specific target value.

For example, suppose the target value is 300 and the students' weights in each class are:

  • Class 1: 60, 52, 80, 40
  • Class 2: 75, 68, 88, 63
  • Class 3: 48, 93, 48, 54
  • Class 4: 56, 73, 49, 75

Choosing 60, 75, 93, and 73 from the four classes gives a total weight of 301, the closest to the target 300. Sometimes two totals are equally close to the target. For instance, if the target is 200 and both 198 and 202 are achievable, each is 2 away from the target; in that case the smaller total is preferred, so 198 is chosen.

Given the boat's target value and the weights of the students in each class, pick one athlete from each of the four classes under the rule above and report the resulting total weight.

Input

Input is given on standard input. The first line contains the number of test cases TT.

The first line of each test case contains two integers kk and nn. Here kk (1k40,000,0001 \le k \le 40{,}000{,}000) is the boat's target value and nn (1n1,0001 \le n \le 1{,}000) is the number of students in each class.

The next four lines give the weights of the students in classes 1 through 4, with nn weights per line. Each weight is an integer between 1 and 10,000,000.

Output

For each test case, print the answer on its own line: the total weight of the four chosen athletes, that is, the achievable total closest to the target kk (and the smaller total when two are equally close).