You have to color*M* X *N*(1*M*,*N*108) Two dimen1_grid. You will be provided*K*(2*K*108) different colors to do so. You will also be provided a list*B*(0*B*500) List of blocked cells of this grid. You cannot color those blocked cells. A cell can be described (*X*,*Y*), Which points to*Y*-Th cell from the left of*X*-Th row from the top.

While coloring the grid, you have to follow these rules-

- You have to color each cell which is not blocked.
- You cannot color a blocked cell.
- You can choose exactly one color from
*K*Given colors to color a cell.
- No two vertically adjacent cells can have the same color, I. e. Cell (
*X*,*Y*) And cell (*X*+ 1,*Y*) Cannot contain the same color.

Now the great problem setter smiled with emotion and thought that he wocould ask the contestants to find how many ways the board can be colored. since the number can be very large and he doesn't want the contestants to be in trouble dealing with big integers; he decided to ask them to find the result modulo 100,000,007. so he prepared the judge data for the problem using a random generator and saved this problem for a future contest as a giveaway (easiest) problem.

But unfortunately he got married and forgot the problem completely. after some days he rediscovered his problem and became very excited. but after a while, he saw that, in the Judge data, he forgot to add the Integer Which supposed to be the 'number of rows '. he didn't find the input generator and his codes, but luckily he has the input file and the correct answer file. so, he asks your help to regenerate the data. yes, you are given the input file which contains all the information except t the 'number of rows 'and the answer file; you have to find the number of rows he might have used for this problem.

Input input starts with an integer

*T*(

*T*150), denoting the number of test cases.

Each test case starts with a line containing four Integers*N*,*K*,*B*And*R*(0*R*<100000007) which denotes the result for this case. Each of the next*B*Lines will contains two integers*X*And*Y*(1*X**M*, 1*Y**N*), Denoting the row and column number of a blocked cell. All the cells will be distinct.

Output for each case, print the case number and the minimum possible value

*M*. You can assume that solution exists for each case.

Sample Input

43 3 0 17284 4 2 1866243 13 32 5 2 201 22 22 3 0 989323

Sample output

Case 1: 3 Case 2: 3 case 3: 2 case 4: 20 a k color is required for the mesh of n columns in the m row, where B does not need to be colored, apply a color to each of the other grids. The two grids in the same column cannot be in the same color. The positions of m, n, k, and B grids are given, the result of the coloring scheme % 100000007 is R. Now we have given n, k, R, and B lattice positions, so that you can find the smallest M idea: first, we can easily think that M must at least be the same as the X coordinate of B grids, and then we try to dye a column by column, each column is stained from top to bottom, if a grid is in the first row or under B grids. So there are K possibilities, otherwise it is the possibility of K-1, now we divide the grid into two parts, the above part is the unchanged part of the B coordinate, below is the variable part, then we calculate the sum of the first row of the unchanged part and the variable part, and then each time we add a row, it is possible to increase (K-1) ^ n. Now we can introduce the equation CNT * P ^ m = R. CNT is calculated as mentioned above, M is to be added, and R is obtained after % processing, this is the process of solving the discrete logarithm modulo equation.#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>#include <map>#include <set>#include <cmath>typedef long long ll;using namespace std;const int maxn = 510;const ll mod = 100000007;int n, m, k, b, r, x[maxn], y[maxn];set<pair<int, int> > best;ll mul_mod(ll a, ll b) {return a * b % mod;}ll pow_mod(ll a, ll p) {ll tmp = 1;while (p) {if (p & 1)tmp = tmp * a % mod;p >>= 1;a = a * a % mod;}return tmp;}void gcd(ll a, ll b, ll &d, ll &x, ll &y) {if (!b) { d = a; x = 1; y = 0; }else { gcd(b, a%b, d, y, x); y -= x*(a/b); }}ll inv(ll a) {ll d, x, y;gcd(a, mod, d, x, y);return d == 1 ? (x+mod) % mod : -1;}int log_mod(int a, int b) {int m, v, e = 1, i;m = (int)sqrt(mod+0.5);v = inv(pow_mod(a, m));map<int, int> x;x[1] = 0;for (int i = 1; i < m; i++) {e = mul_mod(e, a);if (!x.count(e))x[e] = i;}for (int i = 0; i < m; i++) {if (x.count(b))return i*m + x[b];b = mul_mod(b, v);}return -1;}int count() {int c = 0;for (int i = 0; i < b; i++)if (x[i] != m && !best.count(make_pair(x[i]+1, y[i]))) c++;c += n;for (int i = 0; i < b; i++)if (x[i] == 1)c--;return mul_mod(pow_mod(k, c), pow_mod(k-1, (ll)m*n-b-c));}int doit() {int cnt = count();if (cnt == r) return m;int c = 0;for (int i = 0; i < b; i++)if (x[i] == m)c++;m++;cnt = mul_mod(cnt, pow_mod(k, c));cnt = mul_mod(cnt, pow_mod(k-1, n-c));if (cnt == r)return m;return log_mod(pow_mod(k-1, n), mul_mod(r, inv(cnt))) + m;}int main() {int t, cas = 1;scanf("%d", &t);while (t--) {scanf("%d%d%d%d", &n, &k, &b, &r);best.clear();m = 1;for (int i = 0; i < b; i++) {scanf("%d%d", &x[i], &y[i]);if (x[i] > m)m = x[i];best.insert(make_pair(x[i], y[i]));}printf("Case %d: %d\n", cas++, doit());}return 0;}

Ultraviolet A-11916 emoogle grid (discrete logarithm modulo)