#include<bits\stdc++.h>
using namespace std;
long long int num=1000000007;
long long int dfs(long long int s,vector<long long int> &graph[],vector<bool> &visited,vector<long long int> &node_count,long long int n)
{
    long long int count=1;
    visited[s]=1;
    for(int v:graph[s])
    {
        if(!visited[v])
        {
            long long int count1=dfs(v,graph,visited,node_count,n);
            node_count.push_back(count1*(n-count1));
            count+=count1;
        }
    }
    return count;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        long long int n;
        cin>>n;
        vector<long long int> graph[n+1];
        for(int i=0;i<n-1;i++)
        {
            long long int a,b;
            cin>>a>>b;
            graph[a].push_back(b);
            graph[b].push_back(a);
        }
        vector<long long int> count;
        vector<bool> visited(n+1,false);
        dfs(1,graph,visited,count,n);
        long long int m;
        cin>>m;
        vector<long long int> factors(m);
        for(int i=0;i<m;i++)
        {
            cin>>factors[i];
        }
        while(factors.size()!=n-1)
        {
            factors.push_back(1);
        }
        sort(factors.begin(),factors.end());
        long long int ans=0;
        sort(count.begin(),count.end());
        for(int i=0;i<n-1;i++)
        {
            ans+=(count[i]*factors[i]);
            ans%=num;
        }
        // for(int i=0;i<count.size();i++)
        // {
        //     cout<<count[i]<<" ";
        // }
        cout<<ans<<endl;
    }

}


#include<bits\stdc++.h>
using namespace std;
long long int num=10e8+7;
long long int dfs(long long int s,vector<int> &graph,vector<bool> &visited,vector<long long int> &node_count,int n)
{
    long long int count=1;
    visited[s]=1;
    for(int v:graph[s])
    {
        if(!visited[v])
        {
            long long int count1=dfs(v,graph,visited,node_count,n);
            node_count.push_back(count1*(n-count1));
            count+=count1;
            count%=num;
        }
    }
    return count;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        long long int n;
        cin>>n;
        vector<int> graph[n+1];
        for(int i=0;i<n-1;i++)
        {
            int a,b;
            cin>>a>>b;
            graph[a].push_back(b);
            graph[b].push_back(a);
        }
        vector<long long int> count;
        vector<bool> visited(n+1,false);
        dfs(1,graph,visited,count,n);
        int m;
        cin>>m;
        vector<long long int> factors(m);
        for(int i=0;i<m;i++)
        {
            cin>>factors[i];
        }
        while(factors.size()!=n-1)
        {
            factors.push_back(1);
        }
        sort(factors.begin(),factors.end());
        long long int ans=0;
        sort(count.begin(),count.end());
        for(int i=0;i<n-1;i++)
        {
            ans+=(count[i]*factors[i]);
            ans%=num;
        }

        cout<<ans<<endl;
    }
}


#include<bits\stdc++.h>
using namespace std;
long long int num=10e8+7;
long long int dfs(long long int s,vector<int> graph[],vector<bool> &visited,vector<long long int> &node_count,int n)
{
    long long int count=1;
    visited[s]=1;
    for(int v:graph[s])
    {
        if(!visited[v])
        {
            long long int count1=dfs(v,graph,visited,node_count,n);
            node_count.push_back(count1*(n-count1));
            count+=count1;
            count%=num;
        }
    }
    return count;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        vector<int> graph[n+1];
        for(int i=0;i<n-1;i++)
        {
            int a,b;
            cin>>a>>b;
            graph[a].push_back(b);
            graph[b].push_back(a);
        }
        vector<long long int> count;
        vector<bool> visited(n+1,false);
        dfs(1,graph,visited,count,n);
        int m;
        cin>>m;
        vector<long long int> factors(m);
        for(int i=0;i<m;i++)
        {
            cin>>factors[i];
        }
        while(factors.size()!=n-1)
        {
            factors.push_back(1);
        }
        sort(factors.begin(),factors.end());
        long long int ans=0;
        sort(count.begin(),count.end());
        for(int i=0;i<n-1;i++)
        {
            ans+=(count[i]*factors[i]);
            ans%=num;
        }

        cout<<ans<<endl;
    }
}


#include<bits\stdc++.h>
using namespace std;
long long int num=1000000007;
long long int dfs(long long int s,vector<long long int> graph[],vector<bool> &visited,vector<long long int> &node_count,long long int n)
{
    long long int count=1;
    visited[s]=1;
    for(int v:graph[s])
    {
        if(!visited[v])
        {
            long long int count1=dfs(v,graph,visited,node_count,n);
            node_count.push_back(count1*(n-count1));
            count+=count1;
            count%=num;
        }
    }
    return count;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        long long int n;
        cin>>n;
        vector<long long int> graph[n+1];
        for(int i=0;i<n-1;i++)
        {
            long long int a,b;
            cin>>a>>b;
            graph[a].push_back(b);
            graph[b].push_back(a);
        }
        vector<long long int> count;
        vector<bool> visited(n+1,false);
        dfs(1,graph,visited,count,n);
        long long int m;
        cin>>m;
        vector<long long int> factors(m);
        for(int i=0;i<m;i++)
        {
            cin>>factors[i];
        }
        while(factors.size()!=n-1)
        {
            factors.push_back(1);
        }
        sort(factors.begin(),factors.end());
        long long int ans=0;
        sort(count.begin(),count.end());
        for(int i=0;i<n-1;i++)
        {
            ans+=(count[i]*factors[i]);
            ans%=num;
        }
        // for(int i=0;i<count.size();i++)
        // {
        //     cout<<count[i]<<" ";
        // }
        cout<<ans<<endl;
    }

}


