#include <bits/stdc++.h>
using namespace std;
int main() {
  int q;
  cin >> q;
  while (q--) {
    long long n, m;
    cin >> n >> m;
    long long t = (m % 10);
    vector<int> ans;
    ans.push_back(0);
    while (1) {
      if (t % 10 == 0)
        break;
      else {
        ans.push_back(t % 10);
        t += (m % 10);
      }
    }
    long long sum = 0;
    for (auto xx : ans) sum += xx;
    long long sz = (long long)ans.size();
    long long rr = n / m;
    long long rp = sum * (rr / sz);
    long long rem = (rr % sz);
    for (int i = 0; i <= rem; i++) rp += ans[i];
    cout << rp << "\n";
  }
}
