#include #include #include using namespace std; #define ll long long #define WLIM 11 #define MLIM (1< typedef vector slist; typedef vector ilist; slist states[WLIM+1]; int bounds[MLIM+11]; ilist adj[WLIM+1][SLIM]; int idx[MLIM][MLIM]; bool inited[WLIM+1]; int compute_reach(state st) { return ((st.reach << 1) | st.reach | (st.reach >> 1)) & st.mask; } void init(int w) { if (inited[w]) return; inited[w] = true; bounds[0] = 0; for (int m = 0; m < 1 << w; m++) { int r = 0; do { if (r) states[w].push_back(make_pair(m, r)); } while (r = r - m & m); bounds[m+1] = states[w].size(); } for (int i = 0; i < states[w].size(); i++) { int m = states[w][i].mask; int r = states[w][i].reach; idx[m][r] = i; } for (int i = 0; i < states[w].size(); i++) { int r0 = compute_reach(states[w][i]); for (int m = 0; m < 1 << w; m++) { int r = r0 & m; if (!r) continue; int j = idx[m][r]; adj[w][i].push_back(j); } } } ll ct[SLIM]; ll nct[SLIM]; int main() { int z; scanf("%d", &z); while (z--) { int w, h; ll mod; scanf("%d%d%lld", &w, &h, &mod); init(w); for (int i = 0; i < states[w].size(); i++) ct[i] = 0; for (int m = 1; m < 1 << w; m += 2) { ct[bounds[m]] = 1; } while (--h) { for (int i = 0; i < states[w].size(); i++) nct[i] = 0; for (int i = 0; i < states[w].size(); i++) { if (!ct[i]) continue; for (int nb = 0; nb < adj[w][i].size(); nb++) { int j = adj[w][i][nb]; nct[j] += ct[i]; } } for (int i = 0; i < states[w].size(); i++) ct[i] = nct[i] % mod; } ll ans = 0; for (int i = 0; i < states[w].size(); i++) ans += ct[i]; if ((ans %= mod) < 0) ans += mod; printf("%lld\n", ans); } }