#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n, x, t, m, d, y, ans = 1;
  cin >> t;
  while (t--) {
    ans = 1;
    cin >> d >> m;
    for (long long i = 1; i <= d; i *= 2) {
      y = 2 * i - 1;
      y = min(y, d);
      ans = (ans * (y - i + 2)) % m;
    }
    ans--;
    ans %= m;
    if (ans < 0) {
      ans += m;
    }
    cout << ans << endl;
  }
  return 0;
}
