AT_abc417_d [ABC417D] Takahashi's Expectation

Description

Takahashi will receive $ N $ presents. He has a parameter called mood, which is a non-negative integer, and his mood changes every time he receives a present. Each present has three parameters: value $ P $ , mood increase $ A $ , and mood decrease $ B $ , and his mood changes as follows based on these parameters: - When the value $ P $ of the received present is greater than or equal to his mood, he is happy with the present, and his mood increases by $ A $ . - When the value $ P $ of the received present is less than his mood, he is disappointed with the present, and his mood decreases by $ B $ . However, if his mood is originally less than $ B $ , it becomes $ 0 $ . The $ i $ -th $ (1\le i\le N) $ present he receives has value $ P _ i $ , mood increase $ A _ i $ , and mood decrease $ B _ i $ . You are given $ Q $ questions, so answer all of them. In the $ i $ -th $ (1\le i\le Q) $ question, you are given a non-negative integer $ X _ i $ , so answer the following question: > Find Takahashi's mood after receiving all $ N $ presents when his mood is initially $ X _ i $ .

Input Format

The input is given from Standard Input in the following format: > $ N $ $ P _ 1 $ $ A _ 1 $ $ B _ 1 $ $ P _ 2 $ $ A _ 2 $ $ B _ 2 $ $ \vdots $ $ P _ N $ $ A _ N $ $ B _ N $ $ Q $ $ X _ 1 $ $ X _ 2 $ $ \vdots $ $ X _ Q $

Output Format

Output $ Q $ lines. The $ i $ -th line should contain the answer to the $ i $ -th question.

Explanation/Hint

### Sample Explanation 1 When Takahashi's initial mood is $ 10 $ , his mood changes as follows: - The value $ 3 $ of the first present is less than his mood $ 10 $ , so his mood decreases by the mood decrease $ 4 $ , and his mood becomes $ 6 $ . - The value $ 1 $ of the second present is less than his mood $ 6 $ , and Takahashi's mood $ 6 $ is less than the mood decrease $ 9 $ , so his mood becomes $ 0 $ . - The value $ 2 $ of the third present is not less than his mood $ 0 $ , so his mood increases by the mood increase $ 6 $ , and his mood becomes $ 6 $ . - The value $ 3 $ of the fourth present is less than his mood $ 6 $ , and Takahashi's mood $ 6 $ is less than the mood decrease $ 8 $ , so his mood becomes $ 0 $ . Therefore, his final mood is $ 0 $ . ### Sample Explanation 2 Because Takahashi's mood is too high, his mood keeps decreasing even when he receives the best presents. ### Constraints - $ 1\le N\le10000 $ - $ 1\le P _ i\le500\ (1\le i\le N) $ - $ 1\le A _ i\le500\ (1\le i\le N) $ - $ 1\le B _ i\le500\ (1\le i\le N) $ - $ 1\le Q\le5\times10 ^ 5 $ - $ 0\le X _ i\le10 ^ 9\ (1\le i\le Q) $ - All input values are integers.