{"trustable":true,"prependHtml":"\u003cscript\u003e window.katexOptions \u003d { disable: true }; \u003c/script\u003e\n\u003cscript type\u003d\"text/x-mathjax-config\"\u003e\n MathJax.Hub.Config({\n tex2jax: {\n inlineMath: [[\u0027$$$\u0027,\u0027$$$\u0027], [\u0027$\u0027,\u0027$\u0027]],\n displayMath: [[\u0027$$$$$$\u0027,\u0027$$$$$$\u0027], [\u0027$$\u0027,\u0027$$\u0027]]\n }\n });\n\u003c/script\u003e\n\u003cscript async src\u003d\"https://mathjax.codeforces.org/MathJax.js?config\u003dTeX-AMS-MML_HTMLorMML\" type\u003d\"text/javascript\"\u003e\u003c/script\u003e","sections":[{"title":"","value":{"format":"HTML","content":"\u003cdiv class\u003d\"panel_content\"\u003eIn 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 $K$ meters.\u003cbr\u003e\u003cbr\u003eThere are $4$ checkpoints in the campus, indexed as $p_1,p_2,p_3$ and $p_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.\u003cbr\u003e\u003cbr\u003eThe system regards these $4$ checkpoints as a circle. When you are at checkpoint $p_i$, you can just run to $p_{i-1}$ or $p_{i+1}$($p_1$ is also next to $p_4$). You can run more distance between two adjacent checkpoints, but only the distance saved at the system will be counted.\u003cbr\u003e\u003cbr\u003e\u003ccenter\u003e\u003cbr\u003e\u003cimg style\u003d\"max-width:100%;\" src\u003d\"CDN_BASE_URL/45b4ab889ff29611c34525ada5f9d7b8?v\u003d1713712069\"\u003e\u003cbr\u003e\u003c/center\u003e \u003cbr\u003e\u003cbr\u003eCheckpoint $p_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 $K$.\u003c/div\u003e"}},{"title":"Input","value":{"format":"HTML","content":"The first line of the input contains an integer $T(1\\leq T\\leq15)$, denoting the number of test cases.\u003cbr\u003e\u003cbr\u003eIn each test case, there are $5$ 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."}},{"title":"Output","value":{"format":"HTML","content":"For each test case, print a single line containing an integer, denoting the minimum distance."}},{"title":"Sample","value":{"format":"HTML","content":"\u003ctable class\u003d\u0027vjudge_sample\u0027\u003e\n\u003cthead\u003e\n \u003ctr\u003e\n \u003cth\u003eInput\u003c/th\u003e\n \u003cth\u003eOutput\u003c/th\u003e\n \u003c/tr\u003e\n\u003c/thead\u003e\n\u003ctbody\u003e\n \u003ctr\u003e\n \u003ctd\u003e\u003cpre\u003e1\r\n2000 600 650 535 380\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e2165\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n"}},{"title":"Hint","value":{"format":"HTML","content":"\u003cbr\u003eThe best path is 2-1-4-3-2.\u003cbr\u003e"}}]}