# -*- encoding: cp949 -*-
import Queue
answer = []
for t in xrange(int(raw_input())):
n,k = map(int,raw_input().split())
d = map(int,raw_input().split())
adj = [[] for i in xrange(1001)]
indegree = [0 for i in xrange(1001)]
for i in xrange(k):
a,b = map(int,raw_input().split())
adj[a].append(b)
indegree[b] += 1
w = int(raw_input())
result = [0 for i in xrange(1001)]
q = Queue.Queue()
for i in xrange(1,n+1):
if indegree[i]==0:
q.put(i)
while indegree[w] > 0:
u = q.get()
for n in xrange(len(adj[u])):
cur_node = adj[u][n]
result[cur_node] = max(result[cur_node], result[u]+d[u-1])
indegree[cur_node] -= 1
if indegree[cur_node] == 0:
q.put(cur_node)
if indegree[w] <= 0:
break
answer.append(result[w]+d[w-1])
print '\n'.join(map(str,answer))
#include <iostream>
#include <queue>
using namespace std;
int main()
{
int answer[1001]={0,},c=0;
int t;
cin>>t;
for(int i=0;i<t;i++)
{
int n,k,d[1001],adj[1001][1001]={0,},indegree[1001]={0,};
cin>>n>>k;
for(int j=0;j<n;j++)
cin>>d[j];
for(int j=0;j<k;j++)
{
int a,b;
cin>>a>>b;
for(int l=0;l<1001;l++)
if(adj[a][l]==0)
{
adj[a][l] = b;
break;
}
indegree[b]++;
}
int w;
cin>>w;
int result[1001]={0,};
queue<int> q;
for(int j=1;j<n+1;j++)
if(indegree[j]==0)
q.push(j);
while(indegree[w] > 0)
{
int u = q.front(); q.pop();
int len = 0;
for(int j=0;j<1001;j++)
if(adj[u][j]!=0)
len++;
for(int n=0; n<len; n++)
{
int cur_node = adj[u][n];
result[cur_node] = max(result[cur_node], result[u]+d[u-1]);
indegree[cur_node]--;
if(indegree[cur_node]==0)
q.push(cur_node);
}
if(indegree[w] <= 0)
break;
}
answer[c++] = result[w]+d[w-1];
}
for(int j=0;;j++)
{
if(answer[j]==0) break;
cout<<answer[j]<<endl;
}
return 0;
}