//It's all about what U BELIEVE
#include<algorithm>
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<vector>
#include<bitset>
#include<stack>
#include<queue>
#include<deque>
#include<cmath>
#include<map>
#include<set>
#define endl '\n'
#define PI acos(-1)
#define INF ~(1<<31)
#define pb push_back
#define pob pop_back
#define lsone(Z) (Z&-Z)
#define modulo 1000000007
#define gcu getchar_unlocked
#define allof(Z) Z.begin(),Z.end()
#define rallof(Z) Z.rbegin(),Z.rend()
#define mset(z,v) memset(z,v,sizeof(z))
#define lne if(line)puts("");else line =1
#define fo(s,y,z) for(int y=s ; y<(int)z ; y++)
#define readf freopen("/home/ebram96/Desktop/in" , "r" , stdin);
#define writef freopen("/home/ebram96/Desktop/out" , "w" , stdout);
using namespace std;
typedef unsigned long long ull;
typedef pair<ull,ull> pairull;
typedef pair<int,int> pairii;
typedef vector<string> vstr;
typedef deque<int> dqint;
typedef set<ull> setull;
typedef unsigned int ui;
typedef queue<int> qint;
typedef vector<int> vi;
typedef set<int> seti;
typedef long long ll;
//int dx[]={-1,0,1, 0,-1,1, 1,-1};
//int dy[]={ 0,1,0,-1, 1,1,-1,-1};
struct State
{
int u;
ll w;
inline bool operator<(const State &s)const
{return s.w!=w?s.w<w:s.u<u;}
};
struct Rule
{int u , v; ll w;};
Rule r[1000];
int t , n , m , source , destination , u , v , c;
ll res , w , dist[1<<12];
inline int get_set()
{
int ret = 0 , crystal , sz;
scanf("%d",&sz);
fo(0,y,sz)
{
scanf("%d",&crystal);
ret|=(1<<crystal);
}
return ret;
}
inline ll dijkstra()
{
priority_queue<State>pq;
pq.push({source,0});
fo(0,y,1<<n)dist[y] = ull(-1);
dist[source] = 0;
while(!pq.empty())
{
u = pq.top().u , w = pq.top().w;
if(u==destination)return w;
pq.pop();
if(w>dist[u])continue;
fo(0,y,m)if((u&r[y].u)==r[y].u)
if(w+r[y].w<dist[(u^r[y].u)|r[y].v])
{
pq.push({(u^r[y].u)|r[y].v,w+r[y].w});
dist[(u^r[y].u)|r[y].v] = w+r[y].w;
}
}
return -1;
}
int main()
{
//readf
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&m);
source = get_set();
destination = get_set();
fo(0,y,m)
scanf("%lld %d %d",&r[y].w,&r[y].u,&r[y].v);
res = dijkstra();
printf("Case #%d: ",++c);
~res?printf("YES %lld\n",res):puts("NO");
}
}
Ly9JdCdzIGFsbCBhYm91dCB3aGF0IFUgQkVMSUVWRQojaW5jbHVkZTxhbGdvcml0aG0+CiNpbmNsdWRlPGlvc3RyZWFtPgojaW5jbHVkZTxzdGRpby5oPgojaW5jbHVkZTxjc3RyaW5nPgojaW5jbHVkZTx2ZWN0b3I+CiNpbmNsdWRlPGJpdHNldD4KI2luY2x1ZGU8c3RhY2s+CiNpbmNsdWRlPHF1ZXVlPgojaW5jbHVkZTxkZXF1ZT4KI2luY2x1ZGU8Y21hdGg+CiNpbmNsdWRlPG1hcD4KI2luY2x1ZGU8c2V0PgojZGVmaW5lIGVuZGwgJ1xuJwojZGVmaW5lIFBJIGFjb3MoLTEpCiNkZWZpbmUgSU5GIH4oMTw8MzEpCiNkZWZpbmUgcGIgcHVzaF9iYWNrCiNkZWZpbmUgcG9iIHBvcF9iYWNrCiNkZWZpbmUgbHNvbmUoWikgKFomLVopCiNkZWZpbmUgbW9kdWxvIDEwMDAwMDAwMDcKI2RlZmluZSBnY3UgZ2V0Y2hhcl91bmxvY2tlZAojZGVmaW5lIGFsbG9mKFopIFouYmVnaW4oKSxaLmVuZCgpCiNkZWZpbmUgcmFsbG9mKFopIFoucmJlZ2luKCksWi5yZW5kKCkKI2RlZmluZSBtc2V0KHosdikgbWVtc2V0KHosdixzaXplb2YoeikpCiNkZWZpbmUgbG5lIGlmKGxpbmUpcHV0cygiIik7ZWxzZSBsaW5lID0xCiNkZWZpbmUgZm8ocyx5LHopIGZvcihpbnQgeT1zIDsgeTwoaW50KXogOyB5KyspCiNkZWZpbmUgcmVhZGYgZnJlb3BlbigiL2hvbWUvZWJyYW05Ni9EZ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