#P7502. [2017年杭电多校]Build a tree

[2017年杭电多校]Build a tree

Build a tree

Problem Description

HazelFan wants to build a rooted tree. The tree has nn nodes labeled 00 to n1n-1, and the father of the node labeled ii is the node labeled i1k\lfloor\frac{i-1}{k}\rfloor. HazelFan wonders the size of every subtree, and you just need to tell him the XOR value of these answers.

Input

The first line contains a positive integer T(1T5)T(1\leq T\leq5), denoting the number of test cases. For each test case: A single line contains two positive integers n,k(1n,k1018)n,k(1\leq n,k\leq10^{18}).

Output

For each test case: A single line contains a nonnegative integer, denoting the answer.

Sample Input

2

5 2

5 3

Sample Output

7

6

Source

2017 Multi-University Training Contest - Team 7

https://acm.hdu.edu.cn/showproblem.php?pid=6121