#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long tt;
  cin >> tt;
  while (tt--) {
    long long d, m;
    cin >> d >> m;
    long long ans = 1;
    for (long long bit = 0; bit < 32; bit++) {
      long long low = (1 << bit);
      long long high = (1 << (bit + 1)) - 1;
      long long cnt = min(high, d) - low + 1;
      if (cnt < 0) break;
      ans *= cnt + 1;
      if (ans >= m) {
        ans %= m;
      }
    }
    ans--;
    ans = (ans % m + m) % m;
    cout << ans << "\n";
  }
}
