#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 50;
ll f[N];
int main()
{
    int T;
    scanf("%d", &T);
    for (int t = 1; t <= T; t++)
    {
        printf("Case #%d:\n", t);
        int q, m, op, x;
        scanf("%d%d", &q, &m);
        ll ans = 1;
        for (int i = 1; i <= q; i++)
        {
            scanf("%d%d", &op, &x);
            if (op == 1)
                ans = ans * (f[i] = x) % m;
            else
            {
                ans = f[x] = f[i] = 1;
                for (int j = 1; j < i; j++)
                    ans = ans * f[j] % m;
            }
            printf("%lld\n", ans);
        }
    }
    return 0;
}