import java.io.OutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author emotionalBlind
*/
public class Main {
public static void main
(String[] args
) { InputReader in = new InputReader(inputStream);
LOJ_1006 solver = new LOJ_1006();
solver.solve(1, in, out);
out.close();
}
}
class LOJ_1006 {
public void solve
(int testNumber, InputReader in,
PrintWriter out
) { int T = in.readInt();
int[] a = new int[10001];
int MOD = 10000007;
for (int t = 1; t <= T; ++t) {
for (int i = 0; i < 6; ++i) {
a[i] = in.readInt();
a[i] %= MOD;
}
int n = in.readInt();
for (int i = 6; i <= n; ++i) {
long sum = 0;
for (int j = 1; j <= 6; ++j) {
sum = (sum + a[i - j]) % MOD;
}
a[i] = (int) sum;
}
out.printf("Case %d: %d\n", t, a[n]);
}
}
}
class InputReader {
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long readLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public static boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
int c = read();
while (isSpaceChar(c)) c = read();
do {
s += c;
c = read();
} while (!isSpaceChar(c));
return s;
}
}