#include<bits\stdc++.h>
using namespace std;
using ll=long long;
ll n;
ll mod=1e9+7;
void dfs(int s,vector<ll> graph[],vector<bool> &visited,vector<ll> &count,vector<ll> &prod)
{
    visited[s]=1;
    for(int v:graph[s])
    {
        if(!visited[v])
        {
            dfs(v,graph,visited,count,prod);
            count[s]+=count[v];
            count[s]%=mod;
            prod.push_back((count[v]*(n-count[v]))%mod);
        }
    }
    count[s]+=1;
    count[s]%=mod;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        cin>>n;
        vector<ll> graph[n+1];
        for(int i=0;i<n-1;i++)
        {
            ll a,b;
            cin>>a>>b;
            graph[a].push_back(b);
            graph[b].push_back(a);
        }
        vector<ll> count(n+1,0),prod;
        vector<bool> visited(n+1,false);
        dfs(1,graph,visited,count,prod);
        sort(prod.begin(),prod.end());
        ll m;
        cin>>m;
        vector<int> factor(m);
        for(int i=0;i<m;i++)
        {
            cin>>factor[i];
        }
        if(m>n-1)
        {
            sort(factor.begin(),factor.end());
            while(factor.size()!=n-1)
            {
                factor[factor.size()-2]*=factor.back();
                factor[factor.size()-1]%=mod;
                factor.pop_back();
            }
        }
        else
        {
            while(factor.size()!=n-1)
            {
                factor.push_back(1);
            }
            sort(factor.begin(),factor.end());
        }
        ll ans=0;
        for(int i=0;i<n-1;i++)
        {
            ans+=(factor[i]*prod[i]);
            ans%=mod;
        }
        ans%=mod;
        cout<<ans<<endl;
    }
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <vector>
#include <set>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
int n;
vector<int> p[100005];
const int mod = 1e9 + 7;
ll key[100005];
void dfs(int now, int fa)
{
    key[now] = 1;
    for (int i = 0; i < p[now].size(); i++)
    {
        int nxt = p[now][i];
        if (nxt == fa)
            continue;
        dfs(nxt, now);
        key[now] += key[nxt];
    }
}
ll pr[100005];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        cin >> n;
        for (int i = 1; i <= n; i++)
            p[i].clear();
        for (int i = 1; i <= n; i++)
            key[i] = 0;
        for (int i = 0; i < n; i++)
            pr[i] = 1;
        for (int i = 1; i < n; i++)
        {
            int a, b;
            cin >> a >> b;
            p[a].push_back(b);
            p[b].push_back(a);
        }
        int m;
        cin >> m;
        for (int i = 0; i < m; i++)
        {
            cin >> pr[i];
        }
        sort(pr, pr + m);
        for (int i = n - 1; i < m; i++)
        {
            pr[n - 2] = (pr[n - 2] * pr[i]) % mod;
        }
        sort(pr, pr + n - 1);
        reverse(pr, pr + n - 1);
        dfs(1, 1);
        for (int i = 1; i <= n; i++)
            key[i] = key[i] * (1LL * n - key[i]);
        sort(key + 1, key + 1 + n);
        reverse(key + 1, key + 1 + n);
        ll ans = 0;
        for (int i = 1; i <= n; i++)
        {
            ans = (ans + key[i] * pr[i - 1] % mod) % mod;
        }
        cout << ans << endl;
    }
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <vector>
#include <set>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
int n;
vector<int> p[100005];
const int mod = 1e9 + 7;
ll key[100005];
void dfs(int now, int fa)
{
    key[now] = 1;
    for (int i = 0; i < p[now].size(); i++)
    {
        int nxt = p[now][i];
        if (nxt == fa)
            continue;
        dfs(nxt, now);
        key[now] += key[nxt];
    }
}
ll pr[100005];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        cin >> n;
        for (int i = 1; i <= n; i++)
            p[i].clear();
        for (int i = 1; i <= n; i++)
            key[i] = 0;
        for (int i = 0; i < n; i++)
            pr[i] = 1;
        for (int i = 1; i < n; i++)
        {
            int a, b;
            cin >> a >> b;
            p[a].push_back(b);
            p[b].push_back(a);
        }
        int m;
        cin >> m;
        for (int i = 0; i < m; i++)
        {
            cin >> pr[i];
        }
        sort(pr, pr + m);
        if (n >= 2)
        {
            for (int i = n - 1; i < m; i++)
            {
                pr[n - 2] = (pr[n - 2] * pr[i]) % mod;
            }
        }
        reverse(pr, pr + n - 1);
        ll ans = 0;
        dfs(1, 1);
        for (int i = 1; i <= n; i++)
            key[i] = key[i] * (1LL * n - key[i]);
        sort(key + 1, key + 1 + n);
        reverse(key + 1, key + 1 + n);
        for (int i = 0; i < n; i++)
            ll ans = 0;
        for (int i = 1; i <= n - 1; i++)
        {
            ans = (ans + key[i] * pr[i - 1] % mod) % mod;
        }
        cout << ans << endl;
    }
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <vector>
#include <set>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
int n;
vector<int> p[100005];
const int mod = 1e9 + 7;
ll key[100005];
void dfs(int now, int fa)
{
    key[now] = 1;
    for (int i = 0; i < p[now].size(); i++)
    {
        int nxt = p[now][i];
        if (nxt == fa)
            continue;
        dfs(nxt, now);
        key[now] += key[nxt];
    }
}
ll pr[60005];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        cin >> n;
        for (int i = 1; i <= n; i++)
            p[i].clear();
        for (int i = 1; i <= n; i++)
            key[i] = 0;
        for (int i = 0; i < n; i++)
            pr[i] = 1;
        for (int i = 1; i < n; i++)
        {
            int a, b;
            cin >> a >> b;
            if (a > b)
                swap(a, b);
            p[a].push_back(b);
            p[b].push_back(a);
        }
        int m;
        cin >> m;
        for (int i = 0; i < m; i++)
        {
            cin >> pr[i];
        }
        sort(pr, pr + m);
        for (int i = n; i < m; i++)
        {
            pr[n - 1] = (pr[n - 1] * pr[i]) % mod;
        }
        sort(pr, pr + n);
        reverse(pr, pr + n);
        dfs(1, 1);
        for (int i = 2; i <= n; i++)
            key[i] = key[i] * (n - key[i]);
        sort(key + 2, key + 2 + n);
        reverse(key + 2, key + 2 + n);
        ll ans = 0;
        for (int i = 2; i <= n; i++)
        {
            ans = (ans + key[i] * pr[i - 2] % mod) % mod;
        }
        cout << ans << endl;
    }
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <vector>
#include <set>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
int n;
vector<int> p[100005];
const int mod = 1e9 + 7;
ll key[100005];
void dfs(int now, int fa)
{
    key[now] = 1;
    for (int i = 0; i < p[now].size(); i++)
    {
        int nxt = p[now][i];
        if (nxt == fa)
            continue;
        dfs(nxt, now);
        key[now] += key[nxt];
    }
}
ll pr[100005];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        cin >> n;
        for (int i = 1; i <= n; i++)
            p[i].clear();
        for (int i = 1; i <= n; i++)
            key[i] = 0;
        for (int i = 0; i < n; i++)
            pr[i] = 1;
        for (int i = 1; i < n; i++)
        {
            int a, b;
            cin >> a >> b;
            p[a].push_back(b);
            p[b].push_back(a);
        }
        int m;
        cin >> m;
        for (int i = 0; i < m; i++)
        {
            cin >> pr[i];
        }
        sort(pr, pr + m);
        if(n>=2)
        {
            for (int i = n - 1; i < m; i++)
            {
                pr[n - 2] = (pr[n - 2] * pr[i]) % mod;
            }
        }
        sort(pr, pr + n - 1);
        reverse(pr, pr + n - 1);
        dfs(1, 1);
        for (int i = 1; i <= n; i++)
            key[i] = key[i] * (1LL * n - key[i]);
        sort(key + 1, key + 1 + n);
        reverse(key + 1, key + 1 + n);
        ll ans = 0;
        for (int i = 1; i <= n - 1; i++)
        {
            ans = (ans + key[i] * pr[i - 1] % mod) % mod;
        }
        cout << ans << endl;
    }
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <vector>
#include <set>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
int n;
vector<int> p[100005];
const int mod = 1e9 + 7;
ll key[100005];
void dfs(int now, int fa)
{
    key[now] = 1;
    for (int i = 0; i < p[now].size(); i++)
    {
        int nxt = p[now][i];
        if (nxt == fa)
            continue;
        dfs(nxt, now);
        key[now] += key[nxt];
    }
}
ll pr[100005];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        cin >> n;
        for (int i = 1; i <= n; i++)
            p[i].clear();
        for (int i = 1; i <= n; i++)
            key[i] = 0;
        for (int i = 0; i < n; i++)
            pr[i] = 1;
        for (int i = 1; i < n; i++)
        {
            int a, b;
            cin >> a >> b;
            p[a].push_back(b);
            p[b].push_back(a);
        }
        int m;
        cin >> m;
        for (int i = 0; i < m; i++)
        {
            cin >> pr[i];
        }
        sort(pr, pr + m);
        for (int i = n - 1; i < m; i++)
        {
            pr[n - 2] = (pr[n - 2] * pr[i]) % mod;
        }
        sort(pr, pr + n - 1);
        reverse(pr, pr + n - 1);
        dfs(1, 1);
        for (int i = 1; i <= n; i++)
            key[i] = key[i] * (1LL * n - key[i]);
        sort(key + 1, key + 1 + n);
        reverse(key + 1, key + 1 + n);
        ll ans = 0;
        for (int i = 1; i <= n - 1; i++)
        {
            ans = (ans + key[i] * pr[i - 1] % mod) % mod;
        }
        cout << ans << endl;
    }
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <vector>
#include <set>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
int n;
vector<int> p[100005];
const int mod = 1e9 + 7;
ll key[100005];
void dfs(int now, int fa)
{
    key[now] = 1;
    for (int i = 0; i < p[now].size(); i++)
    {
        int nxt = p[now][i];
        if (nxt == fa)
            continue;
        dfs(nxt, now);
        key[now] += key[nxt];
    }
}
ll pr[100005];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        cin >> n;
        for (int i = 1; i <= n; i++)
            p[i].clear();
        for (int i = 1; i <= n; i++)
            key[i] = 0;
        for (int i = 0; i < n; i++)
            pr[i] = 1;
        for (int i = 1; i < n; i++)
        {
            int a, b;
            cin >> a >> b;
            p[a].push_back(b);
            p[b].push_back(a);
        }
        int m;
        cin >> m;
        for (int i = 1; i <= m; i++)
        {
            cin >> pr[i];
        }
        sort(pr + 1, pr + m + 1);
        reverse(pr + 1, pr + m + 1);
        if (m > n - 1)
        {
            for (int i = 2; i <= m; i++)
            {
                if (i <= m - n + 2)
                {
                    pr[1] = pr[1] * pr[i] % mod;
                }
                else
                {
                    pr[i - (m - n + 2) + 1] = pr[i];
                }
            }
        }
        ll ans = 0;
        dfs(1, 1);
        for (int i = 1; i <= n; i++)
            key[i] = key[i] * (1LL * n - key[i]);
        sort(key + 1, key + 1 + n);
        reverse(key + 1, key + 1 + n);
        for (int i = 0; i < n; i++)
            ll ans = 0;
        for (int i = 1; i <= n - 1; i++)
        {
            ans = (ans + key[i] * pr[i] % mod) % mod;
        }
        cout << ans << endl;
    }
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <vector>
#include <set>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
int n;
vector<int> p[100005];
const int mod = 1e9 + 7;
ll key[100005];
void dfs(int now, int fa)
{
    key[now] = 1;
    for (int i = 0; i < p[now].size(); i++)
    {
        int nxt = p[now][i];
        if (nxt == fa)
            continue;
        dfs(nxt, now);
        key[now] += key[nxt];
    }
}
ll pr[60005];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        cin >> n;
        for (int i = 1; i <= n; i++)
            p[i].clear();
        for (int i = 1; i <= n; i++)
            key[i] = 0;
        for (int i = 0; i < n; i++)
            pr[i] = 1;
        for (int i = 1; i < n; i++)
        {
            int a, b;
            cin >> a >> b;

            p[a].push_back(b);
            p[b].push_back(a);
        }
        int m;
        cin >> m;
        for (int i = 0; i < m; i++)
        {
            cin >> pr[i];
        }
        sort(pr, pr + m);
        for (int i = n; i < m; i++)
        {
            pr[n - 1] = (pr[n - 1] * pr[i]) % mod;
        }
        sort(pr, pr + n);
        reverse(pr, pr + n);
        dfs(1, 1);
        for (int i = 1; i <= n; i++)
            key[i] = key[i] * (1LL * n - key[i]);
        sort(key + 1, key + 1 + n);
        reverse(key + 1, key + 1 + n);
        ll ans = 0;
        for (int i = 1; i <= n-1; i++)
        {
            ans = (ans + key[i] * pr[i - 1] % mod) % mod;
        }
        cout << ans << endl;
    }
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <vector>
#include <set>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
int n;
vector<int> p[100005];
const int mod = 1e9 + 7;
ll key[100005];
void dfs(int now, int fa)
{
    key[now] = 1;
    for (int i = 0; i < p[now].size(); i++)
    {
        int nxt = p[now][i];
        if (nxt == fa)
            continue;
        dfs(nxt, now);
        key[now] += key[nxt];
    }
}
ll pr[60005];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        cin >> n;
        for (int i = 1; i <= n; i++)
            p[i].clear();
        for (int i = 1; i <= n; i++)
            key[i] = 0;
        for (int i = 0; i < n; i++)
            pr[i] = 1;
        for (int i = 1; i < n; i++)
        {
            int a, b;
            cin >> a >> b;
            p[a].push_back(b);
            p[b].push_back(a);
        }
        int m;
        cin >> m;
        for (int i = 0; i < m; i++)
        {
            cin >> pr[i];
        }
        sort(pr, pr + m);
        for (int i = n - 1; i < m; i++)
        {
            pr[n - 2] = (pr[n - 2] * pr[i]) % mod;
        }
        sort(pr, pr + n);
        reverse(pr, pr + n);
        dfs(1, 1);
        for (int i = 1; i <= n; i++)
            key[i] = key[i] * (1LL * n - key[i]);
        sort(key + 1, key + 1 + n);
        reverse(key + 1, key + 1 + n);
        ll ans = 0;
        for (int i = 1; i <= n - 1; i++)
        {
            ans = (ans + key[i] * pr[i - 1] % mod) % mod;
        }
        cout << ans << endl;
    }
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <vector>
#include <set>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
int n;
vector<int> p[100005];
const int mod = 1e9 + 7;
ll key[100005];
void dfs(int now, int fa)
{
    key[now] = 1;
    for (int i = 0; i < p[now].size(); i++)
    {
        int nxt = p[now][i];
        if (nxt == fa)
            continue;
        dfs(nxt, now);
        key[now] += key[nxt];
    }
}
ll pr[60005];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        cin >> n;
        for (int i = 1; i <= n; i++)
            p[i].clear();
        for (int i = 1; i <= n; i++)
            key[i] = 0;
        for (int i = 0; i < n; i++)
            pr[i] = 1;
        for (int i = 1; i < n; i++)
        {
            int a, b;
            cin >> a >> b;
            p[a].push_back(b);
            p[b].push_back(a);
        }
        int m;
        cin >> m;
        for (int i = 0; i < m; i++)
        {
            cin >> pr[i];
        }
        sort(pr, pr + m);
        for (int i = n - 1; i < m; i++)
        {
            pr[n - 2] = (pr[n - 2] * pr[i]) % mod;
        }
        sort(pr, pr + n - 1);
        reverse(pr, pr + n - 1);
        dfs(1, 1);
        for (int i = 1; i <= n; i++)
            key[i] = key[i] * (1LL * n - key[i]);
        sort(key + 1, key + 1 + n);
        reverse(key + 1, key + 1 + n);
        ll ans = 0;
        for (int i = 1; i <= n; i++)
        {
            ans = (ans + key[i] * pr[i - 1] % mod) % mod;
        }
        cout << ans << endl;
    }
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <vector>
#include <set>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
int n;
vector<int> p[100005];
const int mod = 1e9 + 7;
ll key[100005];
void dfs(int now, int fa)
{
    key[now] = 1;
    for (int i = 0; i < p[now].size(); i++)
    {
        int nxt = p[now][i];
        if (nxt == fa)
            continue;
        dfs(nxt, now);
        key[now] += key[nxt];
    }
}
ll pr[60005];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        cin >> n;
        for (int i = 1; i <= n; i++)
            p[i].clear();
        for (int i = 1; i <= n; i++)
            key[i] = 0;
        for (int i = 0; i < n; i++)
            pr[i] = 1;
        for (int i = 1; i < n; i++)
        {
            int a, b;
            cin >> a >> b;
            p[a].push_back(b);
            p[b].push_back(a);
        }
        int m;
        cin >> m;
        for (int i = 0; i < m; i++)
        {
            cin >> pr[i];
        }
        sort(pr, pr + m);
        for (int i = n - 1; i < m; i++)
        {
            pr[n - 2] = (pr[n - 2] * pr[i]) % mod;
        }
        sort(pr, pr + n - 1);
        reverse(pr, pr + n - 1);
        dfs(1, 1);
        for (int i = 2; i <= n; i++)
            key[i] = key[i] * (1LL * n - key[i]);
        sort(key + 2, key + 2 + n);
        reverse(key + 2, key + 2 + n);
        ll ans = 0;
        for (int i = 2; i <= n; i++)
        {
            ans = (ans + key[i] * pr[i - 2] % mod) % mod;
        }
        cout << ans << endl;
    }
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <vector>
#include <set>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
int n;
vector<int> p[100005];
const int mod = 1e9 + 7;
ll key[100005];
void dfs(int now, int fa)
{
    key[now] = 1;
    for (int i = 0; i < p[now].size(); i++)
    {
        int nxt = p[now][i];
        if (nxt == fa)
            continue;
        dfs(nxt, now);
        key[now] += key[nxt];
    }
}
ll pr[60005];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        cin >> n;
        for (int i = 1; i <= n; i++)
            p[i].clear();
        for (int i = 1; i <= n; i++)
            key[i] = 0;
        for (int i = 0; i < n; i++)
            pr[i] = 1;
        for (int i = 1; i < n; i++)
        {
            int a, b;
            cin >> a >> b;

            p[a].push_back(b);
            p[b].push_back(a);
        }
        int m;
        cin >> m;
        for (int i = 0; i < m; i++)
        {
            cin >> pr[i];
        }
        sort(pr, pr + m);
        for (int i = n; i < m; i++)
        {
            pr[n - 1] = (pr[n - 1] * pr[i]) % mod;
        }
        sort(pr, pr + n);
        reverse(pr, pr + n);
        dfs(1, 1);
        for (int i = 1; i <= n; i++)
            key[i] = key[i] * (1LL * n - key[i]);
        sort(key + 1, key + 1 + n);
        reverse(key + 1, key + 1 + n);
        ll ans = 0;
        for (int i = 1; i <= n-1; i++)
        {
            ans = (ans + key[i] * pr[i - 2] % mod) % mod;
        }
        cout << ans << endl;
    }
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <vector>
#include <set>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
int n;
vector<int> p[100005];
const int mod = 1e9 + 7;
ll key[100005];
void dfs(int now, int fa)
{
    key[now] = 1;
    for (int i = 0; i < p[now].size(); i++)
    {
        int nxt = p[now][i];
        if (nxt == fa)
            continue;
        dfs(nxt, now);
        key[now] += key[nxt];
    }
}
ll pr[100005];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        cin >> n;
        for (int i = 1; i <= n; i++)
            p[i].clear();
        for (int i = 1; i <= n; i++)
            key[i] = 0;
        for (int i = 0; i < n; i++)
            pr[i] = 1;
        for (int i = 1; i < n; i++)
        {
            int a, b;
            cin >> a >> b;
            p[a].push_back(b);
            p[b].push_back(a);
        }
        int m;
        cin >> m;
        for (int i = 0; i < m; i++)
        {
            cin >> pr[i];
        }
        sort(pr, pr + m);
        for (int i = n - 1; i < m; i++)
        {
            pr[n - 2] = (pr[n - 2] * pr[i]) % mod;
        }
        sort(pr, pr + n - 1);
        reverse(pr, pr + n - 1);
        dfs(1, 1);
        for (int i = 2; i <= n; i++)
            key[i] = key[i] * (1LL * n - key[i]);
        sort(key + 2, key + 2 + n);
        reverse(key + 2, key + 2 + n);
        ll ans = 0;
        for (int i = 2; i <= n; i++)
        {
            ans = (ans + key[i] * pr[i - 2] % mod) % mod;
        }
        cout << ans << endl;
    }
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <vector>
#include <set>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
int n;
vector<int> p[100005];
const int mod = 1e9 + 7;
ll key[100005];
void dfs(int now, int fa)
{
    key[now] = 1;
    for (int i = 0; i < p[now].size(); i++)
    {
        int nxt = p[now][i];
        if (nxt == fa)
            continue;
        dfs(nxt, now);
        key[now] += key[nxt];
    }
}
ll pr[60005];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        cin >> n;
        for (int i = 1; i <= n; i++)
            p[i].clear();
        for (int i = 1; i <= n; i++)
            key[i] = 0;
        for (int i = 0; i < n; i++)
            pr[i] = 1;
        for (int i = 1; i < n; i++)
        {
            int a, b;
            cin >> a >> b;
            if (a > b)
                swap(a, b);
            p[a].push_back(b);
            p[b].push_back(a);
        }
        int m;
        cin >> m;
        for (int i = 0; i < m; i++)
        {
            cin >> pr[i];
        }
        sort(pr, pr + m);
        for (int i = n; i < m; i++)
        {
            pr[n - 1] = (pr[n - 1] * pr[i]) % mod;
        }
        sort(pr, pr + n);
        reverse(pr, pr + n);
        dfs(1, 1);
        for (int i = 2; i <= n; i++)
            key[i] = key[i] * (1LL * n - key[i]);
        sort(key + 2, key + 2 + n);
        reverse(key + 2, key + 2 + n);
        ll ans = 0;
        for (int i = 2; i <= n; i++)
        {
            ans = (ans + key[i] * pr[i - 2] % mod) % mod;
        }
        cout << ans << endl;
    }
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <vector>
#include <set>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
int n;
vector<int> p[100005];
const int mod = 1e9 + 7;
ll key[100005];
void dfs(int now, int fa)
{
    key[now] = 1;
    for (int i = 0; i < p[now].size(); i++)
    {
        int nxt = p[now][i];
        if (nxt == fa)
            continue;
        dfs(nxt, now);
        key[now] += key[nxt];
    }
}
ll pr[60005];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        cin >> n;
        for (int i = 1; i <= n; i++)
            p[i].clear();
        for (int i = 1; i <= n; i++)
            key[i] = 0;
        for (int i = 0; i < n; i++)
            pr[i] = 1;
        for (int i = 1; i < n; i++)
        {
            int a, b;
            cin >> a >> b;
            p[a].push_back(b);
            p[b].push_back(a);
        }
        int m;
        cin >> m;
        for (int i = 0; i < m; i++)
        {
            cin >> pr[i];
        }
        sort(pr, pr + m);
        for (int i = n - 1; i < m; i++)
        {
            pr[n - 2] = (pr[n - 2] * pr[i]) % mod;
        }
        sort(pr, pr + n);
        reverse(pr, pr + n);
        dfs(1, 1);
        for (int i = 2; i <= n; i++)
            key[i] = key[i] * (1LL * n - key[i]);
        sort(key + 2, key + 2 + n);
        reverse(key + 2, key + 2 + n);
        ll ans = 0;
        for (int i = 2; i <= n; i++)
        {
            ans = (ans + key[i] * pr[i - 2] % mod) % mod;
        }
        cout << ans << endl;
    }
}


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

