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