inputs: Solve in Python:
In an array, a $block$ is a maximal sequence of identical elements. Since blocks are maximal, adjacent blocks have distinct elements, so the array breaks up into a series of blocks.  For example, given the array $[3, 3, 2, 2, 2, 1, 5, 8, 4, 4]$, there are 6 blocks: $[3, 3], [2, 2, 2], [1], [5], [8], [4, 4]$ . 
In this task, you are given two arrays, $A$ (of length $n$), and $B$ (of length $m$), and a number $K$. You have to interleave $A$ and $B$ to form an array $C$ such that $C$ has $K$ blocks. Each way of interleaving $A$ and $B$ can be represented as a $0-1$ array $X$ of length $n+m$ in which $X[j]$ is $0$ if $C[j]$ came from $A$ and $X[j]$ is $1$ if $C[j]$ came from $B$.
A formal description of the interleaving process is given at the end.
For example, if $A = [1, 3]$ and $B = [3, 4]$, there are $6$ ways of interleaving $A$ and $B$.  With each interleaving $X$ of $A$ and $B$, we also count the number of blocks in the resulting interleaved array $C$.  The descriptions of the interleavings, $X$, and the outcomes, $C$, are given below.
- $X = [0, 0, 1, 1]$, which corresponds to $C = [1, 3, 3, 4]$, $3$ blocks.
- $X = [0, 1, 0, 1]$, which corresponds to $C = [1, 3, 3, 4]$, $3$ blocks.
- $X = [0, 1, 1, 0]$, which corresponds to $C = [1, 3, 4, 3]$ , $4$ blocks.
- $X = [1, 0, 0, 1]$, which corresponds to $C = [3, 1, 3, 4]$, $4$ blocks.
- $X = [1, 0, 1, 0]$, which corresponds to $C = [3, 1, 4, 3]$, $4$ blocks.
- $X = [1, 1, 0, 0]$, which corresponds to $C = [3, 4, 1, 3]$, $4$ blocks.
Observe that different interleavings $X$ may produce the same array $C$, such as the first two interleavings in the example above.
Your task is the following.  Given arrays $A$ and $B$ and a number $K$, find the number of different interleavings $X$ of $A$ and $B$ that produce an output array $C$ with exactly $K$ blocks.  Note that we are counting the number of interleavings, not the number of different output arrays after interleaving.  For instance, if the same output array C is produced via 2 different interleavings, it gets...
targets: # cook your dish here
mod = 10 ** 8 + 7

def dic_add(dic, k1, v):
 if k1 <= k:
  if k1 in dic:
   dic[k1] = (dic[k1]+v) % mod
  else:
   dic[k1] = v


for _ in range(int(input())):
 n, m, k = list(map(int, input().split()))
 a_l = list(map(int, input().split()))
 b_l = list(map(int, input().split()))

 # 0: m end, 1: n end
 f_dp = [[[{} for _ in range(n+1)] for _ in range(m+1)] for _ in range(2)]

 f_dp[0][1][0] = {1: 1}
 f_dp[1][0][1] = {1: 1}
 dif = 1
 for j in range(1, m):
  if b_l[j] != b_l[j - 1]:
   dif += 1
  f_dp[0][j+1][0] = {dif: 1}

 dif = 1
 for i in range(1, n):
  if a_l[i] != a_l[i - 1]:
   dif += 1
  f_dp[1][0][i+1] = {dif: 1}

 for i in range(1, n + 1):
  for j in range(1, m + 1):
   # m end
   if j -2 >= 0 and b_l[j-1] != b_l[j - 2]:
    addi = 1
   else:
    addi = 0
   for kk, vv in list(f_dp[0][j - 1][i].items()):
    dic_add(f_dp[0][j][i], kk + addi, vv)
   
   if b_l[j-1] != a_l[i-1]:
    addi = 1
   else:
    addi = 0
   for kk, vv in list(f_dp[1][j-1][i].items()):
    dic_add(f_dp[0][j][i], kk + addi, vv)
    
   # n end
   if i -2 >= 0 and a_l[i-1] != a_l[i - 2]:
    addi = 1
   else:
    addi = 0
   for kk, vv in list(f_dp[1][j][i-1].items()):
    dic_add(f_dp[1][j][i], kk + addi, vv)
   
   if b_l[j-1] != a_l[i-1]:
    addi = 1
   else:
    addi = 0
   for kk, vv in list(f_dp[0][j][i-1].items()):
    dic_add(f_dp[1][j][i], kk + addi, vv)

 ans = 0
 if k in f_dp[0][m][n]:
  ans += f_dp[0][m][n][k]
 if k in f_dp[1][m][n]:
  ans += f_dp[1][m][n][k]
 print(ans)