1 #include<cstdio>
2 #include<cstdlib>
3 #include<cmath>
4 #include<cstring>
5 #include<algorithm>
6 #include<iostream>
7 #include<vector>
8 #include<map>
9 #include<set>
10 #include<queue>
11 #include<string>
12 #define inf 1000000000
13 #define maxn 50000
14 #define maxm 500000
15 #define eps 1e-10
16 #define ll long long
17 #define pa pair<int,int>
18 #define for0(i,n) for(int i=0;i<=(n);i++)
19 #define for1(i,n) for(int i=1;i<=(n);i++)
20 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
21 using namespace std;
22 inline int read()
23 {
24 int x=0,f=1;char ch=getchar();
25 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
26 while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();}
27 return x*f;
28 }
29 struct edge{int go,next,w;}e[2*maxm];
30 int n,m,k,tot,a[25],q[maxn],d[25][maxn],head[maxn],dp[22][1<<21];
31 bool v[maxn];
32 inline void ins(int x,int y,int z)
33 {
34 e[++tot].go=y;e[tot].w=z;e[tot].next=head[x];head[x]=tot;
35 }
36 inline void insert(int x,int y,int z)
37 {
38 ins(x,y,z);ins(y,x,z);
39 }
40 inline void spfa(int s)
41 {
42 for(int i=0;i<n;++i) d[s][i]=inf;
43 memset(v,0,sizeof(v));
44 int l=0,r=1,x,y;q[1]=s;d[s][s]=0;
45 while(l!=r)
46 {
47 x=q[++l];if(l==maxn)l=0;v[x]=0;
48 for(int i=head[x];i;i=e[i].next)
49 if(d[s][x]+e[i].w<d[s][y=e[i].go])
50 {
51 d[s][y]=d[s][x]+e[i].w;
52 if(!v[y]){v[y]=1;q[++r]=y;if(r==maxn)r=0;}
53 }
54 }
55 }
56 inline int dfs(int x,int y)
57 {
58 if(dp[x][y]>=0)return dp[x][y];
59 if(y==(1<<k)-1)return d[x][n-1];
60 dp[x][y]=inf;
61 for1(i,k)
62 if((y&a[i])==a[i])
63 dp[x][y]=min(dp[x][y],d[x][i]+dfs(i,y|(1<<(i-1))));
64 return dp[x][y];
65 }
66 int main()
67 {
68 freopen("input.txt","r",stdin);
69 freopen("output.txt","w",stdout);
70 n=read();m=read();k=read();
71 int x,y,z;
72 for1(i,m)x=read()-1,y=read()-1,z=read(),insert(x,y,z);
73 for0(i,k)spfa(i);
74 m=read();
75 memset(dp,-1,sizeof(dp));
76 for1(i,m)x=read()-1,y=read()-1,a[y]|=1<<(x-1);
77 printf("%d\n",dfs(0,0));
78 return 0;
79 }