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