|
<p> After sending smileys, John decided to play with arrays. Did you know that hackers enjoy playing with arrays? John has a zero-based index array, <b>m</b>, which contains <b>n</b> non-negative integers. However, only the first <b>k</b> values of the array are known to him, and he wants to figure out the rest. </p> |
|
|
|
<p> John knows the following: for each index <b>i</b>, where <b>k</b> ≤ <b>i</b> < <b>n, m</b>[<b>i</b>] is the minimum non-negative integer which is *not* contained in the previous *<b>k</b>* values of <b>m</b>. </p> |
|
|
|
<p> For example, if <b>k</b> = 3, <b>n</b> = 4 and the known values of <b>m</b> are [2, 3, 0], he can figure out that <b>m</b>[3] = 1. </p> |
|
|
|
<p> John is very busy making the world more open and connected, as such, he doesn't have time to figure out the rest of the array. It is your task to help him. </p> |
|
|
|
<p> Given the first <b>k</b> values of <b>m</b>, calculate the <b>n</b><sup>th</sup> value of this array. (i.e. <b>m</b>[<b>n</b> - 1]). </p> |
|
|
|
<p> Because the values of <b>n</b> and <b>k</b> can be very large, we use a pseudo-random number generator to calculate the first <b>k</b> values of <b>m</b>. |
|
Given non-negative integers <b>a</b>, <b>b</b>, <b>c</b> and positive integer <b>r</b>, the known values of <b>m</b> can be calculated as follows: </p> |
|
|
|
<ul> |
|
<li><b>m</b>[0] = <b>a</b></li> |
|
<li><b>m</b>[<b>i</b>] = (<b>b</b> * <b>m</b>[<b>i</b> - 1] + <b>c</b>) % <b>r</b>, 0 < <b>i</b> < <b>k</b></li> |
|
</ul> |
|
|
|
<p><h3>Input</h3> |
|
The first line contains an integer <b>T</b> (<b>T</b> ≤ 20), the number of test cases.<br/> |
|
This is followed by <b>T</b> test cases, consisting of 2 lines each.<br/> |
|
The first line of each test case contains 2 space separated integers, <b>n</b>, <b>k</b> (1 ≤ <b>k</b> ≤ 10<sup>5</sup>, <b>k</b> < <b>n</b> ≤10<sup>9</sup>).<br/> |
|
The second line of each test case contains 4 space separated integers <b>a</b>, <b>b</b>, <b>c</b>, <b>r</b> (0 ≤ <b>a</b>, <b>b</b>, <b>c</b> ≤ 10<sup>9</sup>, 1 ≤ <b>r</b> ≤ 10<sup>9</sup>). </p> |
|
|
|
<p><h3>Output</h3> |
|
For each test case, output a single line containing the case number and the <b>n</b><sup>th</sup> element of <b>m</b>. </p> |
|
|