problems / 109

Bob's OR Wish

easy bit-manipulation math 1000 ms · 256 MB

Bob's OR Wish

Alice is holding a number a and Bob is holding a number b, both written in binary. A genie will grant their wish the moment a OR b (bitwise OR) equals a wished-for number c — but the genie is lazy, so Alice and Bob must do the work themselves. In one move, either of them picks their own number and flips a single bit of it (a 0 becomes a 1, or a 1 becomes a 0). They want to reach a OR b == c using as few moves in total as possible.

Given a, b and c, find the minimum number of bit flips needed.

You will be asked this for several independent triples (a, b, c); answer each one separately.

Input

The first line contains a single integer q (1 ≤ q ≤ 10^5), the number of triples.

Each of the next q lines contains three integers a, b, c (0 ≤ a, b, c < 2^60), describing one independent wish.

Output

For each triple, print one line with a single integer: the minimum number of bit flips (across a and b combined) needed so that a OR b equals c exactly.

Notes

Look at each bit position on its own:

  • If a bit of c is 1, the same bit of a or b (or both) must already be 1, or exactly one of them must be flipped to 1.
  • If a bit of c is 0, that bit must be 0 in both a and b, so every 1 among that bit of a and b must be flipped.

Since the 60 bit positions never interact with each other, the total answer is the sum of the per-bit costs.

Example 1

Input

3
2 5 5
1 1 1
0 0 7

Output

1
0
3

Example 2

Input

4
0 0 0
1152921504606846975 0 0
5 2 0
9 0 9

Output

0
60
3
0

Hints · reveal one at a time if you're stuck

Hint 1

Bits never interact under OR, and each flip changes exactly one bit of one number. So the problem splits into independent decisions per bit position, and the answer is a sum of per-bit costs.

Hint 2

For one bit position, list the combinations of a's bit, b's bit and c's bit, and work out the fewest flips that make the OR match. Watch the case where c's bit is 0 but both a and b have a 1 there.

report an issue Prefer your own editor? sooks prep 109 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.