#include <bits/stdc++.h>
using namespace std;
unsigned countBits(unsigned int number) { return (int)log2(number) + 1; }
int main() {
  int t;
  cin >> t;
  while (t--) {
    int d, m;
    cin >> d >> m;
    int k = countBits(d);
    long long arr[k];
    for (int i = 0; i < k; i++) {
      arr[i] = (1 << (i));
    }
    arr[k - 1] = d - arr[k - 1] + 1;
    long long ans = 1;
    for (int i = 0; i < k; i++) {
      ans = ans * (arr[i] + 1) % m;
    }
    cout << (ans - 1 + m) % m << endl;
  }
  return 0;
}
