Cersei wants to be the queen of seven kingdoms.

For this to happen, she needs to address the soldiers in her army. There are n$n$ soldiers in her army (numbered 1$1$ through n$n$). Cersei passes on the message to the first soldier (soldier 1).   
This message needs to reach every soldier in the army. For this, the soldiers communicate among themselves by one soldier passing the message to another soldier through some communication links. It is known that the message could reach every soldier using the given links.
Now, each soldier will receive the message from exactly one soldier or Cersei and could pass on the message to atmost two soldiers. That is each soldier (except soldier 1) has only one incoming link and every soldier (including soldier 1) has atmost two outgoing links.  
Now, the High Sparrow feels that Cersei is planning to kill his people first. Hence, for the sake of his people, he decided to appoint some sparrows to overhear every conversation between the soldiers (The conversation between Cersei and the first soldier needn't be overheard due to the fear of Ser Gregor Clegane).   
To overhear a conversation between soldiers A$A$ and B$B$, there needs to be a sparrow either at soldier A$A$ or soldier B$B$ or both.
Also, by his research, the High Sparrow has found that the soldiers are partitioned into some classes (1$1$ to k$k$). That is, every soldier belongs to exactly one class. He then demands the presence of atleast one sparrow with each class he knows (1$1$ to k$k$).
Find the minimum number of sparrows the High Sparrow needs to recruit for the job or tell that he couldn't.

-----Input:-----
- The first line of the input contains the number of test cases t$t$.
- The first line of each test case gives the number of soldiers n$n$ in the army, the number of communication links m$m$ between the soldiers and the number of classes k$k$ in soldiers.
- The next line of the test case consists of n$n$ integers A1,A2....An$A_1,A_2....A_n$ each denoting the class of the ith$i^{th}$ soldier.
- The next m$m$ lines of the test case contain two integers u$u$ and v$v$, which denotes that soldier u$u$ can pass a message to soldier v$v$ (u≠v$u \neq v$).

-----Output:-----
For each test case, print in a single line the minimum number of sparrows required for the above task or print −1$-1$ if no such way is possible.

-----Constraints-----
- 1≤t≤500$1 \leq t \leq 500$
- 1≤n≤2500$1 \leq n \leq 2500$
- m=n−1$m = n - 1$
- 1≤k≤10$1 \leq k \leq 10$
- 1≤ai≤k$1 \leq a_i \leq k$
- The sum of n$n$ over all test cases is ≤2500$\leq 2500$.

-----Sample Input:-----
1
5 4 3
1 1 2 2 3
1 2
1 3
2 4
2 5

-----Sample Output:-----
3

-----EXPLANATION:-----
Selecting soldiers 1,4,5 would satisfy all the conditions.

-----Sample Input:-----
1
5 4 5
1 1 2 2 3
1 2
1 3
2 4
2 5

-----Sample Output:-----
-1

-----EXPLANATION:-----
Classes 4 and 5 are not present. So, there is no way possible.

Difficulty: interview

Starter code:

Solutions:
dt, a = None, None
def dfs(z):
    r = [{}, {}];ln = len(dt[z])
    if ln == 0:r[0][0] = 0;r[1][1 << a[z]] = 1
    elif ln == 1:
        l = dfs(dt[z][0]);r[0] = l[1]
        for m in l[0]:            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[0][m] + 1) if (1 << a[z]) | m in r[1] else l[0][m] + 1
        for m in l[1]:            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[1][m] + 1) if (1 << a[z]) | m in r[1] else l[1][m] + 1
    elif ln == 2:
        l0 = dfs(dt[z][0]);l1 = dfs(dt[z][1])
        for i0 in range(2):
            for i1 in range(2):
                for m0 in l0[i0]:
                    for m1 in l1[i1]:r[1][(1 << a[z]) | m0 | m1] = min(r[1][(1 << a[z]) | m0 | m1], l0[i0][m0] + l1[i1][m1] + 1) if (1 << a[z]) | m0 | m1 in r[1] else l0[i0][m0] + l1[i1][m1] + 1
        for m0 in l0[1]:
            for m1 in l1[1]:                r[0][m0 | m1] = min(r[0][m0 | m1], l0[1][m0] + l1[1][m1]) if m0 | m1 in r[0] else l0[1][m0] + l1[1][m1]
    return r
for i in range(int(input())):
    n, m, k = map(int, input().split());a = [0] + [int(x) - 1 for x in input().split()];dt = [[] for i in range(n + 1)];
    for i in range(m):u, v = map(int, input().split());dt[u].append(v)
    r = dfs(1);k = (1 << k) - 1
    if (k in r[0]):        v = min(r[0][k], r[1][k])
    elif (k in r[1]):        v = r[1][k]
    else:        v = -1
    print(v)

dt, a = None, None

def dfs(z):
    r = [{}, {}]

    ln = len(dt[z])

    if ln == 0:
        r[0][0] = 0
        r[1][1 << a[z]] = 1

    elif ln == 1:
        l = dfs(dt[z][0])
        r[0] = l[1]

        for m in l[0]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[0][m] + 1) if (1 << a[z]) | m in r[1] else l[0][m] + 1
        for m in l[1]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[1][m] + 1) if (1 << a[z]) | m in r[1] else l[1][m] + 1

    elif ln == 2:
        l0 = dfs(dt[z][0])
        l1 = dfs(dt[z][1])

        for i0 in range(2):
            for i1 in range(2):
                for m0 in l0[i0]:
                    for m1 in l1[i1]:
                        r[1][(1 << a[z]) | m0 | m1] = min(r[1][(1 << a[z]) | m0 | m1], l0[i0][m0] + l1[i1][m1] + 1) if (1 << a[z]) | m0 | m1 in r[1] else l0[i0][m0] + l1[i1][m1] + 1

        for m0 in l0[1]:
            for m1 in l1[1]:
                r[0][m0 | m1] = min(r[0][m0 | m1], l0[1][m0] + l1[1][m1]) if m0 | m1 in r[0] else l0[1][m0] + l1[1][m1]

    return r
