#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int m, d;
    long long ans = 1;
    cin >> d >> m;
    for (long long i = 0; i < 31; ++i) {
      if (d < (1 << i)) break;
      long long x = min((1 << (i + 1)) - 1, d) - (1 << i) + 2;
      ans = ans * x * 1LL % m;
    }
    ans--;
    if (ans < 0) ans += m;
    cout << ans << endl;
  }
}
