#include unsigned long long memo[100000000]; int main(){ int t; std::cin >> t; for (int i = 0; i < t; ++i){ int n, m; std::cin >> n >> m; unsigned long long ans = 1; if (m == 1){ std::cout << 0 << std::endl; continue; } for (int j = 2; j <= n; ++j){ //if(j%m!=0)ans *= j % m; ans *= j; ans %= m; } std::cout << ans << std::endl; } //system("pause"); return 0; }