for i in range(int(input())):
    n, m, k = map(int, input().split());a = [0] + [int(x) - 1 for x in input().split()];dt = [[] for i in range(n + 1)];
    for i in range(m):u, v = map(int, input().split());dt[u].append(v)
    r = dfs(1);k = (1 << k) - 1
    if (k in r[0]):        v = min(r[0][k], r[1][k])
    elif (k in r[1]):        v = r[1][k]
    else:        v = -1
    print(v)

dt, a = None, None

def dfs(z):
    r = [{}, {}]

    ln = len(dt[z])

    if ln == 0:
        r[0][0] = 0
        r[1][1 << a[z]] = 1

    elif ln == 1:
        l = dfs(dt[z][0])
        r[0] = l[1]

        for m in l[0]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[0][m] + 1) if (1 << a[z]) | m in r[1] else l[0][m] + 1
        for m in l[1]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[1][m] + 1) if (1 << a[z]) | m in r[1] else l[1][m] + 1

    elif ln == 2:
        l0 = dfs(dt[z][0])
        l1 = dfs(dt[z][1])

        for i0 in range(2):
            for i1 in range(2):
                for m0 in l0[i0]:
                    for m1 in l1[i1]:
                        r[1][(1 << a[z]) | m0 | m1] = min(r[1][(1 << a[z]) | m0 | m1], l0[i0][m0] + l1[i1][m1] + 1) if (1 << a[z]) | m0 | m1 in r[1] else l0[i0][m0] + l1[i1][m1] + 1

        for m0 in l0[1]:
            for m1 in l1[1]:
                r[0][m0 | m1] = min(r[0][m0 | m1], l0[1][m0] + l1[1][m1]) if m0 | m1 in r[0] else l0[1][m0] + l1[1][m1]

    return r

t = int(input())
for i in range(t):

    n, m, k = map(int, input().split())
    a = [0] + [int(x) - 1 for x in input().split()]

    dt = [[] for i in range(n + 1)]
    for i in range(m):
        u, v = map(int, input().split())
        dt[u].append(v)

    r = dfs(1)

    k = (1 << k) - 1
    if (k in r[0]):
        v = min(r[0][k], r[1][k])
    elif (k in r[1]):
        v = r[1][k]
    else:
        v = -1

    print(v)

# cook your dish here
dt, a = None, None

def dfs(z):
    r = [{}, {}]

    ln = len(dt[z])

    if ln == 0:
        r[0][0] = 0
        r[1][1 << a[z]] = 1

    elif ln == 1:
        l = dfs(dt[z][0])
        r[0] = l[1]

        for m in l[0]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[0][m] + 1) if (1 << a[z]) | m in r[1] else l[0][m] + 1
        for m in l[1]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[1][m] + 1) if (1 << a[z]) | m in r[1] else l[1][m] + 1

    elif ln == 2:
        l0 = dfs(dt[z][0])
        l1 = dfs(dt[z][1])

        for i0 in range(2):
            for i1 in range(2):
                for m0 in l0[i0]:
                    for m1 in l1[i1]:
                        r[1][(1 << a[z]) | m0 | m1] = min(r[1][(1 << a[z]) | m0 | m1], l0[i0][m0] + l1[i1][m1] + 1) if (1 << a[z]) | m0 | m1 in r[1] else l0[i0][m0] + l1[i1][m1] + 1

        for m0 in l0[1]:
            for m1 in l1[1]:
                r[0][m0 | m1] = min(r[0][m0 | m1], l0[1][m0] + l1[1][m1]) if m0 | m1 in r[0] else l0[1][m0] + l1[1][m1]

    return r

t = int(input())
for i in range(t):

    n, m, k = map(int, input().split())
    a = [0] + [int(x) - 1 for x in input().split()]
    
    dt = [[] for i in range(n + 1)]
    for i in range(m):
        u, v = map(int, input().split())
        dt[u].append(v)

    r = dfs(1)

    k = (1 << k) - 1
    if (k in r[0]):
        v = min(r[0][k], r[1][k])
    elif (k in r[1]):
        v = r[1][k]
    else:
        v = -1

    print(v)


# cook your dish here
# cook your dish here
# cook your dish here
dt, a = None, None

def dfs(z):
    r = [{}, {}]

    ln = len(dt[z])

    if ln == 0:
        r[0][0] = 0
        r[1][1 << a[z]] = 1

    elif ln == 1:
        l = dfs(dt[z][0])
        r[0] = l[1]

        for m in l[0]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[0][m] + 1) if (1 << a[z]) | m in r[1] else l[0][m] + 1
        for m in l[1]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[1][m] + 1) if (1 << a[z]) | m in r[1] else l[1][m] + 1

    elif ln == 2:
        l0 = dfs(dt[z][0])
        l1 = dfs(dt[z][1])

        for i0 in range(2):
            for i1 in range(2):
                for m0 in l0[i0]:
                    for m1 in l1[i1]:
                        r[1][(1 << a[z]) | m0 | m1] = min(r[1][(1 << a[z]) | m0 | m1], l0[i0][m0] + l1[i1][m1] + 1) if (1 << a[z]) | m0 | m1 in r[1] else l0[i0][m0] + l1[i1][m1] + 1

        for m0 in l0[1]:
            for m1 in l1[1]:
                r[0][m0 | m1] = min(r[0][m0 | m1], l0[1][m0] + l1[1][m1]) if m0 | m1 in r[0] else l0[1][m0] + l1[1][m1]

    return r

t = int(input())
for i in range(t):

    n, m, k = map(int, input().split())
    a = [0] + [int(x) - 1 for x in input().split()]

    dt = [[] for i in range(n + 1)]
    for i in range(m):
        u, v = map(int, input().split())
        dt[u].append(v)

    r = dfs(1)

    k = (1 << k) - 1
    if (k in r[0]):
        v = min(r[0][k], r[1][k])
    elif (k in r[1]):
        v = r[1][k]
    else:
        v = -1

    print(v)


# cook your dish here
# cook your dish here
dt, a = None, None

