Link for the problem: (CodeChef: Practical coding for everyone)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class COVAXIN {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
}
public static void main(String[] args) {
FastReader rd = new FastReader();
int tc = rd.nextInt();
long x = rd.nextLong(), a = rd.nextLong(), b = rd.nextLong(), c = rd.nextLong(), d = rd.nextLong();
long p = rd.nextLong(), q = rd.nextLong(), r = rd.nextLong(), s = rd.nextLong(), t = rd.nextLong(), m = rd.nextLong();
for (int j = 0; j < tc; j++) {
long ans = 0, temp = 0, covaxin, covishield;
int y = 0, z = 0;
while (temp <= x) {
covaxin = a + y * b;
covishield = c + z * d;
if (covaxin < covishield) {
if (ans + covaxin <= x) {
ans += covaxin;
y++;
}
} else {
if (ans + covishield <= x) {
ans += covishield;
z++;
}
}
temp += ans;
}
System.out.println(y + z);
a = (a + (y + z) * t) % m + p;
b = (b + (y + z) * t) % m + q;
c = (c + (y + z) * t) % m + r;
d = (d + (y + z) * t) % m + s;
}
}
}