Codeforces Round 1035 (Div. 2)
A
Add or XOR
Codeforces
800
原题
←B →
翻译中…

You are given two non-negative integers $a, b$. You can apply two types of operations on $a$ any number of times and in any order:

翻译中…
  • $a \gets a + 1$. The cost of this operation is $x$;
  • $a \gets a \oplus 1$, where $\oplus$ denotes the bitwise XOR operation. The cost of this operation is $y$.
翻译中…

Now you are asked to make $a = b$. If it's possible, output the minimum cost; otherwise, report it.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

翻译中…

The only line of each test case contains four integers $a, b, x, y$ ($1 \le a, b \le 100, 1 \le x, y \le 10^7$) — the two integers given to you and the respective costs of two types of operations.

翻译中…

Output

翻译中…

For each test case, output an integer — the minimum cost to make $a = b$, or $-1$ if it is impossible.

翻译中…

Note

翻译中…

In the first test case, the optimal strategy is to apply $a \gets a + 1$ three times. The total cost is $1+1+1=3$.

翻译中…

In the second test case, the optimal strategy is to apply $a \gets a + 1$, $a \gets a \oplus 1$, $a \gets a + 1$, $a \gets a \oplus 1$ in order. The total cost is $2+1+2+1=6$.

翻译中…

In the fifth test case, it can be proved that there isn't a way to make $a = b$.

翻译中…

样例

Input #1
7
1 4 1 2
1 5 2 1
3 2 2 1
1 3 2 1
2 1 1 2
3 1 1 2
1 100 10000000 10000000
Output #1
3
6
1
3
-1
-1
990000000
InputCopy OutputCopy
题目信息
题号
2119A
时间限制
1 s
内存限制
256 MB
标签
位运算
贪心
数学

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。