#pragma region template #include using namespace std; using ll = long long; using vl = vector; constexpr ll inf = 3001001000100100100LL; #define endl '\n' #define rep(i, n) for (ll i = 0; i < (n); ++i) template bool chmin(T& m, const U& v) { if (v < m) { m = v; return true; } return false; } struct setup_main { setup_main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); std::cout << fixed << setprecision(15); } } setup_main_; ll N, P; ll A[13], ans[1 << 13]; void dfs(ll i, ll s, ll b) { if (i == N) { chmin(ans[b], abs(s)); return; } ll nb = (b << 1); dfs(i + 1, s, nb); dfs(i + 1, s - A[i], nb + 1); dfs(i + 1, s + A[i], nb + 1); } void solve() { cin >> N >> P; rep(i, N) cin >> A[i]; if (N > 1 and A[0] == A[1]) { cout << (N % 2 ? A[0] : 0) << endl; return; } rep(i, 1 << N) ans[i] = inf; dfs(1, 0, 0); dfs(1, A[0], 1); ll a = 0; rep(i, 1 << N) a += ans[i]; cout << a % P << endl; } signed main() { ll T; cin >> T; while (T--) { solve(); } }