def dfs(z):
    r = [{}, {}]

    ln = len(dt[z])

    if ln == 0:
        r[0][0] = 0
        r[1][1 << a[z]] = 1

    elif ln == 1:
        l = dfs(dt[z][0])
        r[0] = l[1]

        for m in l[0]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[0][m] + 1) if (1 << a[z]) | m in r[1] else l[0][m] + 1
        for m in l[1]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[1][m] + 1) if (1 << a[z]) | m in r[1] else l[1][m] + 1

    elif ln == 2:
        l0 = dfs(dt[z][0])
        l1 = dfs(dt[z][1])

        for i0 in range(2):
            for i1 in range(2):
                for m0 in l0[i0]:
                    for m1 in l1[i1]:
                        r[1][(1 << a[z]) | m0 | m1] = min(r[1][(1 << a[z]) | m0 | m1], l0[i0][m0] + l1[i1][m1] + 1) if (1 << a[z]) | m0 | m1 in r[1] else l0[i0][m0] + l1[i1][m1] + 1

        for m0 in l0[1]:
            for m1 in l1[1]:
                r[0][m0 | m1] = min(r[0][m0 | m1], l0[1][m0] + l1[1][m1]) if m0 | m1 in r[0] else l0[1][m0] + l1[1][m1]

    return r

t = int(input())
for i in range(t):

    n, m, k = map(int, input().split())
    a = [0] + [int(x) - 1 for x in input().split()]

    dt = [[] for i in range(n + 1)]
    for i in range(m):
        u, v = map(int, input().split())
        dt[u].append(v)

    r = dfs(1)

    k = (1 << k) - 1
    if (k in r[0]):
        v = min(r[0][k], r[1][k])
    elif (k in r[1]):
        v = r[1][k]
    else:
        v = -1

    print(v)


# cook your dish here
# cook your dish here
# cook your dish here
dt, a = None, None

def dfs(z):
    r = [{}, {}]

    ln = len(dt[z])

    if ln == 0:
        r[0][0] = 0
        r[1][1 << a[z]] = 1

    elif ln == 1:
        l = dfs(dt[z][0])
        r[0] = l[1]

        for m in l[0]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[0][m] + 1) if (1 << a[z]) | m in r[1] else l[0][m] + 1
        for m in l[1]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[1][m] + 1) if (1 << a[z]) | m in r[1] else l[1][m] + 1

    elif ln == 2:
        l0 = dfs(dt[z][0])
        l1 = dfs(dt[z][1])

        for i0 in range(2):
            for i1 in range(2):
                for m0 in l0[i0]:
                    for m1 in l1[i1]:
                        r[1][(1 << a[z]) | m0 | m1] = min(r[1][(1 << a[z]) | m0 | m1], l0[i0][m0] + l1[i1][m1] + 1) if (1 << a[z]) | m0 | m1 in r[1] else l0[i0][m0] + l1[i1][m1] + 1

        for m0 in l0[1]:
            for m1 in l1[1]:
                r[0][m0 | m1] = min(r[0][m0 | m1], l0[1][m0] + l1[1][m1]) if m0 | m1 in r[0] else l0[1][m0] + l1[1][m1]

    return r

t = int(input())
for i in range(t):

    n, m, k = map(int, input().split())
    a = [0] + [int(x) - 1 for x in input().split()]

    dt = [[] for i in range(n + 1)]
    for i in range(m):
        u, v = map(int, input().split())
        dt[u].append(v)

    r = dfs(1)

    k = (1 << k) - 1
    if (k in r[0]):
        v = min(r[0][k], r[1][k])
    elif (k in r[1]):
        v = r[1][k]
    else:
        v = -1

    print(v)


# cook your dish here
# cook your dish here
dt, a = None, None

def dfs(z):
    r = [{}, {}]

    ln = len(dt[z])

    if ln == 0:
        r[0][0] = 0
        r[1][1 << a[z]] = 1

    elif ln == 1:
        l = dfs(dt[z][0])
        r[0] = l[1]

        for m in l[0]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[0][m] + 1) if (1 << a[z]) | m in r[1] else l[0][m] + 1
        for m in l[1]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[1][m] + 1) if (1 << a[z]) | m in r[1] else l[1][m] + 1

    elif ln == 2:
        l0 = dfs(dt[z][0])
        l1 = dfs(dt[z][1])

        for i0 in range(2):
            for i1 in range(2):
                for m0 in l0[i0]:
                    for m1 in l1[i1]:
                        r[1][(1 << a[z]) | m0 | m1] = min(r[1][(1 << a[z]) | m0 | m1], l0[i0][m0] + l1[i1][m1] + 1) if (1 << a[z]) | m0 | m1 in r[1] else l0[i0][m0] + l1[i1][m1] + 1

        for m0 in l0[1]:
            for m1 in l1[1]:
                r[0][m0 | m1] = min(r[0][m0 | m1], l0[1][m0] + l1[1][m1]) if m0 | m1 in r[0] else l0[1][m0] + l1[1][m1]

    return r

t = int(input())
for i in range(t):

    n, m, k = map(int, input().split())
    a = [0] + [int(x) - 1 for x in input().split()]

    dt = [[] for i in range(n + 1)]
    for i in range(m):
        u, v = map(int, input().split())
        dt[u].append(v)

    r = dfs(1)

    k = (1 << k) - 1
    if (k in r[0]):
        v = min(r[0][k], r[1][k])
    elif (k in r[1]):
        v = r[1][k]
    else:
        v = -1

    print(v)


# cook your dish here
# cook your dish here
dt, a = None, None

