#CF2240A. 又一道 Popcount 问题 / A. Another Popcount Problem

又一道 Popcount 问题 / A. Another Popcount Problem

A. Another Popcount Problem

Source: Codeforces 2240A
Contest: Codeforces Round 1105 (Div. 2)
Time limit: 1 second
Memory limit: 256 megabytes

Statement

You are given two integers nn and kk.

Your task is to construct a sequence aa consisting of kk non-negative integers a1,a2,,aka_1, a_2, \ldots, a_k such that:

  • i=1kain\sum_{i=1}^{k} a_i \le n
  • The total number of set bits, i.e., i=1kpopcount(ai)\sum_{i=1}^{k} \operatorname{popcount}(a_i), is as large as possible.

You only need to output the maximum possible value of i=1kpopcount(ai)\sum_{i=1}^{k} \operatorname{popcount}(a_i).

Here, popcount(x)\operatorname{popcount}(x) denotes the number of 11 bits in the binary representation of xx. For example, $\operatorname{popcount}(6) = \operatorname{popcount}((110)_2) = 2$, and popcount(0)=0\operatorname{popcount}(0) = 0.

Input

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

Each of the next tt lines contains two integers nn and kk (1n,k1061 \le n, k \le 10^6) — the maximum allowed sum of the sequence and the length of the sequence, respectively.

Output

For each test case, output a single integer — the maximum possible value of i=1kpopcount(ai)\sum_{i=1}^{k} \operatorname{popcount}(a_i).

Note

In the first test case, n=2n=2 and k=1k=1. We can choose a=[1]a = [1] or a=[2]a = [2]. In both cases, the sum of popcounts is 11.

In the second test case, n=3n=3 and k=1k=1. We can choose a=[3]a = [3], since (3)2=(11)2(3)_2 = (11)_2, popcount(3)=2\operatorname{popcount}(3) = 2.

In the third test case, n=6n=6 and k=2k=2. We can choose a=[3,3]a = [3, 3]. The sum is 3+3=663 + 3 = 6 \le 6, and the total popcount is $\operatorname{popcount}(3) + \operatorname{popcount}(3) = 2 + 2 = 4$.

Examples

6
2 1
3 1
6 2
14142 137205
1000000 100
1000000 1000000
1
2
4
14142
1322
1000000