#include<cstdio>
#include<iostream>
#include<queue>
#include<algorithm>
#include<cstring>
#define int long long
using namespace std;
const int N=1e6+10;
const int mod=1e9+7;
priority_queue< int ,vector<int>,greater<int> > q;
template <class T> in(T &x){
	x = 0;
	bool f = 0;
	char c = getchar();
	while(c < '0'||c > '9'){
		if(c == '-') f = 1;
		c = getchar();
	}
	while(c >= '0'&&c <= '9'){
		x = (x << 3) + (x << 1) + (c ^ 48);
		c = getchar();
	}
	if(f == 1) x = -x;
}
int n,t,size[N],p[N],u,v,tot,m,ans;
int nxt[N<<1],head[N<<1],to[N<<1],du[N<<1];
void pre(){
	while(!q.empty()) q.pop();
	tot=0;
	ans=0;
	memset(size, 0 ,sizeof size);
	memset(p,0,sizeof p);
	memset(to,0,sizeof to);
	memset(nxt,0,sizeof nxt);
	memset(head,0,sizeof head);
}
void add(int x,int y){
	tot++;
	to[tot] = y;
	nxt[tot] = head[x];
	head[x] = tot;
}
void dfs(int x,int fa){
		size[x] = 1;
		for(int i = head[x];i;i = nxt[i]){
			int tt = to[i];
			if(tt != fa){
				dfs(tt,x);
				size[x] = size[x] + size[tt];
			}
		}

}
void dfs_ans(int x,int fa){
	for(int i = head[x];i;i = nxt[i]){
		int tt = to[i];
		if(tt != fa){
			q.push(size[tt]*(n-size[tt]));
			dfs_ans(tt,x);
		}
	}
}
int work(){
	dfs(1,0);
	dfs_ans(1,0);
	n = n - 1;
	ans=0;
	if(m >= n){
		for(int i = 1;i <= n - 1;i ++){
			int tmp = q.top();q.pop();
			ans = ( ans%mod + tmp%mod * p[i]%mod )%mod;

		}
		int tmp = q.top();
		q.pop();
		for(int i = n;i <= m;i ++){
			tmp = ( tmp%mod * p[i]%mod )%mod;
		}
		ans = ( ans%mod + tmp%mod )%mod;
	}
	if(n > m){
		for(int i = 1;i <= n - m;i ++){
			int tmp = q.top();q.pop();
			ans = (ans%mod + tmp%mod)%mod;
		}
		for(int i = 1;i <= m;i ++){
			int tmp = q.top();
			q.pop();
			ans = ( ans%mod + tmp%mod * p[i]%mod )%mod;
		}
	}

	/*for(int i = 1;i <= n - 1;i ++){
		q.push(size[to[tot].y])
	}*/
	return ans%mod;

}
signed main(){
	in(t);
	while(t--){
		pre();
		in(n);
		for(int i = 1;i < n; i ++){
			in(u),in(v);
			add(u,v);
			add(v,u);
			//size[i] = 1;
		}
		in(m);
		for(int i = 1;i <= m;i ++ )
			in(p[i]);
		sort(p + 1,p + m + 1);
		cout<<work()<<endl;
	}
}


#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#define mod ( 1000000007 )

using namespace std;
typedef long long LL;

const int MAXN = 1e5 + 5;

template<typename _T>
void read( _T &x ){
	x = 0; char c = getchar( ); _T f = 1;
	while( c < '0' || c > '9' ){ if( c == '-' )	f = -1; c = getchar( ); }
	while( c >= '0' && c <= '9' ){ x = ( x << 3 ) + ( x << 1 ) + ( c & 15 ); c = getchar( ); }
	x *= f;
}

template<typename _T>
void write( _T x ){
	if( x < 0 ){ putchar( '-' ); x = -x; }
	if( x > 9 )	write( x / 10 );
	putchar( x % 10 + '0' );
}

template<typename _T>
_T add( _T x, _T y ) { return x + y >= mod ? x + y - mod : x + y; }
template<typename _T>
_T mul( _T x, _T y ) { x *= y; if( x >= mod ) x %= mod; return x; }

int N, M;
int tot;
LL fur[MAXN], cont[MAXN], times[MAXN];
vector<int> Graph[MAXN];

inline void dfs(int now,int fath)
{
	fur[now]=1;
	for( unsigned i = 0; i < Graph[now].size( ); ++ i ){
		if (Graph[now][i]!=fath)
		{
			dfs(Graph[now][i],now);
			fur[now]+=fur[Graph[now][i]];
		}
	}
	if (now!=1)  cont[++tot]=fur[now]*(N-fur[now]);
}

bool cmp(int x,int y)
{
	return x>y;
}

