#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  int c, k;
  int x, max;
  int ans[50000];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> c >> k;
    ans[i] = 0;
    x = c / k;
    ans[i] = x * k;
    c -= ans[i];
    max = k / 2;
    if (c >= max)
      ans[i] += max;
    else
      ans[i] += c;
  }
  for (int i = 0; i < n; i++) cout << ans[i] << endl;
  return 0;
}
