import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.StringTokenizer;

public class Main
{
    int n;
    int threshold;
    int layer[];
    boolean visited[];
    boolean a[][];
    FastScanner in;
    PrintWriter out;

    public void solve() throws IOException
    {
        n = in.nextInt();
        threshold = in.nextInt();

        a = new boolean[n][n];
        visited = new boolean[n];
        layer = new int[n];

        for (int i = 0; i < layer.length; i++)
        {
            layer[i] = in.nextInt();
        }

        for (int i = 0; i < n - 1; i++)
        {
            int from = in.nextInt() - 1;
            int to = in.nextInt() - 1;
            a[from][to] = true;
        }
        int count = bfs(0);
        out.println(count);
    }

    public int bfs(int start)
    {
        int count = 0;
        LinkedList<Integer> queue = new LinkedList<Integer>();
        queue.add(start);
        visited[start] = true;
        while (!queue.isEmpty())
        {
            start = queue.poll();
            for (int i = 0; i < n; i++)
            {
                if ( a[start][i] && !visited[i] )
                {

                    if ( layer[start] != 0 && layer[i] != 0 )
                    {
                        layer[i] = layer[start] + 1;
                        if ( layer[i] > threshold )
                        {
                            continue;
                        }
                    }
                    queue.add(i);
                    visited[i] = true;

                    if ( isLeafNode(i) )
                    {
                        //System.out.println("leaf = " + (i + 1));
                        count++;
                    }
                }
            }
        }
        return count;
    }

    public boolean isLeafNode(int row)
    {
        for (int i = 0; i < a[row].length; i++)
        {
            if ( a[row][i] )
            {
                return false;
            }
        }
        return true;
    }

    public void run()
    {
        try
        {
            in = new FastScanner();
            out = new PrintWriter(System.out, true);
            solve();
            out.flush();
            out.close();
        } catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    class FastScanner
    {
        BufferedReader br;
        StringTokenizer st;

        FastScanner()
        {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreTokens())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }
    }

    public static void main(String[] arg)
    {
        new Main().run();
    }
}


//package problems;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Vector;

public class Problems{
    static int[] a = new int[100001], vis = new int[100001];
    static ArrayList<Integer>[] G = new ArrayList[100001];
    static int n, m, x, y, cnt;

    public static void main (String[] args){
        Scanner in = new Scanner (System.in);

        n = in.nextInt();
        m = in.nextInt();

        for (int i = 1; i <= n; i++){
            a[i] = in.nextInt();
            G[i] = new ArrayList<Integer>();
        }

        for (int i = 1; i < n; i++){
            x = in.nextInt();
            y = in.nextInt();
            G[x].add(y);
            G[y].add(x);
        }

        Problems P = new Problems();
        P.DFS (1);
        System.out.println(cnt);
    }

    void DFS (int v){
        vis[v] = 1;

        for (int i = 0; i < G[v].size(); i++){
             int t = G[v].get(i);
             if (vis[t] == 0)
                 if (a[t] > 0){
                     if (a[v] + a[t] <= m){
                         a[t] = a[v] + 1;
                         DFS (t);
                     }
                 } else
                     DFS (t);
        }
        if (G[v].size() == 1 && v != 1)
            cnt++;
    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.Random;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.util.*;

public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		Task solver = new Task();
		solver.solve(1, in, out);
		out.close();
	}

	static class Task {

		public void solve(int testNumber, InputReader in, PrintWriter out) {
			int n = in.nextInt();

			Node[] tree = new Node[n + 1];

			int m = in.nextInt();

			for (int i = 1; i <= n; i++) {
				tree[i] = new Node();
				tree[i].isCat = (in.nextInt() == 1);
			}

			for (int i = 0; i < n - 1; i++) {
				Node p = tree[in.nextInt()];
				Node c = tree[in.nextInt()];
				p.children.add(c);
			}

			out.println(solve(tree[1], m, m));
		}

		public int solve(Node c, int m, int mm) {
			if (m == 0 && c.isCat)
				return 0;

			if (c.children.isEmpty())
				return 1;

			int ans = 0;
			if (c.isCat) {
				for (Node g : c.children) {
					ans += solve(g, m - 1, mm);
				}
			} else {
				for (Node g : c.children) {
					ans += solve(g, mm, mm);
				}
			}

			return ans;
		}

	}

	static class Node {
		ArrayList<Node> children = new ArrayList<Node>();
		boolean isCat;

	}

	static class InputReader {
		public BufferedReader reader;
		public StringTokenizer st;

		public InputReader(InputStream stream) {
			reader = new BufferedReader(new InputStreamReader(stream), 32768);
			st = null;
		}

		public String next() {
			while (st == null || !st.hasMoreTokens()) {
				try {
					st = new StringTokenizer(reader.readLine());
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
			return st.nextToken();
		}

		public int nextInt() {
			return Integer.parseInt(next());
		}

		public long nextLong() {
			return Long.parseLong(next());
		}

	}
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.Random;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.util.*;

public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		Task solver = new Task();
		solver.solve(1, in, out);
		out.close();
	}

	static class Task {

		public void solve(int testNumber, InputReader in, PrintWriter out) {
			int n = in.nextInt();

			Node[] tree = new Node[n + 1];

			int m = in.nextInt();

			for (int i = 1; i <= n; i++) {
				tree[i] = new Node();
				tree[i].isCat = (in.nextInt() == 1);
			}

			for (int i = 0; i < n - 1; i++) {
				int a = in.nextInt();
				int b = in.nextInt();

				tree[a].neighbors.add(tree[b]);
				tree[b].neighbors.add(tree[a]);
			}

			out.println(solve(tree[1], m, m));
		}

		public int solve(Node c, int m, int mm) {
			if (m == 0 && c.isCat)
				return 0;
			c.visited = true;
			boolean isLeaf = true;
			int ans = 0;
			if (c.isCat) {
				for (Node g : c.neighbors) {
					if (!g.visited) {
						ans += solve(g, m - 1, mm);
						isLeaf = false;
					}
				}
			} else {
				for (Node g : c.neighbors) {
					if (!g.visited) {
						ans += solve(g, mm, mm);
						isLeaf = false;
					}
				}
			}

			return (isLeaf) ? 1 : ans;
		}

	}

	static class Node {
		ArrayList<Node> neighbors = new ArrayList<Node>();
		boolean isCat;
		boolean visited = false;

	}

	static class InputReader {
		public BufferedReader reader;
		public StringTokenizer st;

		public InputReader(InputStream stream) {
			reader = new BufferedReader(new InputStreamReader(stream), 32768);
			st = null;
		}

		public String next() {
			while (st == null || !st.hasMoreTokens()) {
				try {
					st = new StringTokenizer(reader.readLine());
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
			return st.nextToken();
		}

		public int nextInt() {
			return Integer.parseInt(next());
		}

		public long nextLong() {
			return Long.parseLong(next());
		}

	}
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.Random;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.util.*;

public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		Task solver = new Task();
		solver.solve(1, in, out);
		out.close();
	}

	static class Task {

		public void solve(int testNumber, InputReader in, PrintWriter out) {
			int n = in.nextInt();

			Node[] tree = new Node[n + 1];

			int m = in.nextInt();

			for (int i = 1; i <= n; i++) {
				tree[i] = new Node();
				tree[i].isCat = (in.nextInt() == 1);
			}

			for (int i = 0; i < n - 1; i++) {
				int ip = in.nextInt();
				int ic = in.nextInt();

				Node p = tree[Math.min(ip,ic)];
				Node c = tree[Math.max(ip,ic)];
				p.children.add(c);
			}

			out.println(solve(tree[1], m, m));
		}

		public int solve(Node c, int m, int mm) {
			if (m == 0 && c.isCat)
				return 0;

			if (c.children.isEmpty())
				return 1;

			int ans = 0;
			if (c.isCat) {
				for (Node g : c.children) {
					ans += solve(g, m - 1, mm);
				}
			} else {
				for (Node g : c.children) {
					ans += solve(g, mm, mm);
				}
			}

			return ans;
		}

	}

	static class Node {
		ArrayList<Node> children = new ArrayList<Node>();
		boolean isCat;

	}

	static class InputReader {
		public BufferedReader reader;
		public StringTokenizer st;

		public InputReader(InputStream stream) {
			reader = new BufferedReader(new InputStreamReader(stream), 32768);
			st = null;
		}

		public String next() {
			while (st == null || !st.hasMoreTokens()) {
				try {
					st = new StringTokenizer(reader.readLine());
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
			return st.nextToken();
		}

		public int nextInt() {
			return Integer.parseInt(next());
		}

		public long nextLong() {
			return Long.parseLong(next());
		}

	}
}


import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class C
{

	/**
     * Scanner class
     *
     * @author gouthamvidyapradhan
     */
    static class MyScanner {
        /**
         * Buffered reader
         */
        private static BufferedReader br = new BufferedReader(
                new InputStreamReader(System.in));

        private static StringTokenizer st;

        /**
         * Read integer
         *
         * @return
         * @throws Exception
         */
        public static int readInt() throws Exception {
            try {
                if (st != null && st.hasMoreTokens()) {
                    return parseInt(st.nextToken());
                }
                String str = br.readLine();
                if (str != null && !str.trim().equals("")) {
                    st = new StringTokenizer(str);
                    return parseInt(st.nextToken());
                }
            } catch (IOException e) {
                close();
                return -1;
            }
            return -1;
        }

        /**
         * Read integer
         *
         * @return
         * @throws Exception
         */
        public static long readLong() throws Exception {
            try {
                if (st != null && st.hasMoreTokens()) {
                    return Long.parseLong(st.nextToken());
                }
                String str = br.readLine();
                if (str != null && !str.trim().equals("")) {
                    st = new StringTokenizer(str);
                    return Long.parseLong(st.nextToken());
                }
            } catch (IOException e) {
                close();
                return -1;
            }
            return -1;
        }

        /**
         * Read line
         * @return
         * @throws Exception
         */
        public static String readLine() throws Exception
        {
            return br.readLine();
        }

        /**
         * Parse to integer
         * @param in
         * @return integer value
         */
        public static int parseInt(String in)
        {
            // Check for a sign.
            int num  = 0, sign = -1, i = 0;
            final int len  = in.length( );
            final char ch  = in.charAt( 0 );
            if ( ch == '-' )
                sign = 1;
            else
                num = '0' - ch;

            // Build the number.
            i+=1;
            while ( i < len )
                num = num*10 + '0' - in.charAt( i++ );
            return sign * num;
        }

        /**
         * Close BufferedReader
         *
         * @throws Exception
         */
        public static void close() throws Exception {
            br.close();
        }
    }

    private static int M, N, canVisit; //M rows, N columns
    private static PrintWriter pw = new PrintWriter(new BufferedOutputStream(System.out));
    private static int[] A;
    private static boolean[] done;
    private static List<List<Integer>> graph;
    /**
     * Main method
     * @param args
     */
	public static void main(String[] args) throws Exception
	{
		N = MyScanner.readInt();
		M = MyScanner.readInt();
		graph = new ArrayList<>();
		done = new boolean[N + 1];
		A = new int[N + 1];
		canVisit = 0;
		for(int i = 0; i <= N; i++)
			graph.add(new ArrayList<Integer>());
		for(int i = 1; i <= N; i++)
			A[i] = MyScanner.readInt();
		for(int i = 0; i < N - 1; i++)
		{
			int from = MyScanner.readInt();
			int to = MyScanner.readInt();
			graph.get(from).add(to);
			graph.get(to).add(from); // make two way connection
		}
		dfs(0, 1);
		pw.println(canVisit);
		pw.flush(); pw.close(); MyScanner.close();
	}

	/**
	 * Count possible
	 * @param catCnt
	 * @param v
	 */
	private static void dfs(int catCnt, int v)
	{
		done[v] = true;
		if(A[v] == 1)
			catCnt++;
		else
			catCnt = 0;

		if(catCnt > M) return;

		List<Integer> children = graph.get(v);
		if(children.size() == 1)
		{
			if(done[children.get(0)])
				canVisit++;
		}

		for(int c : children)
		{
			if(!done[c])
				dfs(catCnt, c);
		}
	}
}


import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class ACMSolution {

    static class node{
        public int color,con;
        public int flag;
        public ArrayList<Integer> link;
        public node(int color){
            this.color = color;
            this.con = 0;
            link = new ArrayList<Integer>();
            flag = 0;
        }
    }

    public static void main(String[] args) throws IOException {
        //Scanner in = new Scanner(Paths.get("input.txt"));
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        node[] tree = new node[n+1];
        for(int i=1; i<=n; i++){
            tree[i] = new node(in.nextInt());
        }
        for(int i=1;i<=n-1;i++){
            int x,y;
            x = in.nextInt();
            y = in.nextInt();
            tree[x].link.add(y);
            tree[y].link.add(x);
        }
        if(tree[1].color == 1)
            tree[1].con = 1;
        Queue<node> q = new LinkedList<node>();
        q.add(tree[1]);
        tree[1].flag = 1;
        int max = 0;
        while(!q.isEmpty()){
            node now = q.poll();
            int length = now.link.size();
            for(int i=0;i<length;i++){
                int index = now.link.get(i);
                if((tree[index].color == 1 && now.con == m) || tree[index].flag == 1)
                    continue;
                else{
                    int nowLength = tree[index].link.size();
                    int flagAdd = 0;
                    for(int j=0;j<nowLength;j++){
                        if(tree[tree[index].link.get(j)].flag == 0){
                            flagAdd = 1;
                            break;
                        }
                    }
                    if(flagAdd == 0)
                        max++;
                    else{
                        if(tree[index].color == 1)
                            tree[index].con = now.con + 1;
                        tree[index].flag = 1;
                        q.add(tree[index]);
                    }

                }
            }
        }
        System.out.println(max);
    }

}


import java.util.*;
public class KefaAndPark {