#define int long long

namespace Modular_Arithmetic {
    //const int mod=998244353;
    const int mod=1e9+7;
    int modpow(int x,int e) {
        int res=1;
        while (e) {
            if (e%2) {
                res=(res*x)%mod;
            }
            e/=2;
            x=(x*x)%mod;
        }
        return res%mod;
    }

    int modinv(int a) {
        return modpow(a,mod-2);
    }

    int mmull(int a,int b) {
        return (a*b)%mod;
    }

    int madd(int a,int b) {
        return (a+b)%mod;
    }

    int msub(int a,int b) {
        return (a-b+mod)%mod;
    }

    int mdiv(int a,int b) {
        return (a*modinv(b)+mod)%mod;
    }
} using namespace Modular_Arithmetic;

const int N=1e5+5;

int n,m;
vector<int> g[N];
vector<int> subs(N);
vector<int> primes;
vector<int> edges;
int ans;

void flush() {
    for (int i=0;i<=n;i++) {
        g[i].clear();
        subs[i]=0;
    }
    edges.clear();
    ans=0;
}

void dfs(int u,int p) {
    subs[u]=1;
    for (int v : g[u]) {
        if (v != p) {
            dfs(v,u);
            subs[u]+=subs[v];
        }
    }
    if (p) {
        edges.push_back(mmull(subs[u],n-subs[u]));
    }
}

