#include using namespace std; #define overload4(a, b, c, d, name, ...) name #define REP0(n) for(long long _abcde12345 = 0; _abcde12345 < (long long)(n); ++_abcde12345) #define REP1(i, n) for(long long i = 0; i < (long long)(n); ++i) #define REP2(i, a, b) for(long long i = (a); i < (long long)(b); ++i) #define REP3(i, a, b, c) for(long long i = (a); i < (long long)(b); i += (c)) #define rep(...) overload4(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__) #define rrep(i, a, b) for(long long i = (a); i >= (long long)(b); --i) #define each_for(...) for (auto&& __VA_ARGS__) #define each_const(...) for (const auto& __VA_ARGS__) #define drop(x) { cout << (x) << endl; return; } templateinline bool chmax(T& a,const S& b){return (a < b ? a = b, 1 : 0);} templateinline bool chmin(T& a,const S& b){return (a > b ? a = b, 1 : 0);} /************************************************************************************************/ void solve(); int main() { cin.tie(nullptr)->sync_with_stdio(false); cout<>T; rep(T)solve(); return 0; } ///////////////////////////////////////////////////////////// long long N,M,B; long long A[1<<17]; long long dp[1<<17]; long long modpow(long long a,long long b) { long long ret=1; while(b>0) { if(b&1)ret=ret*a%B; a=a*a%B; b>>=1; } return ret; } void solve() { cin>>N>>M>>B; rep(i,N)cin>>A[i]; dp[0]=1; rep(i,1,N+1) { dp[i]=dp[i-1]*modpow(M,A[i-1])%B; (dp[i]+=dp[i-1])%=B; } cout<