#include<bits/stdc++.h>
using namespace std;
#define ll long long 
#define pa pair<int,int>
#define fi first
#define se second
#define mp make_pair
#define poly vector<int>
#define int ll
#define N 800005
const int inv2=499122177;
const int mod=998244353;
int ans,cnt,p[N];
int a[N];
poly G[N];
int sta[N];
int tp;
int n,rt;
int mn;
int L[N],R[N];
void ad(int x,int y)
{
    G[x].push_back(y);
}
void dfs(int k,int fa)
{
    p[++cnt]=a[k];
    for (auto u:G[k])
    {
        if (u==fa) continue;
        dfs(u,k);
    }
}
void dfs1(int k,int fa)
{
    mn=min(mn,a[k]);
    for (auto u:G[k])
    {
        if (u==fa) continue;
        dfs1(u,k);
    }
}
void Bellakira()
{
    ans=0;
    cin>>n>>rt;
    for (int i=1;i<=n;i++) cin>>a[i];
    for (int i=1;i<n;i++)
    {
        int x,y;
        cin>>x>>y;
        ad(x,y);
        ad(y,x);
    }
    poly gg;
    for (auto u:G[rt])
    {
        mn=1e18;
        dfs1(u,rt);
        gg.push_back(mn);
    }
    int l=1;
    for (auto u:G[rt])
    {
        l^=1;
        cnt=0;
        dfs(u,rt);
        tp=0;
        sta[0]=0;
        // p[cnt+1]=0;
        for (int i=1;i<=cnt;i++)
        {
            while (tp&&p[sta[tp]]<=p[i]) tp--;
            // cout<<"?"<<i<<" "<<sta[tp]<<" "<<L[i]<<" "<<p[i]<<endl;
            L[i]=i-(sta[tp]);
            sta[++tp]=i;
        }
        tp=0;
        sta[0]=cnt+1;
        for (int i=cnt;i>=1;i--)
        {
            while (tp&&p[sta[tp]]>p[i]) tp--;
            R[i]=(sta[tp]-i);
            // cout<<"?"<<i<<" "<<sta[tp]<<" "<<R[i]<<endl;
            sta[++tp]=i;
            ans=(ans+L[i]*R[i]%mod*p[i]%mod)%mod;
        }
        if (G[rt].size()==1) continue;
        int nowmn=1e18;
        for (int i=1;i<=cnt;i++)
        {
            nowmn=min(nowmn,p[i]);
            ans=(ans+inv2*nowmn%mod)%mod;
            ans=(ans+inv2*min(nowmn,gg[l^1])%mod)%mod;
        }
    }
    ans=(ans+a[rt])%mod;
    cout<<ans<<endl;
    for (int i=1;i<=n;i++) G[i].clear();
}

signed main()
{
    freopen("dfs.in","r",stdin);
    freopen("dfs.out","w",stdout);
    int T;
    cin>>T;
    while (T--)
    {
        Bellakira();
    }
}