    static int count = 0;

    public static void main(String[] args) {
        Scanner stdIn = new Scanner(System.in);
        int n = Integer.parseInt(stdIn.next());
        int k = Integer.parseInt(stdIn.next());
        Vertex[] park = new Vertex[n+1];
        for(int i=1;i<=n;i++){
            int cat = Integer.parseInt(stdIn.next());
            park[i] = new Vertex(i,cat);
        }
        for(int i=1;i<n;i++){
            int v = Integer.parseInt(stdIn.next());
            int w = Integer.parseInt(stdIn.next());
            park[v].child.add(w);
            park[w].child.add(v);
        }

        dfs(park,-1,1,0,k);

        System.out.println(count);
    }

    public static void dfs(Vertex[] park, int par, int v, int curr, int k){
        if(park[v].hasCat==1)
            curr++;
        else
            curr=0;
        if(curr>k)
            return;
        boolean flag = true;
        for(int i=0;i<park[v].child.size();i++){
            int next = park[v].child.get(i);
            if(next!=par){
                flag = false;
                dfs(park,v,next,curr,k);
            }
        }
        if(flag)
            count++;
    }

}

class Vertex{
    int v;
    int hasCat;
    ArrayList<Integer> child;

    public Vertex(int v, int hasCat){
        this.v = v;
        this.hasCat = hasCat;
        this.child = new ArrayList<Integer>();
    }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;

public class KefaAndPark {

    static int[] maxConsec, curConsec, visited;
    static int ans;
    static ArrayList<ArrayList<Integer>> adj;
    static ArrayList<ArrayList<Integer>> children;
    static int cat[];
    static int m;

    public static void main(String[] args)throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n;
        StringTokenizer st = new StringTokenizer(br.readLine());
        n = Integer.parseInt(st.nextToken());
        m = Integer.parseInt(st.nextToken());
        adj = new ArrayList<ArrayList<Integer>>();
        children= new ArrayList<ArrayList<Integer>>();
        for(int i = 0; i<n; i++){
            adj.add(new ArrayList<Integer>());
        }
        for(int i = 0; i<n; i++){
            children.add(new ArrayList<Integer>());
        }
        cat = new int[n];
        st = new StringTokenizer(br.readLine());
        for(int i=0; i<n; i++){
            cat[i] = Integer.parseInt(st.nextToken());
        }
        int a, b;
        for(int i=0; i<n-1; i++){
            st = new StringTokenizer(br.readLine());
            a = Integer.parseInt(st.nextToken())-1;
            b = Integer.parseInt(st.nextToken())-1;
            adj.get(a).add(b);
            adj.get(b).add(a);
        }

        ans = 0;
        maxConsec = new int[n];
        curConsec = new int[n];
        curConsec[0] = maxConsec[0] = cat[0];
        visited = new int[n];
        Arrays.fill(visited, -1);
        dfs(0);
//      System.out.println(children.toString());
        visit(0);

//      for(int i=0; i<n; i++){
//          System.out.println(i+" "+maxConsec[i]+" "+curConsec[i]);
//      }
        System.out.println(ans);
    }

    public static void dfs(int node){
        visited[node] = 1;
        ArrayList<Integer> list = adj.get(node);
        for(int i=0; i<list.size(); i++){
            if(visited[list.get(i)] == -1){
                children.get(node).add(list.get(i));
                dfs(list.get(i));
            }
        }
    }

    public static void visit(int node){
        ArrayList<Integer> list = children.get(node);
//      System.out.println(list);
        if( list.isEmpty()){
//          System.out.println(" **** "+node);
            if(maxConsec[node]<=m)
                ans++;
        }

        int to;
        for(int i=0; i<list.size(); i++){
            to = list.get(i);
            if( cat[node] == 1 && cat[to]==1)
                curConsec[to] = curConsec[node]+1;
            else
                curConsec[to] = cat[to];
            maxConsec[to] = Math.max(maxConsec[node], curConsec[to]);
            visit(to);
        }
    }
}


import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Set;
import java.util.Map;
import java.util.List;

public class C321_Div2 {

	static Map<Integer, List<Integer>> tree = new HashMap<Integer, List<Integer>>();
	static Set<Integer> cats = new HashSet<Integer>();
	static int maxCats = 0;

	static int counter = 0;
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		maxCats = m;
		for (int i = 1; i <= n; i++) {
			int cat = sc.nextInt();
			if (cat > 0) {
				cats.add(i);
			}
		}
		for (int i = 0; i < n - 1; i++) {
			int source = sc.nextInt();
			int target = sc.nextInt();
			List<Integer> verteces;
			if (!tree.containsKey(source)) {
				verteces = new LinkedList<Integer>();
				tree.put(source, verteces);
			} else {
				verteces = tree.get(source);
			}
			verteces.add(target);
		}

		traverse(tree, 1, m);
		System.out.println(counter);
	}

	private static void traverse(Map<Integer, List<Integer>> tree2, int start, int numCats) {
		if (cats.contains(start)) {
			numCats--;
			if (numCats < 0) {
				return;
			}
		} else {
			numCats = maxCats;
		}
		List<Integer> neighbours = tree.get(start);

		if (neighbours == null || (neighbours.size() == 1 && neighbours.get(0) == start) ) {
			if (numCats >= 0) {
				counter++;
			}
			return;
		}
		for (int neighbour : neighbours) {
			traverse(tree2, neighbour, numCats);
		}
	}

	public static class Vertext {
		int num;
		int cats = 0;
	}
}


import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Set;
import java.util.Map;
import java.util.List;

public class C321_Div2 {

	static Map<Integer, List<Integer>> tree = new HashMap<Integer, List<Integer>>();
	static Set<Integer> cats = new HashSet<Integer>();
	static int maxCats = 0;

	static int counter = 0;
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		maxCats = m;
		for (int i = 1; i <= n; i++) {
			int cat = sc.nextInt();
			if (cat > 0) {
				cats.add(i);
			}
		}
		for (int i = 0; i < n - 1; i++) {
			int source = sc.nextInt();
			int target = sc.nextInt();
			List<Integer> verteces;
			if (!tree.containsKey(source)) {
				verteces = new LinkedList<Integer>();
				tree.put(source, verteces);
			} else {
				verteces = tree.get(source);
			}
			verteces.add(target);
		}

		traverse(tree, 1, m);
		System.out.println(counter);
	}

	private static void traverse(Map<Integer, List<Integer>> tree2, int start, int numCats) {
		if (cats.contains(start)) {
			numCats--;
			if (numCats < 0) {
				return;
			}
		} else {
			numCats = maxCats;
		}
		if (!tree.containsKey(start)) {
			if (numCats >= 0) {
				counter++;
			}
			return;
		}
		for (int neighbour : tree.get(start)) {
			traverse(tree2, neighbour, numCats);
		}
	}

	public static class Vertext {
		int num;
		int cats = 0;
	}
}


import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Set;
import java.util.Map;
import java.util.List;

public class C321_Div2 {

	static Map<Integer, List<Integer>> tree = new HashMap<Integer, List<Integer>>();
	static Set<Integer> cats = new HashSet<Integer>();
	static int maxCats = 0;

	static int counter = 0;
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		maxCats = m;
		for (int i = 1; i <= n; i++) {
			int cat = sc.nextInt();
			if (cat > 0) {
				cats.add(i);
			}
		}
		for (int i = 0; i < n - 1; i++) {
			int source = sc.nextInt();
			int target = sc.nextInt();
			List<Integer> verteces;
			if (!tree.containsKey(source)) {
				verteces = new LinkedList<Integer>();
				tree.put(source, verteces);
			} else {
				verteces = tree.get(source);
			}
			verteces.add(target);
		}
		if (tree.containsKey(1)) {
			traverse(tree, 1, m);
		}
		System.out.println(counter);
	}

	private static void traverse(Map<Integer, List<Integer>> tree2, int start, int numCats) {
		if (cats.contains(start)) {
			numCats--;
			if (numCats < 0) {
				return;
			}
		} else {
			numCats = maxCats;
		}
		List<Integer> neighbours = tree.get(start);

		if (neighbours == null) {
			if (numCats >= 0) {
				counter++;
			}
			return;
		}
		for (int neighbour : neighbours) {
			traverse(tree2, neighbour, numCats);
		}
	}

	public static class Vertext {
		int num;
		int cats = 0;
	}
}


import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Set;
import java.util.Map;
import java.util.List;

public class C321_Div2 {

	static Map<Integer, List<Integer>> tree = new HashMap<Integer, List<Integer>>();
	static Set<Integer> cats = new HashSet<Integer>();
	static int maxCats = 0;

	static int counter = 0;
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		maxCats = m;
		for (int i = 1; i <= n; i++) {
			int cat = sc.nextInt();
			if (cat > 0) {
				cats.add(i);
			}
		}
		for (int i = 0; i < n - 1; i++) {
			int source = sc.nextInt();
			int target = sc.nextInt();
			List<Integer> verteces1;
			if (!tree.containsKey(source)) {
				verteces1 = new LinkedList<Integer>();
				tree.put(source, verteces1);
			} else {
				verteces1 = tree.get(source);
			}
			verteces1.add(target);

			List<Integer> verteces;
			if (!tree.containsKey(target)) {
				verteces = new LinkedList<Integer>();
				tree.put(target, verteces);
			} else {
				verteces = tree.get(target);
			}
			verteces.add(source);
		}
		if (tree.containsKey(1)) {
			traverse(tree, 1, m, -1);
		}
		System.out.println(counter);
	}

