#include <bits/stdc++.h>
using namespace std;
long long d, m, ans = 1;
int main() {
  int T;
  cin >> T;
  while (T--) {
    ans = 1;
    cin >> d >> m;
    long long cur = 1, k = 0;
    while ((cur * 2) <= d) {
      ans = ans * (cur + 1) % m;
      cur <<= 1;
    }
    ans *= (d - (cur) + 2) % m;
    ans = (ans + m - 1) % m;
    cout << ans << endl;
  }
}
