#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; void integerFactorization(int n, vector& base, vector& expo) { base.clear(); expo.clear(); int a = 2; while(a * a <= n){ int b = 0; while(n % a == 0){ ++ b; n /= a; } if(b > 0){ base.push_back(a); expo.push_back(b); } ++ a; } if(n > 1){ base.push_back(n); expo.push_back(1); } } int solve(vector x, int k, int b) { int n = x.size(); vector base, expo; integerFactorization(b, base, expo); int ret = INT_MAX; for(unsigned i=0; i v(n, 0); for(int j=0; j> q; while(--q >= 0){ int seed, n, k, b; cin >> seed >> n >> k >> b; vector x(n+1); x[0] = seed; for(int i=1; i<=n; ++i) x[i] = 1 + x[i-1] * (x[i-1] + 12345LL) % 100000009; cout << solve(x, k, b) << endl; } return 0; }