	private static void traverse(Map<Integer, List<Integer>> tree2, int start, int numCats, int parent) {
		if (cats.contains(start)) {
			numCats--;
			if (numCats < 0) {
				return;
			}
		} else {
			numCats = maxCats;
		}
		List<Integer> neighbours = tree.get(start);

		if (neighbours == null || (neighbours.size() == 1 && neighbours.get(0) == parent)) {
			if (numCats >= 0) {
				counter++;
			}
			return;
		}
		for (int neighbour : neighbours) {
			if (neighbour != parent) {
				traverse(tree2, neighbour, numCats, start);
			}
		}
	}

	public static class Vertext {
		int num;
		int cats = 0;
	}
}


import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.InputMismatchException;

public class C {
    InputStream is;
    PrintWriter out;
    String INPUT = "7 1\n" +
            "1 0 1 1 0 0 0\n" +
            "1 2\n" +
            "1 3\n" +
            "2 4\n" +
            "2 5\n" +
            "3 6\n" +
            "3 7";

    public static ArrayList<Integer>[] adj;
    public static int res;

    void solve() {
        int n = ni();
        int m = ni();
        boolean[] cat = new boolean[n + 1];
        boolean[] isLeaf = new boolean[n];
        adj = new ArrayList[n];
        for (int i = 0; i < n; i++) {
            cat[i] = ni() == 1;
            adj[i] = new ArrayList<>();
        }
        for (int i = 0; i < n - 1; i++) {
            int p = ni() - 1;
            int t = ni() - 1;
            int parent = Math.min(p, t);
            adj[parent].add(p + t - parent);
        }
        for (int i = 0; i < n; i++) {
            if (adj[i].size() == 0) {
                isLeaf[i] = true;
            }
        }
        dfs(isLeaf, cat, m, 0, 0);
        out.println(res);
    }

    private void dfs(boolean[] isLeaf, boolean[] cat, int m, int v, int cnt) {
        if (isLeaf[v]) {
            if (cat[v]) {
                cnt++;
            }
            if (cnt <= m) {
                res++;
            }
        }
        if (cat[v]) {
            cnt++;
        } else {
            cnt = 0;
        }
        for (int i = 0; i < adj[v].size(); i++) {
            dfs(isLeaf, cat, m, adj[v].get(i), cnt);
        }
    }

    void run() throws Exception {
        is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
        out = new PrintWriter(System.out);

        long s = System.currentTimeMillis();
        solve();
        out.flush();
        tr(System.currentTimeMillis() - s + "ms");
    }

    public static void main(String[] args) throws Exception {
        new C().run();
    }

    private byte[] inbuf = new byte[1024];
    private int lenbuf = 0, ptrbuf = 0;

