Candidate walk

All submissions for this problem are available.
Read problems statements in Mandarin Chinese and Russian.
Elections in Bytetown are coming soon!
Candidate Boris is going to have a small walk. Bytetown can be represented as Ndimensional cube with edge size D. Initially Boris stands in cell with coordinates (0, 0, ... 0) and his destination is cell (D1, D1,... D1). His route consists of chain of the cells, where each pair of consecutive cells differs in exactly one coordinate by 1 and sum of coordinates strictly increases during his walk. Note that Boris walks through the cells not points.
But not all the people in Bytetown are going to vote for Boris. Some of them are sure that Boris is a liar and bribetaker and they will tell it to Boris if they meet him. Boris doesn't like such people and avoids them. He knows that at cell with coordinares (i_{0}, i_{1},..., i_{N1}) he will meet (i_{0}^ i_{1}^...^i_{N1})x(i_{0}+ i_{1}+...+ i_{N1}) such people (in other words XORsum of coordinates times arithmetic sum of coordinates). Try to help him to find minimal number of unfriendly people he will have to meet.
Input
The first line of the input contains an integer T denoting the number of test cases. Each of the next T lines contain two integer N and D.
Output
Output T lines. Every line should contain one integer  the minimal number of unfriendly people among all the possible routes Boris will have to meet for the corresponding test case.
Constraints
 1 ≤ T ≤ 20
 1 ≤ N ≤ 10
 Total number of cells in one cube doesn't exceed 2^{16}
Subtasks
Subtask #1[26 points]: N = 1
Subtask #2[29 points]: N = 2
Subtask #3[45 points]: N > 2
Example
Input: 4 1 2 1 3 2 2 2 3 Output: 1 5 1 10
Author:  pavel1996 
Tester:  rubanenko 
Editorial  http://discuss.codechef.com/problems/LCH15CD 
Tags  dynamicprogramming, easymedium, implementation, ltime20, pavel1996 
Date Added:  25122014 
Time Limit:  1  2 sec 
Source Limit:  50000 Bytes 
Languages:  C, CPP14, JAVA, PYTH, PYTH 3.6, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, SCALA, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, PERL6, TEXT, PYP3, CLOJ, FS 
Comments
 Please login at the top to post a comment.
SUCCESSFUL SUBMISSIONS
Fetching successful submissions
HELP
If you are still having problems, see a sample solution here. 