import java.util.*; import java.io.*; import java.text.*; //Solution Credits: Taranpreet Singh public class Main{ //SOLUTION BEGIN void pre() throws Exception{} void solve(int TC) throws Exception{ int n = ni(), m = ni(), k = ni(); long[] b = new long[n]; for(int i = 0; i< n; i++)b[i] = nl(); int[][] e = new int[m][]; for(int i = 0; i< m; i++)e[i] = new int[]{ni()-1, ni()-1}; int[][] g = makeD(n, e, false), rg = makeD(n, e, true); Stack s = new Stack(); boolean[] vis = new boolean[n]; for(int i = 0; i< n; i++)if(!vis[i])order(g, s, vis, i); Arrays.fill(vis, false); int[][] cmp = new int[n][];int cnt = 0; int[] a = new int[n]; while(!s.isEmpty()){ int u = s.pop(); if(!vis[u]){ set.clear(); dfs(rg, vis, u); cmp[cnt] = new int[set.size()];int c = 0; for(int v:set){ cmp[cnt][c++] = v; a[v] = cnt; } cnt++; } } // long[][] val = new long[cnt][]; TreeSet[] adj = new TreeSet[cnt]; for(int i = 0; i< cnt; i++)adj[i] = new TreeSet<>(); for(int i = 0; i< m; i++)if(a[e[i][0]]!=a[e[i][1]]){ adj[a[e[i][0]]].add(a[e[i][1]]); } g = make(adj); // for(int i = 0; i< cnt; i++){ // int sz = cmp[i].length; // long[] x = new long[sz]; // for(int j = 0; j< sz; j++)x[j] = b[cmp[i][j]]; // Arrays.sort(x);val[i] = new long[1+k]; // for(int j = 0; j< Math.min(1+k, sz); j++)val[i][j] = (sz-j)*x[j]; // } Arrays.fill(vis, false); for(int i = 0; i< cnt; i++)if(!vis[i])Tsort(g, s, vis, i); long[][] dp = new long[cnt][1+k]; Stack st = new Stack<>(); while(!s.isEmpty())st.push(s.pop()); long[] tmp = new long[1+k], list = new long[n];long ans = 0; while(!st.isEmpty()){ int x = st.pop(); Arrays.fill(tmp, 0); for(int v:g[x]) for(int i = 0; i<= Math.min(k, dp[v].length-1); i++) tmp[i] = Math.max(tmp[i], dp[v][i]); for(int j = 0; j< cmp[x].length; j++)list[j] = b[cmp[x][j]]; Arrays.sort(list, 0, cmp[x].length); for(int i = 0; i<=k; i++) for(int j = 0; j<= Math.min(i, cmp[x].length-1); j++){ dp[x][i] = Math.max(dp[x][i], tmp[i-j]+(cmp[x].length-j)*list[j]); ans = Math.max(ans, dp[x][i]); } } pn(ans%1000000021); } void Tsort(int[][] g, Stack s, boolean[] vis, int u){ vis[u] = true; for(int v:g[u])if(!vis[v])Tsort(g,s,vis,v); s.push(u); } TreeSet set = new TreeSet<>(); void dfs(int[][] g, boolean[] vis, int u){ vis[u] = true;set.add(u); for(int v:g[u])if(!vis[v])dfs(g, vis, v); } void order(int[][] g, Stack s, boolean[] vis, int u){ vis[u] = true; for(int v:g[u]) if(!vis[v]) order(g, s, vis, v); s.push(u); } int[][] make(TreeSet[] set){ int n = set.length; int[][] g= new int[n][]; for(int i = 0; i< n; i++){ g[i] = new int[set[i].size()];int c = 0; for(int j:set[i])g[i][c++] = j; } return g; } int[][] makeD(int n, int[][] edge, boolean rev){ int[][] g = new int[n][]; ;int[] cnt = new int[n]; int x = rev?1:0; for(int i = 0; i< edge.length; i++)cnt[edge[i][x]]++; for(int i = 0; i< n; i++)g[i] = new int[cnt[i]]; for(int i = 0; i< edge.length; i++)g[edge[i][x]][--cnt[edge[i][x]]] = edge[i][x^1]; return g; } //SOLUTION END void hold(boolean b)throws Exception{if(!b)throw new Exception("Hold right there, Sparky!");} long mod = (long)1e9+7, IINF = (long)1e10; final int INF = (int)1e9, MX = (int)1e4+1; DecimalFormat df = new DecimalFormat("0.000000000000000"); double PI = 3.1415926535897932384626433832792884197169399375105820974944, eps = 1e-8; static boolean multipleTC = true, memory = false; FastReader in;PrintWriter out; void run() throws Exception{ in = new FastReader(); out = new PrintWriter(System.out); int T = (multipleTC)?ni():1; //Solution Credits: Taranpreet Singh pre();for(int t = 1; t<= T; t++)solve(t); out.flush(); out.close(); } public static void main(String[] args) throws Exception{ if(memory)new Thread(null, new Runnable() {public void run(){try{new Main().run();}catch(Exception e){e.printStackTrace();}}}, "1", 1 << 28).start(); else new Main().run(); } long gcd(long a, long b){return (b==0)?a:gcd(b,a%b);} int gcd(int a, int b){return (b==0)?a:gcd(b,a%b);} int bit(long n){return (n==0)?0:(1+bit(n&(n-1)));} void p(Object o){out.print(o);} void pn(Object o){out.println(o);} void pni(Object o){out.println(o);out.flush();} String n()throws Exception{return in.next();} String nln()throws Exception{return in.nextLine();} int ni()throws Exception{return Integer.parseInt(in.next());} long nl()throws Exception{return Long.parseLong(in.next());} double nd()throws Exception{return Double.parseDouble(in.next());} class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String s) throws Exception{ br = new BufferedReader(new FileReader(s)); } String next() throws Exception{ while (st == null || !st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); }catch (IOException e){ throw new Exception(e.toString()); } } return st.nextToken(); } String nextLine() throws Exception{ String str = ""; try{ str = br.readLine(); }catch (IOException e){ throw new Exception(e.toString()); } return str; } } }