#include <bits/stdc++.h>
using namespace std;
void print_vector2d(vector<vector<int>>& a, string name) {
  cout << name << " is\n";
  for (int i = 0; i < a.size(); i++) {
    for (int j = 0; j < a[0].size(); j++) {
      cout << a[i][j] << " ";
    }
    cout << "\n";
  }
  cout << "\n";
}
void print_vector(vector<int>& a, string name) {
  cout << name << " is\n";
  for (int i = 0; i < a.size(); i++) {
    cout << a[i] << " ";
  }
  cout << "\n";
}
int main() {
  int t;
  cin >> t;
  int d, m;
  while (t--) {
    cin >> d >> m;
    vector<int> choices(40, 1);
    int exp = 0;
    long long n = 1;
    while (n <= d) {
      choices[exp] = (min(2 * n - 1, (long long)d) - n + 2) % m;
      n = n * 2;
      exp += 1;
    }
    int ans = 1;
    for (int i = 0; i < 40; i++) {
      ans = ((long long)ans * choices[i]) % m;
    }
    cout << (((ans - 1) % m) + m) % m << "\n";
  }
  return (0);
}