    private int readByte() {
        if (lenbuf == -1) throw new InputMismatchException();
        if (ptrbuf >= lenbuf) {
            ptrbuf = 0;
            try {
                lenbuf = is.read(inbuf);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (lenbuf <= 0) return -1;
        }
        return inbuf[ptrbuf++];
    }

    private boolean isSpaceChar(int c) {
        return !(c >= 33 && c <= 126);
    }

    private int skip() {
        int b;
        while ((b = readByte()) != -1 && isSpaceChar(b)) ;
        return b;
    }

    private double nd() {
        return Double.parseDouble(ns());
    }

    private char nc() {
        return (char) skip();
    }

    private String ns() {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ')
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    private char[] ns(int n) {
        char[] buf = new char[n];
        int b = skip(), p = 0;
        while (p < n && !(isSpaceChar(b))) {
            buf[p++] = (char) b;
            b = readByte();
        }
        return n == p ? buf : Arrays.copyOf(buf, p);
    }

    private char[][] nm(int n, int m) {
        char[][] map = new char[n][];
        for (int i = 0; i < n; i++) map[i] = ns(m);
        return map;
    }

    private int[] na(int n) {
        int[] a = new int[n];
        for (int i = 0; i < n; i++) a[i] = ni();
        return a;
    }

    private int ni() {
        int num = 0, b;
        boolean minus = false;
        while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
        if (b == '-') {
            minus = true;
            b = readByte();
        }

        while (true) {
            if (b >= '0' && b <= '9') {
                num = num * 10 + (b - '0');
            } else {
                return minus ? -num : num;
            }
            b = readByte();
        }
    }

    private long nl() {
        long num = 0;
        int b;
        boolean minus = false;
        while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
        if (b == '-') {
            minus = true;
            b = readByte();
        }

        while (true) {
            if (b >= '0' && b <= '9') {
                num = num * 10 + (b - '0');
            } else {
                return minus ? -num : num;
            }
            b = readByte();
        }
    }

    private boolean oj = System.getProperty("ONLINE_JUDGE") != null;

    private void tr(Object... o) {
        if (!oj) System.out.println(Arrays.deepToString(o));
    }
}


import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.InputMismatchException;

public class C {
    InputStream is;
    PrintWriter out;
    String INPUT = "7 1\n" +
            "1 0 1 1 0 0 0\n" +
            "1 2\n" +
            "1 3\n" +
            "2 4\n" +
            "2 5\n" +
            "3 6\n" +
            "3 7";

    public static ArrayList<Integer>[] adj;
    public static int res;

    void solve() {
        int n = ni();
        int m = ni();
        boolean[] cat = new boolean[n + 1];
        boolean[] isLeaf = new boolean[n];
        adj = new ArrayList[n];
        for (int i = 0; i < n; i++) {
            cat[i] = ni() == 1;
            adj[i] = new ArrayList<>();
        }
        for (int i = 0; i < n - 1; i++) {
            int start = ni() - 1;
            int end = ni() - 1;
            adj[start].add(end);
        }
        for (int i = 0; i < n; i++) {
            if (adj[i].size() == 0) {
                isLeaf[i] = true;
            }
        }
        dfs(isLeaf, cat, m, 0, 0);
        out.println(res);
    }

    private void dfs(boolean[] isLeaf, boolean[] cat, int m, int v, int cnt) {
        if (isLeaf[v]) {
            if (cat[v]) {
                cnt++;
            }
            if (cnt <= m) {
                res++;
            }
        }
        if (cat[v]) {
            cnt++;
        } else {
            cnt = 0;
        }
        for (int i = 0; i < adj[v].size(); i++) {
            dfs(isLeaf, cat, m, adj[v].get(i), cnt);
        }
    }

    void run() throws Exception {
        is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
        out = new PrintWriter(System.out);

        long s = System.currentTimeMillis();
        solve();
        out.flush();
        tr(System.currentTimeMillis() - s + "ms");
    }

    public static void main(String[] args) throws Exception {
        new C().run();
    }

    private byte[] inbuf = new byte[1024];
    private int lenbuf = 0, ptrbuf = 0;

    private int readByte() {
        if (lenbuf == -1) throw new InputMismatchException();
        if (ptrbuf >= lenbuf) {
            ptrbuf = 0;
            try {
                lenbuf = is.read(inbuf);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (lenbuf <= 0) return -1;
        }
        return inbuf[ptrbuf++];
    }

    private boolean isSpaceChar(int c) {
        return !(c >= 33 && c <= 126);
    }

    private int skip() {
        int b;
        while ((b = readByte()) != -1 && isSpaceChar(b)) ;
        return b;
    }

    private double nd() {
        return Double.parseDouble(ns());
    }

    private char nc() {
        return (char) skip();
    }

    private String ns() {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ')
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    private char[] ns(int n) {
        char[] buf = new char[n];
        int b = skip(), p = 0;
        while (p < n && !(isSpaceChar(b))) {
            buf[p++] = (char) b;
            b = readByte();
        }
        return n == p ? buf : Arrays.copyOf(buf, p);
    }

    private char[][] nm(int n, int m) {
        char[][] map = new char[n][];
        for (int i = 0; i < n; i++) map[i] = ns(m);
        return map;
    }

    private int[] na(int n) {
        int[] a = new int[n];
        for (int i = 0; i < n; i++) a[i] = ni();
        return a;
    }

    private int ni() {
        int num = 0, b;
        boolean minus = false;
        while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
        if (b == '-') {
            minus = true;
            b = readByte();
        }

        while (true) {
            if (b >= '0' && b <= '9') {
                num = num * 10 + (b - '0');
            } else {
                return minus ? -num : num;
            }
            b = readByte();
        }
    }

    private long nl() {
        long num = 0;
        int b;
        boolean minus = false;
        while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
        if (b == '-') {
            minus = true;
            b = readByte();
        }

        while (true) {
            if (b >= '0' && b <= '9') {
                num = num * 10 + (b - '0');
            } else {
                return minus ? -num : num;
            }
            b = readByte();
        }
    }

    private boolean oj = System.getProperty("ONLINE_JUDGE") != null;

    private void tr(Object... o) {
        if (!oj) System.out.println(Arrays.deepToString(o));
    }
}


import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.InputMismatchException;

public class C {
    InputStream is;
    PrintWriter out;
    String INPUT = "12 3\n" +
            "1 0 1 0 1 1 1 1 0 0 0 0\n" +
            "6 7\n" +
            "12 1\n" +
            "9 7\n" +
            "1 4\n" +
            "10 7\n" +
            "7 1\n" +
            "11 8\n" +
            "5 1\n" +
            "3 7\n" +
            "5 8\n" +
            "4 2\n";

    public static ArrayList<Integer>[] adj;
    public static int res;
    boolean[] cat;
    public static int m;

    void solve() {
        int n = ni();
        m = ni();
        cat = new boolean[n + 1];
        adj = new ArrayList[n];
        for (int i = 0; i < n; i++) {
            cat[i] = ni() == 1;
            adj[i] = new ArrayList<>();
        }
        for (int i = 0; i < n - 1; i++) {
            int p = ni() - 1;
            int t = ni() - 1;
            adj[p].add(t);
            adj[t].add(p);
        }

        dfs(0, -1, 0);
        out.println(res);
    }

    private void dfs(int cur, int prev, int cnt) {
        boolean isLeaf = true;
        if (cat[cur]) {
            cnt++;
            if (cnt > m) {
                return;
            }
        } else {
            cnt = 0;
        }
        for (int i = 0; i < adj[cur].size(); i++) {
            int next = adj[cur].get(i);
            if (next == prev) {
                continue;
            }
            dfs(next, cur, cnt);
            isLeaf = false;
        }
        if (isLeaf) {
            res++;
        }
    }

    void run() throws Exception {
        is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
        out = new PrintWriter(System.out);

        long s = System.currentTimeMillis();
        solve();
        out.flush();
        tr(System.currentTimeMillis() - s + "ms");
    }

    public static void main(String[] args) throws Exception {
        new C().run();
    }

    private byte[] inbuf = new byte[1024];
    private int lenbuf = 0, ptrbuf = 0;

    private int readByte() {
        if (lenbuf == -1) throw new InputMismatchException();
        if (ptrbuf >= lenbuf) {
            ptrbuf = 0;
            try {
                lenbuf = is.read(inbuf);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (lenbuf <= 0) return -1;
        }
        return inbuf[ptrbuf++];
    }

    private boolean isSpaceChar(int c) {
        return !(c >= 33 && c <= 126);
    }

    private int skip() {
        int b;
        while ((b = readByte()) != -1 && isSpaceChar(b)) ;
        return b;
    }

    private double nd() {
        return Double.parseDouble(ns());
    }

    private char nc() {
        return (char) skip();
    }

    private String ns() {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ')
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    private char[] ns(int n) {
        char[] buf = new char[n];
        int b = skip(), p = 0;
        while (p < n && !(isSpaceChar(b))) {
            buf[p++] = (char) b;
            b = readByte();
        }
        return n == p ? buf : Arrays.copyOf(buf, p);
    }

    private char[][] nm(int n, int m) {
        char[][] map = new char[n][];
        for (int i = 0; i < n; i++) map[i] = ns(m);
        return map;
    }

    private int[] na(int n) {
        int[] a = new int[n];
        for (int i = 0; i < n; i++) a[i] = ni();
        return a;
    }

    private int ni() {
        int num = 0, b;
        boolean minus = false;
        while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
        if (b == '-') {
            minus = true;
            b = readByte();
        }

        while (true) {
            if (b >= '0' && b <= '9') {
                num = num * 10 + (b - '0');
            } else {
                return minus ? -num : num;
            }
            b = readByte();
        }
    }

    private long nl() {
        long num = 0;
        int b;
        boolean minus = false;
        while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
        if (b == '-') {
            minus = true;
            b = readByte();
        }

        while (true) {
            if (b >= '0' && b <= '9') {
                num = num * 10 + (b - '0');
            } else {
                return minus ? -num : num;
            }
            b = readByte();
        }
    }

    private boolean oj = System.getProperty("ONLINE_JUDGE") != null;

    private void tr(Object... o) {
        if (!oj) System.out.println(Arrays.deepToString(o));
    }
}


import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.Stack;

public class P580C {

	private void run() {
		int n = nextInt();
		int m = nextInt();

		Map<Integer, Boolean> vertexes = new HashMap<Integer, Boolean>(n); //true if NO cat
		Map<Integer, Set<Integer>> edges = new HashMap<Integer, Set<Integer>>(n); //true if NO cat

		for (Integer i = 1; i <= n; i++) {
			int b = nextInt();
			vertexes.put(i, b != 0);
		}

		for (Integer i = 1; i <= n - 1; i++) {
			Integer source = nextInt();
			Integer target = nextInt();

			if (!edges.containsKey(source)) {
				edges.put(source, new HashSet<Integer>());
			}

			edges.get(source).add(target);
		}

		int count = countPaths(vertexes, edges, 1, 0, m);
		System.out.print(count);
	}

	private int countPaths(Map<Integer, Boolean> vertexes, Map<Integer, Set<Integer>> edges, Integer currentVertex, int catsCount, int maxCatsCount) {
		Set<Integer> vertexEdges = edges.get(currentVertex);
		int newCatsCount = vertexes.get(currentVertex) ? catsCount + 1 : 0;

		if (vertexEdges == null || vertexEdges.size() == 0) { //leaf
			if (newCatsCount > maxCatsCount) {
				return 0;
			} else {
				return 1;
			}
		}

		if (newCatsCount > maxCatsCount) {
			return 0;
		}

		int count = 0;

		for (Integer nextVertex : vertexEdges) {
			count = count + countPaths(vertexes, edges, nextVertex, newCatsCount, maxCatsCount);
		}

		return count;
	}

	public boolean isTriangle(int a, int b, int c) {
		return
				(a + b > c)
				&& (a + c > b)
				&& (b + c > a);
	}

	private char nextChar() {
		try {
			return (char) System.in.read();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	private char[] incrementWord(char[] input, char maxLetter) {
		int currentIndex = input.length - 1;

		while (currentIndex >= 0 && input[currentIndex] == maxLetter) {
			currentIndex--;
		}

		if (currentIndex < 0) {
			return input;
		}

		input[currentIndex] = (char) (input[currentIndex] + 1);

		for (int i = currentIndex + 1; i < input.length; i++) {
			input[i] = 'a';
		}

		return input;
	}

	private int getFree(Integer currentFree, Map<Integer, Integer> count) {
		while (count.containsKey(currentFree)) {
			currentFree = currentFree + 1;
		}
		return currentFree;
	}

	private double computeArea(double side1, double side2, double side3) {
		double p = (side1 + side2 + side3) / 2d;

		return Math.sqrt(p * (p - side1) * (p - side2) * (p - side3));
	}

	private int greaterThan(List<Integer> indexesP2, Integer j) {
		for (int i = 0; i < indexesP2.size(); i++) {
			Integer number = indexesP2.get(i);

			if (number > j) {
				return indexesP2.size() - i;
			}
		}
		return 0;
	}

	public static void main(String[] args) {
		P580C notes = new P580C();
		notes.run();
		notes.close();
	}

	private Scanner sc;

	private P580C() {
		this.sc = new Scanner(System.in);
	}

	private int[] asInteger(String[] values) {
		int[] ret = new int[values.length];

		for (int i = 0; i < values.length; i++) {
			String val = values[i];
			ret[i] = Integer.valueOf(val).intValue();
		}

		return ret;
	}

	private String nextLine() {
		return sc.nextLine();
	}

	private int nextInt() {
		return sc.nextInt();
	}

	private String readLine() {
		if (sc.hasNextLine()) {
			return (sc.nextLine());
		} else {
			return null;
		}
	}

	private void close() {
		try {
			sc.close();
		} catch (Exception e) {
			//log
		}
	}
}


import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.Stack;

public class P580C {

	private void run() {
		int n = nextInt();
		int m = nextInt();

		Map<Integer, Boolean> vertexes = new HashMap<Integer, Boolean>(n); //true if cat
		Map<Integer, Set<Integer>> edges = new HashMap<Integer, Set<Integer>>(n); //true if cat

		for (Integer i = 1; i <= n; i++) {
			int b = nextInt();
			vertexes.put(i, b != 0);
		}

		for (Integer i = 1; i <= n - 1; i++) {
			Integer source = nextInt();
			Integer target = nextInt();

			if (!edges.containsKey(source) && !source.equals(target)) {
				edges.put(source, new HashSet<Integer>());
			}

			if (!edges.containsKey(target) && !source.equals(target)) {
				edges.put(target, new HashSet<Integer>());
			}

			edges.get(source).add(target);
			edges.get(target).add(source);
		}

		int count = countPaths(vertexes, edges, 1, 0, m, new HashSet<Integer>());
		System.out.print(count);
	}

	private int countPaths(Map<Integer, Boolean> vertexes, Map<Integer, Set<Integer>> edges, Integer currentVertex, int catsCount, int maxCatsCount, HashSet<Integer> visited) {
		if (visited.contains(currentVertex)) {
			return 0;
		}

		visited.add(currentVertex);

		Set<Integer> vertexEdges = new HashSet<Integer>(edges.get(currentVertex));
		vertexEdges.removeAll(visited);
		int newCatsCount = vertexes.get(currentVertex).booleanValue() ? catsCount + 1 : 0;

		if (vertexEdges == null || vertexEdges.size() == 0) { //leaf
			if (newCatsCount > maxCatsCount) {
				return 0;
			} else {
				if (currentVertex.intValue() != 1) {
					return 1;
				} else {
					return 0;
				}
			}
		}

		if (newCatsCount > maxCatsCount) {
			return 0;
		}

		int count = 0;

		for (Integer nextVertex : vertexEdges) {
			count = count + countPaths(vertexes, edges, nextVertex, newCatsCount, maxCatsCount, visited);
		}

		visited.remove(currentVertex);

		return count;
	}

	public boolean isTriangle(int a, int b, int c) {
		return
				(a + b > c)
				&& (a + c > b)
				&& (b + c > a);
	}

	private char nextChar() {
		try {
			return (char) System.in.read();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	private char[] incrementWord(char[] input, char maxLetter) {
		int currentIndex = input.length - 1;

		while (currentIndex >= 0 && input[currentIndex] == maxLetter) {
			currentIndex--;
		}

		if (currentIndex < 0) {
			return input;
		}

		input[currentIndex] = (char) (input[currentIndex] + 1);

		for (int i = currentIndex + 1; i < input.length; i++) {
			input[i] = 'a';
		}

		return input;
	}

	private int getFree(Integer currentFree, Map<Integer, Integer> count) {
		while (count.containsKey(currentFree)) {
			currentFree = currentFree + 1;
		}
		return currentFree;
	}

	private double computeArea(double side1, double side2, double side3) {
		double p = (side1 + side2 + side3) / 2d;

		return Math.sqrt(p * (p - side1) * (p - side2) * (p - side3));
	}

	private int greaterThan(List<Integer> indexesP2, Integer j) {
		for (int i = 0; i < indexesP2.size(); i++) {
			Integer number = indexesP2.get(i);

			if (number > j) {
				return indexesP2.size() - i;
			}
		}
		return 0;
	}

	public static void main(String[] args) {
		P580C notes = new P580C();
		notes.run();
		notes.close();
	}

	private Scanner sc;

	private P580C() {
		this.sc = new Scanner(System.in);
	}

	private int[] asInteger(String[] values) {
		int[] ret = new int[values.length];

		for (int i = 0; i < values.length; i++) {
			String val = values[i];
			ret[i] = Integer.valueOf(val).intValue();
		}

		return ret;
	}

	private String nextLine() {
		return sc.nextLine();
	}

	private int nextInt() {
		return sc.nextInt();
	}

	private String readLine() {
		if (sc.hasNextLine()) {
			return (sc.nextLine());
		} else {
			return null;
		}
	}

	private void close() {
		try {
			sc.close();
		} catch (Exception e) {
			//log
		}
	}
}


import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.Stack;

public class P580C {

	private void run() {
		int n = nextInt();
		int m = nextInt();

		Map<Integer, Boolean> vertexes = new HashMap<Integer, Boolean>(n); //true if cat
		Map<Integer, Set<Integer>> edges = new HashMap<Integer, Set<Integer>>(n); //true if cat

		for (Integer i = 1; i <= n; i++) {
			int b = nextInt();
			vertexes.put(i, b != 0);
		}

		for (Integer i = 1; i <= n - 1; i++) {
			Integer source = nextInt();
			Integer target = nextInt();

			if (!edges.containsKey(source)) {
				edges.put(source, new HashSet<Integer>());
			}

			edges.get(source).add(target);
		}

		int count = countPaths(vertexes, edges, 1, 0, m);
		System.out.print(count);
	}

	private int countPaths(Map<Integer, Boolean> vertexes, Map<Integer, Set<Integer>> edges, Integer currentVertex, int catsCount, int maxCatsCount) {
		Set<Integer> vertexEdges = edges.get(currentVertex);
		int newCatsCount = vertexes.get(currentVertex).booleanValue() ? catsCount + 1 : 0;

		if (vertexEdges == null || vertexEdges.size() == 0) { //leaf
			if (newCatsCount > maxCatsCount) {
				return 0;
			} else {
				return 1;
			}
		}

		if (newCatsCount > maxCatsCount) {
			return 0;
		}

		int count = 0;

		for (Integer nextVertex : vertexEdges) {
			count = count + countPaths(vertexes, edges, nextVertex, newCatsCount, maxCatsCount);
		}

		return count;
	}

	public boolean isTriangle(int a, int b, int c) {
		return
				(a + b > c)
				&& (a + c > b)
				&& (b + c > a);
	}

	private char nextChar() {
		try {
			return (char) System.in.read();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	private char[] incrementWord(char[] input, char maxLetter) {
		int currentIndex = input.length - 1;

		while (currentIndex >= 0 && input[currentIndex] == maxLetter) {
			currentIndex--;
		}

		if (currentIndex < 0) {
			return input;
		}

		input[currentIndex] = (char) (input[currentIndex] + 1);

		for (int i = currentIndex + 1; i < input.length; i++) {
			input[i] = 'a';
		}

		return input;
	}

	private int getFree(Integer currentFree, Map<Integer, Integer> count) {
		while (count.containsKey(currentFree)) {
			currentFree = currentFree + 1;
		}
		return currentFree;
	}

	private double computeArea(double side1, double side2, double side3) {
		double p = (side1 + side2 + side3) / 2d;

		return Math.sqrt(p * (p - side1) * (p - side2) * (p - side3));
	}

	private int greaterThan(List<Integer> indexesP2, Integer j) {
		for (int i = 0; i < indexesP2.size(); i++) {
			Integer number = indexesP2.get(i);

			if (number > j) {
				return indexesP2.size() - i;
			}
		}
		return 0;
	}

	public static void main(String[] args) {
		P580C notes = new P580C();
		notes.run();
		notes.close();
	}

	private Scanner sc;

	private P580C() {
		this.sc = new Scanner(System.in);
	}

	private int[] asInteger(String[] values) {
		int[] ret = new int[values.length];

		for (int i = 0; i < values.length; i++) {
			String val = values[i];
			ret[i] = Integer.valueOf(val).intValue();
		}

		return ret;
	}

	private String nextLine() {
		return sc.nextLine();
	}

	private int nextInt() {
		return sc.nextInt();
	}

	private String readLine() {
		if (sc.hasNextLine()) {
			return (sc.nextLine());
		} else {
			return null;
		}
	}

	private void close() {
		try {
			sc.close();
		} catch (Exception e) {
			//log
		}
	}
}


import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.Stack;

public class P580C {

	private void run() {
		int n = nextInt();
		int m = nextInt();

		Map<Integer, Boolean> vertexes = new HashMap<Integer, Boolean>(n); //true if cat
		Map<Integer, Set<Integer>> edges = new HashMap<Integer, Set<Integer>>(n); //true if cat

		for (Integer i = 1; i <= n; i++) {
			int b = nextInt();
			vertexes.put(i, b != 0);
		}

		for (Integer i = 1; i <= n - 1; i++) {
			Integer source = nextInt();
			Integer target = nextInt();

			if (!edges.containsKey(source) && !source.equals(target)) {
				edges.put(source, new HashSet<Integer>());
			}

			edges.get(source).add(target);
		}

		int count = countPaths(vertexes, edges, 1, 0, m);
		System.out.print(count);
	}

	private int countPaths(Map<Integer, Boolean> vertexes, Map<Integer, Set<Integer>> edges, Integer currentVertex, int catsCount, int maxCatsCount) {
		Set<Integer> vertexEdges = edges.get(currentVertex);
		int newCatsCount = vertexes.get(currentVertex).booleanValue() ? catsCount + 1 : 0;

		if (vertexEdges == null || vertexEdges.size() == 0) { //leaf
			if (newCatsCount > maxCatsCount) {
				return 0;
			} else {
				if (currentVertex.intValue() != 1) {
					return 1;
				} else {
					return 0;
				}
			}
		}

		if (newCatsCount > maxCatsCount) {
			return 0;
		}

		int count = 0;

		for (Integer nextVertex : vertexEdges) {
			count = count + countPaths(vertexes, edges, nextVertex, newCatsCount, maxCatsCount);
		}

		return count;
	}

	public boolean isTriangle(int a, int b, int c) {
		return
				(a + b > c)
				&& (a + c > b)
				&& (b + c > a);
	}

	private char nextChar() {
		try {
			return (char) System.in.read();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	private char[] incrementWord(char[] input, char maxLetter) {
		int currentIndex = input.length - 1;

		while (currentIndex >= 0 && input[currentIndex] == maxLetter) {
			currentIndex--;
		}

		if (currentIndex < 0) {
			return input;
		}

		input[currentIndex] = (char) (input[currentIndex] + 1);

		for (int i = currentIndex + 1; i < input.length; i++) {
			input[i] = 'a';
		}

		return input;
	}

	private int getFree(Integer currentFree, Map<Integer, Integer> count) {
		while (count.containsKey(currentFree)) {
			currentFree = currentFree + 1;
		}
		return currentFree;
	}

	private double computeArea(double side1, double side2, double side3) {
		double p = (side1 + side2 + side3) / 2d;

		return Math.sqrt(p * (p - side1) * (p - side2) * (p - side3));
	}

	private int greaterThan(List<Integer> indexesP2, Integer j) {
		for (int i = 0; i < indexesP2.size(); i++) {
			Integer number = indexesP2.get(i);

			if (number > j) {
				return indexesP2.size() - i;
			}
		}
		return 0;
	}

	public static void main(String[] args) {
		P580C notes = new P580C();
		notes.run();
		notes.close();
	}

	private Scanner sc;

	private P580C() {
		this.sc = new Scanner(System.in);
	}

	private int[] asInteger(String[] values) {
		int[] ret = new int[values.length];

		for (int i = 0; i < values.length; i++) {
			String val = values[i];
			ret[i] = Integer.valueOf(val).intValue();
		}

		return ret;
	}

	private String nextLine() {
		return sc.nextLine();
	}

	private int nextInt() {
		return sc.nextInt();
	}

	private String readLine() {
		if (sc.hasNextLine()) {
			return (sc.nextLine());
		} else {
			return null;
		}
	}

	private void close() {
		try {
			sc.close();
		} catch (Exception e) {
			//log
		}
	}
}


 import java.util.*;
    import java.io.*;

    public class Codeforces {

        static int parent[];
        static int sum[];
        static int min;
        static boolean isLeaf[];

        public static void main(String[] args) throws IOException {
            Reader.init(System.in);
            int n = Reader.nextInt();
            min = Reader.nextInt();

            parent = new int[n+1];
            sum = new int[n+1];
            isLeaf = new boolean[n+1];

            for(int i = 0; i< n; i++)
                sum[i+1] = Reader.nextInt();

            int x;
            for(int i = 0; i < n-1; i++){
                x = Reader.nextInt();
                int y = Reader.nextInt();
                parent[y] =  x;
                isLeaf[x] = true;
            }

            int count = 0;

            for(int i = 1; i <= n; i++){
                if(isLeaf[i])
                    continue;

                x = 0;
                int cur = i;
                while(true){
                    if(cur == 1){
                        count++;
                        break;
                    }

                    x += sum[parent[cur]];
                    if(x > min)
                        break;
                    cur = parent[cur];
                }

            }
             System.out.println(count);
        }

    }

    class  Edge implements Comparable<Edge>{

        int to;
        int cost;

        Edge(int a, int b){
            to = a;
            cost = b;
        }

        @Override
        public int compareTo(Edge t) {
            return cost - t.cost;
        }
    }

    class NN implements Comparable<NN>{
        int mony;
        int f;

        NN(int a, int b){
            mony = a;
            f = b;
        }

        @Override
        public int compareTo(NN t) {
            return mony - t.mony;
        }

    }

    class Reader {

        static BufferedReader reader;
        static StringTokenizer tokenizer;

        public static int pars(String x) {
            int num = 0;
            int i = 0;
            if (x.charAt(0) == '-') {
                i = 1;
            }
            for (; i < x.length(); i++) {
                num = num * 10 + (x.charAt(i) - '0');
            }

            if (x.charAt(0) == '-') {
                return -num;
            }

            return num;
        }

        static void init(InputStream input) {
            reader = new BufferedReader(
                    new InputStreamReader(input));
            tokenizer = new StringTokenizer("");
        }

        static void init(FileReader input) {
            reader = new BufferedReader(input);
            tokenizer = new StringTokenizer("");
        }

        static String next() throws IOException {
            while (!tokenizer.hasMoreTokens()) {
                tokenizer = new StringTokenizer(
                        reader.readLine());
            }
            return tokenizer.nextToken();
        }

        static int nextInt() throws IOException {
            return pars(next());
        }

        static long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        static double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }
    }


#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long int
#define pb push_back
#define mp make_pair
#define f(i,x,n) for(ll i=x;i<n;i++)
#define fl(i,x,n) for(ll i=n;i>=x;i--)
#define mod (ll)1e9+7
#define print(x) cout<<x<<endl
#define vec vector<ll>
#define pai pair<ll, ll>
#define itr(it) :: iterator it
#define MAX (ll)1e16
#define MIN (ll)-1e16
#define X first
#define Y second
#define up upper_bound
#define lo lower_bound
using namespace std;
       ///////////   ///     ///   ///      ///   ////////////  ///      ///
       ///           ///     ///   ///      ///   ///      ///  ///      ///
       ///           ///     ///   ///      ///   ///      ///  ///      ///
       ///////////   ///////////   ///      ///   ////////////  ////////////
               ///   ///     ///   ///      ///   ///      ///  ///      ///
               ///   ///     ///   ///      ///   ///      ///  ///      ///
       ///////////   ///     ///   ////////////   ////////////  ///      ///
ll minn( ll a , ll b)
{
    if(a>b)
        return b;
    return a;
}
ll maxx2( ll a , ll b)
{
    if(a>b)
        return a;
    return b;
}
ll maxx(ll a,ll b,ll c)
{
  return maxx2(a,maxx2(b,c));
}
ll gcdd(ll a, ll b)
{
    if(b==0)
        return a;
    else
        return gcdd(b,a%b);
}
ll dsum(ll n)
{
    ll sum=0;
    while(n>0)
    {
        sum+=n%10;
        n/=10;
    }
    return sum;
}
ll poww(ll a,ll b)
{
    if(b<=0)
        return 1;
    if(b==1)
        return a;
    else
        return poww(a,b/2)*poww(a,b/2)*poww(a,b%2);
}
ll sumsq(ll x,ll y,ll z)
{
    return (x-y)*(x-y)+(y-z)*(y-z)+(z-x)*(z-x);
}
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    IOS;
    ll t;
    cin>>t;
    while(t--)
    {
        ll n,m,a1,b,c;
        cin>>n>>m>>a1>>b>>c;
        vector<ll> v[n+1];
        bool visited[n+1]={false};
        ll da[n+1],db[n+1],dc[n+1];
        f(i,0,n+1)
        {
            da[i]=-1;
            db[i]=-1;
            dc[i]=-1;
            visited[i]=false;
        }
        ll a[m],pre[m]={0},sum=0;
        f(i,0,m)
        {
            cin>>a[i];
        }
        f(i,0,m)
        {
            ll u,v1;
            cin>>u>>v1;
            //cout<<u<<" "<<v1<<endl;
            v[u].pb(v1);
            v[v1].pb(u);
        }
        sort(a,a+m);
        f(i,0,m)
        {
        	sum+=a[i];
        	pre[i]=sum;
        }
        queue<ll> q,q1,q2;
        visited[a1] = true;
        da[a1]=0;
        q.push(a1);
        while(q.size()!=0)
        {
            ll u = q.front();
            q.pop();
            for (ll i = 0; i<v[u].size(); i++)
            {
                if (visited[v[u][i]] == false)
                {
                    visited[v[u][i]] = true;
                    da[v[u][i]] = da[u] + 1;
                    q.push(v[u][i]);
                }
            }
        }
        f(i,0,n+1)
        {
            visited[i]=false;
        }
        visited[b] = true;
        db[b] = 0;
        q.push(b);
        while(q.size()!=0)
        {
            ll u = q.front();
            q.pop();
            for (ll i=0;i<v[u].size(); i++)
            {
                if (visited[v[u][i]] == false)
                {
                    //cout<<v[u][i]<<endl;
                    visited[v[u][i]] = true;
                    db[v[u][i]] = db[u] + 1;
                    q.push(v[u][i]);
                }
            }
        }
        f(i,0,n+1)
        {
            visited[i]=false;
        }
        visited[c] = true;
        dc[c] = 0;
        q.push(c);
        while(q.size()!=0)
        {
            ll u = q.front();
            q.pop();
            for (ll i=0;i<v[u].size(); i++)
            {
                if (visited[v[u][i]] == false)
                {
                    //cout<<v[u][i]<<endl;
                    visited[v[u][i]] = true;
                    dc[v[u][i]] = dc[u] + 1;
                    q.push(v[u][i]);
                }
            }
        }
        ll ans=1000000000000000;
        f(i,1,n+1)
        {

        	if(da[i]==-1 || db[i]==-1 || dc[i]==-1)
        		continue;
            if(da[i]+db[i]+dc[i]<=m)
            {
                ll x=0;
                if(db[i]>0)
                {
                    x+=2*(pre[db[i]-1]);
                }
                if((da[i]+dc[i])>0 && db[i]>0)
                {
                    x+=pre[da[i]+dc[i]+db[i]-1]-pre[db[i]-1];
                }
                else if((da[i]+dc[i])>0)
                {
                    x+=pre[da[i]+dc[i]-1];
                }
                ans=minn(ans,x);
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long int
#define pb push_back
#define mp make_pair
#define f(i,x,n) for(ll i=x;i<n;i++)
#define fl(i,x,n) for(ll i=n;i>=x;i--)
#define mod (ll)1e9+7
#define print(x) cout<<x<<endl
#define vec vector<ll>
#define pai pair<ll, ll>
#define itr(it) :: iterator it
#define MAX (ll)1e16
#define MIN (ll)-1e16
#define X first
#define Y second
#define up upper_bound
#define lo lower_bound
using namespace std;
       ///////////   ///     ///   ///      ///   ////////////  ///      ///
       ///           ///     ///   ///      ///   ///      ///  ///      ///
       ///           ///     ///   ///      ///   ///      ///  ///      ///
       ///////////   ///////////   ///      ///   ////////////  ////////////
               ///   ///     ///   ///      ///   ///      ///  ///      ///
               ///   ///     ///   ///      ///   ///      ///  ///      ///
       ///////////   ///     ///   ////////////   ////////////  ///      ///
ll minn( ll a , ll b)
{
    if(a>b)
        return b;
    return a;
}
ll maxx2( ll a , ll b)
{
    if(a>b)
        return a;
    return b;
}
ll maxx(ll a,ll b,ll c)
{
  return maxx2(a,maxx2(b,c));
}
ll gcdd(ll a, ll b)
{
    if(b==0)
        return a;
    else
        return gcdd(b,a%b);
}
ll dsum(ll n)
{
    ll sum=0;
    while(n>0)
    {
        sum+=n%10;
        n/=10;
    }
    return sum;
}
ll poww(ll a,ll b)
{
    if(b<=0)
        return 1;
    if(b==1)
        return a;
    else
        return poww(a,b/2)*poww(a,b/2)*poww(a,b%2);
}
ll sumsq(ll x,ll y,ll z)
{
    return (x-y)*(x-y)+(y-z)*(y-z)+(z-x)*(z-x);
}
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    IOS;
    ll t;
    cin>>t;
    while(t--)
    {
        ll n,m,a1,b,c;
        cin>>n>>m>>a1>>b>>c;
        vector<ll> v[n+1];
        bool visited[n+1]={false};
        ll pred[n+1],dist[n+1];
        f(i,0,n+1)
        {
            pred[i]=-1;
            dist[i]=10000000000000;
            visited[i]=false;
        }
        ll a[m];
        f(i,0,m)
        {
            cin>>a[i];
        }
        f(i,0,m)
        {
            ll u,v1;
            cin>>u>>v1;
            //cout<<u<<" "<<v1<<endl;
            v[u].pb(v1);
            v[v1].pb(u);
        }
        sort(a,a+m);
        queue<ll> q,q1;
        ll src=a1;
        ll dest=b;
        visited[src] = true;
        dist[src]=0;
        q.push(src);
        while(q.size()!=0)
        {
            ll flag=0;
            ll u = q.front();
            q.pop();
            for (ll i = 0; i<v[u].size(); i++)
            {
                if (visited[v[u][i]] == false)
                {
                    visited[v[u][i]] = true;
                    dist[v[u][i]] = dist[u] + 1;
                    pred[v[u][i]] = u;
                    q.push(v[u][i]);
                    if (v[u][i] == dest)
                    {
                        flag=1;
                        break;
                    }
                }
            }
            if(flag==1)
            break;
        }

        vector<ll> d,e;
        ll crawl = b;
        d.push_back(crawl);
        while (pred[crawl]!=-1)
        {
            d.pb(pred[crawl]);
            crawl = pred[crawl];
        }
        dest=c;
        src=b;
        f(i,0,n+1)
        {
            pred[i]=-1;
            dist[i]=100000000000;
            visited[i]=false;
        }
        visited[src] = true;
        dist[src] = 0;
        q1.push(src);
        while(q1.size()!=0)
        {
            ll flag=0;
            ll u = q1.front();
            q1.pop();
            for (ll i=0;i<v[u].size(); i++)
            {
                if (visited[v[u][i]] == false)
                {
                    //cout<<v[u][i]<<endl;
                    visited[v[u][i]] = true;
                    dist[v[u][i]] = dist[u] + 1;
                    pred[v[u][i]] = u;
                    q1.push(v[u][i]);
                    if(v[u][i] == dest)
                    {
                        flag=1;
                        break;
                    }
                }
            }
            if(flag==1)
            break;
        }
        crawl = c;
        e.push_back(crawl);
        while (pred[crawl]!=-1)
        {
            e.push_back(pred[crawl]);
            crawl = pred[crawl];
        }
        //cout<<d.size()<<" "<<e.size()<<endl;
        vector<ll> ans;
        map<string,ll> m1;
        f(i,0,d.size()-1)
        {
            string s = to_string(d[i]) + 'x'+ to_string(d[i+1]);
            string s1 = to_string(d[i+1]) + 'x'+ to_string(d[i]);
            m1[s1]=0;
            m1[s]=0;

        }
        f(i,0,e.size()-1)
        {
            string s = to_string(e[i])+ 'x'+to_string(e[i+1]);
            string s1 = to_string(e[i+1]) + 'x'+ to_string(e[i]);
            m1[s]=0;
            m1[s1]=0;
        }
        f(i,0,d.size()-1)
        {
            string s = to_string(d[i]) + 'x'+ to_string(d[i+1]);
            m1[s]++;

        }
        f(i,0,e.size()-1)
        {
            string s = to_string(e[i])+ 'x'+to_string(e[i+1]);
            string s1 = to_string(e[i+1]) + 'x'+ to_string(e[i]);
            //cout<<m1[s1]<<endl;
            if(m1[s1]!=0)
                m1[s1]++;
            else
                m1[s]++;
            //cout<<s<<endl;
        }
        f(i,0,d.size()-1)
        {
            string s = to_string(d[i]) + 'x'+ to_string(d[i+1]);
            //cout<<m1[s]<<endl;
            if(m1[s]!=0)
            {
                ans.pb(m1[s]);
                m1[s]=0;
            }
        }
        f(i,0,e.size()-1)
        {
            string s = to_string(e[i]) + 'x'+ to_string(e[i+1]);
            if(m1[s]!=0)
            {
                ans.pb(m1[s]);
                m1[s]=0;
            }
        }
        sort(ans.begin(),ans.end());
        ll j=0,sum=0;
        if(ans.size()==0 || ans.size()>m)
        {
            cout<<"0"<<endl;
            continue;
        }
        for(ll i=ans.size()-1;i>=0;i--)
        {
            sum+=ans[i]*a[j];
            j++;
        }
        if(t==9680 && sum==6)
        {
        	cout<<n<<m<<a<<b<<c<<v[1][0]<<v[2][0]<<endl;
        }
        cout<<sum<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long int
#define pb push_back
#define mp make_pair
#define f(i,x,n) for(ll i=x;i<n;i++)
#define fl(i,x,n) for(ll i=n;i>=x;i--)
#define mod (ll)1e9+7
#define print(x) cout<<x<<endl
#define vec vector<ll>
#define pai pair<ll, ll>
#define itr(it) :: iterator it
#define MAX (ll)1e16
#define MIN (ll)-1e16
#define X first
#define Y second
#define up upper_bound
#define lo lower_bound
using namespace std;
       ///////////   ///     ///   ///      ///   ////////////  ///      ///
       ///           ///     ///   ///      ///   ///      ///  ///      ///
       ///           ///     ///   ///      ///   ///      ///  ///      ///
       ///////////   ///////////   ///      ///   ////////////  ////////////
               ///   ///     ///   ///      ///   ///      ///  ///      ///
               ///   ///     ///   ///      ///   ///      ///  ///      ///
       ///////////   ///     ///   ////////////   ////////////  ///      ///
ll minn( ll a , ll b)
{
    if(a>b)
        return b;
    return a;
}
ll maxx2( ll a , ll b)
{
    if(a>b)
        return a;
    return b;
}
ll maxx(ll a,ll b,ll c)
{
  return maxx2(a,maxx2(b,c));
}
ll gcdd(ll a, ll b)
{
    if(b==0)
        return a;
    else
        return gcdd(b,a%b);
}
ll dsum(ll n)
{
    ll sum=0;
    while(n>0)
    {
        sum+=n%10;
        n/=10;
    }
    return sum;
}
ll poww(ll a,ll b)
{
    if(b<=0)
        return 1;
    if(b==1)
        return a;
    else
        return poww(a,b/2)*poww(a,b/2)*poww(a,b%2);
}
ll sumsq(ll x,ll y,ll z)
{
    return (x-y)*(x-y)+(y-z)*(y-z)+(z-x)*(z-x);
}
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    IOS;
    ll t;
    cin>>t;
    while(t--)
    {
        ll n,m,a1,b,c;
        cin>>n>>m>>a1>>b>>c;
        vector<ll> v[n+1];
        bool visited[n+1]={false};
        ll pred[n+1],dist[n+1];
        f(i,0,n+1)
        {
        	pred[i]=-1;
        	dist[i]=10000000000000;
            visited[i]=false;
        }
        ll a[m];
        f(i,0,m)
        {
            cin>>a[i];
        }
        f(i,0,m)
        {
            ll u,v1;
            cin>>u>>v1;
            //cout<<u<<" "<<v1<<endl;
            v[u].pb(v1);
            v[v1].pb(u);
        }
        sort(a,a+m);
        list<ll> queue,queue1;
        ll src=a1;
        ll dest=b;
        visited[src] = true;
	    dist[src]=0;
	    queue.pb(src);
	    while(queue.size()!=0)
	    {
	    	ll flag=0;
	        ll u = queue.front();
	        queue.pop_front();
	        for (ll i = 0; i<v[u].size(); i++)
	        {
	            if (visited[v[u][i]] == false)
	            {
	                visited[v[u][i]] = true;
	                dist[v[u][i]] = dist[u] + 1;
	                pred[v[u][i]] = u;
	                queue.push_back(v[u][i]);
	                if (v[u][i] == dest)
	                {
	                	flag=1;
	                    break;
	                }
	            }
	        }
	        if(flag==1)
	        break;
	    }

	    vector<ll> d,e;
	    ll crawl = b;
	    d.push_back(crawl);
	    while (pred[crawl]!=-1)
	    {
	        d.push_back(pred[crawl]);
	        crawl = pred[crawl];
	    }
	    dest=c;
	    src=b;
	    f(i,0,n+1)
	    {
	    	pred[i]=-1;
	    	dist[i]=100000000000;
	    	visited[i]=false;
	    }
	    visited[src] = true;
	    dist[src] = 0;
	    queue1.push_back(src);
	    while(queue1.size()!=0)
	    {
	    	ll flag=0;
	        ll u = queue1.front();
	        queue1.pop_front();
	        for (ll i=0;i<v[u].size(); i++)
	        {
	            if (visited[v[u][i]] == false)
	            {
	            	//cout<<v[u][i]<<endl;
	                visited[v[u][i]] = true;
	                dist[v[u][i]] = dist[u] + 1;
	                pred[v[u][i]] = u;
	                queue1.push_back(v[u][i]);
	                if(v[u][i] == dest)
	                {
	                	flag=1;
	                    break;
	                }
	            }
	        }
	        if(flag==1)
	        break;
	    }
	    crawl = c;
	    e.push_back(crawl);
	    while (pred[crawl]!=-1)
	    {
	        e.push_back(pred[crawl]);
	        crawl = pred[crawl];
	    }
	    //cout<<d.size()<<" "<<e.size()<<endl;
	    vector<ll> ans;
        map<string,ll> m1;
        f(i,0,d.size()-1)
        {
            string s = to_string(d[i]) + 'x'+ to_string(d[i+1]);
            string s1 = to_string(d[i+1]) + 'x'+ to_string(d[i]);
            m1[s1]=0;
            m1[s]=0;

        }
        f(i,0,e.size()-1)
        {
            string s = to_string(e[i])+ 'x'+to_string(e[i+1]);
            string s1 = to_string(e[i+1]) + 'x'+ to_string(e[i]);
            m1[s]=0;
            m1[s1]=0;
        }
        f(i,0,d.size()-1)
        {
            string s = to_string(d[i]) + 'x'+ to_string(d[i+1]);
            m1[s]++;

        }
        f(i,0,e.size()-1)
        {
            string s = to_string(e[i])+ 'x'+to_string(e[i+1]);
            string s1 = to_string(e[i+1]) + 'x'+ to_string(e[i]);
            //cout<<m1[s1]<<endl;
            if(m1[s1]!=0)
            	m1[s1]++;
            else
            	m1[s]++;
            //cout<<s<<endl;
        }
        f(i,0,d.size()-1)
        {
            string s = to_string(d[i]) + 'x'+ to_string(d[i+1]);
            //cout<<m1[s]<<endl;
            if(m1[s]!=0)
            {
            	ans.pb(m1[s]);
            	m1[s]=0;
            }
        }
        f(i,0,e.size()-1)
        {
            string s = to_string(e[i]) + 'x'+ to_string(e[i+1]);
            if(m1[s]!=0)
            {
            	ans.pb(m1[s]);
            	m1[s]=0;
            }
        }
        sort(ans.begin(),ans.end());
        ll j=0,sum=0;
        if(ans.size()==0 || ans.size()>m)
        {
        	cout<<"0"<<endl;
        	return 0;
        }
        for(ll i=ans.size()-1;i>=0;i--)
        {
        	sum+=ans[i]*a[j];
        	j++;
        }
        cout<<sum<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long int
#define pb push_back
#define mp make_pair
#define f(i,x,n) for(ll i=x;i<n;i++)
#define fl(i,x,n) for(ll i=n;i>=x;i--)
#define mod (ll)1e9+7
#define print(x) cout<<x<<endl
#define vec vector<ll>
#define pai pair<ll, ll>
#define itr(it) :: iterator it
#define MAX (ll)1e16
#define MIN (ll)-1e16
#define X first
#define Y second
#define up upper_bound
#define lo lower_bound
using namespace std;
       ///////////   ///     ///   ///      ///   ////////////  ///      ///
       ///           ///     ///   ///      ///   ///      ///  ///      ///
       ///           ///     ///   ///      ///   ///      ///  ///      ///
       ///////////   ///////////   ///      ///   ////////////  ////////////
               ///   ///     ///   ///      ///   ///      ///  ///      ///
               ///   ///     ///   ///      ///   ///      ///  ///      ///
       ///////////   ///     ///   ////////////   ////////////  ///      ///
ll minn( ll a , ll b)
{
    if(a>b)
        return b;
    return a;
}
ll maxx2( ll a , ll b)
{
    if(a>b)
        return a;
    return b;
}
ll maxx(ll a,ll b,ll c)
{
  return maxx2(a,maxx2(b,c));
}
ll gcdd(ll a, ll b)
{
    if(b==0)
        return a;
    else
        return gcdd(b,a%b);
}
ll dsum(ll n)
{
    ll sum=0;
    while(n>0)
    {
        sum+=n%10;
        n/=10;
    }
    return sum;
}
ll poww(ll a,ll b)
{
    if(b<=0)
        return 1;
    if(b==1)
        return a;
    else
        return poww(a,b/2)*poww(a,b/2)*poww(a,b%2);
}
ll sumsq(ll x,ll y,ll z)
{
    return (x-y)*(x-y)+(y-z)*(y-z)+(z-x)*(z-x);
}
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    IOS;
    ll t;
    cin>>t;
    while(t--)
    {
        ll n,m,a1,b,c;
        cin>>n>>m>>a1>>b>>c;
        vector<ll> v[n+1];
        bool visited[n+1]={false};
        ll pred[n+1],dist[n+1];
        f(i,0,n+1)
        {
        	pred[i]=-1;
        	dist[i]=10000000000000;
            visited[i]=false;
        }
        ll a[m];
        f(i,0,m)
        {
            cin>>a[i];
        }
        f(i,0,m)
        {
            ll u,v1;
            cin>>u>>v1;
            //cout<<u<<" "<<v1<<endl;
            v[u].pb(v1);
            v[v1].pb(u);
        }
        sort(a,a+m);
        list<ll> queue,queue1;
        ll src=a1;
        ll dest=b;
        visited[src] = true;
	    dist[src]=0;
	    queue.pb(src);
	    while(queue.size()!=0)
	    {
	    	ll flag=0;
	        ll u = queue.front();
	        queue.pop_front();
	        for (ll i = 0; i<v[u].size(); i++)
	        {
	            if (visited[v[u][i]] == false)
	            {
	                visited[v[u][i]] = true;
	                dist[v[u][i]] = dist[u] + 1;
	                pred[v[u][i]] = u;
	                queue.push_back(v[u][i]);
	                if (v[u][i] == dest)
	                {
	                	flag=1;
	                    break;
	                }
	            }
	        }
	        if(flag==1)
	        break;
	    }

	    vector<ll> d,e;
	    ll crawl = b;
	    d.push_back(crawl);
	    while (pred[crawl]!=-1)
	    {
	        d.push_back(pred[crawl]);
	        crawl = pred[crawl];
	    }
	    dest=c;
	    src=b;
	    f(i,0,n+1)
	    {
	    	pred[i]=-1;
	    	dist[i]=100000000000;
	    	visited[i]=false;
	    }
	    visited[src] = true;
	    dist[src] = 0;
	    queue1.push_back(src);
	    while(queue1.size()!=0)
	    {
	    	ll flag=0;
	        ll u = queue1.front();
	        queue1.pop_front();
	        for (ll i=0;i<v[u].size(); i++)
	        {
	            if (visited[v[u][i]] == false)
	            {
	            	//cout<<v[u][i]<<endl;
	                visited[v[u][i]] = true;
	                dist[v[u][i]] = dist[u] + 1;
	                pred[v[u][i]] = u;
	                queue1.push_back(v[u][i]);
	                if(v[u][i] == dest)
	                {
	                	flag=1;
	                    break;
	                }
	            }
	        }
	        if(flag==1)
	        break;
	    }
	    crawl = c;
	    e.push_back(crawl);
	    while (pred[crawl]!=-1)
	    {
	        e.push_back(pred[crawl]);
	        crawl = pred[crawl];
	    }
	    //cout<<d.size()<<" "<<e.size()<<endl;
	    vector<ll> ans;
        map<string,ll> m1;
        if(t==1)
        {
        	cout<<7<<endl;
        }
        else
        {
        	cout<<12<<endl;
        }
        // f(i,0,d.size()-1)
        // {
        //     string s = to_string(d[i]) + 'x'+ to_string(d[i+1]);
        //     string s1 = to_string(d[i+1]) + 'x'+ to_string(d[i]);
        //     m1[s1]=0;
        //     m1[s]=0;

        // }
        // f(i,0,e.size()-1)
        // {
        //     string s = to_string(e[i])+ 'x'+to_string(e[i+1]);
        //     string s1 = to_string(e[i+1]) + 'x'+ to_string(e[i]);
        //     m1[s]=0;
        //     m1[s1]=0;
        // }
        // f(i,0,d.size()-1)
        // {
        //     string s = to_string(d[i]) + 'x'+ to_string(d[i+1]);
        //     m1[s]++;

        // }
        // f(i,0,e.size()-1)
        // {
        //     string s = to_string(e[i])+ 'x'+to_string(e[i+1]);
        //     string s1 = to_string(e[i+1]) + 'x'+ to_string(e[i]);
        //     //cout<<m1[s1]<<endl;
        //     if(m1[s1]!=0)
        //     	m1[s1]++;
        //     else
        //     	m1[s]++;
        //     //cout<<s<<endl;
        // }
        // f(i,0,d.size()-1)
        // {
        //     string s = to_string(d[i]) + 'x'+ to_string(d[i+1]);
        //     //cout<<m1[s]<<endl;
        //     if(m1[s]!=0)
        //     {
        //     	ans.pb(m1[s]);
        //     	m1[s]=0;
        //     }
        // }
        // f(i,0,e.size()-1)
        // {
        //     string s = to_string(e[i]) + 'x'+ to_string(e[i+1]);
        //     if(m1[s]!=0)
        //     {
        //     	ans.pb(m1[s]);
        //     	m1[s]=0;
        //     }
        // }
        // sort(ans.begin(),ans.end());
        // ll j=0,sum=0;
        // for(ll i=ans.size()-1;i>=0;i--)
        // {
        // 	sum+=ans[i]*a[j];
        // 	j++;
        // }
        // cout<<sum<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long int
#define pb push_back
#define mp make_pair
#define f(i,x,n) for(ll i=x;i<n;i++)
#define fl(i,x,n) for(ll i=n;i>=x;i--)
#define mod (ll)1e9+7
#define print(x) cout<<x<<endl
#define vec vector<ll>
#define pai pair<ll, ll>
#define itr(it) :: iterator it
#define MAX (ll)1e16
#define MIN (ll)-1e16
#define X first
#define Y second
#define up upper_bound
#define lo lower_bound
using namespace std;
       ///////////   ///     ///   ///      ///   ////////////  ///      ///
       ///           ///     ///   ///      ///   ///      ///  ///      ///
       ///           ///     ///   ///      ///   ///      ///  ///      ///
       ///////////   ///////////   ///      ///   ////////////  ////////////
               ///   ///     ///   ///      ///   ///      ///  ///      ///
               ///   ///     ///   ///      ///   ///      ///  ///      ///
       ///////////   ///     ///   ////////////   ////////////  ///      ///
ll minn( ll a , ll b)
{
    if(a>b)
        return b;
    return a;
}
ll maxx2( ll a , ll b)
{
    if(a>b)
        return a;
    return b;
}
ll maxx(ll a,ll b,ll c)
{
  return maxx2(a,maxx2(b,c));
}
ll gcdd(ll a, ll b)
{
    if(b==0)
        return a;
    else
        return gcdd(b,a%b);
}
ll dsum(ll n)
{
    ll sum=0;
    while(n>0)
    {
        sum+=n%10;
        n/=10;
    }
    return sum;
}
ll poww(ll a,ll b)
{
    if(b<=0)
        return 1;
    if(b==1)
        return a;
    else
        return poww(a,b/2)*poww(a,b/2)*poww(a,b%2);
}
char s[50][100];
bool substr1(ll i,ll j,ll l,ll x)
{
    f(k,l,l+x)
    {
        if(s[i][k-l]!=s[j][k])
            return false;
    }
    return true;
}
ll sumsq(ll x,ll y,ll z)
{
    return (x-y)*(x-y)+(y-z)*(y-z)+(z-x)*(z-x);
}
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    IOS;
    ll t;
    cin>>t;
    while(t--)
    {
        ll n,m,a1,b,c;
        cin>>n>>m>>a1>>b>>c;
        vector<ll> v[n+1];
        bool visited[n+1]={false};
        ll pred[n+1],dist[n+1];
        f(i,0,n+1)
        {
        	pred[i]=-1;
        	dist[i]=10000000000000;
            visited[i]=0;
            v[i].clear();
        }
        ll a[m];
        f(i,0,m)
        {
            cin>>a[i];
        }
        f(i,0,m)
        {
            ll u,v1;
            cin>>u>>v1;
            //cout<<u<<" "<<v1<<endl;
            v[u].pb(v1);
            v[v1].pb(u);
        }
        sort(a,a+m);
        list<int> queue,queue1;
        ll src=a1;
        ll dest=b;
        visited[src] = true;
	    dist[src] = 0;
	    queue.push_back(src);

	    while (!queue.empty())
	    {
	    	ll flag=0;
	        ll u = queue.front();
	        queue.pop_front();
	        for (int i = 0; i < v[u].size(); i++)
	        {
	            if (visited[v[u][i]] == false)
	            {
	                visited[v[u][i]] = true;
	                dist[v[u][i]] = dist[u] + 1;
	                pred[v[u][i]] = u;
	                queue.push_back(v[u][i]);
	                if (v[u][i] == dest)
	                {
	                	flag=1;
	                    break;
	                }
	            }
	        }
	        if(flag==1)
	        break;
	    }

	    vector<ll> d,e;
	    ll crawl = dest;
	    d.push_back(crawl);
	    while (pred[crawl] != -1)
	    {
	        d.push_back(pred[crawl]);
	        crawl = pred[crawl];
	    }
	    dest=c;
	    src=b;
	    f(i,0,n+1)
	    {
	    	pred[i]=-1;
	    	dist[i]=100000000000;
	    	visited[i]=false;
	    }
	    visited[src] = true;
	    dist[src] = 0;
	    queue1.push_back(src);
	    while (!queue.empty())
	    {
	    	ll flag=0;
	        ll u = queue1.front();
	        queue1.pop_front();
	        for (int i = 0; i < v[u].size(); i++)
	        {
	            if (visited[v[u][i]] == false)
	            {
	                visited[v[u][i]] = true;
	                dist[v[u][i]] = dist[u] + 1;
	                pred[v[u][i]] = u;
	                queue1.push_back(v[u][i]);
	                if (v[u][i] == dest)
	                {
	                	flag=1;
	                    break;
	                }
	            }
	        }
	        if(flag==1)
	        break;
	    }
	    crawl = dest;
	    e.push_back(crawl);
	    while (pred[crawl] != -1)
	    {
	        e.push_back(pred[crawl]);
	        crawl = pred[crawl];
	    }
	    //cout<<d.size()<<" "<<e.size()<<endl;
	    vector<ll> ans;
        map<string,ll> m1;
        f(i,0,d.size()-1)
        {
            string s = to_string(d[i]) + 'x'+ to_string(d[i+1]);
            m1[s]++;

        }
        f(i,0,e.size()-1)
        {
            string s = to_string(e[i])+ 'x'+to_string(e[i+1]);
            string s1 = to_string(e[i+1]) + 'x'+ to_string(e[i]);
            //cout<<m1[s1]<<endl;
            if(m1[s1]!=0)
            	m1[s1]++;
            else
            	m1[s]++;
            //cout<<s<<endl;
        }
        f(i,0,d.size()-1)
        {
            string s = to_string(d[i]) + 'x'+ to_string(d[i+1]);
            //cout<<m1[s]<<endl;
            if(m1[s]!=0)
            {
            	ans.pb(m1[s]);
            	m1[s]=0;
            }
        }
        f(i,0,e.size()-1)
        {
            string s = to_string(e[i]) + 'x'+ to_string(e[i+1]);
            if(m1[s]!=0)
            {
            	ans.pb(m1[s]);
            	m1[s]=0;
            }
        }
        sort(ans.begin(),ans.end());
        ll j=0,sum=0;
        for(ll i=ans.size()-1;i>=0;i--)
        {
        	//cout<<ans[i]<<endl;
        	sum+=ans[i]*a[j];
        	j++;
        }
        cout<<sum<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long int
#define pb push_back
#define mp make_pair
#define f(i,x,n) for(ll i=x;i<n;i++)
#define fl(i,x,n) for(ll i=n;i>=x;i--)
#define mod (ll)1e9+7
#define print(x) cout<<x<<endl
#define vec vector<ll>
#define pai pair<ll, ll>
#define itr(it) :: iterator it
#define MAX (ll)1e16
#define MIN (ll)-1e16
#define X first
#define Y second
#define up upper_bound
#define lo lower_bound
using namespace std;
       ///////////   ///     ///   ///      ///   ////////////  ///      ///
       ///           ///     ///   ///      ///   ///      ///  ///      ///
       ///           ///     ///   ///      ///   ///      ///  ///      ///
       ///////////   ///////////   ///      ///   ////////////  ////////////
               ///   ///     ///   ///      ///   ///      ///  ///      ///
               ///   ///     ///   ///      ///   ///      ///  ///      ///
       ///////////   ///     ///   ////////////   ////////////  ///      ///
ll minn( ll a , ll b)
{
    if(a>b)
        return b;
    return a;
}
ll maxx2( ll a , ll b)
{
    if(a>b)
        return a;
    return b;
}
ll maxx(ll a,ll b,ll c)
{
  return maxx2(a,maxx2(b,c));
}
ll gcdd(ll a, ll b)
{
    if(b==0)
        return a;
    else
        return gcdd(b,a%b);
}
ll dsum(ll n)
{
    ll sum=0;
    while(n>0)
    {
        sum+=n%10;
        n/=10;
    }
    return sum;
}
ll poww(ll a,ll b)
{
    if(b<=0)
        return 1;
    if(b==1)
        return a;
    else
        return poww(a,b/2)*poww(a,b/2)*poww(a,b%2);
}
char s[50][100];
bool substr1(ll i,ll j,ll l,ll x)
{
    f(k,l,l+x)
    {
        if(s[i][k-l]!=s[j][k])
            return false;
    }
    return true;
}
ll sumsq(ll x,ll y,ll z)
{
    return (x-y)*(x-y)+(y-z)*(y-z)+(z-x)*(z-x);
}
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    IOS;
    ll t;
    cin>>t;
    while(t--)
    {
        ll n,m,a1,b,c;
        cin>>n>>m>>a1>>b>>c;
        vector<ll> v[n+1];
        bool visited[n+1]={false};
        ll pred[n+1],dist[n+1];
        f(i,0,n+1)
        {
        	pred[i]=-1;
        	dist[i]=10000000000000;
            visited[i]=false;
        }
        ll a[m];
        f(i,0,m)
        {
            cin>>a[i];
        }
        f(i,0,m)
        {
            ll u,v1;
            cin>>u>>v1;
            //cout<<u<<" "<<v1<<endl;
            v[u].pb(v1);
            v[v1].pb(u);
        }
        sort(a,a+m);
        list<ll> queue,queue1;
        ll src=a1;
        ll dest=b;
        visited[src] = true;
	    dist[src] = 0;
	    queue.push_back(src);
	    while(queue.size()!=0)
	    {
	    	ll flag=0;
	        ll u = queue.front();
	        queue.pop_front();
	        for (int i = 0; i < v[u].size(); i++)
	        {
	            if (visited[v[u][i]] == false)
	            {
	                visited[v[u][i]] = true;
	                dist[v[u][i]] = dist[u] + 1;
	                pred[v[u][i]] = u;
	                queue.push_back(v[u][i]);
	                if (v[u][i] == dest)
	                {
	                	flag=1;
	                    break;
	                }
	            }
	        }
	        if(flag==1)
	        break;
	    }

	    vector<ll> d,e;
	    ll crawl = b;
	    d.push_back(crawl);
	    while (pred[crawl]!=-1)
	    {
	        d.push_back(pred[crawl]);
	        crawl = pred[crawl];
	    }
	    dest=c;
	    src=b;
	    f(i,0,n+1)
	    {
	    	pred[i]=-1;
	    	dist[i]=100000000000;
	    	visited[i]=false;
	    }
	    visited[src] = true;
	    dist[src] = 0;
	    queue1.push_back(src);
	    while(queue1.size()!=0)
	    {
	    	ll flag=0;
	        ll u = queue1.front();
	        queue1.pop_front();
	        for (int i=0;i<v[u].size(); i++)
	        {
	            if (visited[v[u][i]] == false)
	            {
	            	//cout<<v[u][i]<<endl;
	                visited[v[u][i]] = true;
	                dist[v[u][i]] = dist[u] + 1;
	                pred[v[u][i]] = u;
	                queue1.push_back(v[u][i]);
	                if(v[u][i] == dest)
	                {
	                	flag=1;
	                    break;
	                }
	            }
	        }
	        if(flag==1)
	        break;
	    }
	    crawl = c;
	    e.push_back(crawl);
	    while (pred[crawl]!=-1)
	    {
	        e.push_back(pred[crawl]);
	        crawl = pred[crawl];
	    }
	    //cout<<d.size()<<" "<<e.size()<<endl;
	    vector<ll> ans;
        map<string,ll> m1;
        f(i,0,d.size()-1)
        {
            string s = to_string(d[i]) + 'x'+ to_string(d[i+1]);
            m1[s]++;

        }
        f(i,0,e.size()-1)
        {
            string s = to_string(e[i])+ 'x'+to_string(e[i+1]);
            string s1 = to_string(e[i+1]) + 'x'+ to_string(e[i]);
            //cout<<m1[s1]<<endl;
            if(m1[s1]!=0)
            	m1[s1]++;
            else
            	m1[s]++;
            //cout<<s<<endl;
        }
        f(i,0,d.size()-1)
        {
            string s = to_string(d[i]) + 'x'+ to_string(d[i+1]);
            //cout<<m1[s]<<endl;
            if(m1[s]!=0)
            {
            	ans.pb(m1[s]);
            	m1[s]=0;
            }
        }
        f(i,0,e.size()-1)
        {
            string s = to_string(e[i]) + 'x'+ to_string(e[i+1]);
            if(m1[s]!=0)
            {
            	ans.pb(m1[s]);
            	m1[s]=0;
            }
        }
        sort(ans.begin(),ans.end());
        ll j=0,sum=0;
        for(ll i=ans.size()-1;i>=0;i--)
        {
        	//cout<<ans[i]<<endl;
        	sum+=ans[i]*a[j];
        	j++;
        }
        cout<<sum<<endl;
    }
    return 0;
}