#include<bits/stdc++.h>
using namespace std;
const int inf=0x3f3f3f3f;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int T;
cin>>T;
while(T--)
{
int n,m,k,s;
cin>>n>>m>>k>>s;
vector<vector<pair<int,int>>> out(m+5);
vector<int> in(m+5),pa(m+5),sumdeg(m+5),maxdel(m+5,0),minn(m+5,inf);
function<int(int)> find=[&](int x){return pa[x]?pa[x]=find(pa[x]):x;};
vector<tuple<int,int,int>> edges;
long long ans=0;
for(int i=1;i<=n;i++)
{
int u,v,w;
cin>>u>>w>>v;
ans+=w;
edges.emplace_back(u,v,w);
out[u].emplace_back(w,v);
in[v]++;
}
for(int i=1;i<=k;i++)
{
int x;
cin>>x;
in[x]++;
}
in[s]++;
for(int i=1;i<=m;i++)
{
sort(out[i].begin(),out[i].end(),greater<pair<int,int>>());
while((int)out[i].size()>in[i])
{
// cerr<<"del "<<i<<' '<<out[i].back().second<<' '<<out[i].back().first<<endl;
ans-=out[i].back().first;
maxdel[i]=max(maxdel[i],out[i].back().first);
out[i].pop_back();
}
}
for(int u=1;u<=m;u++)
{
for(auto [w,v]:out[u])
{
int pu=find(u),pv=find(v);
if(pu!=pv)
pa[pv]=pu;
}
}
for(int u=1;u<=m;u++)
{
int pu=find(u);
// cerr<<u<<' '<<out[u].size()<<' '<<in[u]<<endl;
sumdeg[pu]+=out[u].size();
sumdeg[pu]-=in[u];
if(not out[u].empty())
{
minn[pu]=min(minn[pu],out[u].back().first-maxdel[u]);
}
}
for(int u=1;u<=m;u++)
{
// cerr<<u<<' '<<find(u)<<' '<<sumdeg[find(u)]<<endl;
if(find(u)==u)
{
if(sumdeg[u]==0 and minn[u]!=inf)
ans-=minn[u];
}
}
cout<<ans<<"\n";
}
return 0;
}