Sample input don't make sense , unless table is round. (not specified)

First sample input
last query is 3,0 decoded query is 3,1
Table is [c,d] = [1,2] [3,1] [1,1]
So answer should be 2, unless table is round so he can pick up last and first, sums up to 3.