def dfs(z):
    r = [{}, {}]

    ln = len(dt[z])

    if ln == 0:
        r[0][0] = 0
        r[1][1 << a[z]] = 1

    elif ln == 1:
        l = dfs(dt[z][0])
        r[0] = l[1]

        for m in l[0]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[0][m] + 1) if (1 << a[z]) | m in r[1] else l[0][m] + 1
        for m in l[1]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[1][m] + 1) if (1 << a[z]) | m in r[1] else l[1][m] + 1

    elif ln == 2:
        l0 = dfs(dt[z][0])
        l1 = dfs(dt[z][1])

        for i0 in range(2):
            for i1 in range(2):
                for m0 in l0[i0]:
                    for m1 in l1[i1]:
                        r[1][(1 << a[z]) | m0 | m1] = min(r[1][(1 << a[z]) | m0 | m1], l0[i0][m0] + l1[i1][m1] + 1) if (1 << a[z]) | m0 | m1 in r[1] else l0[i0][m0] + l1[i1][m1] + 1

        for m0 in l0[1]:
            for m1 in l1[1]:
                r[0][m0 | m1] = min(r[0][m0 | m1], l0[1][m0] + l1[1][m1]) if m0 | m1 in r[0] else l0[1][m0] + l1[1][m1]

    return r

t = int(input())
for i in range(t):

    n, m, k = map(int, input().split())
    a = [0] + [int(x) - 1 for x in input().split()]

    dt = [[] for i in range(n + 1)]
    for i in range(m):
        u, v = map(int, input().split())
        dt[u].append(v)

    r = dfs(1)

    k = (1 << k) - 1
    if (k in r[0]):
        v = min(r[0][k], r[1][k])
    elif (k in r[1]):
        v = r[1][k]
    else:
        v = -1

    print(v)



dt, a = None, None

def dfs(z):
    r = [{}, {}]

    ln = len(dt[z])

    if ln == 0:
        r[0][0] = 0
        r[1][1 << a[z]] = 1

    elif ln == 1:
        l = dfs(dt[z][0])
        r[0] = l[1]

        for m in l[0]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[0][m] + 1) if (1 << a[z]) | m in r[1] else l[0][m] + 1
        for m in l[1]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[1][m] + 1) if (1 << a[z]) | m in r[1] else l[1][m] + 1

    elif ln == 2:
        l0 = dfs(dt[z][0])
        l1 = dfs(dt[z][1])

        for i0 in range(2):
            for i1 in range(2):
                for m0 in l0[i0]:
                    for m1 in l1[i1]:
                        r[1][(1 << a[z]) | m0 | m1] = min(r[1][(1 << a[z]) | m0 | m1], l0[i0][m0] + l1[i1][m1] + 1) if (1 << a[z]) | m0 | m1 in r[1] else l0[i0][m0] + l1[i1][m1] + 1

        for m0 in l0[1]:
            for m1 in l1[1]:
                r[0][m0 | m1] = min(r[0][m0 | m1], l0[1][m0] + l1[1][m1]) if m0 | m1 in r[0] else l0[1][m0] + l1[1][m1]

    return r

t = int(input())
for i in range(t):

    n, m, k = map(int, input().split())
    a = [0] + [int(x) - 1 for x in input().split()]

    dt = [[] for i in range(n + 1)]
    for i in range(m):
        u, v = map(int, input().split())
        dt[u].append(v)

    r = dfs(1)

    k = (1 << k) - 1
    if (k in r[0]):
        v = min(r[0][k], r[1][k])
    elif (k in r[1]):
        v = r[1][k]
    else:
        v = -1

    print(v)


# cook your dish here
# cook your dish here
dt, a = None, None

def dfs(z):
    r = [{}, {}]

    ln = len(dt[z])

    if ln == 0:
        r[0][0] = 0
        r[1][1 << a[z]] = 1

    elif ln == 1:
        l = dfs(dt[z][0])
        r[0] = l[1]

        for m in l[0]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[0][m] + 1) if (1 << a[z]) | m in r[1] else l[0][m] + 1
        for m in l[1]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[1][m] + 1) if (1 << a[z]) | m in r[1] else l[1][m] + 1

    elif ln == 2:
        l0 = dfs(dt[z][0])
        l1 = dfs(dt[z][1])

        for i0 in range(2):
            for i1 in range(2):
                for m0 in l0[i0]:
                    for m1 in l1[i1]:
                        r[1][(1 << a[z]) | m0 | m1] = min(r[1][(1 << a[z]) | m0 | m1], l0[i0][m0] + l1[i1][m1] + 1) if (1 << a[z]) | m0 | m1 in r[1] else l0[i0][m0] + l1[i1][m1] + 1

        for m0 in l0[1]:
            for m1 in l1[1]:
                r[0][m0 | m1] = min(r[0][m0 | m1], l0[1][m0] + l1[1][m1]) if m0 | m1 in r[0] else l0[1][m0] + l1[1][m1]

    return r

t = int(input())
for i in range(t):

    n, m, k = map(int, input().split())
    a = [0] + [int(x) - 1 for x in input().split()]

    dt = [[] for i in range(n + 1)]
    for i in range(m):
        u, v = map(int, input().split())
        dt[u].append(v)

    r = dfs(1)

    k = (1 << k) - 1
    if (k in r[0]):
        v = min(r[0][k], r[1][k])
    elif (k in r[1]):
        v = r[1][k]
    else:
        v = -1

    print(v)


# cook your dish here
# cook your dish here
dt, a = None, None

def dfs(z):
    r = [{}, {}]

    ln = len(dt[z])

    if ln == 0:
        r[0][0] = 0
        r[1][1 << a[z]] = 1

    elif ln == 1:
        l = dfs(dt[z][0])
        r[0] = l[1]

        for m in l[0]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[0][m] + 1) if (1 << a[z]) | m in r[1] else l[0][m] + 1
        for m in l[1]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[1][m] + 1) if (1 << a[z]) | m in r[1] else l[1][m] + 1

    elif ln == 2:
        l0 = dfs(dt[z][0])
        l1 = dfs(dt[z][1])

        for i0 in range(2):
            for i1 in range(2):
                for m0 in l0[i0]:
                    for m1 in l1[i1]:
                        r[1][(1 << a[z]) | m0 | m1] = min(r[1][(1 << a[z]) | m0 | m1], l0[i0][m0] + l1[i1][m1] + 1) if (1 << a[z]) | m0 | m1 in r[1] else l0[i0][m0] + l1[i1][m1] + 1

        for m0 in l0[1]:
            for m1 in l1[1]:
                r[0][m0 | m1] = min(r[0][m0 | m1], l0[1][m0] + l1[1][m1]) if m0 | m1 in r[0] else l0[1][m0] + l1[1][m1]

    return r

