/*
刚开始没有看懂题意,让求一个从1到n+1的Max_sum
dp[i] = dp[i] + max(dp[j]);
当前点等于所有可达点的最大值+当前点的值
记录路径并输出
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int map[105][105];
int path[105],Int[105],ans[105];
int n;
int solve()
{
int i,j,k,max;
path[1] = -1;
for(i=2;i<=n+1;i++){
k = i;
max = -1;
for(j=1;j<i;j++){
if(map[j][i] && Int[j] > max){
k = j ;max = Int[j];
}
}
/*看到这里有木有一种求最长上升子序列的感觉?*/
if(max != - 1){
path[i] = k;Int[i] +=max;
}
}
/*回溯路径*/
k = path[n+1];
j = 1;
while(k != (-1)){
ans[j++] = k;
k = path[k];
}
return j;
}
int main()
{
int T,k=1,j,i,m;
scanf("%d",&T);
while(T-- && scanf("%d",&n)){
if(k != 1)puts("");
for(i=1;i<=n;i++)scanf("%d",&Int[i]);
scanf("%d",&m);
while(m-- && scanf("%d%d",&i,&j))map[i][j] = 1;
i = solve();
printf("CASE %d#\npoints : %d\ncircuit : ",k++,Int[n+1]);
for(j=i-1;j>=1;j--)printf("%d->",ans[j]);
puts("1");
memset(map,0,sizeof(map));
memset(path,0,sizeof(path));
memset(Int,0,sizeof(Int));
memset(ans,0,sizeof(ans));
}
return 0;
}