#include #include #include using namespace std; using namespace atcoder; using mint = modint; #define rep(i,n) for (int i = 0; i < (n); ++i) #define Inf32 1000000001 #define Inf64 4000000000000000001 void Gauss(vector> &B){ int now = 0; rep(i,405){ int temp = -1; for(int j=now;j>n>>m>>b>>q; vector u(m),v(m); vector t; rep(i,m){ cin>>u[i]>>v[i]; t.push_back(u[i]); t.push_back(v[i]); } sort(t.begin(),t.end()); t.erase(unique(t.begin(),t.end()),t.end()); rep(i,m){ u[i] = distance(t.begin(),lower_bound(t.begin(),t.end(),u[i])); v[i] = distance(t.begin(),lower_bound(t.begin(),t.end(),v[i])); } dsu D(t.size()); long long cnt = 0; rep(i,m){ if(D.same(u[i],v[i]))cnt++; else D.merge(u[i],v[i]); } vector> Bs(q); rep(i,q){ int x,y,z;cin>>x>>y>>z; Bs[i][x] =1; Bs[i][y] =1; Bs[i][z] =1; } Gauss(Bs); long long T = 1; rep(i,Bs.size()){ T *= 2; T %= b; } long long S = 1; rep(i,cnt){ S *= 2; S %= b; } S -= T; if(S<0)S += b; S %= b; cout<