#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1e9 + 7;
const double PI = 2 * acos(0.0);
void solve() {
  long long int m, d;
  cin >> d >> m;
  long long int tmp = d;
  long long int cnt = 0;
  while (tmp > 0) {
    cnt++;
    tmp /= 2;
  }
  vector<long long int> store(cnt + 5, 0);
  long long int prev = 0;
  for (long long int i = 1; i < cnt; i++) {
    long long int no = pow(2, i) - 1;
    store[i] = no - prev;
    prev = no;
  }
  store[cnt] = d - prev;
  long long int ans = 1;
  for (long long int i = 1; i <= cnt; i++) {
    ans *= (store[i] + 1);
    ans %= m;
  }
  ans--;
  ans += m;
  ans %= m;
  cout << ans << endl;
}
int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
