rt
code:
#include<bits/stdc++.h>
#define For( i , a , b ) for( register ll i = ( a ) ; i <= ( b ) ; ++i )
#define Rep( i , a , b ) for( register ll i = ( a ) ; i >= ( b ) ; --i )
#define ll long long
using namespace std ;
const ll N = 200005 , M = 1000005 ;
inline ll read(){
ll s = 0 ; char ch = getchar() ; bool f = 0 ;
for( ; !isdigit( ch ) ; ch = getchar() ) f ^= !( 45 ^ ch ) ;
for( ; isdigit( ch ) ; ch = getchar() ) s = ( s << 3 ) + ( s << 1 ) + ( ch ^ 48 ) ;
if( f ) return -s ; return s ;
}
ll n , m , k , tot , head[ N ] , ans , a , b , c ;
ll t[ N ] , dis[ N ] , type[ N ] , T ;
bool vis[ N ] ;
struct edge{
ll to , nxt , w ;
} e[ M ] ;
void add( ll u , ll v , ll w ) {
e[ ++tot ].nxt = head[ u ] ;
e[ tot ].to = v ;
e[ tot ].w = w ;
head[ u ] = tot ;
}
struct node{
ll val , num ;
bool operator < ( const node &y ) const{ return y.val < val ; }
} tmp , tp ;
priority_queue < node > q ;
void clear() {
tot = 0 ;
For( i , 1 , n ) head[ i ] = 0 ;
}
void solve() {
ans = 0x3f3f3f3f ;
For( bit , 0 , 30 ) {
while( !q.empty() ) q.pop() ;
For( i , 1 , n ) dis[ i ] = 0x3f3f3f3f , vis[ i ] = 0 ;
tmp.val = 0 ;
For( i , 1 , k )
if( t[ i ] & ( 1ll << bit ) ) type[ t[ i ] ] = 2 , tmp.num = t[ i ] , q.push( tmp ) , dis[ t[ i ] ] = 0 ;
else type[ t[ i ] ] = 1 , dis[ t[ i ] ] = 0x3f3f3f3f ;
while( !q.empty() ) {
tmp = q.top() ;
q.pop() ;
if( vis[ tmp.num ] ) continue ;
vis[ tmp.num ] = 1 ;
if( type[ tmp.num ] == 1 ) { ans = min( ans , dis[ tmp.num ] ) ; break ; }
for( ll i = head[ tmp.num ] ; i ; i = e[ i ].nxt ) {
ll v = e[ i ].to ;
if( dis[ v ] >= dis[ tmp.num ] + e[ i ].w ) {
dis[ v ] = dis[ tmp.num ] + e[ i ].w ;
tp.val = dis[ v ] ; tp.num = v ; q.push( tp ) ;
}
}
}
}
For( bit , 0 , 30 ) {
while( !q.empty() ) q.pop() ;
For( i , 1 , n ) dis[ i ] = 0x3f3f3f3f , vis[ i ] = 0 ;
tmp.val = 0 ;
For( i , 1 , k )
if( ( t[ i ] & ( 1ll << bit ) ) == 0 ) type[ t[ i ] ] = 2 , tmp.num = t[ i ] , q.push( tmp ) , dis[ t[ i ] ] = 0 ;
else type[ t[ i ] ] = 1 , dis[ t[ i ] ] = 0x3f3f3f3f ;
while( !q.empty() ) {
tmp = q.top() ;
q.pop() ;
if( vis[ tmp.num ] ) continue ;
vis[ tmp.num ] = 1 ;
if( type[ tmp.num ] == 1 ) { ans = min( ans , dis[ tmp.num ] ) ; break ; }
for( ll i = head[ tmp.num ] ; i ; i = e[ i ].nxt ) {
ll v = e[ i ].to ;
if( dis[ v ] >= dis[ tmp.num ] + e[ i ].w ) {
dis[ v ] = dis[ tmp.num ] + e[ i ].w ;
tp.val = dis[ v ] ; tp.num = v ; q.push( tp ) ;
}
}
}
}
printf("%lld\n" , ans ) ;
}
int main(){
T = read() ;
while( T-- ) {
n = read() ; m = read() ; k = read() ;
clear() ;
For( i , 1 , m ) a = read() , b = read() , c = read() , add( a , b , c ) ;
For( i , 1 , k ) t[ i ] = read() ;
solve() ;
}
return 0 ;
}