t = int(input())
for i in range(t):

    n, m, k = map(int, input().split())
    a = [0] + [int(x) - 1 for x in input().split()]

    dt = [[] for i in range(n + 1)]
    for i in range(m):
        u, v = map(int, input().split())
        dt[u].append(v)

    r = dfs(1)

    k = (1 << k) - 1
    if (k in r[0]):
        v = min(r[0][k], r[1][k])
    elif (k in r[1]):
        v = r[1][k]
    else:
        v = -1

    print(v)


# cook your dish here
dt, a = None, None

def dfs(z):
    r = [{}, {}]

    ln = len(dt[z])

    if ln == 0:
        r[0][0] = 0
        r[1][1 << a[z]] = 1

    elif ln == 1:
        l = dfs(dt[z][0])
        r[0] = l[1]

        for m in l[0]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[0][m] + 1) if (1 << a[z]) | m in r[1] else l[0][m] + 1
        for m in l[1]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[1][m] + 1) if (1 << a[z]) | m in r[1] else l[1][m] + 1

    elif ln == 2:
        l0 = dfs(dt[z][0])
        l1 = dfs(dt[z][1])

        for i0 in range(2):
            for i1 in range(2):
                for m0 in l0[i0]:
                    for m1 in l1[i1]:
                        r[1][(1 << a[z]) | m0 | m1] = min(r[1][(1 << a[z]) | m0 | m1], l0[i0][m0] + l1[i1][m1] + 1) if (1 << a[z]) | m0 | m1 in r[1] else l0[i0][m0] + l1[i1][m1] + 1

        for m0 in l0[1]:
            for m1 in l1[1]:
                r[0][m0 | m1] = min(r[0][m0 | m1], l0[1][m0] + l1[1][m1]) if m0 | m1 in r[0] else l0[1][m0] + l1[1][m1]

    return r

t = int(input())
for i in range(t):

    n, m, k = map(int, input().split())
    a = [0] + [int(x) - 1 for x in input().split()]

    dt = [[] for i in range(n + 1)]
    for i in range(m):
        u, v = map(int, input().split())
        dt[u].append(v)

    r = dfs(1)

    k = (1 << k) - 1
    if (k in r[0]):
        v = min(r[0][k], r[1][k])
    elif (k in r[1]):
        v = r[1][k]
    else:
        v = -1

    print(v)


dt, a = None, None

def dfs(z):
    r = [{}, {}]

    ln = len(dt[z])

    if ln == 0:
        r[0][0] = 0
        r[1][1 << a[z]] = 1

    elif ln == 1:
        l = dfs(dt[z][0])
        r[0] = l[1]

        for m in l[0]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[0][m] + 1) if (1 << a[z]) | m in r[1] else l[0][m] + 1
        for m in l[1]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[1][m] + 1) if (1 << a[z]) | m in r[1] else l[1][m] + 1

    elif ln == 2:
        l0 = dfs(dt[z][0])
        l1 = dfs(dt[z][1])

        for i0 in range(2):
            for i1 in range(2):
                for m0 in l0[i0]:
                    for m1 in l1[i1]:
                        r[1][(1 << a[z]) | m0 | m1] = min(r[1][(1 << a[z]) | m0 | m1], l0[i0][m0] + l1[i1][m1] + 1) if (1 << a[z]) | m0 | m1 in r[1] else l0[i0][m0] + l1[i1][m1] + 1

        for m0 in l0[1]:
            for m1 in l1[1]:
                r[0][m0 | m1] = min(r[0][m0 | m1], l0[1][m0] + l1[1][m1]) if m0 | m1 in r[0] else l0[1][m0] + l1[1][m1]

    return r

t = int(input())
for i in range(t):

    n, m, k = map(int, input().split())
    a = [0] + [int(x) - 1 for x in input().split()]

    dt = [[] for i in range(n + 1)]
    for i in range(m):
        u, v = map(int, input().split())
        dt[u].append(v)

    r = dfs(1)

    k = (1 << k) - 1
    if (k in r[0]):
        v = min(r[0][k], r[1][k])
    elif (k in r[1]):
        v = r[1][k]
    else:
        v = -1

    print(v)


dt, a = None, None

def dfs(z):
    r = [{}, {}]

    ln = len(dt[z])

    if ln == 0:
        r[0][0] = 0
        r[1][1 << a[z]] = 1

    elif ln == 1:
        l = dfs(dt[z][0])
        r[0] = l[1]

        for m in l[0]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[0][m] + 1) if (1 << a[z]) | m in r[1] else l[0][m] + 1
        for m in l[1]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[1][m] + 1) if (1 << a[z]) | m in r[1] else l[1][m] + 1

    elif ln == 2:
        l0 = dfs(dt[z][0])
        l1 = dfs(dt[z][1])

        for i0 in range(2):
            for i1 in range(2):
                for m0 in l0[i0]:
                    for m1 in l1[i1]:
                        r[1][(1 << a[z]) | m0 | m1] = min(r[1][(1 << a[z]) | m0 | m1], l0[i0][m0] + l1[i1][m1] + 1) if (1 << a[z]) | m0 | m1 in r[1] else l0[i0][m0] + l1[i1][m1] + 1

        for m0 in l0[1]:
            for m1 in l1[1]:
                r[0][m0 | m1] = min(r[0][m0 | m1], l0[1][m0] + l1[1][m1]) if m0 | m1 in r[0] else l0[1][m0] + l1[1][m1]

    return r

t = int(input())
for i in range(t):

    n, m, k = map(int, input().split())
    a = [0] + [int(x) - 1 for x in input().split()]

    dt = [[] for i in range(n + 1)]
    for i in range(m):
        u, v = map(int, input().split())
        dt[u].append(v)

    r = dfs(1)

    k = (1 << k) - 1
    if (k in r[0]):
        v = min(r[0][k], r[1][k])
    elif (k in r[1]):
        v = r[1][k]
    else:
        v = -1

    print(v)


# cook your dish here
dt, a = None, None