void clearIt( ){
	memset( Graph, 0, sizeof( Graph ) );
	memset( cont, 0, sizeof( cont ) );
	memset( times, 0, sizeof( times ) );
	memset( fur, 0, sizeof( fur ) );
}
#include<iostream>
signed main()
{
	int T;
	cin>>T;
	while (T--)
	{
		clearIt();
		cin>>N;
		tot=0;
//		for (int i=1;i<=2*N;i++)  head[i]=e[i].to=e[i].next=0;
		for (int i=1;i<N;i++)
		{
			int u,v;
			cin>>u>>v;
			Graph[u].push_back( v );
			Graph[v].push_back( u );
//			add_edge(u,v);
//			add_edge(v,u);
		}
		dfs(1,0);
		cin>>M;
		for (int i=1;i<=M;i++)  cin>>times[i];
		sort(cont+1,cont+N,cmp);
		sort(times+1,times+M+1,cmp);
		if (M>N-1)
		{
			int tot=1;
			for (int i=1;i<=M-N+2;i++)  tot=(tot*times[i])%mod;
			times[M-N+2]=tot;

			for (int i=M-N+2;i<=M;i++)  times[i-M+N-1]=times[i];
		}
		int Ans=0;
		for (int i=1;i<=N-1;i++)
		{
			if (i<=M)  Ans=(Ans+((cont[i]*times[i])%mod))%mod;
			else Ans=(Ans+cont[i])%mod;
		}
		cout<<Ans<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll df=1e6+7;
ll i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,head[df],fa[df],siz[df],mod=1e9+7,a[df],b[df],ans=0,tot=0,c[df];
struct egde
{
	ll from,to,next,far;
}e[df<<2];
bool cmp(ll xx,ll yy)
{
	return xx>yy;
}
void add(ll u,ll v)
{
	e[k].from=u;
	e[k].to=v;	e[k].far=0;
	e[k].next=head[u];	head[u]=k++;
	return ;
}
ll mo(ll x)
{
	if(x<0)
		return (x%mod+mod)%mod;
	if(x<mod)
	{
		return x;
	}
	else
	{
		return x%mod;
	}
}
void dfs(ll u)
{
	ll i=0,v=0;
	siz[u]=1;
	for(i=head[u];i!=-1;i=e[i].next)
	{
		v=e[i].to;
		if(v!=fa[u])
		{
			fa[v]=u;
			dfs(v);
			siz[u]+=siz[v];
		}
	}
	return ;
}
int main()
{
	scanf("%lld",&o);
	while(o--)
	{
		scanf("%lld",&n);
		for(i=1;i<=n;i++)
			head[i]=-1;

		for(i=1;i<n;i++)
		{
			scanf("%lld%lld",&u,&v);
			add(u,v),add(v,u);
		}

		scanf("%lld",&m);
		for(i=1;i<=m;i++)
		{
			scanf("%lld",&a[i]);
		}
		if(n-1>m)
		{
			for(i=m+1;i<=n-1;i++)
			{
				a[i]=1;
			}
			sort(a+1,a+n,cmp);
			for(i=1;i<=n-1;i++)
				c[i]=a[i];
		}
		else
		{
			sort(a+1,a+m+1);
			for(i=1;i<n-1;i++)
			{
				c[i]=a[i];
			}
			c[n-1]=a[n-1];
			for(i=n;i<=m;i++)
				c[n-1]=mo(c[n-1]*a[i]);
		}
		dfs(1);
		for(i=0;i<k;i++)
		{
			if(siz[e[i].from]>siz[e[i].to])
			{
				b[++tot]=(n-siz[e[i].to])*siz[e[i].to];
			}
		}
//		cout<<tot<<" "<<n-1<<endl;
		ans=0;
		sort(b+1,b+tot+1,cmp);

		for(i=1;i<=n-1;i++)
			ans+=mo(c[i]*mo(b[i])),ans%=mod;
		printf("%lld\n",ans%mod);
		ans=0;
		for(i=0;i<k;i++)
			e[i].from=e[i].to=e[i].next=e[i].far=0;
		for(i=0;i<=max(m,n-1);i++)
			b[i]=a[i]=c[i]=0;
		for(i=0;i<=n;i++)
			head[i]=-1,siz[i]=0,fa[i]=0;
		ans=tot=0;
		k=0;
	}
}
//1
//4
//1 2
//2 3
//3 4
//2
//2 2


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const long long mo=1000000007;
int h[100005],T,tot,n,m;
long long cnt[100005],p[60005];
struct DX
{
	int next,v;
}dx[200005];
void add(int u,int v)
{
	dx[++tot].v=v;
	dx[tot].next=h[u];
	h[u]=tot;
}
int dfs(int u,int fa)
{
	long long s=1;
	for(long long i=h[u];i;i=dx[i].next)
	{
		long long v=dx[i].v;
		if(v==fa)
			continue;
		s+=dfs(v,u);
	}
	cnt[u-1]=s*(n-s);
	return s;
}
int main()
{
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&n);
		tot=0;
		memset(h,0,sizeof(h));
		for(int i=1;i<n;i++)
		{
			int a,b;
			scanf("%d%d",&a,&b);
			add(a,b);
			add(b,a);
		}
		scanf("%d",&m);
		for(int i=1;i<=m;i++)
			scanf("%lld",&p[i]);
		if(m<n)
		{
			for(int i=m+1;i<n;i++)
				p[i]=1;
			sort(p+1,p+n);
		}
		else
		{
			sort(p+1,p+m+1);
			for(int i=m;i>=n;i--)
				p[i-1]=(p[i-1]*p[i])%mo;
		}
		dfs(1,1);
		sort(cnt+1,cnt+n);
		long long ans=0;
		for(int i=1;i<n;i++)
			ans=(ans+p[i]*cnt[i])%mo;
		printf("%lld\n",ans);
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <stdlib.h>
#include <stack>
#include <queue>
#define ri register int
#define int long long

inline int read() {
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9') {if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9') {x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
	return x*f;
}

const int N=1e5+5,inf=1e9+7;

int T,n,m;
int to[N<<1],next[N<<1],head[N],val[N],cnt=1,tot=0;
int p[N],siz[N],son[N<<1],f[N<<1],ans=0;

bool cmp(int x,int y){
	return x>y;
}

void edge_add(int u,int v){
	to[cnt]=v;
	next[cnt]=head[u];
	head[u]=cnt++;
}

void Read(){
	n=read();
	for(ri i=1;i<n;i++){
		int u=read(),v=read();
		edge_add(u,v),edge_add(v,u);
	}
	m=read();
	for(ri i=1;i<=m;i++)p[i]=read();
	std::sort(p+1,p+m+1,cmp);
}

void dfs(int now,int fa){
	siz[now]=1;
	for(ri i=head[now];i;i=next[i]){
		int v=to[i];
		if(v==fa)continue;
		son[i]=v,f[i]=now;
		dfs(v,now);
		siz[now]+=siz[v];
	}
}

void work(){
	for(ri i=1;i<=(n<<1);i++){
		if(son[i]==0)continue;
		val[++tot]=siz[son[i]]%inf*(n-siz[son[i]])%inf;
	}
//	printf("%d\n",tot);
//	for(ri i=1;i<n;i++)printf("%d ",val[i]);
//	printf("\n");
	std::sort(val+1,val+tot+1,cmp);
	int ls=1,pp=1;n-=1;
	if(n>=m)pp=1;
	else {
		int ls=1;
		for(ri i=1;i<=m-n+1;i++)ls*=p[i],pp++,ls%=inf;
		p[pp-1]=ls;
		pp-=1;
	}
	for(ri i=1;i<=n;i++){
		if(pp<=m)ans+=val[i]%inf*p[pp]%inf;
		else ans+=val[i];
//		printf("%d %d %d\n",val[i],p[pp],pp);
		ans%=inf;
		pp++;
	}
	printf("%lld\n",ans);
	n+=1;
}

void clear(){
	for(ri i=1;i<=(n<<1);i++)to[i]=0,next[i]=0,son[i]=0,f[i]=0;
	for(ri i=1;i<=n;i++)head[i]=0,siz[i]=0,val[i]=0,p[i]=0;
	cnt=1,ans=0,tot=0;
}

signed main(void){
	T=read();
	while(T--){
		Read();
		dfs(1,0);
		work();
		clear();
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const long long mo=1000000007;
vector<int> h[100005];
long long cnt[100005],p[60005],T,tot,n,m,siz[100005],ans;
void dfs(long long u,long long fa)
{
	siz[u]=1;
	for(vector<int>::iterator it=h[u].begin();it!=h[u].end();it++)
	{
		if((*it)==fa)
			continue;
		dfs(*it,u);
		siz[u]+=siz[*it];
	}
	if(siz[u]*(n-siz[u])!=0)
		cnt[++tot]=siz[u]*(n-siz[u]);
}
int main()
{
	scanf("%lld",&T);
	while(T--)
	{
		long long a,b;
		ans=0;
		tot=0;
		scanf("%lld",&n);
		for(int i=1;i<=n;i++)
			h[i].clear();
		memset(cnt,0,sizeof(cnt));
		memset(p,0,sizeof(p));
		for(long long i=1;i<n;i++)
		{
			scanf("%lld%lld",&a,&b);
			h[a].push_back(b);
			h[b].push_back(a);
		}
		scanf("%lld",&m);
		for(long long i=1;i<=m;i++)
			scanf("%lld",&p[i]);
		if(m<n)
		{
			for(long long i=m+1;i<n;i++)
				p[i]=1;
			sort(p+1,p+n);
		}
		else
		{
			sort(p+1,p+m+1);
			for(long long i=m;i>=n;i--)
				p[i-1]=(p[i-1]*p[i])%mo;
		}
		dfs(1LL,1LL);
		sort(cnt+1,cnt+n);
		for(long long i=1;i<n;i++)
			ans=(ans+(p[i]%mo)*(cnt[i]%mo)%mo)%mo;
		printf("%lld\n",ans);
	}
	return 0;
}


#include <bits/stdc++.h>
#define For(i,a,b) for ( int i=(a);i<=(b);i++ )
#define Dow(i,b,a) for ( int i=(b);i>=(a);i-- )
#define GO(i,x) for ( int i=head[x];i;i=e[i].nex )
#define mem(x,s) memset(x,s,sizeof(x))
#define cpy(x,s) memcpy(x,s,sizeof(x))
#define YES return puts("YES"),0
#define NO return puts("NO"),0
#define GG return puts("-1"),0
#define pb push_back
#define int long long
using namespace std;

struct IO
{
	#define MAXSIZE (1 << 20)
	#define isdigit(x) (x >= '0' && x <= '9')
  	char buf[MAXSIZE],*p1,*p2;
  	char pbuf[MAXSIZE],*pp;
  	IO():p1(buf),p2(buf),pp(pbuf){}
  	inline char gc() {
    	return getchar();
    	if (p1==p2) p2=(p1=buf)+fread(buf,1,MAXSIZE,stdin);
    	return p1==p2?' ':*p1++;
  	}
	inline bool blank(char ch) {return ch==' '||ch =='\n'||ch == '\r'||ch == '\t';}
  	template <class T>
  	inline void read(T &x)
	{
	    register double tmp=1;register bool sign=0; x=0;
	    register char ch=gc();
	    for (;!isdigit(ch);ch=gc()) if(ch=='-') sign=1;
	    for (;isdigit(ch);ch=gc()) x=x*10+(ch-'0');
	    if (ch=='.') for (ch=gc();isdigit(ch);ch=gc()) tmp/=10.0,x+=tmp*(ch-'0');
	    if (sign) x=-x;
  	}
  	inline void read(char *s)
	{
    	register char ch=gc();
		for (;blank(ch);ch=gc());
		for (;!blank(ch);ch=gc()) *s++=ch;
    	*s=0;
  	}
  	inline void read(char &c) {for(c=gc();blank(c);c=gc());}
  	template<class t> inline void write(t x){
		if(x<0) putchar('-'),write(-x);
		else{if(x>9) write(x/10);putchar('0'+x%10);}
	}
} io;

const int mod=1e9+7;
const int mo=998244353;
const int N=2e5+5;

int n,m,siz[N],head[N],cnt,P[N],S[N],bo[N],cwy,ans;

struct Node
{
	int nex,to;
};
Node e[N<<1];

inline void jia(int u,int v)
{
	e[++cnt].nex=head[u];
	head[u]=cnt;
	e[cnt].to=v;
}

inline void dfs(int u,int fa)
{
	siz[u]=1;
	GO(i,u)
	{
		int v=e[i].to;
		if(v==fa) continue;
		dfs(v,u);
		siz[u]+=siz[v];
		S[++cwy]=siz[v]*(n-siz[v]);
	}
}

inline bool Cmp(int x,int y) { return x>y; }
inline void Add(int &x,int y) { x+=y; if(x>=mod) x-=mod; }

signed main()
{
	int Q;
	io.read(Q);
	for (;Q--;)
	{
		io.read(n);
		For(i,1,n) head[i]=0,siz[i]=0;
		cwy=cnt=ans=0;
		For(i,1,n-1)
		{
			int x,y;
			io.read(x),io.read(y);
			jia(x,y);
			jia(y,x);
		}
		io.read(m);
		For(i,1,m) io.read(P[i]);
		dfs(1,0);
		sort(P+1,P+m+1,Cmp);
		sort(S+1,S+cwy+1,Cmp);
//		For(i,1,n) cout<<S[i]<<" "; puts("");
		if(m<n)
		{
			For(i,m+1,n) P[i]=1;
			For(i,1,n-1) Add(ans,P[i]*S[i]%mod);
			io.write((ans+mod)%mod);
			puts("");
		}
		else
		{
			int st=m-n+2;
			int tot=1ll;
			For(i,1,st) tot=tot*P[i]%mod;
			ans=tot*S[1]%mod;
			For(i,2,n-1) Add(ans,P[st+i-1]*S[i]%mod);
			io.write((ans+mod)%mod);
			puts("");
		}
	}
	return 0;
}


#include<map>
#include<list>
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define in inline
#define ll long long
#define INF 2147483647
#define ri register int
#define rc register char
using namespace std;
in int read(){
	rc c=' ';
	ri v=0,x=1;
	while((c<'0')||(c>'9')){if(c=='-') x=-1;c=getchar();}
	while((c>='0')&&(c<='9')){v=(v<<1)+(v<<3)+(c^48);c=getchar();}
	return v*x;
}
in void write(ll x){
	if(x<0) x=-x,putchar('-');
	if(x>9) write(x/10);
	putchar(x%10^48);
}
const int N=2000010,mod=1e9+7;
struct Edge{
	int v,nxt;
}e[N];
int t,n,m,cnt,tot,head[N],siz[N];
ll ans,w1[N],w2[N];
in void add(ri u,ri v){
	e[++cnt]=(Edge){v,head[u]};
	head[u]=cnt;
}
void dfs(ri u,ri fa){
	siz[u]=1;
	for(ri i=head[u];i;i=e[i].nxt){
		ri v=e[i].v;
		if(v!=fa) dfs(v,u),siz[u]+=siz[v];
	}
	if(u!=1) w2[++tot]=(n-siz[u])*siz[u];
}
in bool cmp(ll x,ll y){
	return x>y;
}
int main(){
	t=read();
	while(t--){
		n=read(),ans=cnt=tot=0;
		for(ri i=1;i<=n*2;i++)
			head[i]=e[i].v=e[i].nxt=0;
		for(ri i=1,u,v;i<=n-1;i++)
			u=read(),v=read(),add(u,v),add(v,u);
		dfs(1,0),m=read();
		for(ri i=1;i<=m;i++)
			w1[i]=read();
		sort(w1+1,w1+m+1,cmp);
		sort(w2+1,w2+n,cmp);
		if(m<n){
			for(ri i=m+1;i<=n-1;i++)
				w1[i]=1;
		}
		else{
			for(ri i=n;i<=m;i++)
				w1[1]=w1[1]*w1[i]%mod;
		}
		for(ri i=1;i<=n-1;i++)
			ans=(ans+w1[i]*w2[i])%mod;
		printf("%lld\n",ans);
	}
	return ~~(0-0);
}


#include<bits/stdc++.h>
using namespace std;
int t,n,m;
const int N=1000005;
typedef long long ll;
const ll mod=1e9+7;
ll sz[N],d[N],p[N],val[N];
int f[N],cnt,h[N*2],nxt[N*2],to[N*2],bian[N][2];
void add(int x,int y){
	cnt++;
	nxt[cnt]=h[x];
	h[x]=cnt;
	to[cnt]=y;
}
void dfs(int u,int fa){
	sz[u]=1;
	f[u]=fa;
	for(int i=h[u];i;i=nxt[i]){
		int v=to[i];
		if(v!=fa){
			dfs(v,u);
			sz[u]+=sz[v];
		}
	}
}
int main(){
	int i;
	cin>>t;
	while(t--){
		scanf("%d",&n);
		cnt=0;memset(h,0,sizeof(h));memset(nxt,0,sizeof(nxt));memset(to,0,sizeof(to));memset(sz,0,sizeof(sz));
		for(i=1;i<n;i++){
			int x,y;
			scanf("%d%d",&x,&y);
			add(x,y),add(y,x);
			bian[i][0]=x,bian[i][1]=y;
		}
		dfs(1,0);
		ll ans=0;
		for(i=1;i<n;i++){
			if(f[bian[i][0]]==bian[i][1])swap(bian[i][0],bian[i][1]);
			d[i]=(1ll*n-sz[bian[i][1]])*sz[bian[i][1]],ans=(ans+d[i])%mod;
		}
		scanf("%d",&m);
		for(i=1;i<=m;i++)scanf("%lld",&p[i]);
		sort(p+1,p+m+1);sort(d+1,d+n);
		if(m<n){
			for(i=1;i<=m;i++)ans=(ans+d[n-1-m+i]*(p[i]-1)%mod)%mod;
			printf("%lld\n",ans);
		}else{
			for(i=1;i<m;i++)ans=(ans+d[i]*(p[i]-1)%mod)%mod;
			ll w=d[m]%mod;
			for(i=m;i<n;i++)w=(w*(p[i]-1))%mod;
			ans=(ans+w)%mod;
			printf("%lld\n",ans);
		}
	}
	return 0;
}


#include<bits/stdc++.h>
#define mod 1000000007
#define int long long
using namespace std;
int n,m;
int g[100010],num[100010];
priority_queue<int> q1,q2;
int head[100010],cnt=0;
struct ss{
	int to,next;
}e[100010];
inline void add(int a,int b){
	cnt++;
	e[cnt].to=b;
	e[cnt].next=head[a];
	head[a]=cnt;
}
void dfs(int u,int f){
	num[u]=1;
	for(int i=head[u];i!=0;i=e[i].next){
		if(e[i].to==f){
			continue;
		}
		dfs(e[i].to,u);
		num[u]+=num[e[i].to];
	}
	if(num[u]!=n) q1.push(num[u]*(n-num[u]));
	return;
}
bool cmp(int a,int b){
	return a>=b;
}
signed main(){
	int t;
	scanf("%lld",&t);
	while(t--){
		int ans=0;
		memset(head,0,sizeof(head));
		memset(e,0,sizeof(e));
		memset(num,0,sizeof(num));
		memset(g,1,sizeof(g));
		cnt=0;
		scanf("%lld",&n);
		for(int i=1,a,b;i<n;i++){
			scanf("%lld%lld",&a,&b);
			add(a,b);
			add(b,a);
			g[i]=1;
		}
		g[n]=1;
		scanf("%lld",&m);
		for(int i=1;i<=m;i++){
			scanf("%lld",&g[i]);
			q2.push(g[i]);
		}
		dfs(1,0);
		if(m>=n)
		{
			long long s=q2.top();
			q2.pop();
			for(long long i=m;i>=n;i--)
			{
				s=(s*q2.top())%mod;
				q2.pop();
			}
			if(m>=n)
			{
				ans=((q1.top()%mod)*s)%mod;
				q1.pop();
			}
		}
		else
			for(long long i=m;i<n;i++)
				q2.push(1);
	while(!q1.empty()){
		ans=(ans+(q1.top()%mod)*(q2.top()%mod)%mod)%mod;
		q1.pop(),q2.pop();
	}
	while(!q2.empty()){
		q2.pop();
	}
	printf("%lld\n",ans);
	/*	sort(g+1,g+m+1,cmp);
		for(int i=n;i<=m;i++){
			g[1]+=g[i];
			g[i]=1;
		}
		int ans=0;
		sort(num+1,num+n+1,cmp);
		for(int i=1;i<=n;i++){
			ans+=(num[i]*g[i])%mod;
			ans=ans%mod;
		}
		printf("%lld\n",ans);*/
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define tkn "va"
#define bignum vector<int>
#define ii pair<int,int>
#define X first
#define Y second
const int N=1e5+7;
const int M=6e4+7;
const ll base=311;
const ll mod=1e9+7;
int n,m;
ll p[N],child[N],cnt[N];
vector<int> ke[N];
void dfs(int u,int cha){
    child[u]=1;
    for (int v: ke[u]){
        if (v==cha) continue;
        dfs(v,u);
        child[u]+=child[v];
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    //freopen(tkn".inp", "r", stdin);
    //freopen(tkn".out", "w", stdout);
    int t; cin>>t;
    while(t--){
        cin>>n;
        for (int i=1;i<=n;i++) ke[i].clear();
        for (int i=1;i<n;i++){
            p[i]=1ll;
            int u,v;
            cin>>u>>v;
            ke[u].push_back(v);
            ke[v].push_back(u);
        }
        cin>>m;
        for (int i=1;i<=m;i++) cin>>p[i];
        if (m<n-1) m=n-1;
        sort(p+1,p+m+1);
        if (m>n-1){
            for (int i=n;i<=m;i++) p[n-1]=(p[n-1]*p[i])%mod;
            m=n-1;
        }
        dfs(1,0); ll res=0;
        for (int i=1;i<n;i++)   cnt[i]=((n-child[i+1])*child[i+1])%mod;
        sort(cnt+1,cnt+n);
        for (int i=1;i<n;i++)
            res=(res+(p[i]*cnt[i])%mod+mod*mod)%mod;
        cout<<res<<'\n';
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define tkn "va"
#define bignum vector<int>
#define ii pair<int,int>
#define X first
#define Y second
const int N=1e5+7;
const int M=6e4+7;
const ll base=311;
const ll mod=1e9+7;
ll n,m;
ll p[N],child[N],cnt[N];
vector<int> ke[N];
void dfs(int u,int cha){
    child[u]=1;
    for (int v: ke[u]){
        if (v==cha) continue;
        dfs(v,u);
        child[u]+=child[v];
    }
}
void sol(){
    dfs(1,0);
    ll res=0;
    if(m>n-1)
        for(int i=n;i<=m;i++)
            p[n-1]=((p[n-1]%mod)*(p[i]%mod))%mod;
    for(int i=1;i<n;i++)
        cnt[i]=(n-child[i+1])*child[i+1];
    sort(cnt+1,cnt+n);
    for(int i=1;i<n;i++)
        res=(res%mod+(cnt[i]%mod)*(p[i]%mod))%mod;
    cout<<res<<'\n';
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
 //   freopen(tkn".inp", "r", stdin);
  //  freopen(tkn".out", "w", stdout);
    int t; cin>>t;
    while(t--){
        cin>>n;
        for (int i=1;i<=n;i++) ke[i].clear();
        for (int i=1;i<n;i++){
            p[i]=1ll;
            int u,v;
            cin>>u>>v;
            ke[u].push_back(v);
            ke[v].push_back(u);
        }
        cin>>m;
		for(int i=1;i<=m;i++) cin>>p[i]; m=max(n-1,m);
		sort(p+1,p+m+1);
		sol();
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define tkn "va"
#define bignum vector<int>
#define ii pair<int,int>
#define X first
#define Y second
const int N=1e5+7;
const int M=6e4+7;
const ll base=311;
const ll mod=1e9+7;
int n,m;
ll p[N],child[N],cnt[N];
vector<int> ke[N];
void dfs(int u,int cha){
    child[u]=1ll;
    for (int v: ke[u]){
        if (v==cha) continue;
        dfs(v,u);
        child[u]+=child[v];
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
   // freopen(tkn".inp", "r", stdin);
  //  freopen(tkn".out", "w", stdout);
    int t; cin>>t;
    while(t--){
        cin>>n;
        for (int i=1;i<=n;i++) ke[i].clear();
        for (int i=1;i<n;i++){
            p[i]=1ll;
            int u,v;
            cin>>u>>v;
            ke[u].push_back(v);
            ke[v].push_back(u);
        }
        p[n]=1ll;
        cin>>m;
        for (int i=1;i<=m;i++) cin>>p[i];
        if (m<n-1) m=n-1;
        sort(p+1,p+m+1);
        if (m>n-1){
            for (int i=n;i<=m;i++)
                p[n-1]=(p[n-1]*p[i])%mod;
        }
        dfs(1,0); ll res=0;
        for (int i=1;i<n;i++)   cnt[i]=((n-child[i+1])*child[i+1])%mod;
        sort(cnt+1,cnt+n);
        for (int i=1;i<n;i++)
            res=(res+(p[i]*cnt[i])%mod)%mod;
        cout<<res<<'\n';
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define tkn "va"
#define bignum vector<int>
#define ii pair<int,int>
#define X first
#define Y second
const int N=1e5+7;
const int M=6e4+7;
const ll base=311;
const ll mod=1e9+7;
int n,m;
ll p[M],child[N],cnt[N];
vector<int> ke[N];
void dfs(int u,int cha){
    child[u]=1;
    for (int v: ke[u]){
        if (v==cha) continue;
        dfs(v,u);
        child[u]+=child[v];
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
  //  freopen(tkn".inp", "r", stdin);
  //  freopen(tkn".out", "w", stdout);
    int t; cin>>t;
    while(t--){
        cin>>n;
        for (int i=1;i<=n;i++) ke[i].clear();
        for (int i=1;i<n;i++){
            int u,v;
            cin>>u>>v;
            ke[u].push_back(v);
            ke[v].push_back(u);
        }
        cin>>m;
        for (int i=1;i<=m;i++) cin>>p[i];
        sort(p+1,p+m+1);
        if (m>n-1){
            for (int i=n;i<=m;i++) p[n-1]=(p[n-1]*p[i])%mod;
            m=n-1;
        }
        else if (m<n-1) {
            for (int i=m+1;i<=n-1;i++) p[i]=1ll;
            m=n-1;
        }
        sort(p+1,p+n);
        dfs(1,0); ll res=0;
        for (int i=1;i<n;i++)   cnt[i]=((n-child[i+1])*child[i+1])%mod;
        sort(cnt+1,cnt+n);
        for (int i=1;i<n;i++)
            res=(res+(p[i]*cnt[i])%mod+mod)%mod;
        cout<<res<<'\n';
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define tkn "va"
#define bignum vector<int>
#define ii pair<int,int>
#define X first
#define Y second
const int N=1e5+7;
const int M=6e4+7;
const ll base=311;
const ll mod=1e9+7;
int n,m;
ll p[N],child[N],cnt[N];
vector<int> ke[N];
void dfs(int u,int cha){
    child[u]=1;
    for (int v: ke[u]){
        if (v==cha) continue;
        dfs(v,u);
        child[u]+=child[v];
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
  //  freopen(tkn".inp", "r", stdin);
   // freopen(tkn".out", "w", stdout);
    int t; cin>>t;
    while(t--){
        cin>>n;
        for (int i=1;i<=n;i++) ke[i].clear();
        for (int i=1;i<n;i++){
            int u,v;
            cin>>u>>v;
            ke[u].push_back(v);
            ke[v].push_back(u);
        }
        cin>>m;
        for (int i=1;i<=m;i++) cin>>p[i];
        sort(p+1,p+m+1);
        if (m>n-1){
            for (int i=n;i<=m;i++) p[n-1]=(p[n-1]*p[i])%mod;
            m=n-1;
        }
        else if (m<n-1) {
            for (int i=m+1;i<=n-1;i++) p[i]=1ll;
            m=n-1;
        }
        sort(p+1,p+n);
        dfs(1,0); ll res=0;
        for (int i=1;i<n;i++)   cnt[i]=((n-child[i+1])*child[i+1])%mod;
        sort(cnt+1,cnt+n);
        for (int i=1;i<n;i++)
            res=(res+(p[i]*cnt[i])%mod+mod)%mod;
        cout<<res<<'\n';
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define tkn "va"
#define bignum vector<int>
#define ii pair<int,int>
#define X first
#define Y second
const int N=1e5+7;
const int M=6e4+7;
const ll base=311;
const ll mod=1e9+7;
int n,m;
ll p[N],child[N],cnt[N];
vector<int> ke[N];
void dfs(int u,int cha){
    child[u]=1;
    for (int v: ke[u]){
        if (v==cha) continue;
        dfs(v,u);
        child[u]+=child[v];
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
 //   freopen(tkn".inp", "r", stdin);
  //  freopen(tkn".out", "w", stdout);
    int t; cin>>t;
    while(t--){
        cin>>n;
        for (int i=1;i<=n;i++) ke[i].clear();
        for (int i=1;i<n;i++){
            int u,v;
            cin>>u>>v;
            ke[u].push_back(v);
            ke[v].push_back(u);
        }
        cin>>m;
        for (int i=1;i<=m;i++) cin>>p[i];
        if (m<n-1) for (int i=m+1;i<=n-1;i++) {p[i]=1ll; m=n-1;}
        sort(p+1,p+m+1);
        if (m>n-1){
            for (int i=n;i<=m;i++) p[n-1]=(p[n-1]*p[i])%mod;
            m=n-1;
        }
        sort(p+1,p+n);
        dfs(1,0); ll res=0;
        for (int i=1;i<n;i++)   cnt[i]=((n-child[i+1])*child[i+1])%mod;
        sort(cnt+1,cnt+n);
        for (int i=1;i<n;i++)
            res=(res+(p[i]*cnt[i])%mod+mod)%mod;
        cout<<res<<'\n';
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define tkn "va"
#define bignum vector<int>
#define ii pair<int,int>
#define X first
#define Y second
const int N=1e5+7;
const int M=6e4+7;
const ll base=311;
const ll mod=1e9+7;
int n,m;
ll p[M],child[N],cnt[N];
vector<int> ke[N];
void dfs(int u,int cha){
    child[u]=1;
    for (int v: ke[u]){
        if (v==cha) continue;
        dfs(v,u);
        child[u]+=child[v];
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
  //  freopen(tkn".inp", "r", stdin);
  // freopen(tkn".out", "w", stdout);
    int t; cin>>t;
    while(t--){
        cin>>n;
        for (int i=1;i<=n;i++) ke[i].clear();
        for (int i=1;i<n;i++){
            int u,v;
            cin>>u>>v;
            ke[u].push_back(v);
            ke[v].push_back(u);
        }
        cin>>m;
        for (int i=1;i<=m;i++) cin>>p[i];
        if (m>n-1){
            for (int i=n;i<=m;i++) p[1]=(p[1]*p[i])%mod;
            m=n-1;
        }
        else if (m<n-1) {
            for (int i=m+1;i<=n-1;i++) p[i]=1ll;
            m=n-1;
        }
        sort(p+1,p+n);
        dfs(1,0); ll res=0;
        for (int i=1;i<n;i++)   cnt[i]=((n-child[i+1])*child[i+1])%mod;
        sort(cnt+1,cnt+n);
        for (int i=1;i<n;i++)
            res=(res+(p[i]*cnt[i])%mod+mod)%mod;
        cout<<res<<'\n';
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define tkn "va"
#define bignum vector<int>
#define ii pair<int,int>
#define X first
#define Y second
const int N=1e5+7;
const int M=6e4+7;
const ll base=311;
const ll mod=1e9+7;
ll n,m;
ll p[N],child[N],cnt[N];
vector<int> ke[N];
void dfs(int u,int cha){
    child[u]=1;
    for (int v: ke[u]){
        if (v==cha) continue;
        dfs(v,u);
        child[u]+=child[v];
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
  //  freopen(tkn".inp", "r", stdin);
  //  freopen(tkn".out", "w", stdout);
    int t; cin>>t;
    while(t--){
        cin>>n;
        for (int i=1;i<=n;i++) ke[i].clear();
        for (int i=1;i<n;i++){
            p[i]=1ll;
            int u,v;
            cin>>u>>v;
            ke[u].push_back(v);
            ke[v].push_back(u);
        }
        cin>>m;
        for (int i=1;i<=m;i++) {cin>>p[i];p[i]%=mod;} m=max(m,n-1);
        sort(p+1,p+m+1);
        if (m>n-1){
            for (int i=n;i<=m;i++)
                p[n-1]=(p[n-1]*p[i])%mod;
        }
        dfs(1,0); ll res=0;
        for (int i=1;i<n;i++)   cnt[i]=((n-child[i+1])*child[i+1])%mod;
        sort(cnt+1,cnt+n);
        for (int i=1;i<n;i++)
            res=(res+(p[i]*cnt[i])%mod)%mod;
        cout<<res<<'\n';
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, m, n) for(int i = m; i <= n; i++)
#define per(i, m, n) for(int i = m; i >= n; i--)
#define pb push_back
#define DEBUG(x) cout<<x<<endl;
const int INF = 0x3f3f3f3f;

const int mod = 1e9 + 7;
const int maxn = 1e5 + 5;
const int maxm = 6e4 + 5;

int subtree[maxn], p[maxm], num[maxn], N, M, T, cnt;

struct edge {
	int u, v, weight;
} E[maxn];

vector<int> G[maxn];

bool cmp(edge &e1, edge &e2) {
	return e1.weight > e2.weight;
}

void dfs(int u, int k) {
	// par[u] = k;
	subtree[u] = 1;
	for (int i = 0; i < (int) G[u].size(); i++) {
		int v = G[u][i];
		if (v == k) continue;
		dfs(v, u);
		subtree[u] += subtree[v];
	}
	if (u != 1) E[++cnt] = (edge) {u, k, (ll) subtree[u] * (ll) (N - subtree[u]) % mod};
}

void solve() {
	ll ans = 0;
	cin >> N;
	rep(i, 1, N) G[i].clear();
	memset(E, 0, sizeof(E));
	cnt = 0;
	rep(i, 1, N - 1) {
		int u, v;
		cin >> u >> v;
		G[u].pb(v), G[v].pb(u);
	}
	dfs(1, -1);
	cin >> M;
	rep(i, 1, M) cin >> p[i];
	sort(p + 1, p + M + 1, greater<int>());
	sort(E + 1, E + N + 1, cmp);
	if (M <= N - 1) {
		for (int i = 1; i <= M; i++) ans = (ans + E[i].weight * p[i]) % mod;
		for (int i = M + 1; i <= N - 1; i++) ans = (ans + E[i].weight) % mod;
	} else {
		ll mul = 1;
		rep(i, 1, M - N + 2) mul = (ll) mul * (ll) p[i] % mod;
		ans = (ll) E[1].weight * (ll) mul % mod;
		rep(i, 2, N - 1) ans = (ans + E[i].weight * p[M - N + 1 + i]) % mod;
	}
	cout << ans << endl;
}

int main() {
	ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> T;
    while (T--) {
    	solve();
    }
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, m, n) for(int i = m; i <= n; i++)
#define per(i, m, n) for(int i = m; i >= n; i--)
#define pb push_back
#define DEBUG(x) cout<<x<<endl;
const int INF = 0x3f3f3f3f;

const int mod = 1e9 + 7;
const int maxn = 1e5 + 5;
const int maxm = 6e4 + 5;

int subtree[maxn], p[maxm], N, M, T, cnt;
ll weight[maxn];

vector<int> G[maxn];

void dfs(int u, int k) {
	subtree[u] = 1;
	for (int i = 0; i < (int) G[u].size(); i++) {
		int v = G[u][i];
		if (v == k) continue;
		dfs(v, u);
		subtree[u] += subtree[v];
	}
	if (u != 1) weight[++cnt] = (ll) subtree[u] * (ll) (N - subtree[u]) % mod;
}

void solve() {
	ll ans = 0;
	cin >> N;
	rep(i, 1, N) G[i].clear();
	memset(weight, 0, sizeof(weight));
	cnt = 0;
	rep(i, 1, N - 1) {
		int u, v;
		cin >> u >> v;
		G[u].pb(v), G[v].pb(u);
	}
	dfs(1, -1);
	cin >> M;
	rep(i, 1, M) cin >> p[i];
	sort(p + 1, p + M + 1, greater<int>());
	sort(weight + 1, weight + N, greater<int>());
	if (M <= N - 1) {
		for (int i = 1; i <= M; i++) ans = ((ll) ans + (ll) weight[i] * p[i]) % mod;
		for (int i = M + 1; i <= N - 1; i++) ans = ((ll) ans + (ll) weight[i]) % mod;
	} else {
		ll mul = 1;
		rep(i, 1, M - N + 2) mul = (ll) mul * (ll) p[i] % mod;
		ans = (ll) weight[1] * (ll) mul % mod;
		rep(i, 2, N - 1) ans = ((ll) ans + (ll) weight[i] * p[M - N + 1 + i]) % mod;
	}
	cout << ans << endl;
}

int main() {
	ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> T;
    while (T--) {
    	solve();
    }
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, m, n) for(ll i = m; i <= n; i++)
#define per(i, m, n) for(ll i = m; i >= n; i--)
#define pb push_back
#define DEBUG(x) cout<<x<<endl;
const ll INF = 0x3f3f3f3f;

const ll mod = 1e9 + 7;
const ll maxn = 1e5 + 5;
const ll maxm = 6e4 + 5;

ll subtree[maxn], p[maxm], N, M, T, cnt;
ll weight[maxn];

vector<ll> G[maxn];

void dfs(ll u, ll k) {
	subtree[u] = 1;
	for (ll i = 0; i < (ll) G[u].size(); i++) {
		ll v = G[u][i];
		if (v == k) continue;
		dfs(v, u);
		subtree[u] += subtree[v];
	}
	if (u != 1) weight[++cnt] = (ll) subtree[u] * (ll) (N - subtree[u]) % mod;
}

void solve() {
	ll ans = 0;
	cin >> N;
	rep(i, 1, N) G[i].clear();
	memset(weight, 0, sizeof(weight));
	cnt = 0;
	rep(i, 1, N - 1) {
		ll u, v;
		cin >> u >> v;
		G[u].pb(v), G[v].pb(u);
	}
	dfs(1, -1);
	cin >> M;
	rep(i, 1, M) cin >> p[i];
	sort(p + 1, p + M + 1, greater<ll>());
	sort(weight + 1, weight + N, greater<ll>());
	if (M <= N - 1) {
		for (ll i = 1; i <= M; i++) ans = ((ll) ans + (ll) weight[i] * p[i]) % mod;
		for (ll i = M + 1; i <= N - 1; i++) ans = ((ll) ans + (ll) weight[i]) % mod;
	} else {
		ll mul = 1;
		rep(i, 1, M - N + 2) mul = (ll) mul * (ll) p[i] % mod;
		ans = (ll) weight[1] * (ll) mul % mod;
		rep(i, 2, N - 1) ans = ((ll) ans + (ll) weight[i] * p[M - N + 1 + i]) % mod;
	}
	cout << ans << endl;
}

int main() {
	ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> T;
    while (T--) {
    	solve();
    }
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, m, n) for(ll i = m; i <= n; i++)
#define per(i, m, n) for(ll i = m; i >= n; i--)
#define pb push_back
#define DEBUG(x) cout<<x<<endl;
const ll INF = 0x3f3f3f3f;

const ll mod = 1e9 + 7;
const ll maxn = 1e5 + 5;
const ll maxm = 6e4 + 5;

ll subtree[maxn], p[maxm], N, M, T;
vector<ll> weight;
vector<ll> G[maxn];

void dfs(ll u, ll k) {
	subtree[u] = 1;
	for (ll i = 0; i < (ll) G[u].size(); i++) {
		ll v = G[u][i];
		if (v == k) continue;
		dfs(v, u);
		subtree[u] += subtree[v];
		weight.pb((ll) subtree[v] * (N - subtree[v]));
	}
}

void solve() {
	ll ans = 0;
	cin >> N;
	rep(i, 1, N) G[i].clear();
	weight.clear();
	rep(i, 1, N - 1) {
		ll u, v;
		cin >> u >> v;
		G[u].pb(v), G[v].pb(u);
	}
	dfs(1, -1);
	cin >> M;
	rep(i, 1, M) cin >> p[i];
	sort(p + 1, p + M + 1);
	while (M > N - 1) {
		M--;
		p[M] = (ll) p[M] * p[M + 1] % mod;
	}
	while (M < N - 1) p[++M] = 1;
	sort(p + 1, p + M + 1);
	sort(weight.begin(), weight.end());
	per(i, (int) weight.size(), 1) ans = (ans + (ll) (weight[i - 1] * p[i])) % mod;
	cout << ans << endl;
}

int main() {
	ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> T;
    while (T--) {
    	solve();
    }
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, m, n) for(int i = m; i <= n; i++)
#define per(i, m, n) for(int i = m; i >= n; i--)
#define pb push_back
#define DEBUG(x) cout<<x<<endl;
const int INF = 0x3f3f3f3f;

const int mod = 1e9 + 7;
const int maxn = 1e5 + 5;
const int maxm = 6e4 + 5;

int subtree[maxn], p[maxm], N, M, T, cnt;
ll weight[maxn];

vector<int> G[maxn];

void dfs(int u, int k) {
	subtree[u] = 1;
	for (int i = 0; i < (int) G[u].size(); i++) {
		int v = G[u][i];
		if (v == k) continue;
		dfs(v, u);
		subtree[u] += subtree[v];
	}
	if (u != 1) weight[++cnt] = (ll) subtree[u] * (ll) (N - subtree[u]) % mod;
}

void solve() {
	ll ans = 0;
	cin >> N;
	rep(i, 1, N) G[i].clear();
	memset(weight, 0, sizeof(weight));
	cnt = 0;
	rep(i, 1, N - 1) {
		int u, v;
		cin >> u >> v;
		G[u].pb(v), G[v].pb(u);
	}
	dfs(1, -1);
	cin >> M;
	rep(i, 1, M) cin >> p[i];
	sort(p + 1, p + M + 1);
	while (M > N - 1) {
		M--;
		p[M] = (ll) p[M] * p[M + 1] % mod;
	}
	while (M < N - 1) p[++M] = 1;
	sort(p + 1, p + M + 1);
	sort(weight + 1, weight + cnt + 1);
	rep(i, 1, cnt) ans = (ans + (ll) (weight[i] * p[i])) % mod;
	cout << ans << endl;
}

int main() {
	ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> T;
    while (T--) {
    	solve();
    }
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, m, n) for(int i = m; i <= n; i++)
#define per(i, m, n) for(int i = m; i >= n; i--)
#define pb push_back
#define DEBUG(x) cout<<x<<endl;
const int INF = 0x3f3f3f3f;

const int mod = 1e9 + 7;
const int maxn = 1e5 + 5;
const int maxm = 6e4 + 5;

int subtree[maxn], p[maxm], num[maxn], N, M, T, cnt;
ll weight[maxn];

vector<int> G[maxn];

void dfs(int u, int k) {
	// par[u] = k;
	subtree[u] = 1;
	for (int i = 0; i < (int) G[u].size(); i++) {
		int v = G[u][i];
		if (v == k) continue;
		dfs(v, u);
		subtree[u] += subtree[v];
	}
	if (u != 1) weight[++cnt] = subtree[u] * (ll) (N - subtree[u]) % mod;
}

void solve() {
	ll ans = 0;
	cin >> N;
	rep(i, 1, N) G[i].clear();
	memset(weight, 0, sizeof(weight));
	cnt = 0;
	rep(i, 1, N - 1) {
		int u, v;
		cin >> u >> v;
		G[u].pb(v), G[v].pb(u);
	}
	dfs(1, -1);
	cin >> M;
	rep(i, 1, M) cin >> p[i];
	sort(p + 1, p + M + 1, greater<int>());
	sort(weight + 1, weight + N, greater<int>());
	if (M <= N - 1) {
		for (int i = 1; i <= M; i++) ans = (ans + weight[i] * p[i]) % mod;
		for (int i = M + 1; i <= N - 1; i++) ans = (ans + weight[i]) % mod;
	} else {
		ll mul = 1;
		rep(i, 1, M - N + 2) mul = (ll) mul * (ll) p[i] % mod;
		ans = (ll) weight[1] * (ll) mul % mod;
		rep(i, 2, N - 1) ans = (ans + weight[i] * p[M - N + 1 + i]) % mod;
	}
	cout << ans << endl;
}

int main() {
	ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> T;
    while (T--) {
    	solve();
    }
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, m, n) for(ll i = m; i <= n; i++)
#define per(i, m, n) for(ll i = m; i >= n; i--)
#define pb push_back
#define DEBUG(x) cout<<x<<endl;
const ll INF = 0x3f3f3f3f;

const ll mod = 1e9 + 7;
const ll maxn = 1e5 + 5;
const ll maxm = 6e4 + 5;

ll subtree[maxn], p[maxm], N, M, T;
vector<ll> weight;
vector<ll> G[maxn];

void dfs(ll u, ll k) {
	subtree[u] = 1;
	for (ll i = 0; i < (ll) G[u].size(); i++) {
		ll v = G[u][i];
		if (v == k) continue;
		dfs(v, u);
		subtree[u] += subtree[v];
		weight.pb((ll) subtree[v] * (N - subtree[v]) % mod);
	}
}

void solve() {
	ll ans = 0;
	cin >> N;
	rep(i, 1, N) G[i].clear();
	weight.clear();
	rep(i, 1, N - 1) {
		ll u, v;
		cin >> u >> v;
		G[u].pb(v), G[v].pb(u);
	}
	dfs(1, -1);
	cin >> M;
	rep(i, 1, M) cin >> p[i];
	sort(p + 1, p + M + 1);
	while (M > N - 1) {
		M--;
		p[M] = (ll) p[M] * p[M + 1] % mod;
	}
	while (M < N - 1) p[++M] = 1;
	sort(p + 1, p + M + 1);
	sort(weight.begin(), weight.end());
	per(i, (int) weight.size(), 1) ans = (ans + (ll) (weight[i - 1] * p[i])) % mod;
	cout << ans << endl;
}

int main() {
	ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> T;
    while (T--) {
    	solve();
    }
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, m, n) for(int i = m; i <= n; i++)
#define per(i, m, n) for(int i = m; i >= n; i--)
#define pb push_back
#define DEBUG(x) cout<<x<<endl;
const int INF = 0x3f3f3f3f;

const int mod = 1e9 + 7;
const int maxn = 1e5 + 5;
const int maxm = 6e4 + 5;

int subtree[maxn], p[maxm], N, M, T;
vector<ll> weight;
vector<int> G[maxn];

void dfs(int u, int k) {
	subtree[u] = 1;
	for (int i = 0; i < (int) G[u].size(); i++) {
		int v = G[u][i];
		if (v == k) continue;
		dfs(v, u);
		subtree[u] += subtree[v];
		weight.pb((ll) subtree[v] * (N - subtree[v]) % mod);
	}
}

void solve() {
	ll ans = 0;
	cin >> N;
	rep(i, 1, N) G[i].clear();
	weight.clear();
	rep(i, 1, N - 1) {
		int u, v;
		cin >> u >> v;
		G[u].pb(v), G[v].pb(u);
	}
	dfs(1, -1);
	cin >> M;
	rep(i, 1, M) cin >> p[i];
	sort(p + 1, p + M + 1);
	while (M > N - 1) {
		M--;
		p[M] = (ll) p[M] * p[M + 1] % mod;
	}
	while (M < N - 1) p[++M] = 1;
	sort(p + 1, p + M + 1);
	sort(weight.begin(), weight.end());
	rep(i, 1, weight.size()) ans = (ans + (ll) (weight[i - 1] * p[i])) % mod;
	cout << ans << endl;
}

int main() {
	ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> T;
    while (T--) {
    	solve();
    }
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, m, n) for(int i = m; i <= n; i++)
#define per(i, m, n) for(int i = m; i >= n; i--)
#define pb push_back
#define DEBUG(x) cout<<x<<endl;
const int INF = 0x3f3f3f3f;

const int mod = 1e9 + 7;
const int maxn = 1e5 + 5;
const int maxm = 6e4 + 5;

int subtree[maxn], p[maxm], N, M, T, cnt;
ll weight[maxn];

vector<int> G[maxn];

void dfs(int u, int k) {
	// par[u] = k;
	subtree[u] = 1;
	for (int i = 0; i < (int) G[u].size(); i++) {
		int v = G[u][i];
		if (v == k) continue;
		dfs(v, u);
		subtree[u] += subtree[v];
	}
	if (u != 1) weight[++cnt] = subtree[u] * (ll) (N - subtree[u]) % mod;
}

void solve() {
	ll ans = 0;
	cin >> N;
	rep(i, 1, N) G[i].clear();
	memset(weight, 0, sizeof(weight));
	cnt = 0;
	rep(i, 1, N - 1) {
		int u, v;
		cin >> u >> v;
		G[u].pb(v), G[v].pb(u);
	}
	dfs(1, -1);
	cin >> M;
	rep(i, 1, M) cin >> p[i];
	sort(p + 1, p + M + 1, greater<int>());
	sort(weight + 1, weight + N, greater<int>());
	if (M <= N - 1) {
		for (int i = 1; i <= M; i++) ans = ((ll) ans + (ll) weight[i] * p[i]) % mod;
		for (int i = M + 1; i <= N - 1; i++) ans = ((ll) ans + (ll) weight[i]) % mod;
	} else {
		ll mul = 1;
		rep(i, 1, M - N + 2) mul = (ll) mul * (ll) p[i] % mod;
		ans = (ll) weight[1] * (ll) mul % mod;
		rep(i, 2, N - 1) ans = ((ll) ans + (ll) weight[i] * p[M - N + 1 + i]) % mod;
	}
	cout << ans << endl;
}

int main() {
	ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> T;
    while (T--) {
    	solve();
    }
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, m, n) for(int i = m; i <= n; i++)
#define per(i, m, n) for(int i = m; i >= n; i--)
#define pb push_back
#define DEBUG(x) cout<<x<<endl;
const int INF = 0x3f3f3f3f;

const int mod = 1e9 + 7;
const int maxn = 1e5 + 5;
const int maxm = 6e4 + 5;

int subtree[maxn], p[maxm], N, M, T, cnt;
ll weight[maxn];

vector<int> G[maxn];

void dfs(int u, int k) {
	subtree[u] = 1;
	for (int i = 0; i < (int) G[u].size(); i++) {
		int v = G[u][i];
		if (v == k) continue;
		dfs(v, u);
		subtree[u] += subtree[v];
	}
	if (u != 1) weight[++cnt] = (ll) subtree[u] * (ll) (N - subtree[u]) % mod;
}

void solve() {
	ll ans = 0;
	cin >> N;
	rep(i, 1, N) G[i].clear();
	memset(weight, 0, sizeof(weight));
	cnt = 0;
	rep(i, 1, N - 1) {
		int u, v;
		cin >> u >> v;
		G[u].pb(v), G[v].pb(u);
	}
	dfs(1, -1);
	cin >> M;
	rep(i, 1, M) cin >> p[i];
	sort(p + 1, p + M + 1, greater<int>());
	sort(weight + 1, weight + N, greater<ll>());
	if (M <= N - 1) {
		for (int i = 1; i <= M; i++) ans = ((ll) ans + (ll) weight[i] * p[i]) % mod;
		for (int i = M + 1; i <= N - 1; i++) ans = ((ll) ans + (ll) weight[i]) % mod;
	} else {
		ll mul = 1;
		rep(i, 1, M - N + 2) mul = (ll) mul * (ll) p[i] % mod;
		ans = (ll) weight[1] * (ll) mul % mod;
		rep(i, 2, N - 1) ans = ((ll) ans + (ll) weight[i] * p[M - N + 1 + i]) % mod;
	}
	cout << ans << endl;
}

int main() {
	ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> T;
    while (T--) {
    	solve();
    }
	return 0;
}


#include<cstdio>
#include<iostream>
#include<cstring>
#include <map>
#include <queue>
#include <set>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <bitset>
#include <array>
#include <cctype>
#include <time.h>

//#pragma GCC optimize(2)

void read_f() { freopen("1.in", "r", stdin); freopen("1.out", "w", stdout); }
void fast_cin() { std::ios::sync_with_stdio(false); std::cin.tie(); }
void run_time() { std::cout << "ESC in : " << clock() * 1000.0 / CLOCKS_PER_SEC << "ms" << std::endl; }

#define ll long long
#define ull unsigned ll
#define _min(x, y) ((x)>(y)?(y):(x))
#define _max(x, y) ((x)>(y)?(x):(y))
#define max3(x, y, z) ( max( (x), max( (y), (z) ) ) )
#define min3(x, y, z) ( min( (x), min( (y), (z) ) ) )
#define pr(x, y) (make_pair((x), (y)))
#define pb(x) push_back(x);
using namespace std;

const int N = 2e6+5;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;

int he[N], ver[N], ne[N], tot;
void add(int x, int y)
{
    ver[++tot] = y;
    ne[tot]  = he[x];
    he[x] = tot;
}
int sz[N], cnt; ll sum[N], val[N];
ll su[N];
int n;
void dfs(int cur, int fa)
{
    sz[cur] = 1;
    for (int i = he[cur]; i; i = ne[i])
    {
        int y = ver[i];
        if(y == fa) continue;
        dfs(y, cur);
        sz[cur] += sz[y];
    }
    if (cur != 1) sum[++cnt] = 1ll * sz[cur] * (n - sz[cur]);
}
void init(int n)
{
    for (int i = 0; i <= n; i++)
        he[i] = 0;
    tot = 0; cnt = 0;
}
int main()
{
    int t; cin >> t;
    while(t--)
    {
        scanf("%d", &n);
        init(n);
        for (int i = 1; i < n; i++)
        {
            int x, y; scanf("%d%d", &x, &y);
            add(x, y); add(y, x);
        }
        dfs(1, 0);
        int m; scanf("%d", &m);
        for (int i = 1; i <= m; i++)
            scanf("%lld", &su[i]);
        sort(su+1, su+m+1);
        sort(sum+1, sum+n);
        if (m <= n - 1)
        {
            for (int j = n - 1, cnt = m; j >= 1 && cnt >= 1; j--, cnt--)
                sum[j] = (sum[j] * su[cnt]) % mod;
        }
        else
        {
            for (int i = 1; i < n - 1; i++)
                sum[i] = sum[i] * su[i] % mod;
            for (int i = n - 1; i <= m; i++)
                sum[n-1] = (sum[n-1] * su[i]) % mod;
        }
        ll ans = 0;
        for (int i = 1; i < n; i++)
            ans = (ans + sum[i] % mod) % mod;
        printf("%lld\n", ans);
    }
    return 0;
}


#include<cstdio>
#include<iostream>
#include<cstring>
#include <map>
#include <queue>
#include <set>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <bitset>
#include <array>
#include <cctype>
#include <time.h>

//#pragma GCC optimize(2)

void read_f() { freopen("1.in", "r", stdin); freopen("1.out", "w", stdout); }
void fast_cin() { std::ios::sync_with_stdio(false); std::cin.tie(); }
void run_time() { std::cout << "ESC in : " << clock() * 1000.0 / CLOCKS_PER_SEC << "ms" << std::endl; }

#define ll long long
#define ull unsigned ll
#define _min(x, y) ((x)>(y)?(y):(x))
#define _max(x, y) ((x)>(y)?(x):(y))
#define max3(x, y, z) ( max( (x), max( (y), (z) ) ) )
#define min3(x, y, z) ( min( (x), min( (y), (z) ) ) )
#define pr(x, y) (make_pair((x), (y)))
#define pb(x) push_back(x);
using namespace std;

const int N = 2e6+5;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;

int he[N], ver[N], ne[N], tot;
void add(int x, int y)
{
    ver[++tot] = y;
    ne[tot]  = he[x];
    he[x] = tot;
}
int sz[N], cnt; ll sum[N], val[N];
ll su[N];
int n;
void dfs(int cur, int fa)
{
    sz[cur] = 1;
    for (int i = he[cur]; i; i = ne[i])
    {
        int y = ver[i];
        if(y == fa) continue;
        dfs(y, cur);
        sz[cur] += sz[y];
        sum[++cnt] = 1ll * sz[y] * (n - sz[y]);
    }
}
void init(int n)
{
    for (int i = 0; i <= n; i++)
        he[i] = 0;
    tot = 0; cnt = 0;
}
int main()
{
    int t; cin >> t;
    while(t--)
    {
        scanf("%d", &n);
        init(n);
        for (int i = 1; i < n; i++)
        {
            int x, y; scanf("%d%d", &x, &y);
            add(x, y); add(y, x);
        }
        dfs(1, 0);
        int m; scanf("%d", &m);
        for (int i = 1; i <= m; i++)
            scanf("%lld", &su[i]);
        sort(su+1, su+m+1);
        sort(sum+1, sum+n);

        if (m < n - 1)
        {
            for (int j = n - 1, cnt = m; j >= 1; j--, cnt--)
                val[j] = cnt > 0 ? su[cnt] : 1;
        }
        else
        {
            for (int i = 1; i < n - 1; i++)
                val[i] = su[i];
            ll sum = 0;
            for (int i = n - 1; i <= m; i++)
                sum += su[i];
            val[n-1] = sum;
        }
        ll ans = 0;

        for (int i = 1; i < n; i++)
            ans = (ans + val[i] * sum[i] % mod) % mod;
        printf("%lld\n", ans);
    }
    return 0;
}


#include<cstdio>
#include<iostream>
#include<cstring>
#include <map>
#include <queue>
#include <set>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <bitset>
#include <array>
#include <cctype>
#include <time.h>

//#pragma GCC optimize(2)

void read_f() { freopen("1.in", "r", stdin); freopen("1.out", "w", stdout); }
void fast_cin() { std::ios::sync_with_stdio(false); std::cin.tie(); }
void run_time() { std::cout << "ESC in : " << clock() * 1000.0 / CLOCKS_PER_SEC << "ms" << std::endl; }

#define ll long long
#define ull unsigned ll
#define _min(x, y) ((x)>(y)?(y):(x))
#define _max(x, y) ((x)>(y)?(x):(y))
#define max3(x, y, z) ( max( (x), max( (y), (z) ) ) )
#define min3(x, y, z) ( min( (x), min( (y), (z) ) ) )
#define pr(x, y) (make_pair((x), (y)))
#define pb(x) push_back(x);
using namespace std;

const int N = 2e6+5;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;

int he[N], ver[N], ne[N], tot;
void add(int x, int y)
{
    ver[++tot] = y;
    ne[tot]  = he[x];
    he[x] = tot;
}
int sz[N], cnt; ll sum[N], val[N];
ll su[N];
int n;
void dfs(int cur, int fa)
{
    sz[cur] = 1;
    for (int i = he[cur]; i; i = ne[i])
    {
        int y = ver[i];
        if(y == fa) continue;
        dfs(y, cur);
        sz[cur] += sz[y];
        sum[++cnt] = 1ll * sz[y] * (n - sz[y]) % mod;
    }
}
void init(int n)
{
    for (int i = 0; i <= n; i++)
        he[i] = 0;
    tot = 0; cnt = 0;
}
int main()
{
    int t; cin >> t;
    while(t--)
    {
        scanf("%d", &n);
        init(n);
        for (int i = 1; i < n; i++)
        {
            int x, y; scanf("%d%d", &x, &y);
            add(x, y); add(y, x);
        }
        dfs(1, 0);
        int m; scanf("%d", &m);
        for (int i = 1; i <= m; i++)
            scanf("%lld", &su[i]);
        sort(su+1, su+m+1);
        sort(sum+1, sum+n);

        if (m <= n - 1)
        {
            for (int j = n - 1, cnt = m; j >= 1; j--, cnt--)
                val[j] = (cnt > 0 ? su[cnt] : 1) % mod;
        }
        else
        {
            for (int i = 1; i < n - 1; i++)
                val[i] = su[i] % mod;
            ll sum = 0;
            for (int i = n - 1; i <= m; i++)
                sum = (sum * su[i]) % mod;
            val[n-1] = sum % mod;
        }

        ll ans = 0;
        for (int i = 1; i < n; i++)
            ans = (ans + val[i] * sum[i] % mod) % mod;
        printf("%lld\n", ans);
    }
    return 0;
}


#include<cstdio>
#include<iostream>
#include<cstring>
#include <map>
#include <queue>
#include <set>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <bitset>
#include <array>
#include <cctype>
#include <time.h>

//#pragma GCC optimize(2)

void read_f() { freopen("1.in", "r", stdin); freopen("1.out", "w", stdout); }
void fast_cin() { std::ios::sync_with_stdio(false); std::cin.tie(); }
void run_time() { std::cout << "ESC in : " << clock() * 1000.0 / CLOCKS_PER_SEC << "ms" << std::endl; }

#define ll long long
#define ull unsigned ll
#define _min(x, y) ((x)>(y)?(y):(x))
#define _max(x, y) ((x)>(y)?(x):(y))
#define max3(x, y, z) ( max( (x), max( (y), (z) ) ) )
#define min3(x, y, z) ( min( (x), min( (y), (z) ) ) )
#define pr(x, y) (make_pair((x), (y)))
#define pb(x) push_back(x);
using namespace std;

const int N = 2e6+5;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;

int he[N], ver[N], ne[N], tot;
void add(int x, int y)
{
    ver[++tot] = y;
    ne[tot]  = he[x];
    he[x] = tot;
}
int sz[N], cnt; ll sum[N], val[N];
ll su[N];
int n;
void dfs(int cur, int fa)
{
    sz[cur] = 1;
    for (int i = he[cur]; i; i = ne[i])
    {
        int y = ver[i];
        if(y == fa) continue;
        dfs(y, cur);
        sz[cur] += sz[y];
        sum[++cnt] = 1ll * sz[y] * (n - sz[y]);
    }
}
void init(int n)
{
    for (int i = 0; i <= n; i++)
        he[i] = 0;
    tot = 0; cnt = 0;
}
int main()
{
    int t; cin >> t;
    while(t--)
    {
        scanf("%d", &n);
        init(n);
        for (int i = 1; i < n; i++)
        {
            int x, y; scanf("%d%d", &x, &y);
            add(x, y); add(y, x);
        }
        dfs(1, 0);
        int m; scanf("%d", &m);
        for (int i = 1; i <= m; i++)
            scanf("%lld", &su[i]);
        sort(su+1, su+m+1);
        sort(sum+1, sum+n);

        if (m < n - 1)
        {
            for (int j = n - 1, cnt = m; j >= 1; j--, cnt--)
                val[j] = cnt > 0 ? su[cnt] : 1;
        }
        else
        {
            for (int i = 1; i < n - 1; i++)
                val[i] = su[i];
            ll sum = 1;
            for (int i = n - 1; i <= m; i++)
                sum = (sum * su[i]) % mod;
            val[n-1] = sum;
        }
        ll ans = 0;

        for (int i = 1; i < n; i++)
            ans = (ans + val[i] * sum[i] % mod) % mod;
        printf("%lld\n", ans);
    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define vi vector<int>
#define vvi vector<vector<int>>
#define vll vector<long long>
#define vs vector<string>
#define vc vector<char>
#define vb vector<bool>
#define forn(i, s, n)  for(ll i=(ll)s; i<(ll)(n); i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define pll pair<long long int, long long int>
#define pii pair<int, int>
#define pss pair<string, string>
#define ull unsigned long long int
#define lld long double
#define F first
#define S second
#define PI 3.141592653589793238
#define prec(n) fixed<<setprecision(n)
#define ordered_set tree<ll, null_type,less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define itsval(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); itval(_it, args); }
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;

void itval(istream_iterator<string> it) {}
template<typename T, typename... Args>
void itval(istream_iterator<string> it, T a, Args... args) {
    cerr << *it << " = " << a << endl;
    itval(++it, args...);
}
const ll MOD = 998244353;
template <typename T>
inline void print(T x) {cout << x << "\n";}
template <typename T>
inline void printvec(T x) {for (auto a : x) cout << a << ' '; cout << '\n';}
// -----------------------------------------------------------------------

struct custom {
    bool operator()(const pair<pll, ll> &p1, const pair<pll, ll> &p2)const {
        if (p1.F.F == p2.F.F)
        {
            return p1.S < p2.S;
        }
        return p1.F.F < p2.F.F;
    }
};

// Calculate a^b % MOD -------------------------------------------------
ll get_pow(ll a, ll b) {
    ll res = 1;
    while (b) {
        if (b & 1) res = (res * a) % MOD;
        a = (a * a) % MOD;
        b >>= 1;
    }
    return res;
}
// ---------------------------------------------------------------------
const ll N = 1e5 + 10, inf = 4e18;
ll n, k, P;
vll v, keys;
bool check(ll x)
{
    int up = 0, d = 0;
    while (d < k && up < n)
    {
        while (d < k && abs(keys[d] - v[up]) + abs(P - keys[d]) > x)
            d++;
        if (d >= k)
            return false;
        d++;
        up++;
    }
    if (up < n)
        return false;
    return true;
}
void solve()
{
    cin >> n >> k >> P;
    v.resize(n); keys.resize(k);
    forn(i, 0, n)
    cin >> v[i];
    forn(i, 0, k)
    cin >> keys[i];
    sort(all(v));
    sort(all(keys));
    ll lo = -1, hi = inf;
    while (lo + 1 < hi)
    {
        ll mid = (lo + hi) / 2;
        if (check(mid))
        {
            hi = mid;
        }
        else
        {
            lo = mid;
        }
    }
    cout << hi;
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int test = 1;
    //cin >> test;

    clock_t z = clock();
    forn(tes, 0, test)
    {
        solve();
    }
    debug("Total Time:%.4f\n", (double)(clock() - z) / CLOCKS_PER_SEC);
    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define vi vector<int>
#define vvi vector<vector<int>>
#define vll vector<long long>
#define vs vector<string>
#define vc vector<char>
#define vb vector<bool>
#define forn(i, s, n)  for(ll i=(ll)s; i<(ll)(n); i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define pll pair<long long int, long long int>
#define pii pair<int, int>
#define pss pair<string, string>
#define ull unsigned long long int
#define lld long double
#define F first
#define S second
#define PI 3.141592653589793238
#define prec(n) fixed<<setprecision(n)
#define ordered_set tree<ll, null_type,less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define itsval(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); itval(_it, args); }
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;

void itval(istream_iterator<string> it) {}
template<typename T, typename... Args>
void itval(istream_iterator<string> it, T a, Args... args) {
    cerr << *it << " = " << a << endl;
    itval(++it, args...);
}
const ll MOD = 998244353;
template <typename T>
inline void print(T x) {cout << x << "\n";}
template <typename T>
inline void printvec(T x) {for (auto a : x) cout << a << ' '; cout << '\n';}
// -----------------------------------------------------------------------

struct custom {
    bool operator()(const pair<pll, ll> &p1, const pair<pll, ll> &p2)const {
        if (p1.F.F == p2.F.F)
        {
            return p1.S < p2.S;
        }
        return p1.F.F < p2.F.F;
    }
};

// Calculate a^b % MOD -------------------------------------------------
ll get_pow(ll a, ll b) {
    ll res = 1;
    while (b) {
        if (b & 1) res = (res * a) % MOD;
        a = (a * a) % MOD;
        b >>= 1;
    }
    return res;
}
// ---------------------------------------------------------------------
const ll N = 1e5 + 10, inf = 1e17;
ll n, k, P;
vll v, keys;
ll dp[1002][2002];
ll calc(int i, int j)
{
    if (j >= k)
        return inf;
    if (i >= n)
        return 0;
    ll &res = dp[i][j];
    if (res != -1)
        return res;
    res = min(calc(i, j + 1), max(calc(i + 1, j + 1) , abs(v[i] - keys[j]) + abs(keys[j] - P)));
    return res;
}
void solve()
{
    cin >> n >> k >> P;
    v.resize(n); keys.resize(k);
    forn(i, 0, n)
    cin >> v[i];
    forn(i, 0, k)
    cin >> keys[i];
    sort(all(v));
    sort(all(keys));
    forn(i, 0, 1002)
    forn(j, 0, 2002)
    dp[i][j] = -1;
    cout << calc(0, 0);
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int test = 1;
    //cin >> test;

    clock_t z = clock();
    forn(tes, 0, test)
    {
        solve();
    }
    debug("Total Time:%.4f\n", (double)(clock() - z) / CLOCKS_PER_SEC);
    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define vi vector<int>
#define vvi vector<vector<int>>
#define vll vector<long long>
#define vs vector<string>
#define vc vector<char>
#define vb vector<bool>
#define forn(i, s, n)  for(ll i=(ll)s; i<(ll)(n); i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define pll pair<long long int, long long int>
#define pii pair<int, int>
#define pss pair<string, string>
#define ull unsigned long long int
#define lld long double
#define F first
#define S second
#define PI 3.141592653589793238
#define prec(n) fixed<<setprecision(n)
#define ordered_set tree<ll, null_type,less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define itsval(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); itval(_it, args); }
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;

void itval(istream_iterator<string> it) {}
template<typename T, typename... Args>
void itval(istream_iterator<string> it, T a, Args... args) {
    cerr << *it << " = " << a << endl;
    itval(++it, args...);
}
const ll MOD = 998244353;
template <typename T>
inline void print(T x) {cout << x << "\n";}
template <typename T>
inline void printvec(T x) {for (auto a : x) cout << a << ' '; cout << '\n';}
// -----------------------------------------------------------------------

struct custom {
    bool operator()(const pair<pll, ll> &p1, const pair<pll, ll> &p2)const {
        if (p1.F.F == p2.F.F)
        {
            return p1.S < p2.S;
        }
        return p1.F.F < p2.F.F;
    }
};

// Calculate a^b % MOD -------------------------------------------------
ll get_pow(ll a, ll b) {
    ll res = 1;
    while (b) {
        if (b & 1) res = (res * a) % MOD;
        a = (a * a) % MOD;
        b >>= 1;
    }
    return res;
}
// ---------------------------------------------------------------------
const ll N = 1e5 + 10, inf = 4e18;
ll n, k, P;
vll v, keys;
ll dp[1002][2003];
void solve()
{
    cin >> n >> k >> P;
    v.resize(n + 1); keys.resize(k + 1);
    forn(i, 1, n + 1)
    cin >> v[i];
    forn(i, 1, k + 1)
    cin >> keys[i];
    sort(v.begin() + 1, v.end());
    sort(keys.begin() + 1, keys.end());
    forn(i, 1, n + 1)
    {
        dp[i][0] = inf;
        forn(j, 1, k + 1)
        {
            dp[i][j] = max(abs(v[i] - keys[j]) + abs(P - keys[j]), dp[i - 1][j - 1]);
            dp[i][j] = min(dp[i][j], dp[i][j - 1]);
        }
    }
    cout << dp[n][k];

}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int test = 1;
    //cin >> test;

    clock_t z = clock();
    forn(tes, 0, test)
    {
        solve();
    }
    debug("Total Time:%.4f\n", (double)(clock() - z) / CLOCKS_PER_SEC);
    return 0;
}


/* You can't connect the dots looking forward
   you can only connect them looking backwards. */

/* Nothing is impossible; impossible itself says "I'm possible" */

#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define boost ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cout.precision(10);cout << fixed;
#define dbset(x) for(int i=0 ; i<x.size(); i++) cerr << x[i] << " "; cerr << endl;
#define inf 1000000007
#define INF 1000000000000000000LL
#define PI 3.14159265358979323846
#define mod 1000000007
#define mod1 1000696969
#define flusz fflush(stdout);
#define VI vector<int>
#define VPI vector < pair<int,int> >
#define PII pair<int, int>
#define BIT bitset<N>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define endl '\n'
#define REP(x, y) for(int x = 0; x < (y); ++x)
#define FOR(x, y, z) for(int x = y; x <= (z); ++x)
#define FORR(x, y, z) for(int x = y; x >= (z); --x)
using namespace std;

template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";}
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<","; _dbg(sdbg+1, a...);
}
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)

#define N 4007

int test;

int n,m,k,s,t;

int t1[N],G[N];

int lvl[N];

queue<int>q;

VI g[N];

int cap[N][N];

bool bfs(){
    q.push(s);
    FOR(i,s,t){
        lvl[i] = -1;
    }
    lvl[s]=0;

    while(!q.empty()){
        auto v = q.front();
        q.pop();

        for(auto it:g[v]){
            if ( lvl[it] == -1 && cap[v][it] > 0 ){
                lvl[it] = lvl[v] + 1;
                q.push(it);
            }
        }
    }

    return lvl[t]!=-1;
}

int dfs(int v,int val){
    if (v == t || val == 0){
        return val;
    }

    int res = 0;

    for(auto it:g[v]){
        if ( lvl[it] == lvl[v] + 1 && cap[v][it] > 0 ){
            int kek = dfs( it, min(val, cap[v][it] ) );

            res += kek;
            val -= kek;

            cap[v][it] -= kek;
            cap[it][v] += kek;

            if (val == 0){
                return res;
            }
        }
    }

    return res;
}

int dinic(){
    int max_flow = 0;

    while(bfs()){
        max_flow += dfs(s,inf);
    }

    return max_flow;
}

bool check(int mid){
    bool czy=false;

    FOR(i,1,n){
        FOR(j,1,m){
            if ( abs(t1[i]-G[j]) + abs(G[j]-k) <= mid ){
                cap[i][j+n]=1;
                g[i].pb(j+n);
                g[j+n].pb(i);
            }
        }

        if (g[i].size()==0){
            return false;
        }
        if (g[i].size()<n){
            czy=true;
        }
    }

    if (!czy){
        return true;
    }

    FOR(i,s,t){
        FOR(j,s,t){
            cap[i][j] = 0;
        }
        g[i].clear();
    }

    FOR(i,1,n){
        g[s].pb(i);
        g[i].pb(s);
        cap[s][i] = 1;
    }

    FOR(i,1,m){
        g[i+n].pb(t);
        g[t].pb(i+n);
        cap[i+n][t]=1;
    }

    if (dinic()==n){
        return true;
    }else{
        return false;
    }
}

void solve()
{
    cin >> n >> m >> k;

    FOR(i,1,n){
        cin >> t1[i];
    }

    FOR(i,1,m){
        cin >> G[i];
    }

    s = 0,t = n+m+1;

    int l = 0,r = 2*inf;

    while(l!=r){
        int mid = (l+r)/2;

        if (check(mid)){
            r = mid;
        }else{
            l = mid+1;
        }
    }

    cout << l;

    return;
}

int32_t main()
{
    boost

    test=1;

    while(test--)
    {
        solve();
    }

  return 0;
}


/* You can't connect the dots looking forward
   you can only connect them looking backwards. */

/* Nothing is impossible; impossible itself says "I'm possible" */

#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define boost ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cout.precision(10);cout << fixed;
#define dbset(x) for(int i=0 ; i<x.size(); i++) cerr << x[i] << " "; cerr << endl;
#define inf 1000000007
#define INF 1000000000000000000LL
#define PI 3.14159265358979323846
#define mod 1000000007
#define mod1 1000696969
#define flusz fflush(stdout);
#define VI vector<int>
#define VPI vector < pair<int,int> >
#define PII pair<int, int>
#define BIT bitset<N>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define endl '\n'
#define REP(x, y) for(int x = 0; x < (y); ++x)
#define FOR(x, y, z) for(int x = y; x <= (z); ++x)
#define FORR(x, y, z) for(int x = y; x >= (z); --x)
using namespace std;

template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";}
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<","; _dbg(sdbg+1, a...);
}
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define N 500007

int test;

int n,k,p;

int t[N],g[N];

void solve()
{
    cin >> n >> k >> p;

    REP(i,n){
        cin >> t[i];
    }

    FOR(i,1,k){
        cin >> g[i];
    }

    sort(t,t+n);
    sort(g+1,g+1+k);

    int res = 2*inf;

    FOR(i,1,k-n+1){
        int wyn = 0;

        REP(j,n){
            wyn = max( wyn, abs(t[j]-g[i+j]) + abs(g[i+j]-p) );
        }

        res = min(wyn,res);
    }

    cout << res;

    return;
}

int32_t main()
{
    boost

    test=1;

    while(test--)
    {
        solve();
    }

  return 0;
}


/* You can't connect the dots looking forward
   you can only connect them looking backwards. */

/* Nothing is impossible; impossible itself says "I'm possible" */

#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define boost ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cout.precision(10);cout << fixed;
#define dbset(x) for(int i=0 ; i<x.size(); i++) cerr << x[i] << " "; cerr << endl;
#define inf 1000000007
#define INF 1000000000000000000LL
#define PI 3.14159265358979323846
#define mod 1000000007
#define mod1 1000696969
#define flusz fflush(stdout);
#define VI vector<int>
#define VPI vector < pair<int,int> >
#define PII pair<int, int>
#define BIT bitset<N>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define endl '\n'
#define REP(x, y) for(int x = 0; x < (y); ++x)
#define FOR(x, y, z) for(int x = y; x <= (z); ++x)
#define FORR(x, y, z) for(int x = y; x >= (z); --x)
using namespace std;

template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";}
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<","; _dbg(sdbg+1, a...);
}
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define N 500007

int test;

int n,k,p;

int t[N],g[N];

void solve()
{
    cin >> n >> k >> p;

    REP(i,n){
        cin >> t[i];
    }

    FOR(i,1,k){
        cin >> g[i];
    }

    sort(t+1,t+1+n);
    sort(g+1,g+1+k);

    int res = 2*inf;

    FOR(i,1,k-n+1){
        int wyn = 0;

        REP(j,n){
            wyn = max( wyn, abs(t[j]-g[i+j]) + abs(g[i+j]-p) );
        }

        res = min(wyn,res);
    }

    cout << res;

    return;
}

int32_t main()
{
    boost

    test=1;

    while(test--)
    {
        solve();
    }

  return 0;
}


/* You can't connect the dots looking forward
   you can only connect them looking backwards. */

/* Nothing is impossible; impossible itself says "I'm possible" */

#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define boost ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cout.precision(10);cout << fixed;
#define dbset(x) for(int i=0 ; i<x.size(); i++) cerr << x[i] << " "; cerr << endl;
#define inf 1000000007
#define INF 1000000000000000000LL
#define PI 3.14159265358979323846
#define mod 1000000007
#define mod1 1000696969
#define flusz fflush(stdout);
#define VI vector<int>
#define VPI vector < pair<int,int> >
#define PII pair<int, int>
#define BIT bitset<N>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define endl '\n'
#define REP(x, y) for(int x = 0; x < (y); ++x)
#define FOR(x, y, z) for(int x = y; x <= (z); ++x)
#define FORR(x, y, z) for(int x = y; x >= (z); --x)
using namespace std;

template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";}
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<","; _dbg(sdbg+1, a...);
}
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)

#define N 4007

int test;

int n,m,k,s,t;

int t1[N],G[N];

int lvl[N];

queue<int>q;

VI g[N];

int cap[N][N];

bool bfs(){
    q.push(s);
    FOR(i,s,t){
        lvl[i] = -1;
    }
    lvl[s]=0;

    while(!q.empty()){
        auto v = q.front();
        q.pop();

        for(auto it:g[v]){
            if ( lvl[it] == -1 && cap[v][it] > 0 ){
                lvl[it] = lvl[v] + 1;
                q.push(it);
            }
        }
    }

    return lvl[t]!=-1;
}

int dfs(int v,int val){
    if (v == t || val == 0){
        return val;
    }

    int res = 0;

    for(auto it:g[v]){
        if ( lvl[it] == lvl[v] + 1 && cap[v][it] > 0 ){
            int kek = dfs( it, min(val, cap[v][it] ) );

            res += kek;
            val -= kek;

            cap[v][it] -= kek;
            cap[it][v] += kek;

            if (val == 0){
                return res;
            }
        }
    }

    return res;
}

int dinic(){
    int max_flow = 0;

    while(bfs()){
        max_flow += dfs(s,inf);
    }

    return max_flow;
}

bool check(int mid){
    bool czy=false;

    FOR(i,s,t){
        FOR(j,s,t){
            cap[i][j] = 0;
        }
        g[i].clear();
    }

    FOR(i,1,n){
        FOR(j,1,m){
            if ( abs(t1[i]-G[j]) + abs(G[j]-k) <= mid ){
                cap[i][j+n]=1;
                g[i].pb(j+n);
                g[j+n].pb(i);
            }
        }

        if (g[i].size()==0){
            return false;
        }
        if (g[i].size()<n){
            czy=true;
        }
    }

    if (!czy){
        return true;
    }

    FOR(i,1,n){
        g[s].pb(i);
        g[i].pb(s);
        cap[s][i] = 1;
    }

    FOR(i,1,m){
        g[i+n].pb(t);
        g[t].pb(i+n);
        cap[i+n][t]=1;
    }

    if (dinic()==n){
        return true;
    }else{
        return false;
    }
}

void solve()
{
    cin >> n >> m >> k;

    FOR(i,1,n){
        cin >> t1[i];
    }

    FOR(i,1,m){
        cin >> G[i];
    }

    s = 0,t = n+m+1;

    int l = 0,r = 2*inf;

    while(l!=r){
        int mid = (l+r)/2;

        if (check(mid)){
            r = mid;
        }else{
            l = mid+1;
        }
    }

    cout << l;

    return;
}

int32_t main()
{
    boost

    test=1;

    while(test--)
    {
        solve();
    }

  return 0;
}


/* You can't connect the dots looking forward
   you can only connect them looking backwards. */

/* Nothing is impossible; impossible itself says "I'm possible" */

#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define boost ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cout.precision(10);cout << fixed;
#define dbset(x) for(int i=0 ; i<x.size(); i++) cerr << x[i] << " "; cerr << endl;
#define inf 1000000007
#define INF 1000000000000000000LL
#define PI 3.14159265358979323846
#define mod 1000000007
#define mod1 1000696969
#define flusz fflush(stdout);
#define VI vector<int>
#define VPI vector < pair<int,int> >
#define PII pair<int, int>
#define BIT bitset<N>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define endl '\n'
#define REP(x, y) for(int x = 0; x < (y); ++x)
#define FOR(x, y, z) for(int x = y; x <= (z); ++x)
#define FORR(x, y, z) for(int x = y; x >= (z); --x)
using namespace std;

template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";}
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<","; _dbg(sdbg+1, a...);
}
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)

#define N 4007

int test;

int n,m,k,s,t;

int t1[N],G[N];

int lvl[N];

queue<int>q;

VI g[N];

int cap[N][N];

bool bfs(){
    q.push(s);
    FOR(i,s,t){
        lvl[i] = -1;
    }
    lvl[s]=0;

    while(!q.empty()){
        auto v = q.front();
        q.pop();

        for(auto it:g[v]){
            if ( lvl[it] == -1 && cap[v][it] > 0 ){
                lvl[it] = lvl[v] + 1;
                q.push(it);
            }
        }
    }

    return lvl[t]!=-1;
}

int dfs(int v,int val){
    if (v == t || val == 0){
        return val;
    }

    int res = 0;

    for(auto it:g[v]){
        if ( lvl[it] == lvl[v] + 1 && cap[v][it] > 0 ){
            int kek = dfs( it, min(val, cap[v][it] ) );

            res += kek;
            val -= kek;

            cap[v][it] -= kek;
            cap[it][v] += kek;

            if (val == 0){
                return res;
            }
        }
    }

    return res;
}

int dinic(){
    int max_flow = 0;

    while(bfs()){
        max_flow += dfs(s,inf);
    }

    return max_flow;
}

bool check(int mid){
    FOR(i,s,t){
        FOR(j,s,t){
            cap[i][j] = 0;
        }
        g[i].clear();
    }

    FOR(i,1,n){
        g[s].pb(i);
        g[i].pb(s);
        cap[s][i] = 1;
    }

    FOR(i,1,m){
        g[i+n].pb(t);
        g[t].pb(i+n);
        cap[i+n][t]=1;
    }

    FOR(i,1,n){
        FOR(j,1,m){
            if ( abs(t1[i]-G[j]) + abs(G[j]-k) <= mid ){
                cap[i][j+n]=1;
                g[i].pb(j+n);
                g[j+n].pb(i);
            }
        }
    }

    if (dinic()==n){
        return true;
    }else{
        return false;
    }
}

void solve()
{
    cin >> n >> m >> k;

    FOR(i,1,n){
        cin >> t1[i];
    }

    FOR(i,1,m){
        cin >> G[i];
    }

    s = 0,t = n+m+1;

    int l = 0,r = 2*inf;

    while(l!=r){
        int mid = (l+r)/2;

        if (check(mid)){
            r = mid;
        }else{
            l = mid+1;
        }
    }

    cout << l;

    return;
}

int32_t main()
{
    boost

    test=1;

    while(test--)
    {
        solve();
    }

  return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

int main() {
	long long people[1000],keys[2000],p,k,n;
	cin>>n>>k>>p;
	long long ans;
	bool flag;
	for(long long i=0;i<n;i++)
		cin>>people[i];
	for(long long j=0;j<k;j++)
		cin>>keys[j];
	sort(people,people+n);
	long long ansmax=0;
	bool markey[2000];
	sort(keys,keys+k);
	long long ansi;
	for(int i=0;i<n;i++)
	{
		ans=100000000000000;
		flag=false;
		for(long long o=0;o<k;o++)
		{
			if(markey[o]==false)
			{
				if(keys[o]<p && keys[o]>people[i])
				{
					ans=abs(p-people[i]);
					ansi=o;
					flag=true;
					break;
				}
				if(abs(keys[o]-people[i])+abs(keys[o]-p)<ans)
				{
					ans=abs(keys[o]-people[i])+abs(keys[o]-p);
					ansi=o;
				}
			}
			if(flag==true)
				break;
		}
		markey[ansi]=true;
		if(ans>ansmax)
			ansmax=ans;
	}
	cout<<ansmax;
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

int main() {
	long long people[1000],keys[2000],p,k,n;
	cin>>n>>k>>p;
	long long ans;
	for(int i=0;i<n;i++)
		cin>>people[i];
	for(int j=0;j<k;j++)
		cin>>keys[j];
	long long min=10000000000;
	long long mini;
	long long max=0;
	for(int i=0;i<n;i++)
	{
		min=10000000000;
		for(int j=0;j<k;j++)
			if(abs(p-keys[j])+abs(keys[j]-people[i])<min)
			{
				min=abs(p-keys[j])+abs(keys[j]-people[i]);
				mini=j;
			}
		if(min>max)
			max=min;
	}
	cout<<max;

	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
long long people[1001],keys[2001],p,k,n;
bool markey[2001];
const long long MAXMIN=1e10;
long long mind;
long long maxd,maxdi;
	long long ptokey[1001];
	long long minjd;
void maxdv()
{
	maxd=0;
	for(long long  i=0;i<n;i++)
	{
		if(abs(keys[ptokey[i]]-people[i])+abs(keys[ptokey[i]]-p)>maxd)
		{
			maxd=abs(keys[ptokey[i]]-people[i])+abs(keys[ptokey[i]]-p);
			maxdi=i;
		}
	}
}
int main() {

	cin>>n>>k>>p;
	for(long long i=0;i<n;i++)
		cin>>people[i];
	for(long long j=0;j<k;j++)
	{
		cin>>keys[j];
		markey[j]=false;
	}
	sort(people,people+n);
	sort(keys,keys+k);
	for(long long i=0;i<n;i++)
	{
		mind=MAXMIN;
		for(long long j=0;j<k;j++)
		{
			if(markey[j]==false)
			{
				if(abs(keys[j]-people[i])+abs(keys[j]-p)<mind)
				{
					minjd=j;
					mind=abs(keys[j]-people[i])+abs(keys[j]-p);
				}
			}
		}
		markey[minjd]=true;
		ptokey[i]=minjd;
	}
	long long flagi;

	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++)
			if(i!=j)
			{
				if(i>j && keys[ptokey[i]]<keys[ptokey[j]])
					swap(ptokey[i],ptokey[j]);
				if(i<j && keys[ptokey[i]]>keys[ptokey[j]])
					swap(ptokey[i],ptokey[j]);
			}
	maxdv();
	long long permaxd=maxd+1;
	while(maxd<permaxd)
	{
		if(keys[ptokey[maxdi]]>p)
		{
			flagi=-1;
			for(int i=maxdi;i>=0;i--)
			{
				if(ptokey[i]>0 && markey[ptokey[i]-1]==false)
				{
					flagi=i;
					break;
				}
			}
			if(flagi!=-1)
			{
				for(int i=flagi;i<=maxdi;i++)
				{
					markey[ptokey[i]]=false;
					ptokey[i]--;
					markey[ptokey[i]]=true;
				}
			}
		}
		else if(keys[ptokey[maxdi]]<p)
		{
			flagi=-1;
			for(int i=maxdi;i<n;i++)
			{
				if(ptokey[i]<k-1 && markey[ptokey[i]+1]==false)
				{
					flagi=i;
					break;
				}
			}
			if(flagi!=-1)
			{
				for(int i=flagi;i>=maxdi;i--)
				{
					markey[ptokey[i]]=false;
					ptokey[i]++;
					markey[ptokey[i]]=true;
				}
			}
		}
		permaxd=maxd;
		maxdv();
	}
	cout<<permaxd;
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

int main() {
	long long people[1000],keys[2000],p,k,n;
	cin>>n>>k>>p;
	long long ans;
	bool flag;
	for(int i=0;i<n;i++)
		cin>>people[i];
	for(int j=0;j<k;j++)
		cin>>keys[j];
	sort(people,people+n);
	long long ansmax=0;
	bool markey[2000];
	sort(keys,keys+k);
	long long ansi;
	for(int i=0;i<n;i++)
	{
		ans=10000000;
		flag=false;
		for(int o=0;o<k;o++)
		{
			if(markey[o]==false)
			{
				if(keys[o]<p && keys[o]>people[i])
				{
					ans=abs(p-people[i]);
					ansi=o;
					flag=true;
					break;
				}
				if(abs(keys[o]-people[i])+abs(keys[o]-p)<ans)
				{
					ans=abs(keys[o]-people[i])+abs(keys[o]-p);
					ansi=o;
				}
			}
			if(flag==true)
				break;
		}
		markey[ansi]=true;
		if(ans>ansmax)
			ansmax=ans;
	}
	cout<<ansmax;
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

int main() {
	long long people[1000],keys[2000],p,k,n;
	cin>>n>>k>>p;
	long long ans;
	bool flag;
	for(int i=0;i<n;i++)
		cin>>people[i];
	for(int j=0;j<k;j++)
		cin>>keys[j];
	sort(people,people+n);
	long long ansmax=0;
	bool markey[2000];
	sort(keys,keys+k);
	long long ansi;
	for(int i=0;i<n;i++)
	{
		ans=1000000000;
		flag=false;
		for(int o=0;o<k;o++)
		{
			if(markey[o]==false)
			{
				if(keys[o]<p && keys[o]>people[i])
				{
					ans=abs(p-people[i]);
					ansi=o;
					flag=true;
					break;
				}
				if(abs(keys[o]-people[i])+abs(keys[o]-p)<ans)
				{
					ans=abs(keys[o]-people[i])+abs(keys[o]-p);
					ansi=o;
				}
			}
			if(flag==true)
				break;
		}
		markey[ansi]=true;
		if(ans>ansmax)
			ansmax=ans;
	}
	cout<<ansmax;
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
long long people[1001],keys[2001],p,k,n;
bool markey[2001];
const long long MAXMIN=1e9;
long long mind;
long long maxd,maxdi;
	long long ptokey[1001];
	long long minjd;
long long maxdv()
{
	maxd=0;
	for(long long  i=0;i<n;i++)
	{
		if(abs(keys[ptokey[i]]-people[i])+abs(keys[ptokey[i]]-p)>maxd)
		{
			maxd=abs(keys[ptokey[i]]-people[i])+abs(keys[ptokey[i]]-p);
			maxdi=i;
		}
	}
	return maxd;
}
int main() {

	cin>>n>>k>>p;
	long long ans;
	for(long long i=0;i<n;i++)
		cin>>people[i];
	for(long long j=0;j<k;j++)
	{
		cin>>keys[j];
		markey[j]=false;
	}
	sort(people,people+n);
	sort(keys,keys+k);
	cout<<endl;

	for(long long i=0;i<n;i++)
	{
		mind=MAXMIN;
		for(long long j=0;j<k;j++)
		{
			if(markey[j]==false)
			{
				if(abs(keys[j]-people[i])+abs(keys[j]-p)<mind)
				{
					minjd=j;
					mind=abs(keys[j]-people[i])+abs(keys[j]-p);
				}
			}
		}
		markey[minjd]=true;
		ptokey[i]=minjd;
	}
	maxdv();
	cout<<maxd;
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
long long people[1001],keys[2001],p,k,n;
bool markey[2001];
const long long MAXMIN=1e10;
long long mind;
long long maxd,maxdi;
	long long ptokey[1001];
	long long minjd;
void maxdv()
{
	maxd=0;
	for(long long  i=0;i<n;i++)
	{
		if(abs(keys[ptokey[i]]-people[i])+abs(keys[ptokey[i]]-p)>maxd)
		{
			maxd=abs(keys[ptokey[i]]-people[i])+abs(keys[ptokey[i]]-p);
			maxdi=i;
		}
	}
}
int main() {

	cin>>n>>k>>p;
	for(long long i=0;i<n;i++)
		cin>>people[i];
	for(long long j=0;j<k;j++)
	{
		cin>>keys[j];
		markey[j]=false;
	}
	sort(people,people+n);
	sort(keys,keys+k);
	for(long long i=0;i<n;i++)
	{
		mind=MAXMIN;
		for(long long j=0;j<k;j++)
		{
			if(markey[j]==false)
			{
				if(abs(keys[j]-people[i])+abs(keys[j]-p)<mind)
				{
					minjd=j;
					mind=abs(keys[j]-people[i])+abs(keys[j]-p);
				}
			}
		}
		markey[minjd]=true;
		ptokey[i]=minjd;
	}
	long long flagi;

	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++)
			if(i!=j)
			{
				if(i>j && keys[ptokey[i]]<keys[ptokey[j]])
					swap(ptokey[i],ptokey[j]);
				if(i<j && keys[ptokey[i]]>keys[ptokey[j]])
					swap(ptokey[i],ptokey[j]);
			}

	maxdv();
	cout<<maxd;
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
long long people[1000],keys[2000],p,k,n;
bool markey[2000];
const long long MAXMIN=1e9;
long long mind;
long long maxd,maxdi;
	long long ptokey[1000];
long long maxdv()
{
	maxd=0;
	for(int i=0;i<n;i++)
	{
		if(abs(keys[ptokey[i]]-people[i])+abs(keys[ptokey[i]]-p)>maxd)
		{
			maxd=abs(keys[ptokey[i]]-people[i])+abs(keys[ptokey[i]]-p);
			maxdi=i;
		}
	}
	return maxd;
}
int main() {

	cin>>n>>k>>p;
	long long ans;
	for(int i=0;i<n;i++)
		cin>>people[i];
	for(int j=0;j<k;j++)
	{
		cin>>keys[j];
		markey[j]=false;
	}
	sort(people,people+n);
	sort(keys,keys+k);
	cout<<endl;
	long long minjd;

	for(int i=0;i<n;i++)
	{
		mind=MAXMIN;
		for(int j=0;j<k;j++)
		{
			if(markey[j]==false)
			{
				if(abs(keys[j]-people[i])+abs(keys[j]-p)<mind)
				{
					minjd=j;
					mind=abs(keys[j]-people[i])+abs(keys[j]-p);
				}
			}
		}
		markey[minjd]=true;
		ptokey[i]=minjd;
	}
	maxdv();

	cout<<maxd;
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

int main() {
	long long people[1000],keys[2000],p,k,n;
	cin>>n>>k>>p;
	long long ans;
	bool flag;
	for(int i=0;i<n;i++)
		cin>>people[i];
	for(int j=0;j<k;j++)
		cin>>keys[j];
	sort(people,people+n);
	long long ansmax=0;
	bool markey[2000];
	sort(keys,keys+k);
	long long ansi;
	for(int i=0;i<n;i++)
	{
		ans=1000000000000;
		flag=false;
		for(int o=0;o<k;o++)
		{
			if(markey[o]==false)
			{
				if(keys[o]<p && keys[o]>people[i])
				{
					ans=abs(p-people[i]);
					ansi=o;
					flag=true;
					break;
				}
				if(abs(keys[o]-people[i])+abs(keys[o]-p)<ans)
				{
					ans=abs(keys[o]-people[i])+abs(keys[o]-p);
					ansi=o;
				}
			}
			if(flag==true)
				break;
		}
		markey[ansi]=true;
		if(ans>ansmax)
			ansmax=ans;
	}
	cout<<ansmax;
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

int main() {
	long long people[1000],keys[2000],p,k,n;
	cin>>n>>k>>p;
	long long ans;
	bool flag;
	for(int i=0;i<n;i++)
		cin>>people[i];
	for(int j=0;j<k;j++)
		cin>>keys[j];
	long long ansmax=0;
	bool markey[2000];
	sort(keys,keys+(k-1));
	long long ansi;
	for(int i=0;i<n;i++)
	{
		ans=10000000;
		flag=false;
		for(int o=0;o<k;o++)
		{
			if(markey[o]==false)
			{
				if(keys[o]<p && keys[o]>people[i])
				{
					ans=abs(p-people[i]);
					ansi=o;
					flag=true;
					break;
				}
				if(abs(keys[o]-people[i])+abs(keys[o]-p)<ans)
				{
					ans=abs(keys[o]-people[i])+abs(keys[o]-p);
					ansi=o;
				}
			}
			if(flag==true)
				break;
		}
		markey[ansi]=true;
		if(ans>ansmax)
			ansmax=ans;
	}
	cout<<ansmax;
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;

int main() {
	long long people[1000],keys[2000],p,k,n;
	cin>>n>>k>>p;
	long long ans;
	bool flag;
	for(int i=0;i<n;i++)
		cin>>people[i];
	for(int j=0;j<k;j++)
		cin>>keys[j];
	long long ansmax=0;
	bool markey[2000];
	long long ansi;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<k;j++)
			for(int z=0;z<k;z++)
			{
				if(j!=z)
					swap(keys[j],keys[z]);
				ans=10000000000;
				flag=false;
				for(int o=0;o<k;o++)
				{
					if(markey[o]==false)
					{
						if(keys[o]<p && keys[o]>people[i])
						{
							ans=abs(p-people[i]);
							ansi=o;
							flag=true;
							break;
						}
						if(abs(keys[o]-people[i])+abs(keys[o]-p)<ans)
						{
							ans=abs(keys[o]-people[i])+abs(keys[o]-p);
							ansi=o;
						}
					}
					if(flag==true)
						break;
				}
			}
		markey[ansi]=true;
		if(ans>ansmax)
			ansmax=ans;
	}
	cout<<ansmax;
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
long long people[1000],keys[2000],p,k,n;
bool markey[2000];
const long long MAXMIN=1e9;
long long mind;
long long maxd,maxdi;
	long long ptokey[1000];
long long maxdv()
{
	maxd=0;
	for(int i=0;i<n;i++)
	{
		if(abs(keys[ptokey[i]]-people[i])+abs(keys[ptokey[i]]-p)>maxd)
		{
			maxd=abs(keys[ptokey[i]]-people[i])+abs(keys[ptokey[i]]-p);
			maxdi=i;
		}
	}
	return maxd;
}
int main() {

	cin>>n>>k>>p;
	long long ans;
	for(int i=0;i<n;i++)
		cin>>people[i];
	for(int j=0;j<k;j++)
	{
		cin>>keys[j];
		markey[j]=false;
	}
	sort(people,people+n);
	sort(keys,keys+k);
	for(int i=0;i<k;i++)
		cout<<keys[i]<<" ";
	cout<<endl;
	long long minjd;

	for(int i=0;i<n;i++)
	{
		mind=MAXMIN;
		for(int j=0;j<k;j++)
		{
			if(markey[j]==false)
			{
				if(abs(keys[j]-people[i])+abs(keys[j]-p)<mind)
				{
					minjd=j;
					mind=abs(keys[j]-people[i])+abs(keys[j]-p);
				}
			}
		}
		markey[minjd]=true;
		ptokey[i]=minjd;
	}
	maxdv();

	cout<<maxd;
	return 0;
}


#include<cstdio>
#include<algorithm>
#define ll long long

using namespace std;

const ll N=2010;

ll n,k,p, a[N], b[N];

int main ()
{
    scanf ("%lld%lld%lld",&n,&k,&p);

    for (ll i=1;i<=n;i++) scanf ("%lld",&a[i]);
    for (ll i=1;i<=k;i++) scanf ("%lld",&b[i]);
    sort(a+1,a+n+1);
    sort(b+1,b+k+1);

    ll l=0,r=2e9+1,g,ans;

    while (l<r-1)
    {
        g=(l+r)/2;
        ll poz=0,z=0;
        for (ll i=1;i<=n;i++)
        {
            int t=poz;

            if (poz==k) { z=1; break;}

            int z2=0;

            for (ll j=t+1;j<=k;j++)
            {
                poz=j;
                if ((abs(p-b[j])+abs(a[i]-b[j]))<=g)
                    {z2=1; break;}
            }
            if (!z2) { z=1; break;}
        }

        if (z) l=g;
        else { r=g; ans=g; }
    }
    printf("%lld",ans);
    return 0;
}


#include<cstdio>
#include<algorithm>
#define ll long long

using namespace std;

const int N=2010;

int n,k,p, a[N], b[N];

int main ()
{
    scanf ("%d%d%d",&n,&k,&p);

    for (int i=1;i<=n;i++) scanf ("%d",&a[i]);
    for (int i=1;i<=k;i++) scanf ("%d",&b[i]);
    sort(a+1,a+n+1);
    sort(b+1,b+k+1);

    int l=0,r=1e9+10,g,ans;

    while (l<r-1)
    {
        g=(l+r)/2;

        int poz=0,z=0;
        for (int i=1;i<=n;i++)
        {
            int t=poz;

            if (poz==k) { z=1; break;}

            int z2=0;

            for (int j=t+1;j<=k;j++)
            {
                poz=j;
                if ((abs(p-b[j])+abs(a[i]-b[j]))<=g)
                    {z2=1; break;}
            }
            if (!z2) { z=1; break;}
        }

        if (z) l=g;
        else { r=g; ans=g; }
    }
    printf("%d",ans);
    return 0;
}


#include<cstdio>
#include<algorithm>
#define ll long long

using namespace std;

const ll N=2010;

ll n,k,p, a[N], b[N];

int main ()
{
    scanf ("%lld%lld%lld",&n,&k,&p);

    for (ll i=1;i<=n;i++) scanf ("%lld",&a[i]);
    for (ll i=1;i<=k;i++) scanf ("%lld",&b[i]);
    sort(a+1,a+n+1);
    sort(b+1,b+k+1);

    ll l=-1,r=2e9+2,g,ans=0;

    while (l<r-1)
    {
        g=(l+r)/2;
        ll poz=0,z=0;
        for (ll i=1;i<=n;i++)
        {
            ll t=poz;

            if (poz==k) { z=1; break;}

            ll z2=0;

            for (ll j=t+1;j<=k;j++)
            {
                poz=j;
                if ((abs(p-b[j])+abs(a[i]-b[j]))<=g)
                    {z2=1; break;}
            }
            if (!z2) { z=1; break;}
        }

        if (z) l=g;
        else { r=g; ans=g; }
    }
    printf("%lld",ans);
    return 0;
}


#include<cstdio>
#include<algorithm>
#define ll long long

using namespace std;

const int N=2010;

int n,k,p, a[N], b[N];

int main ()
{
    scanf ("%d%d%d",&n,&k,&p);

    for (int i=1;i<=n;i++) scanf ("%d",&a[i]);
    for (int i=1;i<=k;i++) scanf ("%d",&b[i]);
    sort(a+1,a+n+1);
    sort(b+1,b+k+1);

    int l=0,r=2e9+10,g,ans;

    while (l<r-1)
    {
        g=(l+r)/2;

        int poz=0,z=0;
        for (int i=1;i<=n;i++)
        {
            int t=poz;

            if (poz==k) { z=1; break;}

            int z2=0;

            for (int j=t+1;j<=k;j++)
            {
                poz=j;
                if ((abs(p-b[j])+abs(a[i]-b[j]))<=g)
                    {z2=1; break;}
            }
            if (!z2) { z=1; break;}
        }

        if (z) l=g;
        else { r=g; ans=g; }
    }
    printf("%d",ans);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

long absa(long x){return x>0?x:-x;}

int main()
{
	//freopen("in.txt","r",stdin);
	long k,n,p,R = INT_MAX,tmp = 0;

	cin>>n>>k>>p;
	long a[n],b[k];
	for(long i=0;i<n;i++)
		cin>>a[i];
	for(long i=0;i<k;i++)
		cin>>b[i];
	sort(a,a+n);
	sort(b,b+n);

	for(int i=0;i<k-n;i++)
	{
		for(int j=0;j<n;j++)
			tmp=max(tmp,absa(a[j]-b[i+j])+absa(p-b[i+j]));
		R = min(tmp,R);
	}
	cout<<R;
return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

long absa(long x){return x>0?x:-x;}

int main()
{
	//freopen("in.txt","r",stdin);
	long k,n,p,R = INT_MAX,tmp = 0;

	cin>>n>>k>>p;
	long a[n],b[k];
	for(long i=0;i<n;i++)
		cin>>a[i];
	for(long i=0;i<k;i++)
		cin>>b[i];
	sort(a,a+n);
	sort(b,b+k);

	for(int i=0;i<=k-n;i++)
	{
		tmp = 0;
		for(int j=0;j<n;j++)
			tmp=max(tmp,absa(a[j]-b[i+j])+absa(p-b[i+j]));
		R = min(tmp,R);
	}
	cout<<R;
return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

long absa(long x){return x>0?x:-x;}

int main()
{
	//freopen("in.txt","r",stdin);
	long k,n,p,R = INT_MAX,tmp = 0;

	cin>>n>>k>>p;
	long a[n],b[k];
	for(long i=0;i<n;i++)
		cin>>a[i];
	for(long i=0;i<k;i++)
		cin>>b[i];
	sort(a,a+n);
	sort(b,b+k);

	for(int i=0;i<=k-n;i++)
	{
		for(int j=0;j<n;j++)
			tmp=max(tmp,absa(a[j]-b[i+j])+absa(p-b[i+j]));
		R = min(tmp,R);
	}
	cout<<R;
return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

long absa(long x){return x>0?x:-x;}

int main()
{
	//freopen("in.txt","r",stdin);
	long k,n,p,R = INT_MAX,tmp = 0;

	cin>>n>>k>>p;
	long a[n],b[k];
	for(long i=0;i<n;i++)
		cin>>a[i];
	for(long i=0;i<k;i++)
		cin>>b[i];
	sort(a,a+n);
	sort(b,b+n);

	long maxa = a[n-1],mina = a[0];
	long maxb = b[k-1],minb = b[0];

	if(n == 1)
	{
		for(int i=0;i<k;i++)
	{
		if((a[0]<=b[i] && b[i]<=p) || (p<=b[i] && b[i]<=a[0]))
			tmp  = absa(a[0]-p);
		else
			tmp = absa(a[0]-b[i])+absa(b[i]-p);
		if(R>tmp)
			R = tmp;

	}
	cout<<R;
	return 0;
	}
	for(int i=0,j=0;i<n && j<k;i++,j++)
	{
		if((a[i]<=b[i] && b[i]<=p) || (p<=b[i] && b[i]<=a[i]))
			tmp  = absa(a[i]-p);
		else
			tmp = absa(a[i]-b[i])+absa(b[i]-p);
		if(R>tmp)
			R = tmp;
	}
	cout<<R;
return 0;
}


#include <bits/stdc++.h>
using namespace std;

struct BipartiteGraph
{
    int L, R;
    vector<vector<int>> adjacency_list;
    vector<int> lmatch, rmatch;

    BipartiteGraph(const int L, const int R) : L(L), R(R),
        adjacency_list(L), lmatch(L, -1), rmatch(R, -1) {}

    void add_edge(const int l, const int r)
    {
        adjacency_list[l].push_back(r);
    }

    int max_matching()
    {
        vector<bool> seen(L);
        vector<int> distances(L);
        const function<bool(int)> dfs = [&](const int l)
        {
            if (seen[l]) return false;
            else seen[l] = true;

            for (const int r : adjacency_list[l])
            {
                const int l2 = rmatch[r];
                if (l2 == -1 || (distances[l2] == distances[l]+1 && dfs(l2)))
                {
                    lmatch[l] = r;
                    rmatch[r] = l;
                    return true;
                }
            }
            return false;
        };

        int result = 0;
        bool path_found = true;
        while (path_found)
        {
            const int infinity = numeric_limits<int>::max();
            fill(begin(distances), end(distances), infinity);
            std::queue<int> queue;
            for (int l = 0; l < L; l++)
                if (lmatch[l] == -1)
                {
                    queue.push(l);
                    distances[l] = 0;
                }

            path_found = false;
            while (!queue.empty())
            {
                const int l = queue.front();
                queue.pop();

                for (const int r : adjacency_list[l])
                {
                    const int l2 = rmatch[r];
                    if (l2 == -1) path_found = true;
                    else if (distances[l2] == infinity)
                    {
                        distances[l2] = distances[l] + 1;
                        queue.push(l2);
                    }
                }
            }

            fill(begin(seen), end(seen), false);
            for (int l = 0; l < L; l++)
                if (lmatch[l] == -1)
                    result += dfs(l);
        }
        return result;
    }
};

int main()
{
    ios::sync_with_stdio(false);

    int n, k, p;
    cin >> n >> k >> p;

    vector<int> people(n);
    for (int& x : people)
        cin >> x;

    vector<int> keys(k);
    for (int& x : keys)
        cin >> x;

    int lower = 0, upper = int(2e9);
    while (lower < upper)
    {
        int guess = lower + (upper - lower) / 2;

        BipartiteGraph graph(n, k);
        for (int i = 0; i < n; i++)
            for (int j = 0; j < k; j++)
                if (abs(people[i]-keys[j]) + abs(keys[j]-p) <= guess)
                    graph.add_edge(i, j);

        if (graph.max_matching() == n)
            upper = guess;
        else
            lower = guess+1;
    }
    cout << lower << endl;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n, k, p;
    cin >> n >> k >> p;

    vector<int> people(n);
    for (int& x : people)
        cin >> x;

    vector<int> keys(k);
    for (int& x : keys)
        cin >> x;

    sort(begin(people), end(people));
    sort(begin(keys), end(keys));

    const int infinity = numeric_limits<int>::max();
    vector<int> dp(k+1, infinity);
    dp[0] = 0;
    for (const int x : people)
    {
        const vector<int> previous = dp;
        dp[0] = infinity;
        for (int i = 0; i < k; i++)
        {
            const int cost = abs(x-keys[i]) + abs(keys[i]-p);
            dp[i+1] = min(dp[i], max(previous[i], cost));
        }
    }
    cout << dp[k] << endl;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n, k, p;
    cin >> n >> k >> p;

    vector<int> people(n);
    for (int& x : people)
        cin >> x;

    vector<int> keys(k);
    for (int& x : keys)
        cin >> x;

    sort(begin(people), end(people));
    sort(begin(keys), end(keys));

    const int infinity = numeric_limits<int>::max();
    vector<int> dp(k+1);
    for (const int x : people)
    {
        const vector<int> previous = dp;
        dp[0] = infinity;
        for (int i = 0; i < k; i++)
        {
            const int cost = abs(x-keys[i]) + abs(keys[i]-p);
            dp[i+1] = min(dp[i], max(previous[i], cost));
        }
    }
    cout << dp[k] << endl;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n, k, p;
    cin >> n >> k >> p;

    vector<int> people(n);
    for (int& x : people)
        cin >> x;

    vector<int> keys(k);
    for (int& x : keys)
        cin >> x;

    sort(begin(people), end(people));
    sort(begin(keys), end(keys));

    int answer = numeric_limits<int>::max();
    for (int i = 0; i+n <= k; i++)
    {
        int option = 0;
        for (int j = 0; j < n; j++)
            option = max(option, abs(people[j]-keys[i+j]) + abs(keys[i+j]-p));
        answer = min(answer, option);
    }
    cout << answer << endl;
}