#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  long long int t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    long long int k;
    cin >> k;
    long long int n = s.size();
    string ans(n, '1');
    for (long long int i = 0; i < n; i++) {
      if (s[i] == '0') {
        if (i - k >= 0) ans[i - k] = '0';
        if (i + k < n) ans[i + k] = '0';
      }
    }
    long long int flag = 0;
    for (long long int i = 0; i < n; i++) {
      bool a = false;
      if (s[i] == '1') {
        if (i - k >= 0 and ans[i - k] == '1') a = true;
        if (i + k < n and ans[i + k] == '1') a = true;
        if (!a) {
          flag = 1;
          break;
        }
      }
    }
    if (flag == 1)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }
}
