#include <bits/stdc++.h>
using namespace std;
long long tab[12], pr[12];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int q;
  cin >> q;
  long long n, m, w, x;
  for (int z = 0; z < q; z++) {
    cin >> n >> m;
    x = 1;
    for (long long i = 1; i <= 11; i++) {
      tab[i] = (tab[i - 1] + (m % 10)) % 10;
      pr[i] = pr[i - 1] + tab[i];
      if (tab[i] == tab[1] && i > 1) {
        x = i - 1;
        break;
      }
    }
    w = pr[x] * (n / (x * m));
    n %= x * m;
    w += pr[n / m];
    cout << w << "\n";
  }
  return 0;
}
