#K45252. Alternating Sequence Query
Alternating Sequence Query
Alternating Sequence Query
You are given a starting integer n and a positive integer k. Consider a sequence where the first term is n and each subsequent term is defined as follows:
If the initial number is even, then:
- If the index is odd: the value is n.
- If the index is even: the value is \( n+1 \).
If the initial number is odd, then:
- If the index is odd: the value is n.
- If the index is even: the value is \( n-1 \).
In other words, the sequence alternates between n and \( n+1 \) (if n is even) or n and \( n-1 \) (if n is odd). Formally, let \( a_1 = n \) and for \( i \ge 2 \), $$ a_i = \begin{cases} n+1, & \text{if } n \text{ is even and } i \text{ is even}, \\ n, & \text{if } n \text{ is even and } i \text{ is odd}, \\ n-1, & \text{if } n \text{ is odd and } i \text{ is even}, \\ n, & \text{if } n \text{ is odd and } i \text{ is odd}. \end{cases} $$ Your task is to compute the \( k \)-th term of this sequence for multiple queries.
inputFormat
The first line contains an integer T
, the number of queries.
Each of the following T
lines contains two space-separated integers: n
and k
.
outputFormat
For each query, output a single line containing the \( k \)-th term of the sequence.
## sample3
7 5
4 3
2 4
7
4
3