Bookshelf Filling
题号:NC237053
时间限制:C/C++/Rust/Pascal 1秒,其他语言2秒
空间限制:C/C++/Rust/Pascal 256 M,其他语言512 M
64bit IO Format: %lld

题目描述

Recently, Joseph bought lots of books and was going to put them all on the shelf. He found that there are only two types of books -- the type A with height a and thickness 1, and the type B with height  and thickness 1. The number of each type of book are n and m respectively. And he already knew the height of the bookshelf which is .
In the beginning, he arranged all the books on the shelf in height-ascending order, which means the books of height a are all on the left, and the books of height b are all on the right. The total length these books occupied on the bookshelf will be
However, Joseph found there was still a vacancy on top of these books, so he prepared to select  books with height b from the rightmosside and put them all horizontally on top of other books to reduce the length they occupied. He wanted to know the minimum width if he arranges these books optimally.  

输入描述:

The first line of the input gives the number of test cases . T test cases follow.
For each test case, only one line contains five integers .

输出描述:

For each test case, print a line contains one integer w, indicating the minimum width.
示例1

输入

复制
4
2 5 5 8 5
2 4 5 7 5
2 6 5 2 6
3 4 3 2 5

输出

复制
10
8
7
4

说明

The following pictures from left to right show the possible arrangement of books for the testcases 2,3 and 4.