void solve() {
    cin>>n;
    flush();
    for (int i=0,u,v;i<n-1;i++) {
        cin>>u>>v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    cin>>m;
    primes.resize(m);
    for (int i=0;i<m;i++) {
        cin>>primes[i];
    }
    dfs(1,0);
    sort(edges.begin(),edges.end());
    if ((int)primes.size() < n-1) {
        primes.resize(n-1,1);
    }
    sort(primes.begin(),primes.end());
    while ((int)primes.size() >= n) {
        int x=primes.back();
        primes.pop_back();
        primes.back()=mmull(primes.back(),x);
    }
    for (int i=0;i<n-1;i++) {
        ans=madd(ans,mmull(primes[i],edges[i]%mod));
    }
    cout<<ans<<"\n";
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin>>t;
    while (t--) {
        solve();
    }
    return 0;
}


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

#define int long long

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

int n,m;
vector<int> g[N];
vector<int> subs(N);
vector<int> primes;
vector<int> edges;
int ans;

void flush() {
    for (int i=0;i<=n;i++) {
        g[i].clear();
        subs[i]=0;
    }
    edges.clear();
    ans=0;
}

void dfs(int u,int p) {
    subs[u]=1;
    for (int v : g[u]) {
        if (v != p) {
            dfs(v,u);
            subs[u]+=subs[v];
        }
    }
    if (p) {
        edges.push_back(subs[u]*(n-subs[u]));
    }
}

void solve() {
    cin>>n;
    flush();
    for (int i=0,u,v;i<n-1;i++) {
        cin>>u>>v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    cin>>m;
    primes.resize(m);
    for (int i=0;i<m;i++) {
        cin>>primes[i];
    }
    dfs(1,0);
    sort(edges.begin(),edges.end());
    if ((int)primes.size() < n-1) {
        primes.resize(n-1,1);
    }
    sort(primes.begin(),primes.end());
    while ((int)primes.size() >= n) {
        int x=primes.back();
        x%=mod;
        primes.pop_back();
        primes.back()=(primes.back()*x)%mod;
    }
    for (int i=0;i<n-1;i++) {
        ans+=((primes[i]%mod)*(edges[i]%mod))%mod;
        ans%=mod;
    }
    cout<<ans<<"\n";
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin>>t;
    while (t--) {
        solve();
    }
    return 0;
}


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

#define int long long

#ifdef Harsha
    #define arr1(x,n)  cerr<<"("<<#x<<") :- "; disp(x,n);
    #define arr2(x,r,c) cerr<<"("<<#x<<") :- "; disp(x,r,c);
    #define dbg(...) cerr<<"\n("<<#__VA_ARGS__<<") :- "; dbg_out(__VA_ARGS__);
#else
    #define dbg(...)
    #define arr1(x,n)
    #define arr2(x,r,c);
#endif

namespace Debug {
    void dbg_out() {
        cerr<<"\n";
    }

    template<typename Head,typename ...Tail>
    void dbg_out(Head H,Tail... T) {
        cerr<<" "<<H;dbg_out(T...);
    }

    template<typename T>
    void dbg_out(const vector<T> v) {
        cerr<<"\n[";
        for (T i : v) {
            cerr<<" "<<i;
        }
        cerr<<" ]\n";
    }

    template<typename T>
    void disp(const T *a,int n) {
        cerr<<"\n[";
        for (int i=0;i<n;i++) {
            cerr<<" "<<a[i];
        }
        cerr<<" ]\n";
    }

    template<typename T>
    void disp(T **b,int r,int c) {
        for (int i=0;i<r;i++) {
            cerr<<"[";
            for (int j=0;j<c;j++) {
                cerr<<" "<<b[i][j];
            }
        cerr<<" ]\n";
        }
    }

    template<typename T>
    void dbg_out(const vector<vector<T>> v) {
        cerr<<"\n";
        for (vector<T> u : v) {
            cerr<<"[";
            for (T i : u) {
                cerr<<" "<<i;
            }
            cerr<<" ]\n";
        }
    }

    template<typename F,typename S>
    void dbg_out(const vector<pair<F,S>> u) {
        cerr<<"\n";
        for (pair<F,S> v : u) {
            cerr<<"[ "<<v.first<<" "<<v.second<<" ]\n";
        }
    }
} using namespace Debug;

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

int n,m;
vector<int> g[N];
vector<int> primes,edges;
vector<int> subs(N);
int ans;

void flush() {
    for (int i=0;i<=n;i++) {
        g[i].clear();
        subs[i]=0;
    }
    primes.clear();
    edges.clear();
    ans=0;
}

void dfs(int u,int p) {
    subs[u]=1;
    for (int v : g[u]) {
        if (v != p) {
            dfs(v,u);
            subs[u]+=subs[v];
        }
    }
    if (p) {
        edges.push_back(subs[u]*(n-subs[u]));
    }
}

void solve() {
    cin>>n;
    flush();
    for (int i=0,u,v;i<n-1;i++) {
        cin>>u>>v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    cin>>m;
    for (int i=0;i<m;i++) {
        int x;
        cin>>x;
        primes.push_back(x);
    }
    dfs(1,0);
    if ((int)primes.size() < n-1) {
        primes.resize(n-1,1);
    }
    sort(primes.begin(),primes.end());
    while ((int)primes.size() >= n) {
        int x=primes.back();
        x%=mod;
        primes.pop_back();
        primes.back()=(primes.back()*x)%mod;
    }
    sort(edges.begin(),edges.end());
    for (int i=0;i<n-1;i++) {
        ans+=(primes[i]*(edges[i]%mod))%mod;
        ans%=mod;
    }
    cout<<ans<<"\n";
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin>>t;
    while (t--) {
        solve();
    }
    return 0;
}


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

#define int long long

namespace Modular_Arithmetic {
    const int mod=1e9+7;

    int modpow(int x,int e) {
        int res=1;
        while (e) {
            if (e%2) {
                res=(res*x)%mod;
            }
            e/=2;
            x=(x*x)%mod;
        }
        return res%mod;
    }

    int modinv(int a) {
        return modpow(a,mod-2);
    }

    int mmull(int a,int b) {
        return ((a*b)%mod + mod)%mod;
    }

    int madd(int a,int b) {
        return ((a+b)%mod + mod)%mod;
    }

    int msub(int a,int b) {
        return (a-b+mod)%mod;
    }

    int mdiv(int a,int b) {
        return (a*modinv(b)+mod)%mod;
    }
} using namespace Modular_Arithmetic;

#ifdef Harsha
    #define arr1(x,n)  cerr<<"("<<#x<<") :- "; disp(x,n);
    #define arr2(x,r,c) cerr<<"("<<#x<<") :- "; disp(x,r,c);
    #define dbg(...) cerr<<"\n("<<#__VA_ARGS__<<") :- "; dbg_out(__VA_ARGS__);
#else
    #define dbg(...)
    #define arr1(x,n)
    #define arr2(x,r,c);
#endif

namespace Debug {
    void dbg_out() {
        cerr<<"\n";
    }

    template<typename Head,typename ...Tail>
    void dbg_out(Head H,Tail... T) {
        cerr<<" "<<H;dbg_out(T...);
    }

    template<typename T>
    void dbg_out(const vector<T> v) {
        cerr<<"\n[";
        for (T i : v) {
            cerr<<" "<<i;
        }
        cerr<<" ]\n";
    }

    template<typename T>
    void disp(const T *a,int n) {
        cerr<<"\n[";
        for (int i=0;i<n;i++) {
            cerr<<" "<<a[i];
        }
        cerr<<" ]\n";
    }

    template<typename T>
    void disp(T **b,int r,int c) {
        for (int i=0;i<r;i++) {
            cerr<<"[";
            for (int j=0;j<c;j++) {
                cerr<<" "<<b[i][j];
            }
        cerr<<" ]\n";
        }
    }

    template<typename T>
    void dbg_out(const vector<vector<T>> v) {
        cerr<<"\n";
        for (vector<T> u : v) {
            cerr<<"[";
            for (T i : u) {
                cerr<<" "<<i;
            }
            cerr<<" ]\n";
        }
    }

    template<typename F,typename S>
    void dbg_out(const vector<pair<F,S>> u) {
        cerr<<"\n";
        for (pair<F,S> v : u) {
            cerr<<"[ "<<v.first<<" "<<v.second<<" ]\n";
        }
    }
} using namespace Debug;

const int N=1e5+5;

int n,m;
vector<int> p;
vector<int> g[N];
int ans;
vector<int> subs(N);
vector<int> dp(N);
vector<int> edge;

void flush() {
    for (int i=1;i<=n;i++) {
        g[i].clear();
        subs[i]=0;
        dp[i]=0;
    }
    p.clear();
    ans=0;
    edge.clear();
}

void reroot(int u,int par) {
    if (par) {
        dp[u]+=dp[par]-subs[u];
    }
    for (int v : g[u]) {
        if (v != par) {
            int x=mmull(subs[v],dp[u]-subs[v]);
            edge.push_back(x);
            reroot(v,u);
        }
    }
}

void dfs(int u,int par) {
    subs[u]=1;
    for (int v : g[u]) {
        if (v != par) {
            dfs(v,u);
            subs[u]+=subs[v];
        }
    }
    dp[u]=subs[u];
}

void solve() {
    cin>>n;
    flush();
    for (int i=0,u,v;i<n-1;i++) {
        cin>>u>>v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    cin>>m;
    for (int i=0,x;i<m;i++) {
        cin>>x;
        p.push_back(x);
    }
    while (p.size() < n-1) {
        p.push_back(1);
    }
    sort(p.begin(),p.end());
    int x=1;
    while (p.size() > n-1) {
        x=mmull(x,p.back());
        p.pop_back();
    }
    p.back()=mmull(x,p.back());
    sort(p.begin(),p.end());
    dfs(1,0);
    reroot(1,0);
    sort(edge.begin(),edge.end());
    for (int i=0;i<n-1;i++) {
        ans=madd(ans,mmull(p[i],edge[i]));
    }
    cout<<ans<<"\n";
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin>>t;
    while (t--) {
        solve();
    }
    return 0;
}


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

#define int long long

namespace Modular_Arithmetic {
    //const int mod=998244353;
    const int mod=1e9+7;
    int modpow(int x,int e) {
        int res=1;
        while (e) {
            if (e%2) {
                res=(res*x)%mod;
            }
            e/=2;
            x=(x*x)%mod;
        }
        return res%mod;
    }

    int modinv(int a) {
        return modpow(a,mod-2);
    }

    int mmull(int a,int b) {
        return ((a%mod)*(b%mod))%mod;
    }

    int madd(int a,int b) {
        return ((a%mod)+(b%mod))%mod;
    }

    int msub(int a,int b) {
        return (a-b+mod)%mod;
    }

    int mdiv(int a,int b) {
        return (a*modinv(b)+mod)%mod;
    }
} using namespace Modular_Arithmetic;

const int N=1e5+5;

int n,m;
vector<int> g[N];
vector<int> subs(N);
vector<int> primes;
vector<int> edges;
int ans;

void flush() {
    for (int i=0;i<=n;i++) {
        g[i].clear();
        subs[i]=0;
    }
    edges.clear();
    ans=0;
}

void dfs(int u,int p) {
    subs[u]=1;
    for (int v : g[u]) {
        if (v != p) {
            dfs(v,u);
            subs[u]+=subs[v];
        }
    }
    if (p) {
        edges.push_back(mmull(subs[u],n-subs[u]));
    }
}

void solve() {
    cin>>n;
    flush();
    for (int i=0,u,v;i<n-1;i++) {
        cin>>u>>v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    cin>>m;
    primes.resize(m);
    for (int i=0;i<m;i++) {
        cin>>primes[i];
    }
    dfs(1,0);
    sort(edges.begin(),edges.end());
    if ((int)primes.size() < n-1) {
        primes.resize(n-1,1);
    }
    sort(primes.begin(),primes.end());
    while ((int)primes.size() >= n) {
        int x=primes.back();
        primes.pop_back();
        primes.back()=mmull(primes.back(),x);
    }
    for (int i=0;i<n-1;i++) {
        ans=madd(ans,mmull(primes[i],edges[i]));
    }
    cout<<ans<<"\n";
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin>>t;
    while (t--) {
        solve();
    }
    return 0;
}


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

#define int long long

namespace Modular_Arithmetic {
    const int mod=1e9+7;

    int modpow(int x,int e) {
        int res=1;
        while (e) {
            if (e%2) {
                res=(res*x)%mod;
            }
            e/=2;
            x=(x*x)%mod;
        }
        return res%mod;
    }

    int modinv(int a) {
        return modpow(a,mod-2);
    }

    int mmull(int a,int b) {
        return ((a*b)%mod + mod)%mod;
    }

    int madd(int a,int b) {
        return ((a+b)%mod + mod)%mod;
    }

    int msub(int a,int b) {
        return (a-b+mod)%mod;
    }

    int mdiv(int a,int b) {
        return (a*modinv(b)+mod)%mod;
    }
} using namespace Modular_Arithmetic;

#ifdef Harsha
    #define arr1(x,n)  cerr<<"("<<#x<<") :- "; disp(x,n);
    #define arr2(x,r,c) cerr<<"("<<#x<<") :- "; disp(x,r,c);
    #define dbg(...) cerr<<"\n("<<#__VA_ARGS__<<") :- "; dbg_out(__VA_ARGS__);
#else
    #define dbg(...)
    #define arr1(x,n)
    #define arr2(x,r,c);
#endif

namespace Debug {
    void dbg_out() {
        cerr<<"\n";
    }

    template<typename Head,typename ...Tail>
    void dbg_out(Head H,Tail... T) {
        cerr<<" "<<H;dbg_out(T...);
    }

    template<typename T>
    void dbg_out(const vector<T> v) {
        cerr<<"\n[";
        for (T i : v) {
            cerr<<" "<<i;
        }
        cerr<<" ]\n";
    }

    template<typename T>
    void disp(const T *a,int n) {
        cerr<<"\n[";
        for (int i=0;i<n;i++) {
            cerr<<" "<<a[i];
        }
        cerr<<" ]\n";
    }

    template<typename T>
    void disp(T **b,int r,int c) {
        for (int i=0;i<r;i++) {
            cerr<<"[";
            for (int j=0;j<c;j++) {
                cerr<<" "<<b[i][j];
            }
        cerr<<" ]\n";
        }
    }

    template<typename T>
    void dbg_out(const vector<vector<T>> v) {
        cerr<<"\n";
        for (vector<T> u : v) {
            cerr<<"[";
            for (T i : u) {
                cerr<<" "<<i;
            }
            cerr<<" ]\n";
        }
    }

    template<typename F,typename S>
    void dbg_out(const vector<pair<F,S>> u) {
        cerr<<"\n";
        for (pair<F,S> v : u) {
            cerr<<"[ "<<v.first<<" "<<v.second<<" ]\n";
        }
    }
} using namespace Debug;

const int N=1e5+5;

int n,m;
vector<int> p;
vector<int> g[N];
int ans;
vector<int> subs(N);
vector<int> dp(N);
vector<int> edge;

void flush() {
    for (int i=1;i<=n;i++) {
        g[i].clear();
        subs[i]=0;
        dp[i]=0;
    }
    p.clear();
    ans=0;
    edge.clear();
}

void reroot(int u,int par) {
    if (par) {
        dp[u]+=dp[par]-subs[u];
    }
    for (int v : g[u]) {
        if (v != par) {
            int x=mmull(subs[v],dp[u]-subs[v]);
            edge.push_back(x);
            reroot(v,u);
        }
    }
}

void dfs(int u,int par) {
    subs[u]=1;
    for (int v : g[u]) {
        if (v != par) {
            dfs(v,u);
            subs[u]+=subs[v];
        }
    }
    dp[u]=subs[u];
}

void solve() {
    cin>>n;
    flush();
    for (int i=0,u,v;i<n-1;i++) {
        cin>>u>>v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    cin>>m;
    for (int i=0,x;i<m;i++) {
        cin>>x;
        p.push_back(x);
    }
    while (p.size() < n-1) {
        p.push_back(1);
    }
    sort(p.begin(),p.end());
    int x=1;
    while (p.size() > n-1) {
        x=mmull(x,p.back());
        p.pop_back();
    }
    p.back()=mmull(x,p.back());
    dfs(1,0);
    reroot(1,0);
    sort(edge.begin(),edge.end());
    for (int i=0;i<n-1;i++) {
        ans=madd(ans,mmull(p[i],edge[i]));
    }
    cout<<ans<<"\n";
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin>>t;
    while (t--) {
        solve();
    }
    return 0;
}


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

#define int long long

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

int n,m;
vector<int> g[N];
vector<int> subs(N);
vector<int> primes;
vector<int> edges;
int ans;

void flush() {
    for (int i=0;i<=n;i++) {
        g[i].clear();
        subs[i]=0;
    }
    edges.clear();
    ans=0;
}

void dfs(int u,int p) {
    subs[u]=1;
    for (int v : g[u]) {
        if (v != p) {
            dfs(v,u);
            subs[u]+=subs[v];
        }
    }
    if (p) {
        edges.push_back(subs[u]*(n-subs[u]));
    }
}

void solve() {
    cin>>n;
    flush();
    for (int i=0,u,v;i<n-1;i++) {
        cin>>u>>v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    cin>>m;
    primes.resize(m);
    for (int i=0;i<m;i++) {
        cin>>primes[i];
    }
    dfs(1,0);
    sort(edges.begin(),edges.end());
    if ((int)primes.size() < n-1) {
        primes.resize(n-1,1);
    }
    sort(primes.begin(),primes.end());
    while ((int)primes.size() >= n) {
        int x=primes.back();
        primes.pop_back();
        primes.back()=(primes.back()*x)%mod;
    }
    for (int i=0;i<n-1;i++) {
        ans+=((primes[i]%mod)*edges[i])%mod;
        ans%=mod;
    }
    cout<<ans<<"\n";
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin>>t;
    while (t--) {
        solve();
    }
    return 0;
}


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

#define int long long

namespace Modular_Arithmetic {
    const int mod=1e9+7;

    int modpow(int x,int e) {
        int res=1;
        while (e) {
            if (e%2) {
                res=(res*x)%mod;
            }
            e/=2;
            x=(x*x)%mod;
        }
        return res%mod;
    }

    int modinv(int a) {
        return modpow(a,mod-2);
    }

    int mmull(int a,int b) {
        return (a*b + mod)%mod;
    }

    int madd(int a,int b) {
        return (a+b+mod)%mod;
    }

    int msub(int a,int b) {
        return (a-b+mod)%mod;
    }

    int mdiv(int a,int b) {
        return (a*modinv(b)+mod)%mod;
    }
} using namespace Modular_Arithmetic;

#ifdef Harsha
    #define arr1(x,n)  cerr<<"("<<#x<<") :- "; disp(x,n);
    #define arr2(x,r,c) cerr<<"("<<#x<<") :- "; disp(x,r,c);
    #define dbg(...) cerr<<"\n("<<#__VA_ARGS__<<") :- "; dbg_out(__VA_ARGS__);
#else
    #define dbg(...)
    #define arr1(x,n)
    #define arr2(x,r,c);
#endif

namespace Debug {
    void dbg_out() {
        cerr<<"\n";
    }

    template<typename Head,typename ...Tail>
    void dbg_out(Head H,Tail... T) {
        cerr<<" "<<H;dbg_out(T...);
    }

    template<typename T>
    void dbg_out(const vector<T> v) {
        cerr<<"\n[";
        for (T i : v) {
            cerr<<" "<<i;
        }
        cerr<<" ]\n";
    }

    template<typename T>
    void disp(const T *a,int n) {
        cerr<<"\n[";
        for (int i=0;i<n;i++) {
            cerr<<" "<<a[i];
        }
        cerr<<" ]\n";
    }

    template<typename T>
    void disp(T **b,int r,int c) {
        for (int i=0;i<r;i++) {
            cerr<<"[";
            for (int j=0;j<c;j++) {
                cerr<<" "<<b[i][j];
            }
        cerr<<" ]\n";
        }
    }

    template<typename T>
    void dbg_out(const vector<vector<T>> v) {
        cerr<<"\n";
        for (vector<T> u : v) {
            cerr<<"[";
            for (T i : u) {
                cerr<<" "<<i;
            }
            cerr<<" ]\n";
        }
    }

    template<typename F,typename S>
    void dbg_out(const vector<pair<F,S>> u) {
        cerr<<"\n";
        for (pair<F,S> v : u) {
            cerr<<"[ "<<v.first<<" "<<v.second<<" ]\n";
        }
    }
} using namespace Debug;

const int N=1e5+5;

int n,m;
vector<int> g[N];
vector<int> primes,edges;
vector<int> subs(N);
int ans;

void flush() {
    for (int i=0;i<=n;i++) {
        g[i].clear();
        subs[i]=0;
    }
    primes.clear();
    edges.clear();
    ans=0;
}

void dfs(int u,int p) {
    subs[u]=1;
    for (int v : g[u]) {
        if (v != p) {
            dfs(v,u);
            subs[u]+=subs[v];
        }
    }
    if (p) {
        edges.push_back(mmull(subs[u],n-subs[u]));
    }
}

void solve() {
    cin>>n;
    flush();
    for (int i=0,u,v;i<n-1;i++) {
        cin>>u>>v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    cin>>m;
    for (int i=0;i<m;i++) {
        int x;
        cin>>x;
        primes.push_back(x);
    }
    dfs(1,0);
    if ((int)primes.size() < n-1) {
        primes.resize(n-1,1);
    }
    sort(primes.begin(),primes.end());
    while ((int)primes.size() >= n) {
        int x=primes.back();
        primes.pop_back();
        primes.back()=mmull(primes.back(),x);
    }
    sort(primes.begin(),primes.end());
    sort(edges.begin(),edges.end());
    for (int i=0;i<n-1;i++) {
        ans=madd(ans,mmull(primes[i],edges[i]));
    }
    cout<<ans<<"\n";
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin>>t;
    while (t--) {
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std ;
int main()
{
    int n ;
    int x ,s=0 ;

    cin>>n ;
    for(int i=1 ;i<=n ;i++)
     { cin>>x ;
       s+=x ;  }

      cout<<s/n<<endl ;
      return 0 ;
}


#include<bits/stdc++.h>
using namespace std ;
int main()
{
    int n ;
    int x ,s=0 ;

    cin>>n ;
    for(int i=1 ;i<=n ;i++)
     { cin>>x ;
       s+=x ;  }

      cout<<(float)s/n<<endl ;
      return 0 ;
}


#include<bits/stdc++.h>
using namespace std ;
int main()
{
    int n ;
    int x ,s=0 ;

    cin>>n ;
    for(int i=1 ;i<=n ;i++)
     { cin>>x ;
       s+=x ;  }

      cout<<fired<<setprecision(12)<<s/n<<endl ;
      return 0 ;
}


#include<stdio.h>
#include<math.h>
int main()
{
    int n, a[100], sum=0;
    double x;
    scanf("%d", &n);
    for(int i=0; i<n; i++){
        scanf("%d", &a[i]);
        sum=sum+a[i];
    }
    x=(double)sum/(double)n;
    printf("%.12lf", x);
    return 0;
}


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

int main(){
    long double sum = 0;
    int n; cin >> n;
    for (int i = 0; i < n; i++){
        int a; cin >> a;
        sum+=a;
    }
    double kek = n,pidor;
    cout  << fixed << setprecision(5) << sum/n;

    return 0;
}


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

int main(){
    long double sum;
    int n; cin >> n;
    for (int i = 0; i < n; i++){
        int a; cin >> a;
        sum+=a;
    }
    double kek = n;
    cout  << fixed << setprecision(5) << sum/n;

    return 0;
}


#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    int n;
    double x=0;
    cin >> n;
    int ar[n];
    for (int i = 0; i < n; i++)
    {
        cin >> ar[i];
        x = x + ar[i];
    }
    x = x / n;
    cout << fixed;
    cout << setprecision(6);
    cout << x;
    return 0;
}


#include <beats/stdc++.h>
usingname std;
double n, x = 0;
cin >> n;
double ar[n];
for (int i = 0; i < n; i ++)
{
    cin >> ar[i];
    x = x + ar[i];
}
x = x / n;
cout << fixed;
cout << setprecision(6);
cout << x;


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

int main(int argc, char const *argv[])
{
	int n;
	cin>>n;
	long double sum=0,p;
	for(int i=0;i<n;i++){
		cin>>p;
		sum+=p;
	}
	cout<<(sum/n)<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int i;
    double n,a,sum=0.0;
    while(scanf("%lf",&n)!= EOF){
         for(i=1;i<n;i++){
        scanf("%lf",&a);
        sum+=a;
    }
        printf("%.12lf\n",sum/n);
  }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int i;
    double n,a,sum=0.0;
    while(scanf("%lf",&n)!= EOF){
         for(i=1;i<=n;i++){
        scanf("%lf",&a);
        sum+=a;
    }
        printf("%.12lf\n",sum/n);
  }
    return 0;
}


#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
	double n;
	cin>>n;
	double f;
	double d=0;
	for(int i=1;i<=n;i++){
		cin>>f;
		d+=f;
	}
	cout<<d/n;
	return 0;
}


#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
	double n;
	cin>>n;
	double f;
	double d;
	for(int i=1;i<=n;i++){
		cin>>f;
		d+=f;
	}
	cout<<d/n;
	return 0;
}


#include<iostream>
#include<stdio.h>
using namespace std;
main(){
float n,i,l,s=0;
scanf("%f",&n);
for(i=0;i<n;i++){
scanf("%f",&l);
s+=l;
}
s=s/n;
printf("%.5f",s);
}


#include<stdio.h>
int main()
{
	int i,n;
	float k,a[100],sum=0;
	scanf("%d",&n);
	for(i=1;i<=n;i++)
	{
		scanf("%f",&a[i]);
		sum=sum+a[i];
	}
	k=sum/n;
	printf("%.5f",k);
}


#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include <set>
#include <map>
#include <numeric>
#include <bitset>
#include <sstream>
#include <iomanip>
#include<fstream>
#include <cmath>
#include <algorithm>
#include <iterator>
using namespace std;
void fast()
{
	ios_base::sync_with_stdio(0);
	cin.tie(NULL), cout.tie(NULL);
}
long long fact(int num) {
	if (num == 1 || num == 0)return 1;
	return num*fact(num - 1);
}
int main()
{
	fast();  long double ans = 0, n, x;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> x; ans += x;
	}
	cout <<fixed<<setprecision(12)<< ans / (100 * n) * 100 << endl;
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
int main()
{
	int n;
	while (scanf("%d", &n) != EOF)
	{
		int x;
		int sum = 0;
		for (int i = 0; i<n; i++)
		{
			scanf("%d", &x);
			sum += x;
		}
		double p = double(sum) / n;
		printf("%.12lf\n", p);
	}
	return 0;
}


package main

import (
	"bufio"
	"bytes"
	"fmt"
	"os"
	"strings"
	"unicode"
)

func main() {
	n := readi()
	values := readis(n)
	fmt.Fprintf(writer, "%.6f\n", median(values))
	writer.Flush()
}

func median(values []int) float64 {
	res := 0
	for _, v := range values {
		res += v
	}
	return float64(res) / float64(len(values))
}

func perfectPermutation(n int) string {
	if n%2 == 1 {
		return "-1"
	}
	values := make([]int, 0)
	for i := 0; i < n; i++ {
		values = append(values, n-i)
	}
	str := ""
	for _, v := range values {
		str += fmt.Sprint(v, " ")
	}
	return str
}

func isNextPrime(n, m int) bool {
	for i := n + 1; i < m; i++ {
		if isPrime(i) {
			return false
		}
	}
	return isPrime(m)
}

func isPrime(n int) bool {
	for x := 2; x*x <= n; x++ {
		if n%x == 0 {
			return false
		}
	}
	return n > 1
}

func withSameCase(word string) string {
	lowerCount := 0
	for _, letter := range word {
		if unicode.IsLower(letter) {
			lowerCount++
		}
	}
	if lowerCount*2 >= len(word) {
		return strings.ToLower(word)
	}
	return strings.ToUpper(word)
}

func solve(matrix [][]int) []string {
	n := 3
	state := make([][]byte, n)
	for i := range state {
		state[i] = make([]byte, n)
		for j := range matrix[i] {
			state[i][j] = byte(matrix[i][j])
			if i > 0 {
				state[i][j] += byte(matrix[i-1][j])
			}
			if i+1 < n {
				state[i][j] += byte(matrix[i+1][j])
			}
			if j > 0 {
				state[i][j] += byte(matrix[i][j-1])
			}
			if j+1 < n {
				state[i][j] += byte(matrix[i][j+1])
			}
			state[i][j] &= 1
			state[i][j] ^= 1
			state[i][j] += '0'
		}
	}
	res := make([]string, n)
	for i := range res {
		res[i] = string(state[i])
	}
	return res
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func abs64(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func max64(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

var (
	reader = bufio.NewReaderSize(os.Stdin, 1<<13)
	writer = bufio.NewWriterSize(os.Stdout, 1<<13)
)

func readi() int {
	return int(readi64())
}

func read2i() (int, int) {
	return readi(), readi()
}

func read3i() (int, int, int) {
	return readi(), readi(), readi()
}

func read4i() (int, int, int, int) {
	return readi(), readi(), readi(), readi()
}

func readis(n int) []int {
	res := make([]int, n)
	for i := range res {
		res[i] = readi()
	}
	return res
}

func readi64() int64 {
	b, err := reader.ReadByte()
	for !isValid(b, err) {
		b, err = reader.ReadByte()
	}
	sign, res := int64(1), int64(0)
	if b == '-' {
		sign *= -1
		b, err = reader.ReadByte()
	}
	for isValid(b, err) {
		res = res*10 + int64(b-'0')
		b, err = reader.ReadByte()
	}
	return res * sign
}

func readi64s(n int) []int64 {
	res := make([]int64, n)
	for i := range res {
		res[i] = readi64()
	}
	return res
}

func readline() string {
	res := new(bytes.Buffer)
	for {
		line, isPrefix, err := reader.ReadLine()
		if err != nil {
			panic(err)
		}
		res.Write(line)
		if !isPrefix {
			break
		}
	}
	return res.String()
}

func isValid(b byte, err error) bool {
	return err == nil && (('0' <= b && b <= '9') || b == '-')
}


package main

import (
	"bufio"
	"fmt"
	"os"
)

func I(i interface{}, delim string) error {
	_, err := fmt.Fscanf(r, "%v"+delim, i)
	return err
}

func O(o interface{}, delim string) {
	fmt.Fprintf(w, "%v"+delim, o)
}

var (
	r *bufio.Reader
	w *bufio.Writer
)

func init() {
	if f, err := os.Open("in.txt"); err == nil {
		r = bufio.NewReader(f)
	} else {
		r = bufio.NewReader(os.Stdin)
	}
	w = bufio.NewWriter(os.Stdout)
}

func main() {
	defer w.Flush()
	var n int64
	for I(&n, "\n") == nil {
		solve(n)
	}
}

func solve(n int64) {
	var ans, o float64
	for i := int64(0); i < n; i++ {
		s := " "
		if i == n-1 {
			s = "\n"
		}
		I(&o, s)
		ans += o
	}
	ans /= float64(n)
	O(ans, "\n")
}


package main

import (
	"fmt"
)

func main() {
	var n, x, sum int
	// scr := bufio.NewReader(os.Stdin)
	// ocr := bufio.NewWriter(os.Stdout)

	fmt.Scanln(&n)

	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &x)
		sum += x
	}

	fmt.Println(float64(sum) / float64(n))
}


package main

import (
	"bufio"
	"fmt"
	"os"
)

func main(){
	var n int
	var p float64
	in := bufio.NewReader(os.Stdin)
	fmt.Fscan(in,&n)
	sum := 0.0
	for i:=0;i<n;i++{
		fmt.Fscan(in,&p)
		sum +=p
	}
	fmt.Printf("%0.6f\n",sum/float64(n))
}


// B. Drinks
/*
Little Vasya loves orange juice very much. That's why any food and drink in
his kitchen necessarily contains orange juice. There are n drinks in his
fridge, the volume fraction of orange juice in the i-th drink equals pi percent.

One day Vasya decided to make himself an orange cocktail. He took equal
proportions of each of the n drinks and mixed them. Then he wondered,
how much orange juice the cocktail has.

Find the volume fraction of orange juice in the final drink.

Input
The first input line contains a single integer n (1 ≤ n ≤ 100) —
the number of orange-containing drinks in Vasya's fridge. The second line
contains n integers pi (0 ≤ pi ≤ 100) — the volume fraction of orange juice
in the i-th drink, in percent. The numbers are separated by a space.

Output
Print the volume fraction in percent of orange juice in Vasya's cocktail.
The answer will be considered correct if the absolute or relative error
does not exceed 10  - 4.

Examples
Input
3
50 50 100

Output
66.666666666667

Input
4
0 25 50 75

Output
37.500000000000

Note
Note to the first sample: let's assume that Vasya takes x milliliters of each
drink from the fridge. Then the volume of pure juice in the cocktail will equal
milliliters. The total cocktail's volume equals 3·x milliliters, so the volume
fraction of the juice in the cocktail equals , that is, 66.(6) percent.
*/
package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	input1, _ := reader.ReadString('\n')
	n, _ := strconv.ParseFloat(strings.TrimSpace(input1), 32)

	input2, _ := reader.ReadString('\n')
	p := strings.Split(strings.TrimSpace(input2), " ")

	total := 0.0
	for _, v := range p {
		s, _ := strconv.ParseFloat(v, 32)
		total += s
	}
	fmt.Print((total / (n * 100.0)) * 100.0)
}


package main

import(
    "fmt"
)

func main(){
    var (
        n,i int
        res float64
    )
    res =0
    fmt.Scan(&n)
    arr := make([]int,n)
    for i=0;i<n;i++{
        fmt.Scan(&arr[i])
    }
    for i=0;i<n;i++{
        res += float64(arr[i])/100
    }
    fmt.Println(res/float64(n)*100)
}


package main

import "fmt"

func main() {
	var n, p int
	fmt.Scan(&n)

	cnt := 0
	for range make([]struct{}, n) {
		fmt.Scan(&p)
		cnt += p
	}
	fmt.Println(float64(cnt) / float64(n))
}


package main
import (
	"fmt"
)
func main() {
	var n int
	fmt.Scan(&n)
	tem:=0
	var sum float64
	sum =0
	for k:=0;k<n;k++ {
		fmt.Scan(&tem)
		sum += float64(tem)
	}
	sum /= float64(n)
	fmt.Printf("%.6f",sum)

}


package main

import "fmt"

func main() {
    var n int
    var f,total float64
    fmt.Scan(&n)
    for i := 0; i < n; i++ {
        fmt.Scan(&f)
        total += f
    }
    fmt.Println(total/float64(n))
}


package main

import (
	"fmt"
	"bufio"
	"strconv"
	"os"
)

// var fi, _ = os.Open("input.txt")
// var fo, _ = os.Create("output.txt")

var in = bufio.NewScanner(os.Stdin)
var out = bufio.NewWriter(os.Stdout)

func main() {

	defer out.Flush()
	in.Split(bufio.ScanWords)

	var n int = NextInt()
	var sum int = 0
	for i := 0; i < n; i++ {
		sum += NextInt()
	}

	fmt.Fprintln(out, float64(sum) / float64(n))
}

func Max(x, y int) int {
	if x < y {
		return y
	}
	return x
}

func Abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

func Next() string{
	if !in.Scan() {
		panic("Scan error")
	}
	return in.Text()
}

func NextInt() int{
	ret, _ := strconv.Atoi(Next())
	return ret
}


package main

import (
	"bufio"
	"fmt"
	"os"
)

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

func scan(a ...interface{}) {
	fmt.Fscan(reader, a...)
}

func Println(a ...interface{}) {
	fmt.Println(a...)
}

func main() {
	var n int
	scan(&n)
	var V float64 = 0
	for i:=0;i<n;i++{
		var w float64
		scan(&w)
		V+=w
	}
	Println(V/float64(n))
}


package main
import( "fmt"
          )
func main(){
          var t;
          fmt.Scanln(&t);
          for i:=0;i<t;i++ {
                    var n,sum int;
                    var avg float64;
                    fmt.Scanln(&n);
                    var a[n] int;
                    for j:=0;j<n;j++{
                              fmt.Scan(&a[j]);
                              sum+=a[j];
                    }
                    avg=sum/n;
                    fmt.Print(avg)
          }

}


package main
import( "fmt"
          )
func main(){
          var t
          fmt.Scanln(&t)
          for i:=0;i<t;i++ {
                    var n,sum int;
                    var avg float64;
                    fmt.Scanln(&n)
                    var a[n] int;
                    for j:=0;j<n;j++{
                              fmt.Scan(&a[j])
                              sum+=a[j]
                    }
                    avg=sum/n
                    fmt.Print(avg)
          }

}


package main
import "fmt"
func main(){
          var t

          fmt.Scanln(&t);

          for i:=0;i<t;i++{
                    var n,sum int

                    fmt.Scanln(&n)
                    var avg float64
                    var a[n] int
                    for j:=0;j<n;j++{
                              fmt.Scanln(&a[j])
                              sum+=a[j]
                    }
                    avg=sum/n
                    fmt.Print(avg)
          }
}


package main
import "fmt"
func main(){
          var t

          fmt.Scanln(&t)

          for i:=0;i<t;i++{
                    var n,sum int

                    fmt.Scanln(&n)
                    var avg float64
                    var a[n] int
                    for j:=0;j<n;j++{
                              fmt.Scanln(&a[j])
                              sum+=a[j]
                    }
                    avg=sum/n
                    fmt.Print(avg)
          }
}


package main
import( "fmt")
func main(){
          var t
          fmt.Scanln(&t)
          for i:=0;i<t;i++ {
                    var n,sum int
                    var avg float64
                    fmt.Scanln(&n)
                    var a[n] int
                    for j:=0;j<n;j++{
                              fmt.Scan(&a[j])
                              sum+=a[j]
                    }
                    avg=sum/n
                    fmt.Print(avg)
          }

}


package main
import "fmt"
func main(){
          var t
          fmt.Scanln(&t)
          for i:=0;i<t;i++ {
                    var n,sum int
                    var avg float64
                    fmt.Scanln(&n)
                    var a[n] int
                    for j:=0;j<n;j++{
                              fmt.Scanln(&a[j])
                              sum+=a[j];
                    }
                    avg=sum/n
                    fmt.Println(avg)
          }
}


package main
import( "fmt"
          )
func main(){
          var t
          fmt.Scanln(&t)
          for i:=0;i<t;i++ {
                    var n,sum,avg int
                    fmt.Scanln(&n)
                    var a[n] int
                    for j:=0;j<n;j++{
                              fmt.Scan(&a[j])
                              sum+=a[j]
                    }
                    avg=sum/n
                    fmt.Print(avg)
          }

}


package main
import "fmt"
func main(){
          var t
          fmt.Scanln(&t)
          for i:=0;i<t;i++ {
                    var n,sum int
                    var avg float64
                    fmt.Scanln(&n)
                    var a[n] int
                    for j:=0;j<n;j++{
                              fmt.Scan(&a[j])
                              sum+=a[j]
                    }
                    avg=sum/n
                    fmt.Print(avg)
          }

}


package main
import( "fmt"
          )
func main(){
          var t
          fmt.scanln(&t)
          for i:=0;i<t;i++ {
                    var n,sum int
                    var avg float64
                    fmt.scanln(&n)
                    var a[n] int
                    for j:=0;j<n;j++{
                              fmt.scan(&a[j])
                              sum+=a[j]
                    }
                    avg=sum/n
                    fmt.print(avg)
          }

}          v


package main
import( "fmt"
          )
func main(){
          var t
          fmt.Scanln(&t)
          for i:=0;i<t;i++ {
                    var n,sum int,
                    var avg float32
                    fmt.Scanln(&n)
                    var a[n] int
                    for j:=0;j<n;j++{
                              fmt.Scan(&a[j])
                              sum+=a[j]
                    }
                    avg=sum/n
                    fmt.Println(avg)
          }

}


package main
import "fmt"
func main(){
          var t

          fmt.Scanln(&t)

          for i:=0;i<t;i++ {
                    var n,sum int

                    fmt.Scanln(&n)
                    var avg float64
                    println("abcd")
                    var a[n] int

                    for j:=0;j<n;j++{
                              fmt.Scanln(&a[j])
                              sum+=a[j]
                    }
                    avg=sum/n
                    fmt.Print(avg)
          }
}


package main
import "fmt"
func main(){
          var t

          fmt.Scanln(&t)

          for i:=0;i<t;i++ {
                    var n,sum int

                    fmt.Scanln(&n)
                    var avg float64

                    var a[n] int

                    for j:=0;j<n;j++{
                              fmt.Scanln(&a[j])
                              sum+=a[j]
                    }
                    avg=sum/n
                    fmt.Println(avg)
          }
}


package main
import( "fmt")
func main(){
          var t;
          fmt.Scanln(&t);
          for i:=0;i<t;i++ {
                    var n,sum int;
                    var avg float64;
                    fmt.Scanln(&n);
                    var a[n] int;
                    for j:=0;j<n;j++{
                              fmt.Scan(&a[j]);
                              sum+=a[j];
                    }
                    avg=sum/n;
                    fmt.Print(avg);
          }

}


package main

import "fmt"

func main() {
	var n,x int
	var r float64
	fmt.Scan(&n)
	for i:=0; i<n; i++ {
		fmt.Scan(&x)
		r += x
	}
	fmt.Println(r/float64(n))
}


package main
import "fmt"
func main(){
          var t
          fmt.Scanln(&t)
          for i:=0;i<t;i++ {
                    var n,sum int
                    var avg float64
                    fmt.Scanln(&n)
                    var a[n] int
                    for j:=0;j<n;j++{
                              fmt.Scan(&a[j])
                              sum+=a[j]
                    }
                    avg=sum/n
                    fmt.Print(avg)
          }
}


package main

import "fmt"

func main() {
	var n int
	var r,x float64
	fmt.Scan(&n)
	for i:=0; i<n; i++ {
		fmt.Scan(&x)
		r += x
	}
	fmt.Println(r/float64(n))
}


package main
import "fmt"
func main(){
          var t,

          fmt.Scanln(&t)

          for i:=0;i<t;i++{
                    var n,sum int

                    fmt.Scanln(&n)
                    var avg float64
                    var a[n] int
                    for j:=0;j<n;j++{
                              fmt.Scanln(&a[j])
                              sum+=a[j]
                    }
                    avg=sum/n
                    fmt.Print(avg)
          }
}


package main
import( "fmt"
          )
func main(){
          var t
          fmt.Scanln(&t)
          for i:=0;i<t;i++ {
                    var n,sum int
                    var avg float64
                    fmt.Scanln(&n)
                    var a[n] int
                    for j:=0;j<n;j++{
                              fmt.Scan(&a[j])
                              sum+=a[j]
                    }
                    avg=sum/n
                    fmt.Print(avg)
          }

}


package main

import "fmt"

func main() {
	var n,r,x int
	fmt.Scan(&n)
	for i:=0; i<n; i++ {
		fmt.Scan(&x)
		r += x
	}
	fmt.Println(r/float64(n))
}


package main
import( "fmt"
          )
func main(){
          var t
          fmt.Scanln(&t)
          for i:=0;i<t;i++ {
                    var n,sum int,
                    var avg float64
                    fmt.Scanln(&n)
                    var a[n] int
                    for j:=0;j<n;j++{
                              fmt.Scan(&a[j])
                              sum+=a[j]
                    }
                    avg=sum/n
                    fmt.Print(avg)
          }

}


package main
import "fmt"
func main(){
          var t
          fmt.Scanln(&t)
          for i:=0;i<t;i++ {
                    var n,sum int
                    var avg float64
                    fmt.Scanln(&n)
                    var a[n] int
                    for j:=0;j<n;j++{
                              fmt.Scanln(&a[j]),
                              sum+=a[j]
                    }
                    avg=sum/n
                    fmt.Println(avg)
          }
}


package main
import( "fmt"
          )
func main(){
          var t
          fmt.scanln(&t)
          for i:=0;i<t;i++ {
                    var n,sum int
                    var avg float64
                    fmt.scanln(&n)
                    var a[n] int
                    for j:0;j<n;j++{
                              fmt.scan(&a[j])
                              sum+=a[j]
                    }
                    avg=sum/n
                    fmt.print(avg)
          }

}


package main
import "fmt"
func main(){
          var t

          fmt.Scanln(&t);

          for i:=0;i<t;i++{
                    var n,sum int

                    fmt.Scanln(&n)
                    var a[n] int
                    for j:=0;j<n;j++{
                              fmt.Scanln(&a[j])
                              sum+=a[j]
                    }
                    fmt.Print(sum/float64(n))
          }
}


package main

import(
	"fmt"
)

func main() {
	cnt := 0
	sum := 0
	fmt.Scanf("%d", &cnt)
	for i := 0; i < cnt; i ++ {
		tmp := 0
		fmt.Scanf("%d", &tmp)
		sum += tmp
	}
	fmt.Printf("%.12f\n",float64(sum)/float64(cnt))
}


package main

import(
	"fmt"
)

func main() {
	cnt := 0
	sum := 0
	fmt.Scanf("%d", &cnt)
	tmp := 0
	for i := 0; i < cnt; i++ {
		fmt.Scanf("%d", &tmp)
		fmt.Println(tmp)
		sum += tmp
	}
	fmt.Printf("%.12f\n",float64(sum)/float64(cnt))
}


package main

import(
	"fmt"
)

func main() {
	cnt := 0
	sum := 0
	fmt.Scanf("%d", &cnt)
	tmp := 0
	for i := 0; i < cnt+1; i++ {
		fmt.Scanf("%d", &tmp)
		fmt.Println(tmp)
		sum += tmp
	}
	fmt.Printf("%.12f\n",float64(sum)/float64(cnt))
}


package main

import(
	"fmt"
)

func main() {
	cnt := 0
	sum := 0
	fmt.Scanf("%d", &cnt)
	tmp := 0
	for i := 0; i < cnt; i++ {
		fmt.Scanf("%d", &tmp)
		sum += tmp
	}
	fmt.Printf("%.12f\n",float64(sum)/float64(cnt))
}


package main

import(
	"fmt"
)

func main() {
	cnt := 0
	sum := 0
	fmt.Scanf("%d", &cnt)
	for i := 0; i < cnt; i ++ {
		tmp := 0
		fmt.Scanf("%d", &tmp)
		sum += tmp
	}
	fmt.Printf("%f\n",float64(sum)/float64(cnt))
}


package main

import(
	"fmt"
)

func main() {
	cnt := 0
	sum := 0
	fmt.Scanf("%d", &cnt)
	tmp := 0
	for i := 0; i < cnt+1; i++ {
		fmt.Scanf("%d", &tmp)
		sum += tmp
	}
	fmt.Printf("%.12f\n",float64(sum)/float64(cnt))
}


package main

import "fmt"

func main() {
	var n float32
	var a int
	var count float32 = 0
	fmt.Scan(&n)
	var b int = int(n)
	for i := 0; i < b; i++ {
		fmt.Scan(&a)
		count += float32(a)
	}
	var result float32 = count / n
	fmt.Print(result)
}


package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scan(&n)

	var o int = 0
	var t int = 0

	for i := 0; i < n; i++ {
		var p int
		fmt.Scan(&p)

		o += p
		t += 100
	}

	fmt.Printf("%.10f\n", float64(100*o) / float64(t))
}


package main

import "fmt"

func main() {
	var n int
	fmt.Scanf("%d", &n)
	var x [101]int
	sum := 0

	for i :=0; i<n +1 ; i++ {
		fmt.Scanf("%d", &x[i])
		sum += x[i]
	}

	c := float64(sum)/float64(n)

	fmt.Println(c)
}


package main

import "fmt"

func main() {
	var n int
	fmt.Scanf("%d", &n)
	var x [100]int
	sum := 0

	for i :=0; i<n +1 ; i++ {
		fmt.Scanf("%d", &x[i])
		sum += x[i]
	}

	c := float64(sum)/float64(n)

	fmt.Println(c)
}


package main

import "fmt"

func main() {
	var n int
	fmt.Scan(&n)

	sum, v := 0, 0
	for i := 0; i < n; i++ {
		fmt.Scan(&v)
		sum += v
	}

	fmt.Printf("%.12f\n", float64(sum)/float64(n))
}


package main

import (
	"bufio"
	"fmt"
	"os"
)

var n int

func main() {
	stdin := os.Stdin
	// stdin, _ = os.Open("1.in")
	reader := bufio.NewReaderSize(stdin, 1<<20)
	fmt.Fscanf(reader, "%d\n", &n)

	var x, sum int
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &x)
		sum += x
	}

	fmt.Printf("%.12f", float64(sum)/float64(n))
}


package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	in := bufio.NewReader(os.Stdin)
	var n, ic, c uint32
	fmt.Fscanf(in, "%d\n", &n)
	for i:=uint32(0); i < n; i++{
		fmt.Fscanf(in, "%d", &ic)
		c+= ic
	}
	fmt.Println(float64(c) / float64(n * 100) * 100)
}


package main

import "fmt"

func main() {
	var n int
	var x, r float64
	fmt.Scan(&n)
	for i:=0; i<n; i++ {
		fmt.Scan(&x)
		r += x
	}
	fmt.Println(r/float64(n))
}


package main

import "fmt"

func main() {
    var n int
    var f,total float64
    fmt.Scan(&n)
    for i := 0; i < n; i++ {
        fmt.Scan(&f)
        total += f
    }
    fmt.Println(total/float64(n))
}


package main

import "fmt"

func main() {
	var n float32
	var a int
	var count float32 = 0
	fmt.Scan(&n)
	var b int = int(n)
	for i := 1; i <= b; i++ {
		fmt.Scan(&a)
		count += float32(a)
	}
	var result float32 = count / n
	fmt.Print(result)
}


package main

import (
	"bufio"
	"fmt"
	"os"
)

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }

func main() {
	defer writer.Flush()

	var (
		n     int
		p     float64
		total float64
	)

	scanf("%d\n", &n)

	for i := 0; i < n; i++ {
		scanf("%f", &p)
		total += p
	}

	printf("%f\n", total/float64(n))
}


package main

import (
	"bufio"
	"fmt"
	"strconv"
	"strings"
	"os"
)

func main() {
	var n int
	var s int64
	fmt.Scanln(&n)
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()

	s = 0
	for _, v := range strings.Split(scanner.Text(), " ") {
		current, _ := strconv.ParseInt(v, 10, 0)
		s += current
	}
	fmt.Println(float64(s) / float64(n))
}


package main

import (
	"fmt"
)

/*
1. Read first input and save no. test case.  fmt.Scan(&n)
2. Read the inputs and sum it up and fins the average.

*/

func main() {
	var n int
	_, err := fmt.Scan(&n)
	if err != nil {

	}

	var value float64
	var sum float64

	for i:=0; i<n; i++ {

		_, err := fmt.Scan(&value)
		if err != nil {

		}
		sum += value
	}

	fmt.Println(sum/float64(n))
}


package main
import(
    "fmt"
)
func main() {
	var a int
	var c,f float64 = 0.0,0.0
	fmt.Scan(&a)
	var aa float64 = float64(a) * 100.0
	for i:=0; i < a; i++ {
	    fmt.Scan(&f)
	    c+=f
	}
	fmt.Println(c/aa)
}


package main
import(
    "fmt"
)
func main() {
	var a int
	var c,f float64 = 0.0,0.0
	fmt.Scan(&a)
	var aa float64 = float64(a) * 100.0
	for i:=0; i < a; i++ {
	    fmt.Scan(&f)
	    c+=f
	}
	fmt.Println((c/aa)*100.0)
}


package main

import "fmt"

func main() {
    var n int
    fmt.Scan(&n)
    var oj 	float32
    for i := 0 ; i < n ; i++ {
        var x int
        fmt.Scan(&x);
        oj += float32(x)/100.0
    }
    fmt.Println(oj/float32(n)*100)
}


package main

import (
	"fmt"
)

func main() {
	var n, sum, temp int
	var result float64
	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		fmt.Scan(&temp)
		sum += temp
	}
	result = float64(sum) / float64(n)
	fmt.Printf("%.12f\n", result)
}


package main

import (
    "strconv"
    "bufio"
    "os"
    "fmt"
)
// var file, _ = os.Open("input.txt")
// var outfile, _ = os.Create("output.txt")
// var in = bufio.NewScanner(file)
// var out = bufio.NewWriter(outfile)
var in = bufio.NewScanner(os.Stdin)
var out = bufio.NewWriter(os.Stdout)

func next() string {
    if !in.Scan() {
        panic("Scan error")
    }
    return in.Text()
}

func nextInt() int {
    ret, _ := strconv.Atoi(next())
    return ret
}

func nextFloat() float64 {
    ret, _ := strconv.ParseFloat(next(), 64)
    return ret
}

func main() {
    defer out.Flush()
    in.Split(bufio.ScanWords)
    n := nextInt()
    var sum float64
    for i := 0; i < n; i++ {
        sum = sum + nextFloat()
    }
    fmt.Fprintln(out, sum / float64(n))
}


package main

import (
	"bufio"
	"fmt"
	"os"
)

// https://codeforces.com/problemset/problem/200/B
// B. Drinks
// time limit per test
// 2 seconds
// memory limit per test
// 256 megabytes
// input
// standard input
// output
// standard output

// Little Vasya loves orange juice very much. That's why any food and drink in his kitchen necessarily contains orange juice. There are n drinks in his fridge, the volume fraction of orange juice in the i-th drink equals pi percent.

// One day Vasya decided to make himself an orange cocktail. He took equal proportions of each of the n drinks and mixed them. Then he wondered, how much orange juice the cocktail has.

// Find the volume fraction of orange juice in the final drink.
// Input

// The first input line contains a single integer n (1 ≤ n ≤ 100) — the number of orange-containing drinks in Vasya's fridge. The second line contains n integers pi (0 ≤ pi ≤ 100) — the volume fraction of orange juice in the i-th drink, in percent. The numbers are separated by a space.
// Output

// Print the volume fraction in percent of orange juice in Vasya's cocktail. The answer will be considered correct if the absolute or relative error does not exceed 10  - 4.

func main() {
	solveTheProblem()
}

func solveTheProblem() {
	bufStdin := bufio.NewReader(os.Stdin)

	var n int
	_, _ = fmt.Fscan(bufStdin, &n)

	var percent float64
	for i := 0; i < n; i++ {
		var pi int
		_, _ = fmt.Fscan(bufStdin, &pi)

		percent += float64(pi)
	}

	fmt.Print(percent / float64(n))
}


package main

import "fmt"

func main()  {
	var n int
	var res, tmp float64
	res = 0
	fmt.Scanf("%d\n", &n)

	for i := 0; i < n; i++ {
		fmt.Scanf("%f", &tmp)
		res += tmp / float64(n)
	}

	fmt.Println(res)

}


package main

// https://codeforces.com/problemset/problem/200/B

import "fmt"

func taskSolution(a []int) string {
	sum := 0
	for _, ai := range a {
		sum += ai
	}
	avg := float64(sum) / float64(len(a))
	return fmt.Sprintf("%.12f", avg)
}

func main() {
	var n int
	if _, err := fmt.Scan(&n); err != nil {
		panic(err)
	}

	a := []int{}
	for i := 0; i < n; i += 1 {
		var ai int
		if _, err := fmt.Scan(&ai); err != nil {
			panic(err)
		}
		a = append(a, ai)
	}

	fmt.Println(taskSolution(a))
}


package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	_ = scanner.Scan()
	_ = scanner.Scan()
	ps := strings.Split(scanner.Text(), " ")
	p := 0
	for _, v := range ps {
		n, _ := strconv.Atoi(v)
		p += n
	}
	fmt.Printf("%.5f", float64(p)/float64(len(ps)))
}