def dfs(z):
    r = [{}, {}]

    ln = len(dt[z])

    if ln == 0:
        r[0][0] = 0
        r[1][1 << a[z]] = 1

    elif ln == 1:
        l = dfs(dt[z][0])
        r[0] = l[1]

        for m in l[0]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[0][m] + 1) if (1 << a[z]) | m in r[1] else l[0][m] + 1
        for m in l[1]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[1][m] + 1) if (1 << a[z]) | m in r[1] else l[1][m] + 1

    elif ln == 2:
        l0 = dfs(dt[z][0])
        l1 = dfs(dt[z][1])

        for i0 in range(2):
            for i1 in range(2):
                for m0 in l0[i0]:
                    for m1 in l1[i1]:
                        r[1][(1 << a[z]) | m0 | m1] = min(r[1][(1 << a[z]) | m0 | m1], l0[i0][m0] + l1[i1][m1] + 1) if (1 << a[z]) | m0 | m1 in r[1] else l0[i0][m0] + l1[i1][m1] + 1

        for m0 in l0[1]:
            for m1 in l1[1]:
                r[0][m0 | m1] = min(r[0][m0 | m1], l0[1][m0] + l1[1][m1]) if m0 | m1 in r[0] else l0[1][m0] + l1[1][m1]

    return r

t = int(input())
for i in range(t):

    n, m, k = map(int, input().split())
    a = [0] + [int(x) - 1 for x in input().split()]

    dt = [[] for i in range(n + 1)]
    for i in range(m):
        u, v = map(int, input().split())
        dt[u].append(v)

    r = dfs(1)

    k = (1 << k) - 1
    if (k in r[0]):
        v = min(r[0][k], r[1][k])
    elif (k in r[1]):
        v = r[1][k]
    else:
        v = -1

    print(v)


# cook your dish here
dt, a = None, None

def dfs(z):
    r = [{}, {}]

    ln = len(dt[z])

    if ln == 0:
        r[0][0] = 0
        r[1][1 << a[z]] = 1

    elif ln == 1:
        l = dfs(dt[z][0])
        r[0] = l[1]

        for m in l[0]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[0][m] + 1) if (1 << a[z]) | m in r[1] else l[0][m] + 1
        for m in l[1]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[1][m] + 1) if (1 << a[z]) | m in r[1] else l[1][m] + 1

    elif ln == 2:
        l0 = dfs(dt[z][0])
        l1 = dfs(dt[z][1])

        for i0 in range(2):
            for i1 in range(2):
                for m0 in l0[i0]:
                    for m1 in l1[i1]:
                        r[1][(1 << a[z]) | m0 | m1] = min(r[1][(1 << a[z]) | m0 | m1], l0[i0][m0] + l1[i1][m1] + 1) if (1 << a[z]) | m0 | m1 in r[1] else l0[i0][m0] + l1[i1][m1] + 1

        for m0 in l0[1]:
            for m1 in l1[1]:
                r[0][m0 | m1] = min(r[0][m0 | m1], l0[1][m0] + l1[1][m1]) if m0 | m1 in r[0] else l0[1][m0] + l1[1][m1]

    return r

t = int(input())
for i in range(t):

    n, m, k = map(int, input().split())
    a = [0] + [int(x) - 1 for x in input().split()]

    dt = [[] for i in range(n + 1)]
    for i in range(m):
        u, v = map(int, input().split())
        dt[u].append(v)

    r = dfs(1)

    k = (1 << k) - 1
    if (k in r[0]):
        v = min(r[0][k], r[1][k])
    elif (k in r[1]):
        v = r[1][k]
    else:
        v = -1

    print(v)


# cook your dish here
dt, a = None, None

def dfs(z):
    r = [{}, {}]

    ln = len(dt[z])

    if ln == 0:
        r[0][0] = 0
        r[1][1 << a[z]] = 1

    elif ln == 1:
        l = dfs(dt[z][0])
        r[0] = l[1]

        for m in l[0]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[0][m] + 1) if (1 << a[z]) | m in r[1] else l[0][m] + 1
        for m in l[1]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[1][m] + 1) if (1 << a[z]) | m in r[1] else l[1][m] + 1

    elif ln == 2:
        l0 = dfs(dt[z][0])
        l1 = dfs(dt[z][1])

        for i0 in range(2):
            for i1 in range(2):
                for m0 in l0[i0]:
                    for m1 in l1[i1]:
                        r[1][(1 << a[z]) | m0 | m1] = min(r[1][(1 << a[z]) | m0 | m1], l0[i0][m0] + l1[i1][m1] + 1) if (1 << a[z]) | m0 | m1 in r[1] else l0[i0][m0] + l1[i1][m1] + 1

        for m0 in l0[1]:
            for m1 in l1[1]:
                r[0][m0 | m1] = min(r[0][m0 | m1], l0[1][m0] + l1[1][m1]) if m0 | m1 in r[0] else l0[1][m0] + l1[1][m1]

    return r

t = int(input())
for i in range(t):

    n, m, k = map(int, input().split())
    a = [0] + [int(x) - 1 for x in input().split()]

    dt = [[] for i in range(n + 1)]
    for i in range(m):
        u, v = map(int, input().split())
        dt[u].append(v)

    r = dfs(1)

    k = (1 << k) - 1
    if (k in r[0]):
        v = min(r[0][k], r[1][k])
    elif (k in r[1]):
        v = r[1][k]
    else:
        v = -1

    print(v)


# cook your dish here
dt, a = None, None

