Rohith and Circles

All submissions for this problem are available.
Read problems statements in Mandarin Chinese and Russian.
Chef has a friend named Rohith who is very good at geometry. One day Chef came across an interesting problem on the topic of circles and shared it with Rohith.
Next day Rohith also came up to chef with a problem he just created. When our chef couldn't solve it thinking that it uses some advanced geometry, Rohith took the pleasure saying that the solution of this problem uses exactly the same concept which was used to solve the problem that they have discussed the day before. This came as a shock to our chef and he became desperate to solve this problem, but even as he tried a lot, he couldn't solve it. Rohith doesn't want to share his solution, so can you help our chef in solving the problem?
Take a deep breath! You are going to draw a lot of circles.
The problem goes like this:
 First draw a circle of radius R_{1} on a sheet of paper, let us call it C_{1}.
 Draw another circle of radius R_{2} touching the circle C_{1} from the inside (it is given that R_{2} ≤ R_{1}), let's call this circle as C_{2}.
 Draw another circle of radius R_{3} such that it touches the circle C_{1} from inside and the circle C_{2} from outside (it is given that R_{3} + R_{2} ≤ R_{1}), let's call this circle as C_{3}.
 Draw another circle of radius R_{4} which will touch the circles C_{2} and C_{3} externally and the circle C1 internally, let's call it as C_{4}. It is guaranteed that such a set of circles can be drawn.
 Now try to draw a circle C_{5} which will touch the circles C_{2} and C_{4} externally and the circle C_{1} internally. Note that circles C_{5} and C_{3} are not the same (see the picture below for more clarification). Let its radius be R_{5}
 Now try to draw a circle C_{6} which will touch the circles C_{2} and C_{5} externally and the circle C_{1} internally. Let its radius be C_{6}.
 And so on, like this, draw N circles such that the circle C_{N} will touch circles C_{2}, C_{N  1} externally and the circle C_{1} internally. Let its radius be R_{N}
After drawing the four circles, the figure may look something like this:
After drawing all the circles the figure may look like this
You are given N, R_{1}, R_{2}, R_{3} and R_{4}, you need to find the radius of the circle C_{N}, i.e, R_{N}.
You are given four integers N_{0}, P, M, B. There are T test cases in total. Test cases are numbered from 1 to T. The value of N for the t^{th} test case, i.e, N_{t} = (P * N_{t  1}) % M + B. The values of R_{1}, R_{2}, R_{3} and R_{4} remain constant over all the test cases.
Input
The first line of the input contains an integer T denoting the number of test cases.
The next line contains four integers N_{0}, P, M and B separated by a single space.
The next line contains four real valued numbers R_{1}, R_{2}, R_{3} and R_{4} separated by a single space.
Output
As the number of test cases can be huge, you need to output a single line containing the sum of values of R_{N} over all test cases exactly up to 6 decimal places.
Constraints and Subtasks
Subtask 1: 25 points
 1 ≤ T ≤ 10^{3}
 1 ≤ R_{i} ≤ 10^{3}
 1 ≤ N_{0}, P, M, B ≤ 10^{3}
Subtask 2: 75 points
 1 ≤ T ≤ 10^{7}
 1 ≤ R_{i} ≤ 10^{12}
 1 ≤ N_{0}, P, M, B ≤ 10^{9}
Example
Input: 1 1 2 1 5 6 3 3 2 Output: 1.000000Explanation
After drawing 5 circles the figure looks like this.
The radius of the circle C_{5} here is R_{5} = 1.
Author:  adurysk 
Tester:  mugurelionut 
Editorial  http://discuss.codechef.com/problems/NTHCIR 
Tags  adurysk, geometry, july15, maths, mediumhard, recurrence 
Date Added:  16042015 
Time Limit:  1.5 sec 
Source Limit:  50000 Bytes 
Languages:  C, CPP14, JAVA, PYTH, PYTH 3.6, PYPY, 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, SCM chicken, 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. 