{"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\"\u003eYou, a former International Collegiate Programming Contest player, have now become an Assistant Cost Manager of Advanced Computer Manufacturing company. There are so many different kinds of costs you should deal with, including material cost, manufacturing cost and warehouse cost. Also, the customer\u0027s demand must be fully satisfied. Moreover, the space of warehouse is limited. To make things worse, all these parameters vary with time. To resolve this matter once for all, you decide to use your algorithmic knowledge to write a program.\u003cbr\u003e\u003cbr\u003eSpecifically, you have to find an optimal operational planning for the next $k$ months, under the following constraints: $\\textbf{in the i-th month}$,\u003cbr\u003e- the price of raw materials is $c_i$ yuan per unit, and the quantity of raw materials you purchase is not limited.\u003cbr\u003e- the customer\u0027s demand is $d_i$ computers, that is, you must sell exactly $d_i$ computers to customers this month.\u003cbr\u003e- your company can manufacture at most $p_i$ computers. Manufacturing one computer consumes one unit of raw materials plus a manufacturing cost of $m_i$ yuan.\u003cbr\u003e- you can store raw materials and computers in the warehouse for future use. According to the regulations of your company, the raw materials and computers must be placed in two different areas of the warehouse. You can store at most $e_i$ computers to the next month. However, since the volume of raw materials is negligible, the number of raw materials you store is not limited. Storing one unit of raw materials or one computer to the next month takes $R_i$ yuan or $E_i$ yuan, respectively.\u003cbr\u003e\u003cbr\u003eAlso, \u003cbr\u003e- you may immediately use the raw materials you purchase this month to manufacture computers, as long as the the production capacity of this month is enough; likewise, you may manufacture and sell computers in the same month. These raw materials and computers do not take up warehouse space.\u003cbr\u003e- initially, there are no raw materials or computers in your company.\u003cbr\u003e\u003cbr\u003eYour program should report whether all customer\u0027s demands can be fully satisfied, and, if so, report the minimum total cost.\u003c/div\u003e"}},{"title":"Input","value":{"format":"HTML","content":"The first line of input is a single integer $T$ $(1 \\leq T \\leq 200)$, denoting the number of test cases.\u003cbr\u003e\u003cbr\u003eEach test case begins with a single line of integer $k$ $(2 \\leq k \\leq 50000)$, the number of months. The $i$-th of the following $k$ lines contains four integers $c_i, d_i, m_i, p_i$ $(0 \\leq c_i, d_i, m_i, p_i \\leq 10^4)$, the price of raw materials, the customer\u0027s demand, the unit manufacturing cost and the manufacturing capacity of the $i$-th month, respectively. The $i$-th of the next $k-1$ lines contains three integers $e_i, R_i, E_i$ $(0 \\leq e_i \\leq 10^8, 0 \\leq R_i, E_i \\leq 10^4)$, the capacities of computer area of the warehouse, and the warehouse cost of storing a unit of raw material and a computer, between the $i$-th and the $(i+1)$-th month, respectively.\u003cbr\u003e\u003cbr\u003eIt is guaranteed that the sum of $k$ over all test cases does not exceed $3 \\times 10^5$."}},{"title":"Output","value":{"format":"HTML","content":"For each test case, display an integer in a line, denoting the minimum total cost. If customer demands cannot be fully satisfied, display $\\texttt{-1}$ instead."}},{"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\u003e2\r\n2\r\n10 5 3 6\r\n15 7 2 8\r\n2 3 2\r\n2\r\n0 8 0 7\r\n0 0 0 0\r\n0 0 0\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e170\r\n-1\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\u003eIn the first sample test case, you may purchase 12 units of raw materials, which takes 120 yuan, and put 7 of them into the warehouse for the next month, which takes \u003cbr\u003e21 yuan. Then, your company may manufacture 5 computers and sell them, which takes 15 yuan. In the second month, your company may manufacture and sell 7 \u003cbr\u003ecomputers using the raw materials in the warehouse, which takes 14 yuan. The total cost is 170 yuan, which can be proved to be optimal.\u003cbr\u003e\u003cbr\u003eIn the second sample test case, the manufacturing capacity of the first month is less than the customer\u0027s demand, so the customer\u0027s demand cannot be fully satisfied.\u003cbr\u003e"}}]}