def dfs(z):
    r = [{}, {}]

    ln = len(dt[z])

    if ln == 0:
        r[0][0] = 0
        r[1][1 << a[z]] = 1

    elif ln == 1:
        l = dfs(dt[z][0])
        r[0] = l[1]

        for m in l[0]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[0][m] + 1) if (1 << a[z]) | m in r[1] else l[0][m] + 1
        for m in l[1]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[1][m] + 1) if (1 << a[z]) | m in r[1] else l[1][m] + 1

    elif ln == 2:
        l0 = dfs(dt[z][0])
        l1 = dfs(dt[z][1])

        for i0 in range(2):
            for i1 in range(2):
                for m0 in l0[i0]:
                    for m1 in l1[i1]:
                        r[1][(1 << a[z]) | m0 | m1] = min(r[1][(1 << a[z]) | m0 | m1], l0[i0][m0] + l1[i1][m1] + 1) if (1 << a[z]) | m0 | m1 in r[1] else l0[i0][m0] + l1[i1][m1] + 1

        for m0 in l0[1]:
            for m1 in l1[1]:
                r[0][m0 | m1] = min(r[0][m0 | m1], l0[1][m0] + l1[1][m1]) if m0 | m1 in r[0] else l0[1][m0] + l1[1][m1]

    return r

t = int(input())
for i in range(t):

    n, m, k = map(int, input().split())
    a = [0] + [int(x) - 1 for x in input().split()]

    dt = [[] for i in range(n + 1)]
    for i in range(m):
        u, v = map(int, input().split())
        dt[u].append(v)

    r = dfs(1)

    k = (1 << k) - 1
    if (k in r[0]):
        v = min(r[0][k], r[1][k])
    elif (k in r[1]):
        v = r[1][k]
    else:
        v = -1

    print(v)


dt, a = None, None

def dfs(z):
    r = [{}, {}]

    ln = len(dt[z])

    if ln == 0:
        r[0][0] = 0
        r[1][1 << a[z]] = 1

    elif ln == 1:
        l = dfs(dt[z][0])
        r[0] = l[1]

        for m in l[0]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[0][m] + 1) if (1 << a[z]) | m in r[1] else l[0][m] + 1
        for m in l[1]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[1][m] + 1) if (1 << a[z]) | m in r[1] else l[1][m] + 1

    elif ln == 2:
        l0 = dfs(dt[z][0])
        l1 = dfs(dt[z][1])

        for i0 in range(2):
            for i1 in range(2):
                for m0 in l0[i0]:
                    for m1 in l1[i1]:
                        r[1][(1 << a[z]) | m0 | m1] = min(r[1][(1 << a[z]) | m0 | m1], l0[i0][m0] + l1[i1][m1] + 1) if (1 << a[z]) | m0 | m1 in r[1] else l0[i0][m0] + l1[i1][m1] + 1

        for m0 in l0[1]:
            for m1 in l1[1]:
                r[0][m0 | m1] = min(r[0][m0 | m1], l0[1][m0] + l1[1][m1]) if m0 | m1 in r[0] else l0[1][m0] + l1[1][m1]

    return r

t = int(input())
for i in range(t):

    n, m, k = map(int, input().split())
    a = [0] + [int(x) - 1 for x in input().split()]

    dt = [[] for i in range(n + 1)]
    for i in range(m):
        u, v = map(int, input().split())
        dt[u].append(v)

    r = dfs(1)

    k = (1 << k) - 1
    if (k in r[0]):
        v = min(r[0][k], r[1][k])
    elif (k in r[1]):
        v = r[1][k]
    else:
        v = -1

    print(v)


# cook your dish here
dt, a = None, None

def dfs(z):
    r = [{}, {}]

    ln = len(dt[z])

    if ln == 0:
        r[0][0] = 0
        r[1][1 << a[z]] = 1

    elif ln == 1:
        l = dfs(dt[z][0])
        r[0] = l[1]

        for m in l[0]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[0][m] + 1) if (1 << a[z]) | m in r[1] else l[0][m] + 1
        for m in l[1]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[1][m] + 1) if (1 << a[z]) | m in r[1] else l[1][m] + 1

    elif ln == 2:
        l0 = dfs(dt[z][0])
        l1 = dfs(dt[z][1])

        for i0 in range(2):
            for i1 in range(2):
                for m0 in l0[i0]:
                    for m1 in l1[i1]:
                        r[1][(1 << a[z]) | m0 | m1] = min(r[1][(1 << a[z]) | m0 | m1], l0[i0][m0] + l1[i1][m1] + 1) if (1 << a[z]) | m0 | m1 in r[1] else l0[i0][m0] + l1[i1][m1] + 1

        for m0 in l0[1]:
            for m1 in l1[1]:
                r[0][m0 | m1] = min(r[0][m0 | m1], l0[1][m0] + l1[1][m1]) if m0 | m1 in r[0] else l0[1][m0] + l1[1][m1]

    return r

t = int(input())
for i in range(t):

    n, m, k = map(int, input().split())
    a = [0] + [int(x) - 1 for x in input().split()]

    dt = [[] for i in range(n + 1)]
    for i in range(m):
        u, v = map(int, input().split())
        dt[u].append(v)

    r = dfs(1)

    k = (1 << k) - 1
    if (k in r[0]):
        v = min(r[0][k], r[1][k])
    elif (k in r[1]):
        v = r[1][k]
    else:
        v = -1

    print(v)

# cook your dish here
dt, a = None, None

def dfs(z):
    r = [{}, {}]

    ln = len(dt[z])

    if ln == 0:
        r[0][0] = 0
        r[1][1 << a[z]] = 1

    elif ln == 1:
        l = dfs(dt[z][0])
        r[0] = l[1]

        for m in l[0]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[0][m] + 1) if (1 << a[z]) | m in r[1] else l[0][m] + 1
        for m in l[1]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[1][m] + 1) if (1 << a[z]) | m in r[1] else l[1][m] + 1

    elif ln == 2:
        l0 = dfs(dt[z][0])
        l1 = dfs(dt[z][1])

        for i0 in range(2):
            for i1 in range(2):
                for m0 in l0[i0]:
                    for m1 in l1[i1]:
                        r[1][(1 << a[z]) | m0 | m1] = min(r[1][(1 << a[z]) | m0 | m1], l0[i0][m0] + l1[i1][m1] + 1) if (1 << a[z]) | m0 | m1 in r[1] else l0[i0][m0] + l1[i1][m1] + 1

        for m0 in l0[1]:
            for m1 in l1[1]:
                r[0][m0 | m1] = min(r[0][m0 | m1], l0[1][m0] + l1[1][m1]) if m0 | m1 in r[0] else l0[1][m0] + l1[1][m1]

    return r

