#include <bits/stdc++.h>
using namespace std;
int main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n, m;
    cin >> n >> m;
    if (n < m) {
      cout << 0 << endl;
      continue;
    }
    map<long long, long long> mp;
    long long s = 0;
    vector<long long> ar;
    for (long long i = m; i <= n; i += m) {
      long long x = i % 10;
      if (mp[x]) break;
      mp[x] = 1;
      ar.push_back(x);
      s += x;
    }
    long long r = n / m;
    long long ans = (r / ar.size()) * s;
    for (int i = 0; i < r % ar.size(); i++) ans += ar[i];
    cout << ans << endl;
  }
  return 0;
}
