#pragma GCC optimize(3,"Ofast","inline")
#include<bits/stdc++.h>
using namespace std;
const int N = 37;
long long arr[N];
long long pre[1<<20]={0};
unsigned long long Map[N]={0};
int check(int x,int l,int r)
{
for(int i=l;i<=r;i++)
{
if(((1<<i)&x)==0)
{
unsigned long long now=((Map[i]<<(63-r))>>(63-r));
if((now&x)!=now)return 0;
}
}
return 1;
}
int check2(int x,int l,int r,int mid)
{
for(int i=l;i<=r;i++)
{
if(((1<<i)&x)==0)
{
long long now=(Map[i+mid]>>mid);
if((now&x)!=now)return 0;
}
}
return 1;
}
int main()
{
int t,Ca=1;
scanf("%d",&t);
while(t--)
{
int n,m;
long long mod;
scanf("%d %d %lld",&n,&m,&mod);
for(int i=0;i<n;i++)scanf("%lld",&arr[i]);
memset(Map,0,sizeof(Map));
while(m--)
{
int u,v;
scanf("%d %d",&u,&v);
u--;
v--;
Map[u]|=1ll<<v;
Map[v]|=1ll<<u;
}
int mid=n/2;
for(int i=(1<<mid)-1;i>=0;i--)pre[i]=0;
int upper=(1<<mid)-1;
for(int i=0;i<=upper;i++)
{
long long sum=1;
for(int j=0;j<mid;j++)
if((1<<j)&i)sum=(sum*arr[j])%mod;
if(check(i,0,mid-1))pre[i]=sum;
}
for(int i=0;i<mid;i++) //高維後綴和
for(int j=upper;j>=0;j--)
if((j&(1<<i))==0) pre[j]=(pre[j]+pre[j^(1<<i)])%mod;
upper=(1<<(n-mid))-1;
long long ans=0;
for(int i=0;i<=upper;i++)
{
long long sum=1;
for(int j=0;j<n-mid;j++)
if((1<<j)&i)sum=(sum*arr[j+mid])%mod;
if(check2(i,0,n-mid-1,mid))
{
long long base=0;
for(int x=0;x<=mid-1;x++)
{
unsigned long long now=(Map[x]>>mid);
if((now&i)!=now)base|=1<<x;
}
ans=(ans+sum*pre[base]%mod)%mod;
}
}
printf("Case #%d: %lld\n",Ca++,ans);
}
return 0;
}