#include <bits/stdc++.h>
using namespace std;
const int max_n = 1, inf = 1000111222;
int main() {
  int T;
  cin >> T;
  while (T--) {
    long long d, m;
    cin >> d >> m;
    vector<long long> v;
    for (long long cur = 1;; cur *= 2) {
      if (d > cur) {
        v.push_back(cur);
        d -= cur;
      } else {
        v.push_back(d);
        break;
      }
    }
    long long ans = 1;
    for (int i = 0; i < v.size(); ++i) {
      ++v[i];
      ans *= v[i];
      ans %= m;
    }
    ans = (ans + m - 1) % m;
    cout << ans << endl;
  }
  return 0;
}