package main

import (
	"fmt"
	"math"
)

func main() {
	var n int
	fmt.Scanf("%d %d\n", &n)
	sum :=0
	for i:=0;i<n;i++{
		var m int
		fmt.Scanf("%d",&m)
		sum += m
	}
	fmt.Printf("%.12f\n",float64(sum)/float64(n))
}


package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scanf("%d %d\n", &n)
	sum :=0
	for i:=0;i<n;i++{
		var m int
		fmt.Scanf("%d",&m)
		sum += m
	}
	fmt.Printf("%.12f\n",float64(sum)/float64(n))
}


<?php
$a = trim(fgets(STDIN));
$b = explode(" ", trim(fgets(STDIN)));
$c = array_sum($b);
print ($c / ($a * 100)) * 100;
?>


<?php
$i = fgets(STDIN);
if($i==0) die("0");
if($i == 1) die(fgets(STDIN));
$p = explode(" ", fgets(STDIN));
for($t=0; $t<=sizeof($p); $t++)
{
    $pa += $p[$t];
}
echo $pa/$i;
?>


<?php

$data1 = (int) trim(fgets(STDIN));
$data2 = trim(fgets(STDIN));

$main = new MainClass;

echo $main->solution($data1, $data2);

// End of interface

class MainClass {
	//public $problemLink = 'https://codeforces.com/problemset/problem/200/B';
	//public $submissionLink = '';

