#include <stdio.h>

#include <stdlib.h>


 typedef struct ConstructionManager

{

int Number;

int Time;

int StackTime;

int nextNum;

int * next;

}CM;

int main(void)

{

int TCase, N, K, i, num1, num2, num3;

CM * Cptr;

scanf("%d", &TCase);

while(1)

{

if(TCase==0)

break;

scanf("%d %d", &N, &K);

Cptr= (CM *)malloc(sizeof(CM)*(N+1));

for(i=1; i<=N; i++)

{

scanf("%d", &Cptr[i].StackTime);

Cptr[i].Time=Cptr[i].StackTime;

Cptr[i].Number=i;

Cptr[i].nextNum = 0;

}

for( i=1; i<=K; i++)

{

scanf("%d %d", &num1, &num2);

if ( num1> num2)

{

num3= num1;

num1= num2;

num2= num3;

}

Cptr[num1].nextNum +=1;

if( Cptr[num1].nextNum == 1)

Cptr[num1].next = (int*)malloc(sizeof(int));

else

Cptr[num1].next = (int *)realloc(Cptr[num1].next, sizeof(int)*Cptr[num1].nextNum );

Cptr[num1].Number = num1;

Cptr[num1].next[Cptr[num1].nextNum-1] = num2;

}

for(i=1; i<=N; i++)

{

if(Cptr[i].nextNum!=0)

{

num1=Cptr[i].next[Cptr[i].nextNum-1];

if(  Cptr[num1].StackTime < Cptr[i].StackTime+ Cptr[num1].Time )

Cptr[num1].StackTime = Cptr[i].StackTime+ Cptr[num1].Time;

if( Cptr[i].nextNum > 1)

{

Cptr[i].nextNum--;

i--;

}

}

}

scanf("%d", &num1);

printf("%d\n", Cptr[num1].StackTime);

TCase--;

}

return 0;

}


이게 왜 틀렸다는건지 모르겠음...... 잠을 못잠.....