Under construction

Still being built. Things may change

Palindromic Sequence

Time limit20sMemory limit1024 MB

Summary
Given L letters, a length limit N, and K, find the length of the Kth smallest palindrome in lexicographic order, or 0 if none exists.
Level

Medium6 of 10

Topics
Combinatorics, Math, Binary search
Solved
No attempts yet

Problem

Hannah is working on a new language made only of the first LL lowercase letters of the English alphabet. She is obsessed with palindromes, which are words that read the same forward and backward, such as hannah and civic. She has written down every word in her language whose length is at most NN and that is also a palindrome.

Find the length of the KKth smallest palindromic word among all the words she wrote, in lexicographic order. A word a1,a2,…,apa_1, a_2, \ldots, a_p is lexicographically smaller than b1,b2,…,bqb_1, b_2, \ldots, b_q if ai<bia_i < b_i at the first index ii where the two words differ. A prefix of a word is smaller than the word itself. For example, these words are in lexicographically increasing order: a, aa, aba, cabac, d.

Input

The first line contains the number of test cases TT. Each of the next TT lines contains three integers LL, NN, and KK, as described above.

Output

For each test case, print one line in the form Case #x: y, where xx is the test case number starting from 1 and yy is the length of the lexicographically KKth smallest palindromic word among all palindromic words of length at most NN in Hannah's language. If no such word exists, output 0.

Constraints

  • 1≤T≤1001 \le T \le 100
  • 1≤L≤261 \le L \le 26
  • 1≤K≤10121 \le K \le 10^{12}

Examples1

  1. Example 1

    Input
    2
    2 3 4
    2 3 9
    
    Expected output
    Case #1: 3
    Case #2: 0