	public function solution($n, $string)
	{
		$arrayOfStrings = preg_split("/ /", $string);
		$sum = 0;
			for($i = 0; $i < $n; $i++){
				$sum = $sum + (int)$arrayOfStrings[$i];
			}
			return $sum / $n;
	}
}


<?php

$data1 = (int) trim(fgets(STDIN));
$data2 = preg_split("/ /", trim(fgets(STDIN)));

$main = new MainClass;

echo $main->solution($data1, $data2);

// End of interface

class MainClass {
	//public $problemLink = 'https://codeforces.com/problemset/problem/200/B';
	//public $submissionLink = '';

	public function solution($n, $arrayOfStrings)
	{
		$sum = 0;
			foreach ($arrayOfStrings as $value) {
    		$sum += $value;
			}
		return $sum / $n;
	}
}


<?php

$data1 = (int) trim(fgets(STDIN));
$data2 = explode(" ", trim(fgets(STDIN)));

$main = new MainClass;

echo $main->solution($data1, $data2);

// End of interface

class MainClass {
	//public $problemLink = 'https://codeforces.com/problemset/problem/200/B';
	//public $submissionLink = '';

	public function solution($n, $arrayOfStrings)
	{
		$sum = 0;
			foreach ($arrayOfStrings as $value) {
    		$sum += $value;
			}
			return $sum / $n;
	}
}


