#include <bits/stdc++.h>
using namespace std;
void parseArray(long long* A, long long n) {
  for (long long K = 0; K < n; K++) {
    cin >> A[K];
  }
}
long long modInverse(long long a, long long b) {
  return 1 < a ? b - modInverse(b % a, a) * b / a : 1;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
long double dist(long double x, long double y, long double a, long double b) {
  return sqrt((x - a) * (x - a) + (y - b) * (y - b));
}
void debug(long long* a, long long n) {
  for (long long k = 0; k < n; k++) {
    cout << a[k] << " ";
  }
  cout << "\n";
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  ;
  long long t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    long long m;
    cin >> m;
    long long n = s.length();
    char ans[n + 1];
    memset(ans, 0, sizeof ans);
    for (int k = 0; k < n; k++) {
      if (s[k] == '1') {
        if (k - m >= 0) ans[k - m] = '1';
        if (k + m < n) ans[k + m] = '1';
      }
    }
    for (int k = 0; k < n; k++) {
      if (s[k] == '0') {
        if (k + m < n) ans[k + m] = '0';
        if (k - m >= 0) ans[k - m] = '0';
      }
    }
    for (int k = 0; k < n; k++)
      if (ans[k] == 0) ans[k] = '0';
    bool flag = true;
    for (int k = 0; k < n; k++) {
      if (s[k] == '1') {
        bool temp = false;
        if (k + m < n && ans[k + m] == '1') temp = true;
        if (k - m >= 0 && ans[k - m] == '1') temp = true;
        flag = flag && temp;
      } else {
        int temp = 0;
        int cnt = 0;
        if (k + m < n) cnt++;
        if (k - m >= 0) cnt++;
        if (k + m < n && ans[k + m] == '0') temp++;
        if (k - m >= 0 && ans[k - m] == '0') temp++;
        if (temp != cnt) flag = false;
      }
    }
    if (flag)
      cout << ans << "\n";
    else
      cout << "-1\n";
  }
  return 0;
}
