Capeta is working part-time for an animal shipping company. He needs to pick up animals from various zoos and drop them to other zoos. The company ships four kinds of animals: elephants, dogs, cats, and mice.

There are $m$ zoos, numbered $1$ to $m$. Also, there are $n$ animals. For each animal $\boldsymbol{i}$, Capeta knows its type $t_i$ (E for elephant, D for dog, C for cat and M for mouse), source zoo $s_i$ where Capeta has to pick it up from, and destination zoo $d_i$ where Capeta needs to deliver it to. 

Capeta is given a truck with a huge capacity where $n$ animals can easily fit. He is also given additional instructions:

He must visit the zoos in increasing order. He also cannot skip zoos. 

Dogs are scared of elephants, so he is not allowed to bring them together at the same time. 

Cats are scared of dogs, so he is not allowed to bring them together at the same time. 
Mice are scared of cats, so he is not allowed to bring them together at the same time. 
Elephants are scared of mice, so he is not allowed to bring them together at the same time. 

Also, loading and unloading animals are complicated, so once an animal is loaded onto the truck, that animal will only be unloaded at its destination. 

Because of these reasons, Capeta might not be able to transport all animals. He will need to ignore some animals. Which ones? The company decided to leave that decision for Capeta. He is asked to prepare a report and present it at a board meeting of the company.

Capeta needs to report the minimum number of zoos that must be reached so that she is able to transport $\boldsymbol{x}$ animals, for each $\boldsymbol{x}$ from $1$ to $n$. 

Complete the function minimumZooNumbers and return an integer array where the $x^{\text{th}}$ integer is the minimum number of zoos that Capeta needs to reach so that she is able to transport $\boldsymbol{x}$ animals, or $-1$ if it is impossible to transport $\boldsymbol{x}$ animals. 

He is good at driving, but not so much at planning. Hence, he needs your help.

Input Format

The first line contains a single integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases.

Each test case consists of four lines. The first line contains two space-separated integers $m$ and $n$. The second line contains $n$ space-separated characters $t_1,t_2,\ldots,t_n$. The third line contains $n$ space-separated integers $s_1,s_2,\ldots,s_n$. The fourth line contains $n$ space-separated integers $d_1,d_2,\ldots,d_n$.  

$t_i$, $s_i$ and $d_i$ are the details for the $\boldsymbol{i}$th animal, as described in the problem statement.

Constraints

$1\leq t\leq10$  
$1\leq m,n\leq5\cdot10^4$  
$1\leq s_i,d_i\leq m$  
$s_i\neq d_i$
$t_i$ is either E, D, C or M

Subtasks  

For $30\%$ of the total score, $m,n\leq10^3$  

Output Format

For each case, print a single line containing $n$ space-separated integers, where the $x^{\text{th}}$ integer is the minimum number of zoos that Capeta needs to reach so that she is able to transport $\boldsymbol{x}$ animals. If it is not possible to transport $\boldsymbol{x}$ animals at all, then put $-1$ instead.

Sample Input 0
2
10 3
E D C
4 1 4
7 5 8
10 6
E D C M E D
1 1 1 2 9 7
2 2 2 4 10 10

Sample Output 0
5 8 -1
2 2 4 10 -1 -1

Explanation 0

First Test Case

Capeta can transport one animal by traveling up to zoo number $5$. Just drop the dog there. Next, in order to transport $2$ animals (elephant and cat), Capeta has to go up to zoo number $8$.

Second Test Case

 Animal: Drop the elephant to zoo $2$.
Animal: Drop the elephant and cat to zoo $2$.
Animal: Drop the elephant and cat to zoo $2$. Then drop the mouse to zoo $4$.
Animal: Drop the elephant and cat to zoo $2$. Then drop the mouse to zoo $4$. Finally, drop either the elephant or the dog to $10$.
It is impossible to transport $5$ or $6$ animals.

Source: hackerrank | Difficulty: MEDIUM_HARD
Tags: Dynamic programming


Solutions:
C = input
D = range
E = int

def N(l, r, d, ms):
	while l < r:
		if l & 1:
			d[l] += 1
			ms[l] = 0
			l += 1
		if r & 1:
			r -= 1
			d[r] += 1
			ms[r] = 0
		l >>= 1
		r >>= 1

def O(ms, d, c):
	for A in ms:
		A >>= 1
		while A:
			B = A << 1
			C = B + 1
			D = d[B] + c[B]
			E = d[C] + c[C]
			F = D if D > E else E
			if F <= c[A]:
				break
			c[A] = F
			A >>= 1

def P(node, c):
	A = node
	A >>= 1
	while A:
		B = A << 1
		F = B + 1
		C = c[B]
		D = c[F]
		E = C if C > D else D
		if E <= c[A]:
			break
		c[A] = E
		A >>= 1
X = E(C())
I = {}
J = {}
Q = [[] for A in D(10 ** 5)]
for e in D(X):
	R = C().split()
	Y = E(R[0])
	S = E(R[1])
	Z = C().split()
	a = [E(A) for A in C().split()]
	b = [E(A) for A in C().split()]
	A = Y + 1
	for K in D(S):
		L = a[K]
		T = b[K]
		if T > L:
			Q[T].append(L if Z[K] in 'CE' else -L)
	F = [0] * (A << 1)
	B = [*F]
	U = [*F]
	H = [*F]

	def c(sl, zi):
		for E in sl:
			if E > 0:
				N(A, A + E + 1, F, I)
			else:
				N(A, A + -E + 1, U, J)
		O(I.keys(), F, B)
		O(J.keys(), U, H)
		D = B[1]
		G = H[1]
		C = A + zi
		if D ^ G:
			if D > G:
				H[C] = B[C] = D
				P(C, H)
			else:
				B[C] = G
				P(C, B)
		else:
			B[C] = D
		sl.clear()
		I.clear()
		J.clear()
	for G in D(1, A):
		V = Q[G]
		if V:
			c(V, G)
	W = [-1] * S
	M = 0
	for G in D(1, A):
		d = G + A
		while M < B[d]:
			W[M] = G
			M += 1
	print(*W)