<?php

error_reporting(0);

solve();

function solve(){

	//$Input = new Input("input.txt");
	$Input = new Input();

	$n = (int)$Input->in();

	$sum = 0;

	for ($i = 0; $i < $n; $i++)
	{
		$sum += (int)$Input->in();
	}

	$p = 100 * $sum / (100 * $n);

	printf("%.12f",$p );
}

class Input{
	private $handle = "";
	private $values = array();
	private $index = 0;

	public function __construct($localfile){

		if (is_file($localfile)) {

			$this->handle = fopen($localfile, "r");
			$contents = stream_get_contents($this->handle);

		} else {

			$contents = stream_get_contents(STDIN);

		}

		$no_newlines = str_replace(array("\r\n", "\r", "\n"), " ", $contents);
		$this->values = explode(" ", $no_newlines);
	}

	public function in(){

		return $this->values[$this->index++];

	}

	public function close(){

		if (is_file($localfile) && feof($this->handle)) fclose($this->handle);

	}
}

?>


<?php

$n = readline();
$ara = explode(' ', readline());
echo array_sum($ara)/$n;


<?php
fscanf(STDIN , "%f" , $n);
$ans = 0;
for ($i=0; $i<$n; $i++){
	fscanf(STDIN, "%d", $a);
	$ans+=$a;
}
fprintf(STDOUT, "%f", $ans/$n);


<?php
$n = fgets(STDIN);
print array_sum(explode(' ', fgets(STDIN)))/$n;


<?php
    fscanf(STDIN, "%d", $n);
    $arr = explode(" ", trim(fgets(STDIN)));
    $v = 0;
        for ($i = 0; $i < count($arr); $i++){
           $v = $v + $arr[$i];
        }
        $v = $v / $n ;
        printf("%.12f", $v);


<?php
    $stdin = fopen("php://stdin","r");
    $stdout=fopen("php://stdout","w");
    $cnt=0;$tmp=0;
    fscanf($stdin,"%d\n",&$cnt);
    $sum = 0;
    $st = fgets($stdin);
    /*
    for($i=0;$i<$cnt;$i++){
        fscanf($stdin,"%d ",&$tmp);
        $sum = $sum + $tmp;
    }
    */
    $arr = explode(' ',$st);
    foreach($arr as $v){
        $sum += $v;
    }
    fprintf($stdout,"%.12f\n",($sum/$cnt));