t = int(input())
for i in range(t):

    n, m, k = map(int, input().split())
    a = [0] + [int(x) - 1 for x in input().split()]

    dt = [[] for i in range(n + 1)]
    for i in range(m):
        u, v = map(int, input().split())
        dt[u].append(v)

    r = dfs(1)

    k = (1 << k) - 1
    if (k in r[0]):
        v = min(r[0][k], r[1][k])
    elif (k in r[1]):
        v = r[1][k]
    else:
        v = -1

    print(v)

# cook your dish here
dt, a = None, None

def dfs(z):
    r = [{}, {}]

    ln = len(dt[z])

    if ln == 0:
        r[0][0] = 0
        r[1][1 << a[z]] = 1

    elif ln == 1:
        l = dfs(dt[z][0])
        r[0] = l[1]

        for m in l[0]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[0][m] + 1) if (1 << a[z]) | m in r[1] else l[0][m] + 1
        for m in l[1]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[1][m] + 1) if (1 << a[z]) | m in r[1] else l[1][m] + 1

    elif ln == 2:
        l0 = dfs(dt[z][0])
        l1 = dfs(dt[z][1])

        for i0 in range(2):
            for i1 in range(2):
                for m0 in l0[i0]:
                    for m1 in l1[i1]:
                        r[1][(1 << a[z]) | m0 | m1] = min(r[1][(1 << a[z]) | m0 | m1], l0[i0][m0] + l1[i1][m1] + 1) if (1 << a[z]) | m0 | m1 in r[1] else l0[i0][m0] + l1[i1][m1] + 1

        for m0 in l0[1]:
            for m1 in l1[1]:
                r[0][m0 | m1] = min(r[0][m0 | m1], l0[1][m0] + l1[1][m1]) if m0 | m1 in r[0] else l0[1][m0] + l1[1][m1]

    return r

t = int(input())
for i in range(t):

    n, m, k = map(int, input().split())
    a = [0] + [int(x) - 1 for x in input().split()]

    dt = [[] for i in range(n + 1)]
    for i in range(m):
        u, v = map(int, input().split())
        dt[u].append(v)

    r = dfs(1)

    k = (1 << k) - 1
    if (k in r[0]):
        v = min(r[0][k], r[1][k])
    elif (k in r[1]):
        v = r[1][k]
    else:
        v = -1

    print(v)


# cook your dish here
dt, a = None, None

def dfs(z):
    r = [{}, {}]

    ln = len(dt[z])

    if ln == 0:
        r[0][0] = 0
        r[1][1 << a[z]] = 1

    elif ln == 1:
        l = dfs(dt[z][0])
        r[0] = l[1]

        for m in l[0]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[0][m] + 1) if (1 << a[z]) | m in r[1] else l[0][m] + 1
        for m in l[1]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[1][m] + 1) if (1 << a[z]) | m in r[1] else l[1][m] + 1

    elif ln == 2:
        l0 = dfs(dt[z][0])
        l1 = dfs(dt[z][1])

        for i0 in range(2):
            for i1 in range(2):
                for m0 in l0[i0]:
                    for m1 in l1[i1]:
                        r[1][(1 << a[z]) | m0 | m1] = min(r[1][(1 << a[z]) | m0 | m1], l0[i0][m0] + l1[i1][m1] + 1) if (1 << a[z]) | m0 | m1 in r[1] else l0[i0][m0] + l1[i1][m1] + 1

        for m0 in l0[1]:
            for m1 in l1[1]:
                r[0][m0 | m1] = min(r[0][m0 | m1], l0[1][m0] + l1[1][m1]) if m0 | m1 in r[0] else l0[1][m0] + l1[1][m1]

    return r

t = int(input())
for i in range(t):

    n, m, k = map(int, input().split())
    a = [0] + [int(x) - 1 for x in input().split()]

    dt = [[] for i in range(n + 1)]
    for i in range(m):
        u, v = map(int, input().split())
        dt[u].append(v)

    r = dfs(1)

    k = (1 << k) - 1
    if (k in r[0]):
        v = min(r[0][k], r[1][k])
    elif (k in r[1]):
        v = r[1][k]
    else:
        v = -1

    print(v)


dt, a = None, None

def dfs(z):
    r = [{}, {}]

    ln = len(dt[z])

    if ln == 0:
        r[0][0] = 0
        r[1][1 << a[z]] = 1

    elif ln == 1:
        l = dfs(dt[z][0])
        r[0] = l[1]

        for m in l[0]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[0][m] + 1) if (1 << a[z]) | m in r[1] else l[0][m] + 1
        for m in l[1]:
            r[1][(1 << a[z]) | m] = min(r[1][(1 << a[z]) | m], l[1][m] + 1) if (1 << a[z]) | m in r[1] else l[1][m] + 1

    elif ln == 2:
        l0 = dfs(dt[z][0])
        l1 = dfs(dt[z][1])

        for i0 in range(2):
            for i1 in range(2):
                for m0 in l0[i0]:
                    for m1 in l1[i1]:
                        r[1][(1 << a[z]) | m0 | m1] = min(r[1][(1 << a[z]) | m0 | m1], l0[i0][m0] + l1[i1][m1] + 1) if (1 << a[z]) | m0 | m1 in r[1] else l0[i0][m0] + l1[i1][m1] + 1

        for m0 in l0[1]:
            for m1 in l1[1]:
                r[0][m0 | m1] = min(r[0][m0 | m1], l0[1][m0] + l1[1][m1]) if m0 | m1 in r[0] else l0[1][m0] + l1[1][m1]

    return r

t = int(input())
for i in range(t):

    n, m, k = map(int, input().split())
    a = [0] + [int(x) - 1 for x in input().split()]

    dt = [[] for i in range(n + 1)]
    for i in range(m):
        u, v = map(int, input().split())
        dt[u].append(v)

    r = dfs(1)

    k = (1 << k) - 1
    if (k in r[0]):
        v = min(r[0][k], r[1][k])
    elif (k in r[1]):
        v = r[1][k]
    else:
        v = -1

    print(v)