#include <bits/stdc++.h>
using namespace std;
const long long Max = 1e5 + 10;
int T;
long long n, m;
long long number[Max];
int main() {
  cin >> T;
  while (T--) {
    cin >> n >> m;
    long long ans = 1;
    int k = 1;
    for (int i = 1;; i *= 2) {
      number[k] = number[k - 1] + i;
      if (number[k] > n) {
        ans = (ans * (n - number[k - 1] + 1) % m) % m;
        break;
      } else {
        ans = ans * (i + 1) % m;
      }
      k++;
    }
    cout << (ans - 1 + m) % m << endl;
  }
  return 0;
}