?>


<?php
 $stdin = fopen("php://stdin","r");
 $stdout=fopen("php://stdout","w");
 $cnt = fscanf($stdin,"%d");
 $sum = 0;
 for($i=0;$i<$cnt;++$i){
  $cur = fscanf($stdin,"%d");
  $sum+=$cur;
 }
 fprintf($stdout,"%.12f",($sum/$cnt));
?>


<?php
function input()
{
    while (($line = trim(fgets(STDIN))) != PHP_EOL) {
        yield $line;
    }
}
function intParam($l) {
    return array_map('intval', explode(' ', $l));

}
$COUNT = 0;
foreach (input() as $line) {
    if ($COUNT == 0) {
        $n = intval($line);
        ++$COUNT;
        continue;
    }
    $q = intParam($line);

    $s = 0;
    for($i = 0; $i < $n; ++$i) {
        $s += $q[$i];
    }

    echo $s/$n;

    if (++$COUNT > 1) {
        break;
    }
}


<?php
// Retrieve input and show output
$stream = fopen("php://stdin", "r");
$input = stream_get_contents($stream);
$inputs = preg_split("/[\s]+/", $input);
echo func($inputs);

// Codeforces Contest 126 Division 2 Problem B
function func($in) {
$sum = 0;
for ($x = 1; $x <= $in[0]; $x++) {
	$sum += $in[$x];
}
return ($sum / $in[0]);
}

// End of submission file
// Submission by chaotic_iak
?>


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define mp make_pair
#define mod 1000000007
#define ff first
#define ss second
priority_queue<pair<ll,pair<ll,ll> > > pq;
set<ll> st,en;
set<ll> :: iterator its,ite;
ll a[200001],len[200001];
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	ll n,i,c,l,r,sz,j,fl,fr,sl,sr;
	cin>>n;
	for(i=0;i<n;i++)
	cin>>a[i];
	c=-1;
	for(i=0;i<n;i++)
	{
	    if(c!=a[i])
	    {
	        st.insert(i);
	        c=a[i];
	    }
	}
	c=-1;
	for(i=n-1;i>=0;i--)
	{
	    if(c!=a[i])
	    {
	        en.insert(i);
	        c=a[i];
	    }
	}
	if(st.size()!=en.size())
	{
	    cout<<-1<<"\n";
	    return 0;
	}
	its=st.begin();
	for(ite=en.begin();ite!=en.end();ite++)
	{
	    pq.push({*ite-*its+1,{-(*its),*ite}});
	    len[*its]=*ite-*its+1;
	    its++;
	}
	c=0;
	while(!pq.empty())
	{
	    if(c>n)
	    break;
	    l=-(pq.top().ss.ff);
	    r=pq.top().ss.ss;
	    pq.pop();
	    its=lower_bound(st.begin(),st.end(),l);
	    ite=lower_bound(en.begin(),en.end(),r);
	    if(its==st.end()||ite==en.end())
	    continue;
	    c++;
	    fl=fr=sl=sr=-1;
	    if(its!=st.begin())
	    {
	        its--;
	        fl=*its;
	        its++;
	        ite--;
	        fr=*ite;
	        ite++;
	    }
	    its++;
	    ite++;
	    if(its!=st.end())
	    {
	        sl=*its;
	        sr=*ite;
	    }
	    its--;
	    ite--;
	    st.erase(its);
	    en.erase(ite);
	    if(fl==-1||sl==-1)
	    continue;
	    else
	    {
	        if(a[fl]==a[sr])
	        {
	            en.erase(fr);
	            st.erase(sl);
	            pq.push({len[fl]+len[sl],{-fl,sr}});
	            len[fl]=len[fl]+len[sl];
	        }
	    }
	}
	cout<<c<<"\n";
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define mp make_pair
#define mod 1000000007
#define ff first
#define ss second
set<pair<ll,ll> > pq;
set<ll> st;
set<ll> :: iterator its;
ll a[200001],len[200001];
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	ll n,i,j,c,id,l,sz,f,s;
	cin>>n;
	for(i=0;i<n;i++)
	cin>>a[i];
	c=-1;
	id=-1;
	for(i=0;i<n;i++)
	{
	    if(c!=a[i])
	    {
	        if(id!=-1)
	        pq.insert({-len[id],-id});
	        st.insert(i);
	        c=a[i];
	        id=i;
	        len[id]=1;
	    }
	    else
	    len[id]++;
	}
	pq.insert({-len[id],-id});
	c=0;
	while(!pq.empty())
	{
	    l=-((*pq.begin()).ss);
	    sz=-((*pq.begin()).ff);
	    pq.erase(pq.begin());
	    its=lower_bound(st.begin(),st.end(),l);
	    if(its==st.end()||len[*its]!=sz)
	    continue;
	    c++;
	    f=s=-1;
	    if(its!=st.begin())
	    {
	        its--;
	        f=*its;
	        its++;
	    }
	    its++;
	    if(its!=st.end())
	    s=*its;
	    its--;
	    st.erase(its);
	    if(f==-1||s==-1)
	    continue;
	    else
	    {
	        if(a[f]==a[s])
	        {
	            st.erase(s);
	            pq.erase({-len[f],-f});
	            pq.erase({-len[s],-s});
	            len[f]+=len[s];
	            pq.insert({-len[f],-f});
	        }
	    }
	}
	cout<<c<<"\n";
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define mp make_pair
#define mod 1000000007
#define ff first
#define ss second
priority_queue<pair<ll,pair<ll,ll> > > pq;
set<ll> st,en;
set<ll> :: iterator its,ite;
ll a[200001];
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	ll n,i,c,l,r,sz,j,fl,fr,sl,sr;
	cin>>n;
	for(i=0;i<n;i++)
	cin>>a[i];
	c=-1;
	for(i=0;i<n;i++)
	{
	    if(c!=a[i])
	    {
	        st.insert(i);
	        c=a[i];
	    }
	}
	c=-1;
	for(i=n-1;i>=0;i--)
	{
	    if(c!=a[i])
	    {
	        en.insert(i);
	        c=a[i];
	    }
	}
	its=st.begin();
	for(ite=en.begin();ite!=en.end();ite++)
	{
	    pq.push({*ite-*its+1,{-(*its),*ite}});
	    its++;
	}
	c=0;
	while(!pq.empty())
	{
	    l=-(pq.top().ss.ff);
	    r=pq.top().ss.ss;
	    pq.pop();
	    its=lower_bound(st.begin(),st.end(),l);
	    ite=lower_bound(en.begin(),en.end(),r);
	    if(its==st.end()||ite==en.end())
	    continue;
	    c++;
	    fl=fr=sl=sr=-1;
	    if(its!=st.begin())
	    {
	        its--;
	        fl=*its;
	        its++;
	        ite--;
	        fr=*ite;
	        ite++;
	    }
	    its++;
	    ite++;
	    if(its!=st.end())
	    {
	        sl=*its;
	        sr=*ite;
	    }
	    its--;
	    ite--;
	    st.erase(its);
	    en.erase(ite);
	    if(fl==-1||sl==-1)
	    continue;
	    else
	    {
	        if(a[fl]==a[sr])
	        {
	            en.erase(fr);
	            st.erase(sl);
	            pq.push({sr-sl+1+fr-fl+1,{-fl,sr}});
	        }
	    }
	}
	cout<<c<<"\n";
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define mp make_pair
#define mod 1000000007
#define ff first
#define ss second
set<pair<ll,ll> > pq;
set<ll> st;
set<ll> :: iterator its;
ll a[200001],len[200001];
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	ll n,i,j,c,id,l,sz,f,s;
	cin>>n;
	for(i=0;i<n;i++)
	cin>>a[i];
	c=-1;
	id=-1;
	for(i=0;i<n;i++)
	{
	    if(c!=a[i])
	    {
	        if(id!=-1)
	        {
	            pq.insert({-len[id],id});
	            st.insert(id);
	        }
	        c=a[i];
	        id=i;
	        len[id]=1;
	    }
	    else
	    len[id]++;
	}
	pq.insert({-len[id],id});
	st.insert(id);
	c=0;
	while(!pq.empty())
	{
	    pair<ll,ll> l1=*pq.begin();
	    sz=-l1.ff;
	    l=l1.ss;
	    pq.erase(pq.begin());
	    its=st.find(l);
	    if(its==st.end()||len[*its]!=sz)
	    continue;
	    c++;
	    f=s=-1;
	    if(its!=st.begin())
	    f=*(prev(its));
	    if(next(its)!=st.end())
	    s=*(next(its));
	    st.erase(its);
	    if(f==-1||s==-1)
	    continue;
	    else
	    {
	        if(a[f]==a[s])
	        {
	            st.erase(s);
	            pq.erase({-len[f],f});
	            pq.erase({-len[s],s});
	            len[f]+=len[s];
	            pq.insert({-len[f],f});
	        }
	    }
	}
	cout<<c<<"\n";
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define mp make_pair
#define mod 1000000007
#define ff first
#define ss second
priority_queue<pair<ll,ll> > pq;
set<ll> st,en;
set<ll> :: iterator its,ite;
ll a[200001],vis[200001];
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	ll n,i,c,l,r,sz,j,fl,fr,sl,sr;
	cin>>n;
	for(i=0;i<n;i++)
	cin>>a[i];
	c=-1;
	for(i=0;i<n;i++)
	{
	    if(c!=a[i])
	    {
	        st.insert(i);
	        c=a[i];
	    }
	}
	c=-1;
	for(i=n-1;i>=0;i--)
	{
	    if(c!=a[i])
	    {
	        en.insert(i);
	        c=a[i];
	    }
	}
	its=st.begin();
	for(ite=en.begin();ite!=en.end();ite++)
	{
	    pq.push({*ite-*its+1,*its});
	    its++;
	}
	c=0;
	while(!pq.empty())
	{
	    sz=pq.top().ff;
	    l=pq.top().ss;
	    r=l+sz-1;
	    pq.pop();
	    if(vis[l]==1)
	    continue;
	    for(j=l;j<=r;j++)
	    vis[j]=1;
	    c++;
	    its=lower_bound(st.begin(),st.end(),l);
	    ite=lower_bound(en.begin(),en.end(),r);
	    fl=fr=sl=sr=-1;
	    if(its!=st.begin())
	    {
	        its--;
	        fl=*its;
	        its++;
	        ite--;
	        fr=*ite;
	        ite++;
	    }
	    its++;
	    ite++;
	    if(its!=st.end())
	    {
	        sl=*its;
	        sr=*ite;
	    }
	    its--;
	    ite--;
	    st.erase(its);
	    en.erase(ite);
	    if(fl==-1||sl==-1)
	    continue;
	    else
	    {
	        if(a[fl]==a[sr])
	        {
	            en.erase(fr);
	            st.erase(sl);
	            pq.push({sr-fl+1,fl});
	        }
	    }
	}
	cout<<c<<"\n";
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define mp make_pair
#define mod 1000000007
#define ff first
#define ss second
priority_queue<pair<ll,pair<ll,ll> > > pq;
set<ll> st,en;
set<ll> :: iterator its,ite;
ll a[200001],len[200001];
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	ll n,i,c,l,r,sz,j,fl,fr,sl,sr;
	cin>>n;
	for(i=0;i<n;i++)
	cin>>a[i];
	c=-1;
	for(i=0;i<n;i++)
	{
	    if(c!=a[i])
	    {
	        st.insert(i);
	        c=a[i];
	    }
	}
	c=-1;
	for(i=n-1;i>=0;i--)
	{
	    if(c!=a[i])
	    {
	        en.insert(i);
	        c=a[i];
	    }
	}
	if(st.size()!=en.size())
	{
	    cout<<-1<<"\n";
	    return 0;
	}
	its=st.begin();
	for(ite=en.begin();ite!=en.end();ite++)
	{
	    pq.push({*ite-*its+1,{-(*its),*ite}});
	    len[*its]=*ite-*its+1;
	    its++;
	}
	c=0;
	ll d=0;
	while(!pq.empty())
	{
	    if(c>n||d>4*n)
	    break;
	    l=-(pq.top().ss.ff);
	    r=pq.top().ss.ss;
	    pq.pop();
	    d++;
	    its=lower_bound(st.begin(),st.end(),l);
	    ite=lower_bound(en.begin(),en.end(),r);
	    if(its==st.end()||ite==en.end())
	    continue;
	    c++;
	    fl=fr=sl=sr=-1;
	    if(its!=st.begin())
	    {
	        its--;
	        fl=*its;
	        its++;
	        ite--;
	        fr=*ite;
	        ite++;
	    }
	    its++;
	    ite++;
	    if(its!=st.end())
	    {
	        sl=*its;
	        sr=*ite;
	    }
	    its--;
	    ite--;
	    st.erase(its);
	    en.erase(ite);
	    if(fl==-1||sl==-1)
	    continue;
	    else
	    {
	        if(a[fl]==a[sr])
	        {
	            en.erase(fr);
	            st.erase(sl);
	            pq.push({len[fl]+len[sl],{-fl,sr}});
	            len[fl]=len[fl]+len[sl];
	        }
	    }
	}
	cout<<c<<"\n";
	return 0;
}