#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> a[102][102];
bool vis[102][102];
void dfs(int i, int c) {
  vis[i][c] = 1;
  for (auto nx : a[i][c])
    if (!vis[nx][c]) dfs(nx, c);
}
int main() {
  int n, m;
  cin >> n >> m;
  int x, y, z;
  for (int i = 0; i < m; ++i) {
    cin >> x >> y >> z;
    a[x][z].push_back(y);
    a[y][z].push_back(x);
  }
  int q;
  cin >> q;
  int p1[q];
  for (int i = 1; i <= q; ++i) {
    int j, g;
    cin >> j >> g;
    int cnt = 0;
    memset(vis, 0, sizeof(vis));
    for (int o = 1; o <= m; ++o) {
      dfs(j, o);
      if (vis[g][o]) cnt++;
    }
    p1[i] = cnt;
  }
  for (int i = 1; i <= q; ++i) cout << p1[i] << endl;
  return 0;
}
