#P7470. [2017年杭电多校]Lazy Running

[2017年杭电多校]Lazy Running

Lazy Running

Problem Description

In HDU, you have to run along the campus for 24 times, or you will fail in PE. According to the rule, you must keep your speed, and your running distance should not be less than KK meters. There are 44 checkpoints in the campus, indexed as p1,p2,p3p_1,p_2,p_3 and p4p_4. Every time you pass a checkpoint, you should swipe your card, then the distance between this checkpoint and the last checkpoint you passed will be added to your total distance. The system regards these 44 checkpoints as a circle. When you are at checkpoint pip_i, you can just run to pi1p_{i-1} or pi+1p_{i+1}(p1p_1 is also next to p4p_4). You can run more distance between two adjacent checkpoints, but only the distance saved at the system will be counted.

Checkpoint p2p_2 is the nearest to the dormitory, Little Q always starts and ends running at this checkpoint. Please write a program to help Little Q find the shortest path whose total distance is not less than KK.

Input

The first line of the input contains an integer T(1T15)T(1\leq T\leq15), denoting the number of test cases. In each test case, there are 55 integers $K,d_{1,2},d_{2,3},d_{3,4},d_{4,1}(1\leq K\leq 10^{18},1\leq d\leq 30000)$, denoting the required distance and the distance between every two adjacent checkpoints.

Output

For each test case, print a single line containing an integer, denoting the minimum distance.

Sample Input

1

2000 600 650 535 380

Sample Output

2165

Hint

The best path is 2-1-4-3-2.

Source

2017 Multi-University Training Contest - Team 4

https://acm.hdu.edu.cn/showproblem.php?pid=6071